long long FibGenerator(int reset) {
  const static long long INIT_PREV = 0LL, INIT_CURR = 0LL, INIT_NEXT = 0LL;
  static long long prev, curr, next;
  if (reset) {
    prev = INIT_PREV;
    curr = INIT_CURR;
    next = INIT_NEXT;
  }
  if (curr == INIT_CURR) {
    return curr++;
  }
  next = curr + prev;
  prev = curr;
  curr = next;
  return prev;
}


x86 assembly (clang, -O0):
<FibGenerator>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1132 <FibGenerator+0x32>
movq   $0x0,0x2f0c(%rip)
movq   $0x0,0x2f09(%rip)
movq   $0x0,0x2f06(%rip)
cmpq   $0x0,0x2ef6(%rip)
jne    115e <FibGenerator+0x5e>
mov    0x2ee9(%rip),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x2edb(%rip)
mov    %rax,-0x8(%rbp)
jmp    119a <FibGenerator+0x9a>
mov    0x2ecb(%rip),%rax
add    0x2ebc(%rip),%rax
mov    %rax,0x2ec5(%rip)
mov    0x2eb6(%rip),%rax
mov    %rax,0x2ea7(%rip)
mov    0x2eb0(%rip),%rax
mov    %rax,0x2ea1(%rip)
mov    0x2e92(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<FibGenerator>:
test   %edi,%edi
je     110d <FibGenerator+0xd>
mov    $0x1,%ecx
xor    %eax,%eax
jmp    1123 <FibGenerator+0x23>
mov    0x2f1c(%rip),%rax
test   %rax,%rax
je     1132 <FibGenerator+0x32>
mov    0x2f08(%rip),%rcx
add    %rax,%rcx
mov    %rax,0x2efe(%rip)
mov    %rcx,0x2eff(%rip)
ret
mov    $0x1,%ecx
xor    %eax,%eax
mov    %rcx,0x2ef0(%rip)
ret

LLVM IR (-O0):
@FibGenerator.INIT_PREV = internal constant i64 0
@FibGenerator.INIT_CURR = internal constant i64 0
@FibGenerator.INIT_NEXT = internal constant i64 0
@FibGenerator.prev = internal global i64 0
@FibGenerator.curr = internal global i64 0
@FibGenerator.next = internal global i64 0
define i64 @FibGenerator(i32 %0) {
%2 = alloca i64
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %1
store i64 0, i64* @FibGenerator.prev
store i64 0, i64* @FibGenerator.curr
store i64 0, i64* @FibGenerator.next
br label %7
7: ; preds = %6, %1
%8 = load i64, i64* @FibGenerator.curr
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i64, i64* @FibGenerator.curr
%12 = add nsw i64 %11, 1
store i64 %12, i64* @FibGenerator.curr
store i64 %11, i64* %2
br label %20
13: ; preds = %7
%14 = load i64, i64* @FibGenerator.curr
%15 = load i64, i64* @FibGenerator.prev
%16 = add nsw i64 %14, %15
store i64 %16, i64* @FibGenerator.next
%17 = load i64, i64* @FibGenerator.curr
store i64 %17, i64* @FibGenerator.prev
%18 = load i64, i64* @FibGenerator.next
store i64 %18, i64* @FibGenerator.curr
%19 = load i64, i64* @FibGenerator.prev
store i64 %19, i64* %2
br label %20
20: ; preds = %13, %10
%21 = load i64, i64* %2
ret i64 %21
}

LLVM IR (-O2):
@FibGenerator.prev = internal unnamed_addr global i64 0
@FibGenerator.curr = internal unnamed_addr global i64 0
define i64 @FibGenerator(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %9
3: ; preds = %1
%4 = load i64, i64* @FibGenerator.curr
%5 = icmp eq i64 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = load i64, i64* @FibGenerator.prev
%8 = add nsw i64 %7, %4
br label %9
9: ; preds = %1, %6
%10 = phi i64 [ %4, %6 ], [ 0, %1 ]
%11 = phi i64 [ %8, %6 ], [ 1, %1 ]
store i64 %10, i64* @FibGenerator.prev
br label %12
12: ; preds = %9, %3
%13 = phi i64 [ 1, %3 ], [ %11, %9 ]
%14 = phi i64 [ 0, %3 ], [ %10, %9 ]
store i64 %13, i64* @FibGenerator.curr
ret i64 %14
}