#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int fib(int n) {
  int ans;
  printf(" n = %d\n", n);
  if (n == 1 || n == 2) {
    ans = 1;
    printf("ans (base case) = %d\n", ans);
  } else {
    ans = fib(n - 1) + fib(n - 2);
    printf("ans = %d\n", ans);
  }
  return ans;
}


x86 assembly (clang, -O0):
<fib>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xecb(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x1,-0x4(%rbp)
je     114e <fib+0x2e>
cmpl   $0x2,-0x4(%rbp)
jne    1169 <fib+0x49>
movl   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xeaa(%rip),%rdi
call   1030 <printf@plt>
jmp    119b <fib+0x7b>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1040 <fib@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
sub    $0x2,%edi
call   1040 <fib@plt>
mov    %eax,%ecx
mov    -0xc(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe89(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fib>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xed4(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
lea    -0x1(%rbx),%edi
cmp    $0x1,%edi
ja     114e <fib+0x2e>
lea    0xec7(%rip),%rdi
mov    $0x1,%ebx
mov    $0x1,%esi
jmp    116c <fib+0x4c>
call   1040 <fib@plt>
mov    %eax,%ebp
add    $0xfffffffe,%ebx
mov    %ebx,%edi
call   1040 <fib@plt>
mov    %eax,%ebx
add    %ebp,%ebx
lea    0xeb5(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c" n = %d\0A\00"
@.str.1 = [22 x i8] c"ans (base case) = %d\0A\00"
@.str.2 = [10 x i8] c"ans = %d\0A\00"
define i32 @fib(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 1
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %8, %1
store i32 1, i32* %3
%12 = load i32, i32* %3
%13 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %24
14: ; preds = %8
%15 = load i32, i32* %2
%16 = sub nsw i32 %15, 1
%17 = call i32 @fib(i32 %16)
%18 = load i32, i32* %2
%19 = sub nsw i32 %18, 2
%20 = call i32 @fib(i32 %19)
%21 = add nsw i32 %17, %20
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %14, %11
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@.str = [9 x i8] c" n = %d\0A\00"
@.str.1 = [22 x i8] c"ans (base case) = %d\0A\00"
@.str.2 = [10 x i8] c"ans = %d\0A\00"
define i32 @fib(i32 %0) {
%2 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = add i32 %0, -1
%4 = icmp ult i32 %3, 2
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %7
7: ; preds = %5, %9
%8 = phi i32 [ %13, %9 ], [ 1, %5 ]
ret i32 %8
9: ; preds = %1
%10 = tail call i32 @fib(i32 %3)
%11 = add nsw i32 %0, -2
%12 = tail call i32 @fib(i32 %11)
%13 = add nsw i32 %12, %10
%14 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %7
}