#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 /*<<< orphan*/ assert(int);
unsigned long collatz_length(unsigned long n) {
  assert(n != 0);
  if (n == 1) {
    return 1;
  }
  if (n % 2 == 0) {
    n = n / 2;
  } else {
    n = 3 * n + 1;
  }
  return 1 + collatz_length(n);
}


x86 assembly (clang, -O0):
<collatz_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x1,-0x10(%rbp)
jne    1156 <collatz_length+0x36>
movq   $0x1,-0x8(%rbp)
jmp    1197 <collatz_length+0x77>
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    1179 <collatz_length+0x59>
mov    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1186 <collatz_length+0x66>
imul   $0x3,-0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <collatz_length@plt>
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<collatz_length>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1030 <assert@plt>
mov    $0x1,%r14d
cmp    $0x1,%rbx
je     116a <collatz_length+0x5a>
mov    $0x1,%r14d
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rax
shr    %rax
test   $0x1,%bl
lea    0x1(%rbx,%rbx,2),%rbx
cmove  %rax,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1030 <assert@plt>
add    $0x1,%r14
cmp    $0x1,%rbx
jne    1140 <collatz_length+0x30>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i64 @collatz_length(i64 %0) {
%2 = icmp ne i64 %0, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = icmp eq i64 %0, 1
br i1 %5, label %22, label %6
6: ; preds = %1, %6
%7 = phi i64 [ %14, %6 ], [ %0, %1 ]
%8 = phi i64 [ %15, %6 ], [ 0, %1 ]
%9 = and i64 %7, 1
%10 = icmp eq i64 %9, 0
%11 = lshr i64 %7, 1
%12 = mul i64 %7, 3
%13 = add i64 %12, 1
%14 = select i1 %10, i64 %11, i64 %13
%15 = add i64 %8, 1
%16 = icmp ne i64 %14, 0
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = icmp eq i64 %14, 1
br i1 %19, label %20, label %6
20: ; preds = %6
%21 = add i64 %8, 2
br label %22
22: ; preds = %20, %1
%23 = phi i64 [ 1, %1 ], [ %21, %20 ]
ret i64 %23
}