Compiling the next module C
static const int i = 1; void f (const int *i); int g (void) { f (&i); return i; }
using gcc -S -O3 to handle x86_64, you get the following assembly for the g function:
g: leaq i(%rip), %rdi subq $8, %rsp call f@PLT movl $1, %eax
In other words, the return compiled to move the constant $1 to the %eax return register, which makes sense since i declared a constant.
However, if I remove this const , so I have
static int i = 1; void f (const int *i); int g (void) { f (&i); return i; }
the output of gcc -S -O3 suddenly becomes:
g: leaq i(%rip), %rdi subq $8, %rsp call f@PLT movl i(%rip), %eax
That is, the return value is explicitly loaded from memory after calling f .
Why is this so? The argument f declared as a pointer to an int constant, so f should not be changed i . In addition, f cannot call a function that modifies i through a link other than a constant, because g can be the only such function, since i declared static.