The program is actually using two variables. There is a global variable n
, which is used by main()
. There is a local variable, confusingly also named n
, which is used by binary()
and allocated on the stack.
Here are some excerpts from the code generated on my machine:
_binary:
...
movl %edi, -4(%rbp)
cmpl $0, -4(%rbp)
...
_main:
...
movq _n@GOTPCREL(%rip), %rsi
movl (%rsi), %edi
movl %eax, -4(%rbp) ## 4-byte Spill
callq _binary
...
.comm _n,4,2 ## @n
The first excerpt contains part of the code generated for binary()
. The parameter n
is passed in the register edi
, and is then stored on the stack. The following command compares it to zero, implementing part of n>0
.
The second excerpt contains part of the code generated for main()
. The global variable n
is allocated as a common symbol. The excerpt consists of the function call binary(n)
. You can see that n
is pulled out of the global storage and put into edi
, before calling binary()
.