The two transactions:
are exactly the same but as you can see TX (a) failed as it used up all its available gas. The limit was set to 53,356. The second TX (b) is exactly the same but gas limit was set to 160,000 and the transaction succeeded. Although it used only 38,368 gas which is less than the limit of TX (a). But before I get answers like "some instructions take a different amount of gas depending on XYZ" I checked the execution of the contracts:
They seem to be doing the exact same thing with the exact same amounts of gas used. So why did the first transaction fail? And why does is say there's only 31700 gas to start with on TX (a)?