#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 collatz_length(int, int *);
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int main() {
  int *lengths = malloc(1000000 * sizeof(int));
  memset(lengths, 0, 1000000 * sizeof(int));
  lengths[1] = 1;
  int max_length = 0;
  int longest_n = 0;
  for (int n = 1; n < 1000000; n++) {
    int length = collatz_length(n, lengths);
    if (length > max_length) {
      max_length = length;
      longest_n = n;
    }
  }
  printf(
      "The number under 1,000,000 with the longest Collatz sequence is %d.\n",
      longest_n);
  free(lengths);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x3d0900,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x3d0900,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
movl   $0x1,0x4(%rax)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0xf4240,-0x1c(%rbp)
jge    11e4 <main+0x94>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1070 <collatz_length@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11d1 <main+0x81>
mov    -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11d6 <main+0x86>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119d <main+0x4d>
mov    -0x18(%rbp),%esi
lea    0xe12(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x3d0900,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x3d0900,%edx
call   1050 <memset@plt>
movl   $0x1,0x4(%r14)
mov    $0x1,%ebx
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <collatz_length@plt>
cmp    %ebp,%eax
cmovg  %ebx,%r15d
cmovg  %eax,%ebp
add    $0x1,%ebx
cmp    $0xf4240,%ebx
jne    1190 <main+0x40>
lea    0xe4b(%rip),%rdi
mov    %r15d,%esi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [69 x i8] c"The number under 1,000,000 with the longest Collatz sequence 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
%7 = call i32* @malloc(i32 4000000)
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = call i32 @memset(i32* %8, i32 0, i32 4000000)
%10 = load i32*, i32** %2
%11 = getelementptr inbounds i32, i32* %10, i64 1
store i32 1, i32* %11
store i32 0, i32* %3
store i32 0, i32* %4
store i32 1, i32* %5
br label %12
12: ; preds = %26, %0
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 1000000
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = load i32, i32* %5
%17 = load i32*, i32** %2
%18 = call i32 @collatz_length(i32 %16, i32* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = load i32, i32* %3
%21 = icmp sgt i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* %6
store i32 %23, i32* %3
%24 = load i32, i32* %5
store i32 %24, i32* %4
br label %25
25: ; preds = %22, %15
br label %26
26: ; preds = %25
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
store i32 %28, i32* %5
br label %12
29: ; preds = %12
%30 = load i32, i32* %4
%31 = call i32 @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = load i32*, i32** %2
%33 = call i32 @free(i32* %32)
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [69 x i8] c"The number under 1,000,000 with the longest Collatz sequence is %d.\0A\00"
define i32 @main() {
%1 = tail call i32* @malloc(i32 4000000)
%2 = tail call i32 @memset(i32* %1, i32 0, i32 4000000)
%3 = getelementptr inbounds i32, i32* %1, i64 1
store i32 1, i32* %3
br label %7
4: ; preds = %7
%5 = tail call i32 @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0), i32 %13)
%6 = tail call i32 @free(i32* %1)
ret i32 0
7: ; preds = %0, %7
%8 = phi i32 [ 0, %0 ], [ %14, %7 ]
%9 = phi i32 [ 1, %0 ], [ %15, %7 ]
%10 = phi i32 [ 0, %0 ], [ %13, %7 ]
%11 = tail call i32 @collatz_length(i32 %9, i32* %1)
%12 = icmp sgt i32 %11, %8
%13 = select i1 %12, i32 %9, i32 %10
%14 = select i1 %12, i32 %11, i32 %8
%15 = add nuw nsw i32 %9, 1
%16 = icmp eq i32 %15, 1000000
br i1 %16, label %4, label %7
}