I compared 2 C functions returning a struct. We know that on ABI level big structs are going to be passes by pointer as the first function argument.
struct S {
int words[8];
};
struct S fsret() {
struct S s;
s.words[0] = 1;
return s;
}
void fout(struct S* s) {
s->words[0] = 1;
}
For these functions I checked the assembly for x86_64 Linux and Windows. The fsret
is declared as void @fsret(%struct.S* sret %s)
.
Comparing these two variants, there is no difference on the callee side. However, inside the functions the fsret
additionally copies its first argument (the pointer to the struct) to the RAX register. Why?
if (fsret().words[0]==10) { do_something(); }
? the compiler needs a return value in that case (not sure, just an idea) – Openeyed