Skip to content

Instantly share code, notes, and snippets.

@mcfedr
Created November 7, 2014 21:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcfedr/63ad08370d856bad3694 to your computer and use it in GitHub Desktop.
Save mcfedr/63ad08370d856bad3694 to your computer and use it in GitHub Desktop.
Example of C compiler tail recursion
int isEven(int i) {
if (i == 0) {
return 1;
}
else if (i == 1) {
return 0;
}
else if (i < 0) {
return isEven(i + 2);
}
else {
return isEven(i - 2);
}
}
.section __TEXT,__text,regular,pure_instructions
.globl _isEven
.align 4, 0x90
_isEven: ## @isEven
.cfi_startproc
## BB#0:
pushq %rbp
Ltmp2:
.cfi_def_cfa_offset 16
Ltmp3:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp4:
.cfi_def_cfa_register %rbp
## kill: EDI<def> EDI<kill> RDI<def>
jmp LBB0_1
.align 4, 0x90
LBB0_3: ## in Loop: Header=BB0_1 Depth=1
leal 2(%rdi), %eax
testl %edi, %edi
leal -2(%rdi), %ecx
cmovsl %eax, %ecx
movl %ecx, %edi
LBB0_1: ## %tailrecurse
## =>This Inner Loop Header: Depth=1
testl %edi, %edi
je LBB0_4
## BB#2: ## %tailrecurse
## in Loop: Header=BB0_1 Depth=1
xorl %eax, %eax
cmpl $1, %edi
jne LBB0_3
jmp LBB0_5
LBB0_4: ## %.loopexit.loopexit
movl $1, %eax
LBB0_5: ## %.loopexit
popq %rbp
retq
.cfi_endproc
.subsections_via_symbols
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment