Skip to content

Instantly share code, notes, and snippets.

@samdmarshall
Last active June 29, 2016 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samdmarshall/5eea5e70ccebc58d2ad4e997952a348a to your computer and use it in GitHub Desktop.
Save samdmarshall/5eea5e70ccebc58d2ad4e997952a348a to your computer and use it in GitHub Desktop.
basic example of a goto versus an early return statement in C
#include <stdio.h>
int add_goto(int a, int b)
{
int c;
if (a == 0) {
c = b;
goto return_statement;
}
c = a + b;
return_statement:
return c;
}
int add_return(int a, int b)
{
int c;
if (a == 0) {
return b;
}
c = a + b;
return c;
}
int main ()
{
int a = add_return(0, 5);
printf("a = %i\n",a);
int b = add_goto(3, 4);
printf("b = %i\n",b);
return 0;
}
; compiled with `clang -x c -arch x86_64 -O0 add.c -o add`
; ================ B E G I N N I N G O F P R O C E D U R E ================
; Basic Block Registers Used: rsp rbp rsi rdi - Defined: rsp rbp rip CPAZSO - Killed: <nothing> - LiveIn: rsp rbp rsi rdi - LiveOut: rsp rbp - AvailIn: <nothing> - AvailOut: rsp rbp rip CPAZSO
;
; Section __text
;
; Range 0x100000eb0 - 0x100000f82 (210 bytes)
; File offset 3760 (210 bytes)
; Flags : 0x80000400
;
_add_goto:
0000000100000eb0 55 push rbp ; XREF=0x1000000d0, _main+60
0000000100000eb1 4889E5 mov rbp, rsp
0000000100000eb4 897DFC mov dword [ss:rbp+var_4], edi
0000000100000eb7 8975F8 mov dword [ss:rbp+var_8], esi
0000000100000eba 837DFC00 cmp dword [ss:rbp+var_4], 0x0
0000000100000ebe 0F850B000000 jne 0x100000ecf
; Basic Block Registers Used: rbp - Defined: rax rip - Killed: <nothing> - LiveIn: rsp rbp - LiveOut: rsp rbp - AvailIn: rsp rbp rip CPAZSO - AvailOut: rax rsp rbp rip CPAZSO
0000000100000ec4 8B45F8 mov eax, dword [ss:rbp+var_8]
0000000100000ec7 8945F4 mov dword [ss:rbp+var_C], eax
0000000100000eca E909000000 jmp 0x100000ed8
; Basic Block Registers Used: rbp - Defined: rax CPAZSO - Killed: <nothing> - LiveIn: rsp rbp - LiveOut: rsp rbp - AvailIn: rsp rbp rip CPAZSO - AvailOut: rax rsp rbp rip CPAZSO
0000000100000ecf 8B45FC mov eax, dword [ss:rbp+var_4] ; XREF=_add_goto+14
0000000100000ed2 0345F8 add eax, dword [ss:rbp+var_8]
0000000100000ed5 8945F4 mov dword [ss:rbp+var_C], eax
; Basic Block Registers Used: rsp rbp - Defined: rax rsp rbp rip - Killed: <nothing> - LiveIn: rsp rbp - LiveOut: <nothing> - AvailIn: rax rsp rbp rip CPAZSO - AvailOut: rax rsp rbp rip CPAZSO
0000000100000ed8 8B45F4 mov eax, dword [ss:rbp+var_C] ; XREF=_add_goto+26
0000000100000edb 5D pop rbp
0000000100000edc C3 ret
; endp
0000000100000edd 0F1F00 nop dword [ds:rax]
; ================ B E G I N N I N G O F P R O C E D U R E ================
; Basic Block Registers Used: rsp rbp rsi rdi - Defined: rsp rbp rip CPAZSO - Killed: <nothing> - LiveIn: rsp rbp rsi rdi - LiveOut: rsp rbp - AvailIn: <nothing> - AvailOut: rsp rbp rip CPAZSO
_add_return:
0000000100000ee0 55 push rbp ; XREF=_main+22
0000000100000ee1 4889E5 mov rbp, rsp
0000000100000ee4 897DF8 mov dword [ss:rbp+var_8], edi
0000000100000ee7 8975F4 mov dword [ss:rbp+var_C], esi
0000000100000eea 837DF800 cmp dword [ss:rbp+var_8], 0x0
0000000100000eee 0F850B000000 jne 0x100000eff
; Basic Block Registers Used: rbp - Defined: rax rip - Killed: <nothing> - LiveIn: rsp rbp - LiveOut: rsp rbp - AvailIn: rsp rbp rip CPAZSO - AvailOut: rax rsp rbp rip CPAZSO
0000000100000ef4 8B45F4 mov eax, dword [ss:rbp+var_C]
0000000100000ef7 8945FC mov dword [ss:rbp+var_4], eax
0000000100000efa E90F000000 jmp 0x100000f0e
; Basic Block Registers Used: rbp - Defined: rax CPAZSO - Killed: <nothing> - LiveIn: rsp rbp - LiveOut: rsp rbp - AvailIn: rsp rbp rip CPAZSO - AvailOut: rax rsp rbp rip CPAZSO
0000000100000eff 8B45F8 mov eax, dword [ss:rbp+var_8] ; XREF=_add_return+14
0000000100000f02 0345F4 add eax, dword [ss:rbp+var_C]
0000000100000f05 8945F0 mov dword [ss:rbp+var_10], eax
0000000100000f08 8B45F0 mov eax, dword [ss:rbp+var_10]
0000000100000f0b 8945FC mov dword [ss:rbp+var_4], eax
; Basic Block Registers Used: rsp rbp - Defined: rax rsp rbp rip - Killed: <nothing> - LiveIn: rsp rbp - LiveOut: <nothing> - AvailIn: rax rsp rbp rip CPAZSO - AvailOut: rax rsp rbp rip CPAZSO
0000000100000f0e 8B45FC mov eax, dword [ss:rbp+var_4] ; XREF=_add_return+26
0000000100000f11 5D pop rbp
0000000100000f12 C3 ret
; endp
0000000100000f13 666666662E0F1F840000000000 nop word [cs:rax+rax]
; ================ B E G I N N I N G O F P R O C E D U R E ================
; Basic Block Registers Used: rsp rbp rdi rip - Defined: rax rsp rbp rsi rdi rip CPAZSO - Killed: rax rcx rdx rsi rdi r8 r9 r10 r11 st0 st1 st2 st3 st4 st5 st6 st7 mm0 mm1 mm2 mm3 mm4 mm5 mm6 mm7 mm8 mm9 mm10 mm11 mm12 mm13 mm14 mm15 - LiveIn: rsp rbp rdi rip - LiveOut: <nothing> - AvailIn: <nothing> - AvailOut: rax rsp rbp rsi rdi rip CPAZSO
_main:
0000000100000f20 55 push rbp
0000000100000f21 4889E5 mov rbp, rsp
0000000100000f24 4883EC20 sub rsp, 0x20
0000000100000f28 31FF xor edi, edi ; argument #1 for method _add_return
0000000100000f2a BE05000000 mov esi, 0x5 ; argument #2 for method _add_return
0000000100000f2f C745FC00000000 mov dword [ss:rbp+var_4], 0x0
0000000100000f36 E8A5FFFFFF call _add_return
0000000100000f3b 488D3D60000000 lea rdi, qword [ds:0x100000fa2] ; "a = %i\\n", argument "format" for method imp___stubs__printf
0000000100000f42 8945F8 mov dword [ss:rbp+var_8], eax
0000000100000f45 8B75F8 mov esi, dword [ss:rbp+var_8]
0000000100000f48 B000 mov al, 0x0
0000000100000f4a E833000000 call imp___stubs__printf
0000000100000f4f BF03000000 mov edi, 0x3 ; argument #1 for method _add_goto
0000000100000f54 BE04000000 mov esi, 0x4 ; argument #2 for method _add_goto
0000000100000f59 8945F0 mov dword [ss:rbp+var_10], eax
0000000100000f5c E84FFFFFFF call _add_goto
0000000100000f61 488D3D42000000 lea rdi, qword [ds:0x100000faa] ; "b = %i\\n", argument "format" for method imp___stubs__printf
0000000100000f68 8945F4 mov dword [ss:rbp+var_C], eax
0000000100000f6b 8B75F4 mov esi, dword [ss:rbp+var_C]
0000000100000f6e B000 mov al, 0x0
0000000100000f70 E80D000000 call imp___stubs__printf
0000000100000f75 31F6 xor esi, esi
0000000100000f77 8945EC mov dword [ss:rbp+var_14], eax
0000000100000f7a 89F0 mov eax, esi
0000000100000f7c 4883C420 add rsp, 0x20
0000000100000f80 5D pop rbp
0000000100000f81 C3 ret
int _add_return(int arg0, int arg1) {
var_8 = arg0;
var_C = arg1;
if (var_8 == 0x0) {
var_4 = var_C;
}
else {
var_4 = var_8 + var_C;
}
rax = var_4;
return rax;
}
int _add_goto(int arg0, int arg1) {
var_4 = arg0;
var_8 = arg1;
if (var_4 == 0x0) {
var_C = var_8;
}
else {
var_C = var_4 + var_8;
}
rax = var_C;
return rax;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment