Skip to content

Instantly share code, notes, and snippets.

@namachan10777
Created February 6, 2021 14:23
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 namachan10777/a95715369d75d3a90fd1969ba4101072 to your computer and use it in GitHub Desktop.
Save namachan10777/a95715369d75d3a90fd1969ba4101072 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int fib(int n) {
if (n == 0 || n == 1) {
return 1;
}
return fib(n-1) + fib(n-2);
}
int main() {
printf("%d\n", fib(6));
}
; ModuleID = 'test.c'
source_filename = "test.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
; Function Attrs: noinline nounwind optnone sspstrong uwtable
define dso_local i32 @fib(i32 %0) #0 {
%2 = alloca i32, align 4
%3 = alloca i32, align 4
store i32 %0, i32* %3, align 4
%4 = load i32, i32* %3, align 4
%5 = icmp eq i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* %3, align 4
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %10
9: ; preds = %6, %1
store i32 1, i32* %2, align 4
br label %18
10: ; preds = %6
%11 = load i32, i32* %3, align 4
%12 = sub nsw i32 %11, 1
%13 = call i32 @fib(i32 %12)
%14 = load i32, i32* %3, align 4
%15 = sub nsw i32 %14, 2
%16 = call i32 @fib(i32 %15)
%17 = add nsw i32 %13, %16
store i32 %17, i32* %2, align 4
br label %18
18: ; preds = %10, %9
%19 = load i32, i32* %2, align 4
ret i32 %19
}
; Function Attrs: noinline nounwind optnone sspstrong uwtable
define dso_local i32 @main() #0 {
%1 = call i32 @fib(i32 6)
%2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1)
ret i32 0
}
declare i32 @printf(i8*, ...) #1
attributes #0 = { noinline nounwind optnone sspstrong uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.module.flags = !{!0, !1, !2}
!llvm.ident = !{!3}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{!"clang version 11.0.1"}
.text
.file "test.c"
.globl fib # -- Begin function fib
.p2align 4, 0x90
.type fib,@function
fib: # @fib
.cfi_startproc
# %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $16, %rsp
movl %edi, -8(%rbp)
cmpl $0, -8(%rbp)
je .LBB0_2
# %bb.1:
cmpl $1, -8(%rbp)
jne .LBB0_3
.LBB0_2:
movl $1, -4(%rbp)
jmp .LBB0_4
.LBB0_3:
movl -8(%rbp), %eax
subl $1, %eax
movl %eax, %edi
callq fib
movl -8(%rbp), %ecx
subl $2, %ecx
movl %ecx, %edi
movl %eax, -12(%rbp) # 4-byte Spill
callq fib
movl -12(%rbp), %ecx # 4-byte Reload
addl %eax, %ecx
movl %ecx, -4(%rbp)
.LBB0_4:
movl -4(%rbp), %eax
addq $16, %rsp
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Lfunc_end0:
.size fib, .Lfunc_end0-fib
.cfi_endproc
# -- End function
.globl main # -- Begin function main
.p2align 4, 0x90
.type main,@function
main: # @main
.cfi_startproc
# %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $16, %rsp
movl $6, %edi
callq fib
leaq .L.str(%rip), %rdi
movl %eax, %esi
movb $0, %al
callq printf@PLT
xorl %ecx, %ecx
movl %eax, -4(%rbp) # 4-byte Spill
movl %ecx, %eax
addq $16, %rsp
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Lfunc_end1:
.size main, .Lfunc_end1-main
.cfi_endproc
# -- End function
.type .L.str,@object # @.str
.section .rodata.str1.1,"aMS",@progbits,1
.L.str:
.asciz "%d\n"
.size .L.str, 4
.ident "clang version 11.0.1"
.section ".note.GNU-stack","",@progbits
.addrsig
.addrsig_sym fib
.addrsig_sym printf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment