On 22 December 2014 at 22:14, Chaoran Yang wrote: > Shouldn’t it be optimized to: > > 0x0000000000400530 <+0>: mov $0x2,%eax > 0x0000000000400535 <+5>: retq > > just like I compiled it with "gcc -O2 t.c”? When you compile the whole file with -O2 you also compile main() with -O2, which is not the same as just optimising one function. > Anyway, I just want to say that the __attribute__((optimize(“xx”))) doesn’t seem to work. It does work, if you use it right, and you put it on functions where optimisation makes a difference. But I believe unless you use some -Ox option on the command line the optimize attribute has no effect (which is true for optimization options like -finline-functions). You have to enable optimization in the first place before you can control the optimization level. And for a function as trivial as "return 2" you're not going to notice any difference between -O1 and -O3. > I tried to use __attribute__((optimize(0))) to turn off optimization on certain functions, but also failed. That should be __attribute__((optimize("O0"))) although I don't know if -O0 can be used with the attribute.