I compiled the following code with gcc 4.8.4 and with -O0 flag:
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
static jmp_buf env;
static void
doJump(int nvar, int rvar, int vvar)
{
printf("Inside doJump(): nvar=%d rvar=%d vvar=%d\n", nvar, rvar, vvar);
longjmp(env, 1);
}
int
main(int argc, char *argv[])
{
int nvar;
register int rvar;
volatile int vvar;
nvar = 111;
rvar = 222;
vvar = 333;
if (setjmp(env) == 0) {
nvar = 777;
rvar = 888;
vvar = 999;
doJump(nvar, rvar, vvar);
} else {
printf("After longjmp(): nvar=%d rvar=%d vvar=%d\n", nvar, rvar, vvar);
}
exit(EXIT_SUCCESS);
}
It produced the following output:
Inside doJump(): nvar=777 rvar=888 vvar=999
After longjmp(): nvar=777 rvar=222 vvar=999
My expectation was that rvar will be 888 in the second row as all optimizations are disabled.
When I remove 'register' from definition of 'rvar' or when I add 'volatile' in front of 'register', it outputs 888.
So it seems that gcc still performs some optimizations inspite of -O0 flag.
Is there a way to disable absolutely all optimizations in gcc?