#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 /*<<< orphan*/ scanf(char *, int *);
int main() {
  int init, steps;
  steps = 0;
  printf("Inital value: ");
  scanf("%d", &init);
  if (init < 2) {
    printf("Error.");
  } else {
    printf("Initial value is %d\n", init);
    while (init != 1) {
      steps++;
      if (init % 2 == 0) {
        init = init / 2;
        printf("Next value is %d\n", init);
      } else {
        init = (init * 3) + 1;
        printf("Next value is %d\n", init);
      }
    }
    printf("Final value %d, number of steps %d\n", init, steps);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xec3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec4(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
cmpl   $0x2,-0x8(%rbp)
jge    1171 <main+0x51>
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1201 <main+0xe1>
mov    -0x8(%rbp),%esi
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
je     11ed <main+0xcd>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11cd <main+0xad>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e8 <main+0xc8>
imul   $0x3,-0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1182 <main+0x62>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xed5(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xed4(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1040 <scanf@plt>
mov    0x4(%rsp),%esi
cmp    $0x1,%esi
jg     115e <main+0x3e>
lea    0xebd(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    118a <main+0x6a>
lea    0xeb4(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x4(%rsp),%eax
cmp    $0x1,%eax
jne    1194 <main+0x74>
lea    0xec4(%rip),%rdi
mov    $0x1,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe93(%rip),%r14
xor    %ebx,%ebx
jmp    11c0 <main+0xa0>
nop
lea    (%rax,%rax,2),%esi
add    $0x1,%esi
mov    %esi,0x4(%rsp)
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x4(%rsp),%eax
add    $0x1,%ebx
cmp    $0x1,%eax
je     1175 <main+0x55>
test   $0x1,%al
jne    11a0 <main+0x80>
mov    %eax,%esi
shr    $0x1f,%esi
add    %eax,%esi
sar    %esi
jmp    11a6 <main+0x86>

LLVM IR (-O0):
@.str = [15 x i8] c"Inital value: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [7 x i8] c"Error.\00"
@.str.3 = [21 x i8] c"Initial value is %d\0A\00"
@.str.4 = [18 x i8] c"Next value is %d\0A\00"
@.str.5 = [36 x i8] c"Final value %d, number of steps %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 2
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %38
10: ; preds = %0
%11 = load i32, i32* %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %33, %10
%14 = load i32, i32* %2
%15 = icmp ne i32 %14, 1
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
%19 = load i32, i32* %2
%20 = srem i32 %19, 2
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i32, i32* %2
%24 = sdiv i32 %23, 2
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %25)
br label %33
27: ; preds = %16
%28 = load i32, i32* %2
%29 = mul nsw i32 %28, 3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %2
%31 = load i32, i32* %2
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %27, %22
br label %13
34: ; preds = %13
%35 = load i32, i32* %2
%36 = load i32, i32* %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %35, i32 %36)
br label %38
38: ; preds = %34, %8
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [15 x i8] c"Inital value: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [7 x i8] c"Error.\00"
@.str.3 = [21 x i8] c"Initial value is %d\0A\00"
@.str.4 = [18 x i8] c"Next value is %d\0A\00"
@.str.5 = [36 x i8] c"Final value %d, number of steps %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = icmp slt i32 %5, 2
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %32
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %5)
%11 = load i32, i32* %1
%12 = icmp eq i32 %11, 1
br i1 %12, label %29, label %13
13: ; preds = %9, %24
%14 = phi i32 [ %27, %24 ], [ %11, %9 ]
%15 = phi i32 [ %16, %24 ], [ 0, %9 ]
%16 = add nuw nsw i32 %15, 1
%17 = and i32 %14, 1
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = sdiv i32 %14, 2
br label %24
21: ; preds = %13
%22 = mul nsw i32 %14, 3
%23 = add nsw i32 %22, 1
br label %24
24: ; preds = %21, %19
%25 = phi i32 [ %23, %21 ], [ %20, %19 ]
store i32 %25, i32* %1
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %1
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %13
29: ; preds = %24, %9
%30 = phi i32 [ 0, %9 ], [ %16, %24 ]
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 1, i32 %30)
br label %32
32: ; preds = %29, %7
ret i32 0
}