#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 collatz(unsigned long long n) {
  while (n >= 1) {
    if (n % 2 == 0) {
      printf("n is even\n");
      printf("n = %llu\n", n);
      n = n / 2;
    } else if ((n % 2 != 0) && (n != 1)) {
      printf("n is odd\n");
      printf("n = %llu\n", n);
      n = (n * 3) + 1;
    } else if (n == 1) {
      printf("n is odd\n");
      printf("n = %llu\n", n);
      break;
    } else {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<collatz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x1,-0x10(%rbp)
jb     11ff <collatz+0xef>
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    116a <collatz+0x5a>
lea    0xec0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11fa <collatz+0xea>
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     11b9 <collatz+0xa9>
cmpq   $0x1,-0x10(%rbp)
je     11b9 <collatz+0xa9>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
imul   $0x3,-0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11f5 <collatz+0xe5>
cmpq   $0x1,-0x10(%rbp)
jne    11e9 <collatz+0xd9>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ff <collatz+0xef>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <collatz+0xf6>
jmp    11fa <collatz+0xea>
jmp    111c <collatz+0xc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<collatz>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11bb <collatz+0x9b>
mov    %rdi,%rbx
lea    0xed8(%rip),%r14
lea    0xebe(%rip),%r15
lea    0xec1(%rip),%r12
jmp    116d <collatz+0x4d>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
shr    %rbx
test   %rbx,%rbx
je     11bb <collatz+0x9b>
test   $0x1,%bl
je     1150 <collatz+0x30>
cmp    $0x1,%rbx
je     119c <collatz+0x7c>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    (%rbx,%rbx,2),%rbx
add    $0x1,%rbx
test   %rbx,%rbx
jne    116d <collatz+0x4d>
jmp    11bb <collatz+0x9b>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
lea    0xe51(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"n is even\0A\00"
@.str.1 = [10 x i8] c"n = %llu\0A\00"
@.str.2 = [10 x i8] c"n is odd\0A\00"
define i32 @collatz(i64 %0) {
%2 = alloca i32
%3 = alloca i64
store i64 %0, i64* %3
br label %4
4: ; preds = %40, %1
%5 = load i64, i64* %3
%6 = icmp uge i64 %5, 1
br i1 %6, label %7, label %41
7: ; preds = %4
%8 = load i64, i64* %3
%9 = urem i64 %8, 2
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%13 = load i64, i64* %3
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 %13)
%15 = load i64, i64* %3
%16 = udiv i64 %15, 2
store i64 %16, i64* %3
br label %40
17: ; preds = %7
%18 = load i64, i64* %3
%19 = urem i64 %18, 2
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = load i64, i64* %3
%23 = icmp ne i64 %22, 1
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%26 = load i64, i64* %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 %26)
%28 = load i64, i64* %3
%29 = mul i64 %28, 3
%30 = add i64 %29, 1
store i64 %30, i64* %3
br label %39
31: ; preds = %21, %17
%32 = load i64, i64* %3
%33 = icmp eq i64 %32, 1
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%36 = load i64, i64* %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 %36)
br label %41
38: ; preds = %31
store i32 -1, i32* %2
br label %42
39: ; preds = %24
br label %40
40: ; preds = %39, %11
br label %4
41: ; preds = %34, %4
store i32 0, i32* %2
br label %42
42: ; preds = %41, %38
%43 = load i32, i32* %2
ret i32 %43
}

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