#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 int64_t;

/* Variables and functions */
int chain_length(int);
int /*<<< orphan*/ printf(char *, int, int);
int main() {
  struct {
    int64_t n, len;
  } best = {0};
  for (int64_t i = 0; i < 1000000; i++) {
    int64_t len = chain_length(i);
    if (len >= best.len) {
      best.n = i;
      best.len = len;
    }
  }
  printf("%lld = %lld\n", best.n, best.len);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xf4240,-0x14(%rbp)
jge    1199 <main+0x69>
mov    -0x14(%rbp),%edi
call   1030 <chain_length@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     1186 <main+0x56>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    118b <main+0x5b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1156 <main+0x26>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
xor    %ebp,%ebp
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
mov    %eax,%r14d
mov    %ebp,%edi
call   1030 <chain_length@plt>
cmp    %r14d,%eax
cmovl  %r14d,%eax
cmovge %ebp,%ebx
add    $0x1,%ebp
cmp    $0xf4240,%ebp
jne    1130 <main+0x10>
lea    0xeaa(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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