#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*/ printf(char *, int);
int collatz(int n) {
  printf("n = %i\n", n);
  if (n == 1) {
    return 0;
  } else {
    if ((n % 2) == 0) {
      return 1 + collatz(n / 2);
    } else {
      return 1 + collatz((3 * n) + 1);
    }
  }
}


x86 assembly (clang, -O0):
<collatz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xecb(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jne    1150 <collatz+0x30>
movl   $0x0,-0x4(%rbp)
jmp    1193 <collatz+0x73>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1181 <collatz+0x61>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <collatz@plt>
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1193 <collatz+0x73>
imul   $0x3,-0x8(%rbp),%edi
add    $0x1,%edi
call   1030 <collatz@plt>
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<collatz>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xed0(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %r15d,%r15d
cmp    $0x1,%ebx
je     118a <collatz+0x6a>
mov    $0x1,%r15d
lea    0xeb4(%rip),%r14
nopl   0x0(%rax)
test   $0x1,%bl
jne    117c <collatz+0x5c>
mov    %ebx,%ebp
shr    $0x1f,%ebp
add    %ebx,%ebp
sar    %ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
and    $0xfffffffe,%ebx
add    $0x1,%r15d
cmp    $0x2,%ebx
mov    %ebp,%ebx
jne    1150 <collatz+0x30>
add    $0xffffffff,%r15d
jmp    118a <collatz+0x6a>
lea    (%rbx,%rbx,2),%edi
add    $0x1,%edi
call   1030 <collatz@plt>
add    %eax,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"n = %i\0A\00"
define i32 @collatz(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 0, i32* %2
br label %24
9: ; preds = %1
%10 = load i32, i32* %3
%11 = srem i32 %10, 2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = load i32, i32* %3
%15 = sdiv i32 %14, 2
%16 = call i32 @collatz(i32 %15)
%17 = add nsw i32 1, %16
store i32 %17, i32* %2
br label %24
18: ; preds = %9
%19 = load i32, i32* %3
%20 = mul nsw i32 3, %19
%21 = add nsw i32 %20, 1
%22 = call i32 @collatz(i32 %21)
%23 = add nsw i32 1, %22
store i32 %23, i32* %2
br label %24
24: ; preds = %18, %13, %8
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@.str = [8 x i8] c"n = %i\0A\00"
define i32 @collatz(i32 %0) {
%2 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = icmp eq i32 %0, 1
br i1 %3, label %15, label %4
4: ; preds = %1, %9
%5 = phi i32 [ %10, %9 ], [ %0, %1 ]
%6 = phi i32 [ %11, %9 ], [ 0, %1 ]
%7 = and i32 %5, 1
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %4
%10 = sdiv i32 %5, 2
%11 = add nuw nsw i32 %6, 1
%12 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %10)
%13 = and i32 %5, -2
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %4
15: ; preds = %9, %1, %17
%16 = phi i32 [ %22, %17 ], [ 0, %1 ], [ %11, %9 ]
ret i32 %16
17: ; preds = %4
%18 = mul nsw i32 %5, 3
%19 = add nsw i32 %18, 1
%20 = tail call i32 @collatz(i32 %19)
%21 = add nuw i32 %6, 1
%22 = add i32 %21, %20
br label %15
}