#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 chainlength(int);
int /*<<< orphan*/ printf(char *, int, int);
int main() {
  int maxlen = 0, maxval = 0;
  for (int i = 1; i <= 1000000; ++i) {
    if (i % 100000 == 0) {
      printf("%d, max=%d\n", i, maxlen);
    }
    int len = chainlength(i);
    if (len > maxlen) {
      maxlen = len;
      maxval = i;
    }
  }
  printf("maxlen=%d @ val=%d\n", maxlen, maxval);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
cmpl   $0xf4240,-0x10(%rbp)
jg     11ad <main+0x8d>
mov    -0x10(%rbp),%eax
mov    $0x186a0,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1177 <main+0x57>
mov    -0x10(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe8e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
call   1040 <chainlength@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    119a <main+0x7a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    119f <main+0x7f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1144 <main+0x24>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe52(%rip),%rdi
call   1030 <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    $0x1,%ebp
xor    %ebx,%ebx
lea    0xecb(%rip),%r15
xor    %r14d,%r14d
jmp    115b <main+0x3b>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <chainlength@plt>
cmp    %ebx,%eax
cmovg  %ebp,%r14d
cmovg  %eax,%ebx
add    $0x1,%ebp
cmp    $0xf4241,%ebp
je     1183 <main+0x63>
mov    %ebp,%eax
shr    $0x5,%eax
imul   $0xa7c5ac5,%rax,%rax
shr    $0x27,%rax
imul   $0x186a0,%eax,%eax
cmp    %eax,%ebp
jne    1140 <main+0x20>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1030 <printf@plt>
jmp    1140 <main+0x20>
lea    0xe82(%rip),%rdi
mov    %ebx,%esi
mov    %r14d,%edx
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%d, max=%d\0A\00"
@.str.1 = [20 x i8] c"maxlen=%d @ val=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32 1, i32* %4
br label %6
6: ; preds = %27, %0
%7 = load i32, i32* %4
%8 = icmp sle i32 %7, 1000000
br i1 %8, label %9, label %30
9: ; preds = %6
%10 = load i32, i32* %4
%11 = srem i32 %10, 100000
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i32, i32* %2
%16 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
br label %17
17: ; preds = %13, %9
%18 = load i32, i32* %4
%19 = call i32 @chainlength(i32 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = load i32, i32* %2
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32, i32* %5
store i32 %24, i32* %2
%25 = load i32, i32* %4
store i32 %25, i32* %3
br label %26
26: ; preds = %23, %17
br label %27
27: ; preds = %26
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %6
30: ; preds = %6
%31 = load i32, i32* %2
%32 = load i32, i32* %3
%33 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %32)
ret i32 0
}

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