#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 *calloc(int, int);
int collatzLength(int, int *);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ printf(char *, int);
int main() {
  int *collatz;
  int i, ans, tmp, max = 0;
  collatz = calloc(1000000 + 1, sizeof(int));
  collatz[1] = 1;
  for (i = 1; i <= 1000000; i++) {
    tmp = collatzLength(i, collatz);
    if (tmp > max) {
      max = tmp;
      ans = i;
    }
  }
  free(collatz);
  printf("%d\n", ans);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0xf4241,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x1,0x4(%rax)
movl   $0x1,-0x14(%rbp)
cmpl   $0xf4240,-0x14(%rbp)
jg     11c2 <main+0x82>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <collatzLength@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    11af <main+0x6f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11b4 <main+0x74>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117b <main+0x3b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%esi
lea    0xe2b(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xf4241,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%r14
movl   $0x1,0x4(%rax)
mov    $0x1,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <collatzLength@plt>
cmp    %ebp,%eax
cmovg  %ebx,%r15d
cmovg  %eax,%ebp
add    $0x1,%ebx
cmp    $0xf4241,%ebx
jne    1170 <main+0x30>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xe63(%rip),%rdi
mov    %r15d,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%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 0, i32* %6
%7 = call i32* @calloc(i32 1000001, i32 4)
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = getelementptr inbounds i32, i32* %8, i64 1
store i32 1, i32* %9
store i32 1, i32* %3
br label %10
10: ; preds = %24, %0
%11 = load i32, i32* %3
%12 = icmp sle i32 %11, 1000000
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = load i32, i32* %3
%15 = load i32*, i32** %2
%16 = call i32 @collatzLength(i32 %14, i32* %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = icmp sgt i32 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32, i32* %5
store i32 %21, i32* %6
%22 = load i32, i32* %3
store i32 %22, i32* %4
br label %23
23: ; preds = %20, %13
br label %24
24: ; preds = %23
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %10
27: ; preds = %10
%28 = load i32*, i32** %2
%29 = call i32 @free(i32* %28)
%30 = load i32, i32* %4
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %30)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = tail call i32* @calloc(i32 1000001, i32 4)
%2 = getelementptr inbounds i32, i32* %1, i64 1
store i32 1, i32* %2
br label %3
3: ; preds = %0, %3
%4 = phi i32 [ 0, %0 ], [ %10, %3 ]
%5 = phi i32 [ 1, %0 ], [ %11, %3 ]
%6 = phi i32 [ undef, %0 ], [ %9, %3 ]
%7 = tail call i32 @collatzLength(i32 %5, i32* %1)
%8 = icmp sgt i32 %7, %4
%9 = select i1 %8, i32 %5, i32 %6
%10 = select i1 %8, i32 %7, i32 %4
%11 = add nuw nsw i32 %5, 1
%12 = icmp eq i32 %11, 1000001
br i1 %12, label %13, label %3
13: ; preds = %3
%14 = tail call i32 @free(i32* %1)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %9)
ret i32 0
}