#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 calc_length(long);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char **argv) {
  int longest_length = 0;
  int longest_n = 0;
  for (long i = 1; i < 1000000; ++i) {
    const int len = calc_length(i);
    if (len > longest_length) {
      longest_length = len;
      longest_n = i;
    }
  }
  printf("%d\n", longest_n);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
cmpq   $0xf4240,-0x20(%rbp)
jge    1195 <main+0x75>
mov    -0x20(%rbp),%rdi
call   1040 <calc_length@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    117f <main+0x5f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,-0x18(%rbp)
jmp    1184 <main+0x64>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    114c <main+0x2c>
mov    -0x18(%rbp),%esi
lea    0xe61(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x1,%ebx
xor    %ebp,%ebp
xor    %r14d,%r14d
xchg   %ax,%ax
mov    %rbx,%rdi
call   1040 <calc_length@plt>
cmp    %ebp,%eax
cmovg  %ebx,%r14d
cmovg  %eax,%ebp
add    $0x1,%rbx
cmp    $0xf4240,%rbx
jne    1130 <main+0x10>
lea    0xeab(%rip),%rdi
mov    %r14d,%esi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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