// Example of dual value return in C using by-value structs. Contrast // twoatoi.S. // // Compiling with -Os gives assembly like this, showing that the // struct is being returned in rax and edx: // // 107c: e8 18 01 00 00 call 1199 // 1081: 48 39 c7 cmp rdi, rax // 1084: 48 8d 3d 79 0f 00 00 lea rdi, [rip+0xf79] // 108b: 89 d6 mov esi, edx // 108d: 49 0f 45 fc cmovne rdi,r12 // 1091: 31 c0 xor eax,eax // 1093: e8 a8 ff ff ff call 1040 // // Without -Os it still uses the same calling convention, it’s just // harder to understand. #include typedef struct { const char *p; int val; } numconversion; numconversion atoi_two(const char *s) { int n = 0; while ('0' <= *s && *s <= '9') n = 10 * n + *s++ - '0'; return (numconversion){ s, n }; } int main(int argc, char **argv) { while (--argc) { char *s = *++argv; numconversion n = atoi_two(s); printf(n.p == s ? "(fail) " : "%d ", n.val); } printf("\n"); return 0; }