#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_sequence(unsigned long);
unsigned long longest_collatz_sequence(unsigned long limit) {
  int sequence, largest = 0;
  unsigned long num = limit;
  for (unsigned long i = limit / 2; i <= limit; i++) {
    sequence = collatz_sequence(i);
    if (sequence > largest) {
      num = i;
      largest = sequence;
    }
  }
  return num;
}


x86 assembly (clang, -O0):
<longest_collatz_sequence>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
ja     1181 <longest_collatz_sequence+0x71>
mov    -0x20(%rbp),%rdi
call   1030 <collatz_sequence@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    116b <longest_collatz_sequence+0x5b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1170 <longest_collatz_sequence+0x60>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1137 <longest_collatz_sequence+0x27>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<longest_collatz_sequence>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rdi,%rbx
shr    %rbx
xor    %ebp,%ebp
mov    %rdi,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1030 <collatz_sequence@plt>
cmp    %ebp,%eax
cmovg  %eax,%ebp
cmovg  %rbx,%r15
add    $0x1,%rbx
cmp    %r14,%rbx
jbe    1130 <longest_collatz_sequence+0x20>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @longest_collatz_sequence(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i64 %0, i64* %2
store i32 0, i32* %4
%7 = load i64, i64* %2
store i64 %7, i64* %5
%8 = load i64, i64* %2
%9 = udiv i64 %8, 2
store i64 %9, i64* %6
br label %10
10: ; preds = %24, %1
%11 = load i64, i64* %6
%12 = load i64, i64* %2
%13 = icmp ule i64 %11, %12
br i1 %13, label %14, label %27
14: ; preds = %10
%15 = load i64, i64* %6
%16 = call i32 @collatz_sequence(i64 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = load i32, i32* %4
%19 = icmp sgt i32 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i64, i64* %6
store i64 %21, i64* %5
%22 = load i32, i32* %3
store i32 %22, i32* %4
br label %23
23: ; preds = %20, %14
br label %24
24: ; preds = %23
%25 = load i64, i64* %6
%26 = add i64 %25, 1
store i64 %26, i64* %6
br label %10
27: ; preds = %10
%28 = load i64, i64* %5
ret i64 %28
}

LLVM IR (-O2):
define i64 @longest_collatz_sequence(i64 %0) {
%2 = lshr i64 %0, 1
br label %4
3: ; preds = %4
ret i64 %11
4: ; preds = %1, %4
%5 = phi i64 [ %2, %1 ], [ %12, %4 ]
%6 = phi i64 [ %0, %1 ], [ %11, %4 ]
%7 = phi i32 [ 0, %1 ], [ %10, %4 ]
%8 = tail call i32 @collatz_sequence(i64 %5)
%9 = icmp sgt i32 %8, %7
%10 = select i1 %9, i32 %8, i32 %7
%11 = select i1 %9, i64 %5, i64 %6
%12 = add i64 %5, 1
%13 = icmp ugt i64 %12, %0
br i1 %13, label %3, label %4
}