#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 next_fibonacci_number(int, int);
int /*<<< orphan*/ printf(char *, int, int);
int main() {
  int max_value = 4000000;
  int sum = 0;
  int fib_number = 0, p = 0, q = 1;
  while (fib_number < max_value) {
    fib_number = next_fibonacci_number(p, q);
    p = q;
    q = fib_number;
    if (q % 2 == 0)
      sum += q;
  }
  printf("Sum of the even-values fibonacci terms < %d is %d\n", max_value, sum);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x3d0900,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    119a <main+0x7a>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <next_fibonacci_number@plt>
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1195 <main+0x75>
mov    -0x18(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1152 <main+0x32>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe59(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %edi,%edi
mov    $0x1,%eax
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %eax,%ebp
mov    %eax,%esi
call   1040 <next_fibonacci_number@plt>
test   $0x1,%al
mov    $0x0,%ecx
cmove  %eax,%ecx
add    %ecx,%ebx
mov    %ebp,%edi
cmp    $0x3d0900,%eax
jl     1130 <main+0x10>
lea    0xeab(%rip),%rdi
mov    $0x3d0900,%esi
mov    %ebx,%edx
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [51 x i8] c"Sum of the even-values fibonacci terms < %d is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 4000000, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 1, i32* %6
br label %7
7: ; preds = %24, %0
%8 = load i32, i32* %4
%9 = load i32, i32* %2
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %25
11: ; preds = %7
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = call i32 @next_fibonacci_number(i32 %12, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %6
store i32 %15, i32* %5
%16 = load i32, i32* %4
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = srem i32 %17, 2
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %11
%21 = load i32, i32* %6
%22 = load i32, i32* %3
%23 = add nsw i32 %22, %21
store i32 %23, i32* %3
br label %24
24: ; preds = %20, %11
br label %7
25: ; preds = %7
%26 = load i32, i32* %2
%27 = load i32, i32* %3
%28 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %26, i32 %27)
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [51 x i8] c"Sum of the even-values fibonacci terms < %d is %d\0A\00"
define i32 @main() {
br label %1
1: ; preds = %0, %1
%2 = phi i32 [ 1, %0 ], [ %5, %1 ]
%3 = phi i32 [ 0, %0 ], [ %2, %1 ]
%4 = phi i32 [ 0, %0 ], [ %9, %1 ]
%5 = tail call i32 @next_fibonacci_number(i32 %3, i32 %2)
%6 = and i32 %5, 1
%7 = icmp eq i32 %6, 0
%8 = select i1 %7, i32 %5, i32 0
%9 = add nsw i32 %8, %4
%10 = icmp slt i32 %5, 4000000
br i1 %10, label %1, label %11
11: ; preds = %1
%12 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 4000000, i32 %9)
ret i32 0
}