I am curious about the liberties that a compiler has when optimizing. Let\'s limit this question to GCC and C/C++ (any version, any flavour of standard):
Is it possi
The portion of the C++ standard that applies is §1.9 "Program execution". It reads, in part:
conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below. ...
A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible execution sequences of the corresponding instance of the abstract machine with the same program and the same input. ...
The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions. ...
So, yes, code may behave differently at different optimization levels, but (assuming that all levels produce a conforming compiler), but they cannot behave observably differently.
EDIT: Allow me to correct my conclusion: Yes, code may behave differently at different optimization levels as long as each behavior is observably identical to one of the behaviors of the standard's abstract machine.