#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 *, ...);
void fibonacci_optim(unsigned int n) {
  unsigned long long int first = 0;
  unsigned long long int second = 1;
  unsigned long long int next = 0;
  unsigned int c = 0;
  for (c = 0; c < n; c++) {
    if (c <= 1) {
      next = c;
    } else {
      next = first + second;
      first = second;
      second = next;
    }
    printf("%lld ", next);
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<fibonacci_optim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    119f <fibonacci_optim+0x8f>
cmpl   $0x1,-0x24(%rbp)
ja     1163 <fibonacci_optim+0x53>
mov    -0x24(%rbp),%eax
mov    %rax,-0x20(%rbp)
jmp    117f <fibonacci_optim+0x6f>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1141 <fibonacci_optim+0x31>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fibonacci_optim>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %edi,%edi
je     1177 <fibonacci_optim+0x57>
mov    %edi,%r15d
mov    $0x1,%r12d
lea    0xec3(%rip),%r14
xor    %ebx,%ebx
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r12,%r13,1),%rsi
cmp    $0x2,%rbx
cmovae %r12,%r13
cmovae %rsi,%r12
cmovb  %rbx,%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <fibonacci_optim+0x30>
mov    $0xa,%edi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%lld \00"
@.str.1 = [2 x i8] c"\0A\00"
define void @fibonacci_optim(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %2
store i64 0, i64* %3
store i64 1, i64* %4
store i64 0, i64* %5
store i32 0, i32* %6
store i32 0, i32* %6
br label %7
7: ; preds = %26, %1
%8 = load i32, i32* %6
%9 = load i32, i32* %2
%10 = icmp ult i32 %8, %9
br i1 %10, label %11, label %29
11: ; preds = %7
%12 = load i32, i32* %6
%13 = icmp ule i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* %6
%16 = zext i32 %15 to i64
store i64 %16, i64* %5
br label %23
17: ; preds = %11
%18 = load i64, i64* %3
%19 = load i64, i64* %4
%20 = add i64 %18, %19
store i64 %20, i64* %5
%21 = load i64, i64* %4
store i64 %21, i64* %3
%22 = load i64, i64* %5
store i64 %22, i64* %4
br label %23
23: ; preds = %17, %14
%24 = load i64, i64* %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %24)
br label %26
26: ; preds = %23
%27 = load i32, i32* %6
%28 = add i32 %27, 1
store i32 %28, i32* %6
br label %7
29: ; preds = %7
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%lld \00"
define void @fibonacci_optim(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %17, label %3
3: ; preds = %1
%4 = zext i32 %0 to i64
br label %5
5: ; preds = %3, %5
%6 = phi i64 [ 0, %3 ], [ %15, %5 ]
%7 = phi i64 [ 1, %3 ], [ %12, %5 ]
%8 = phi i64 [ 0, %3 ], [ %11, %5 ]
%9 = icmp ult i64 %6, 2
%10 = add i64 %7, %8
%11 = select i1 %9, i64 %8, i64 %7
%12 = select i1 %9, i64 %7, i64 %10
%13 = select i1 %9, i64 %6, i64 %10
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %13)
%15 = add nuw nsw i64 %6, 1
%16 = icmp eq i64 %15, %4
br i1 %16, label %17, label %5
17: ; preds = %5, %1
%18 = tail call i32 @putchar(i32 10)
ret void
}