#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 */
scalar_t__ simerror(char *, int);
int Collatz(int n) {
  int cnt = 0;
  if (simerror("Collatz", 8))
    return -1;
  while (n > 1) {
    cnt++;
    if (n % 2) {
      n = n * 3 + 1;
    } else {
      n /= 2;
    }
  }
  return cnt;
}


x86 assembly (clang, -O0):
<Collatz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xed7(%rip),%rdi
mov    $0x8,%esi
call   1030 <simerror@plt>
cmp    $0x0,%rax
je     1149 <Collatz+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    119d <Collatz+0x8d>
jmp    114e <Collatz+0x3e>
cmpl   $0x1,-0x8(%rbp)
jle    1197 <Collatz+0x87>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1184 <Collatz+0x74>
imul   $0x3,-0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1192 <Collatz+0x82>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
jmp    114e <Collatz+0x3e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Collatz>:
push   %rbx
mov    %edi,%ebx
lea    0xee6(%rip),%rdi
mov    $0x8,%esi
call   1030 <simerror@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1156 <Collatz+0x46>
xor    %eax,%eax
cmp    $0x2,%ebx
jl     1156 <Collatz+0x46>
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
add    $0x1,%eax
mov    %ebx,%ecx
shr    %ecx
test   $0x1,%bl
lea    0x1(%rbx,%rbx,2),%ebx
cmove  %ecx,%ebx
cmp    $0x1,%ebx
jg     1140 <Collatz+0x30>
pop    %rbx
ret

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

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