I have the following C function:
void proc(long a1, long *a1p,
int a2, int *a2p,
short a3, short *a3p,
char a4, char *a4p)
{
*a1p += a1;
*a2p += a2;
*a3p += a3;
*a4p += a4;
}
Using Godbolt, I've converted it to x86_64 assembly (for simplicity, I used the -Og flag to minimize optimizations). It produces the following assembly:
proc:
movq 16(%rsp), %rax
addq %rdi, (%rsi)
addl %edx, (%rcx)
addw %r8w, (%r9)
movl 8(%rsp), %edx
addb %dl, (%rax)
ret
I'm confused by the first line of assembly: movq 16(%rsp), %rax. I know that the %rax register is used to store a return value. But the proc procedure doesn't have a return value. So I'm curious why that register is being used here, as opposed to %r9 or some other register which isn't used for the purpose of returning values.
I'm also confused about the location of this instruction, relative to the others. It appears first, well before its destination register %rax is needed for anything (indeed, this register isn't needed until the last step). It also appears before addq %rdi, (%rsi), which is the translation of the first line of code in the procedure (*a1p += a1;).
What am I missing?