#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*/ print(char *);
int /*<<< orphan*/ put_num(int);
int collatz() {
  int n = 27;
  while (n != 1) {
    put_num(n);
    if (n % 2 == 0) {
      n = n / 2;
    } else {
      n = 3 * n + 1;
    }
    if (n > 1000) {
      print("\n");
      return n;
    }
  }
  print("\n");
  return -1;
}


x86 assembly (clang, -O0):
<collatz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x1b,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
je     119b <collatz+0x7b>
mov    -0x8(%rbp),%edi
call   1040 <put_num@plt>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1168 <collatz+0x48>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
jmp    1172 <collatz+0x52>
imul   $0x3,-0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x3e8,-0x8(%rbp)
jle    1196 <collatz+0x76>
lea    0xe7a(%rip),%rdi
call   1030 <print@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ae <collatz+0x8e>
jmp    112f <collatz+0xf>
lea    0xe5e(%rip),%rdi
call   1030 <print@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<collatz>:
push   %rbx
mov    $0x1b,%ebx
jmp    113e <collatz+0x1e>
nopl   0x0(%rax,%rax,1)
lea    (%rbx,%rbx,2),%ebx
add    $0x1,%ebx
cmp    $0x3e9,%ebx
jge    1169 <collatz+0x49>
cmp    $0x1,%ebx
je     1164 <collatz+0x44>
mov    %ebx,%edi
call   1040 <put_num@plt>
test   $0x1,%bl
jne    1130 <collatz+0x10>
mov    %ebx,%eax
shr    $0x1f,%eax
add    %ebx,%eax
sar    %eax
mov    %eax,%ebx
cmp    $0x3e9,%ebx
jl     113e <collatz+0x1e>
jmp    1169 <collatz+0x49>
mov    $0xffffffff,%ebx
lea    0xe90(%rip),%rdi
call   1030 <print@plt>
mov    %ebx,%eax
pop    %rbx
ret

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

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