#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 */
typedef int uint64_t;

/* Variables and functions */
scalar_t__ calloc(int, int);
int collatz_length(int, int *);
int /*<<< orphan*/ free(int *);
__attribute__((always_)) uint64_t f() {
  uint64_t *memo = (uint64_t *)calloc(1000000 + 1, sizeof(uint64_t));
  uint64_t ans, i, max = 0;
  memo[1] = 1;
  for (i = 1; i <= 1000000; i++) {
    uint64_t tmp = collatz_length(i, memo);
    if (tmp > max) {
      max = tmp;
      ans = i;
    }
  }
  free(memo);
  return ans;
}


x86 assembly (clang, -O0):
<f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0xf4241,%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movl   $0x1,0x4(%rax)
movl   $0x1,-0x10(%rbp)
cmpl   $0xf4240,-0x10(%rbp)
jg     11ab <f+0x7b>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1050 <collatz_length@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1198 <f+0x68>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    119d <f+0x6d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1164 <f+0x34>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xf4241,%edi
mov    $0x4,%esi
call   1040 <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 <collatz_length@plt>
cmp    %ebp,%eax
cmovg  %eax,%ebp
cmovg  %ebx,%r15d
add    $0x1,%ebx
cmp    $0xf4241,%ebx
jne    1160 <f+0x30>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @f() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = call i64 @calloc(i32 1000001, i32 4)
%7 = inttoptr i64 %6 to i32*
store i32* %7, i32** %1
store i32 0, i32* %4
%8 = load i32*, i32** %1
%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** %1
%16 = call i32 @collatz_length(i32 %14, i32* %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = load i32, i32* %4
%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* %4
%22 = load i32, i32* %3
store i32 %22, i32* %2
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** %1
%29 = call i32 @free(i32* %28)
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
define i32 @f() {
%1 = tail call i64 @calloc(i32 1000001, i32 4)
%2 = inttoptr i64 %1 to i32*
%3 = getelementptr inbounds i32, i32* %2, i64 1
store i32 1, i32* %3
br label %4
4: ; preds = %0, %4
%5 = phi i32 [ undef, %0 ], [ %11, %4 ]
%6 = phi i32 [ 0, %0 ], [ %10, %4 ]
%7 = phi i32 [ 1, %0 ], [ %12, %4 ]
%8 = tail call i32 @collatz_length(i32 %7, i32* %2)
%9 = icmp sgt i32 %8, %6
%10 = select i1 %9, i32 %8, i32 %6
%11 = select i1 %9, i32 %7, i32 %5
%12 = add nuw nsw i32 %7, 1
%13 = icmp eq i32 %12, 1000001
br i1 %13, label %14, label %4
14: ; preds = %4
%15 = tail call i32 @free(i32* %2)
ret i32 %11
}