unsigned long long fib(unsigned int n) {
  static unsigned long long cache[10000000] = {1, 1, 0};
  static unsigned int max_entry = 1;
  if (n < 0) {
    return 0;
  } else if (n <= max_entry) {
    return cache[n];
  } else {
    unsigned int i;
    unsigned long long x = cache[max_entry - 1];
    unsigned long long y = cache[max_entry];
    for (i = max_entry + 1; i <= n; i++) {
      unsigned long long z = y;
      y = x + y;
      x = z;
      if (i < 10000000) {
        max_entry = i;
        cache[i] = y;
      }
    }
    return y;
  }
}


x86 assembly (clang, -O0):
<fib>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jae    111e <fib+0x1e>
movq   $0x0,-0x8(%rbp)
jmp    11f2 <fib+0xf2>
mov    -0xc(%rbp),%eax
cmp    0x4c4e309(%rip),%eax
ja     1146 <fib+0x46>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
lea    0x2ef7(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x8(%rbp)
jmp    11f2 <fib+0xf2>
mov    0x4c4e2e4(%rip),%eax
sub    $0x1,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2ed6(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
mov    0x4c4e2c8(%rip),%eax
mov    %eax,%ecx
lea    0x2ebf(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    0x4c4e2b1(%rip),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
ja     11ea <fib+0xea>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x989680,-0x10(%rbp)
jae    11d7 <fib+0xd7>
mov    -0x10(%rbp),%eax
mov    %eax,0x4c4e26d(%rip)
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
lea    0x2e5d(%rip),%rax
mov    %rdx,(%rax,%rcx,8)
jmp    11dc <fib+0xdc>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1185 <fib+0x85>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<fib>:
mov    0x4c4e32a(%rip),%esi
cmp    %edi,%esi
jae    1159 <fib+0x59>
lea    0x2f1f(%rip),%r8
mov    (%r8,%rsi,8),%rax
lea    0x1(%rsi),%edx
add    $0xffffffff,%esi
mov    (%r8,%rsi,8),%rcx
jmp    1137 <fib+0x37>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%edx
cmp    %edi,%edx
ja     1166 <fib+0x66>
mov    %rcx,%rsi
mov    %rax,%rcx
mov    %rsi,%rax
add    %rcx,%rax
cmp    $0x98967f,%edx
ja     1130 <fib+0x30>
mov    %edx,0x4c4e2df(%rip)
mov    %edx,%esi
mov    %rax,(%r8,%rsi,8)
jmp    1130 <fib+0x30>
mov    %edi,%eax
lea    0x2ece(%rip),%rcx
mov    (%rcx,%rax,8),%rax
ret

LLVM IR (-O0):
@fib.cache = internal global <{ i64, i64, [9999998 x i64] }> <{ i64 1, i64 1, [9999998 x i64] zeroinitializer }>
@fib.max_entry = internal global i32 1
define i64 @fib(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = icmp ult i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i64 0, i64* %2
br label %56
11: ; preds = %1
%12 = load i32, i32* %3
%13 = load i32, i32* @fib.max_entry
%14 = icmp ule i32 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* %3
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds [10000000 x i64], [10000000 x i64]* bitcast (<{ i64, i64, [9999998 x i64] }>* @fib.cache to [10000000 x i64]*), i64 0, i64 %17
%19 = load i64, i64* %18
store i64 %19, i64* %2
br label %56
20: ; preds = %11
%21 = load i32, i32* @fib.max_entry
%22 = sub i32 %21, 1
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds [10000000 x i64], [10000000 x i64]* bitcast (<{ i64, i64, [9999998 x i64] }>* @fib.cache to [10000000 x i64]*), i64 0, i64 %23
%25 = load i64, i64* %24
store i64 %25, i64* %5
%26 = load i32, i32* @fib.max_entry
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds [10000000 x i64], [10000000 x i64]* bitcast (<{ i64, i64, [9999998 x i64] }>* @fib.cache to [10000000 x i64]*), i64 0, i64 %27
%29 = load i64, i64* %28
store i64 %29, i64* %6
%30 = load i32, i32* @fib.max_entry
%31 = add i32 %30, 1
store i32 %31, i32* %4
br label %32
32: ; preds = %51, %20
%33 = load i32, i32* %4
%34 = load i32, i32* %3
%35 = icmp ule i32 %33, %34
br i1 %35, label %36, label %54
36: ; preds = %32
%37 = load i64, i64* %6
store i64 %37, i64* %7
%38 = load i64, i64* %5
%39 = load i64, i64* %6
%40 = add i64 %38, %39
store i64 %40, i64* %6
%41 = load i64, i64* %7
store i64 %41, i64* %5
%42 = load i32, i32* %4
%43 = icmp ult i32 %42, 10000000
br i1 %43, label %44, label %50
44: ; preds = %36
%45 = load i32, i32* %4
store i32 %45, i32* @fib.max_entry
%46 = load i64, i64* %6
%47 = load i32, i32* %4
%48 = zext i32 %47 to i64
%49 = getelementptr inbounds [10000000 x i64], [10000000 x i64]* bitcast (<{ i64, i64, [9999998 x i64] }>* @fib.cache to [10000000 x i64]*), i64 0, i64 %48
store i64 %46, i64* %49
br label %50
50: ; preds = %44, %36
br label %51
51: ; preds = %50
%52 = load i32, i32* %4
%53 = add i32 %52, 1
store i32 %53, i32* %4
br label %32
54: ; preds = %32
%55 = load i64, i64* %6
store i64 %55, i64* %2
br label %56
56: ; preds = %54, %15, %10
%57 = load i64, i64* %2
ret i64 %57
}

LLVM IR (-O2):
@fib.cache = internal unnamed_addr global <{ i64, i64, [9999998 x i64] }> <{ i64 1, i64 1, [9999998 x i64] zeroinitializer }>
@fib.max_entry = internal unnamed_addr global i32 1
define i64 @fib(i32 %0) {
%2 = load i32, i32* @fib.max_entry
%3 = icmp ult i32 %2, %0
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = zext i32 %0 to i64
%6 = getelementptr inbounds [10000000 x i64], [10000000 x i64]* bitcast (<{ i64, i64, [9999998 x i64] }>* @fib.cache to [10000000 x i64]*), i64 0, i64 %5
%7 = load i64, i64* %6
br label %29
8: ; preds = %1
%9 = zext i32 %2 to i64
%10 = getelementptr inbounds [10000000 x i64], [10000000 x i64]* bitcast (<{ i64, i64, [9999998 x i64] }>* @fib.cache to [10000000 x i64]*), i64 0, i64 %9
%11 = load i64, i64* %10
%12 = add nuw i32 %2, 1
%13 = add i32 %2, -1
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds [10000000 x i64], [10000000 x i64]* bitcast (<{ i64, i64, [9999998 x i64] }>* @fib.cache to [10000000 x i64]*), i64 0, i64 %14
%16 = load i64, i64* %15
br label %17
17: ; preds = %8, %26
%18 = phi i32 [ %27, %26 ], [ %12, %8 ]
%19 = phi i64 [ %21, %26 ], [ %11, %8 ]
%20 = phi i64 [ %19, %26 ], [ %16, %8 ]
%21 = add i64 %19, %20
%22 = icmp ult i32 %18, 10000000
br i1 %22, label %23, label %26
23: ; preds = %17
store i32 %18, i32* @fib.max_entry
%24 = zext i32 %18 to i64
%25 = getelementptr inbounds [10000000 x i64], [10000000 x i64]* bitcast (<{ i64, i64, [9999998 x i64] }>* @fib.cache to [10000000 x i64]*), i64 0, i64 %24
store i64 %21, i64* %25
br label %26
26: ; preds = %23, %17
%27 = add i32 %18, 1
%28 = icmp ugt i32 %27, %0
br i1 %28, label %29, label %17
29: ; preds = %26, %4
%30 = phi i64 [ %7, %4 ], [ %21, %26 ]
ret i64 %30
}