#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ puts(char *);
scalar_t__ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr,
            "The program has to be run with a number as argument <%s num>",
            argv[0]);
    exit(1);
  }
  int val = 0, save, i;
  if (sscanf(argv[1], "%d", &val) && val >= 1) {
    save = val;
    for (i = 0; val != 1 && val > 1; i++) {
      if (val % 2 == 0) {
        val >>= 1;
      } else {
        val = (val << 1) + val + 1;
      }
    }
    if (val < 0) {
      printf("Detected integer overflow after %d steps!\n", i);
    } else {
      printf("The length of the collatz sequence starting at %d is %d\n", save,
             i);
    }
  } else if (val <= 0) {
    puts("The number has to be greater than 0");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1196 <main+0x46>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe79(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe91(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x0,%rax
je     127c <main+0x12c>
cmpl   $0x1,-0x14(%rbp)
jl     127c <main+0x12c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x14(%rbp)
mov    %al,-0x1d(%rbp)
je     11ef <main+0x9f>
cmpl   $0x1,-0x14(%rbp)
setg   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11ff <main+0xaf>
jmp    1243 <main+0xf3>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1221 <main+0xd1>
mov    -0x14(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1230 <main+0xe0>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
add    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1235 <main+0xe5>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d6 <main+0x86>
cmpl   $0x0,-0x14(%rbp)
jge    1263 <main+0x113>
mov    -0x1c(%rbp),%esi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1277 <main+0x127>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1297 <main+0x147>
cmpl   $0x0,-0x14(%rbp)
jg     1292 <main+0x142>
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
jmp    1297 <main+0x147>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
cmp    $0x1,%edi
jle    11e3 <main+0x93>
movl   $0x0,0x4(%rsp)
mov    0x8(%rsi),%rdi
lea    0xed0(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1060 <sscanf@plt>
mov    0x4(%rsp),%esi
test   %rax,%rax
je     11bd <main+0x6d>
test   %esi,%esi
jle    11bd <main+0x6d>
xor    %edx,%edx
cmp    $0x2,%esi
jb     11ad <main+0x5d>
xor    %edx,%edx
mov    %esi,%eax
nop
mov    %eax,%ecx
shr    %ecx
test   $0x1,%al
lea    0x1(%rax,%rax,2),%eax
cmove  %ecx,%eax
add    $0x1,%edx
cmp    $0x1,%eax
jg     1190 <main+0x40>
mov    %eax,0x4(%rsp)
test   %eax,%eax
js     11cf <main+0x7f>
lea    0xeb7(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11df <main+0x8f>
test   %esi,%esi
jg     11df <main+0x8f>
lea    0xedc(%rip),%rdi
call   1030 <puts@plt>
jmp    11df <main+0x8f>
lea    0xe6a(%rip),%rdi
mov    %edx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe0a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [61 x i8] c"The program has to be run with a number as argument <%s num>\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [43 x i8] c"Detected integer overflow after %d steps!\0A\00"
@.str.3 = [57 x i8] c"The length of the collatz sequence starting at %d is %d\0A\00"
@.str.4 = [36 x i8] c"The number has to be greater than 0\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
store i32 0, i32* %6
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @sscanf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %65
24: ; preds = %18
%25 = load i32, i32* %6
%26 = icmp sge i32 %25, 1
br i1 %26, label %27, label %65
27: ; preds = %24
%28 = load i32, i32* %6
store i32 %28, i32* %7
store i32 0, i32* %8
br label %29
29: ; preds = %51, %27
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 1
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* %6
%34 = icmp sgt i32 %33, 1
br label %35
35: ; preds = %32, %29
%36 = phi i1 [ false, %29 ], [ %34, %32 ]
br i1 %36, label %37, label %54
37: ; preds = %35
%38 = load i32, i32* %6
%39 = srem i32 %38, 2
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32, i32* %6
%43 = ashr i32 %42, 1
store i32 %43, i32* %6
br label %50
44: ; preds = %37
%45 = load i32, i32* %6
%46 = shl i32 %45, 1
%47 = load i32, i32* %6
%48 = add nsw i32 %46, %47
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
br label %50
50: ; preds = %44, %41
br label %51
51: ; preds = %50
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %29
54: ; preds = %35
%55 = load i32, i32* %6
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i32, i32* %8
%59 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %58)
br label %64
60: ; preds = %54
%61 = load i32, i32* %7
%62 = load i32, i32* %8
%63 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i32 %61, i32 %62)
br label %64
64: ; preds = %60, %57
br label %71
65: ; preds = %24, %18
%66 = load i32, i32* %6
%67 = icmp sle i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %65
%69 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %70
70: ; preds = %68, %65
br label %71
71: ; preds = %70, %64
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [61 x i8] c"The program has to be run with a number as argument <%s num>\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [43 x i8] c"Detected integer overflow after %d steps!\0A\00"
@.str.3 = [57 x i8] c"The length of the collatz sequence starting at %d is %d\0A\00"
@.str.4 = [36 x i8] c"The number has to be greater than 0\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @sscanf(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%15 = icmp ne i64 %14, 0
%16 = load i32, i32* %3
%17 = icmp sgt i32 %16, 0
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %19, label %39
19: ; preds = %10
%20 = icmp ugt i32 %16, 1
br i1 %20, label %21, label %36
21: ; preds = %19, %21
%22 = phi i32 [ %30, %21 ], [ 0, %19 ]
%23 = phi i32 [ %29, %21 ], [ %16, %19 ]
%24 = and i32 %23, 1
%25 = icmp eq i32 %24, 0
%26 = mul i32 %23, 3
%27 = add nsw i32 %26, 1
%28 = lshr i32 %23, 1
%29 = select i1 %25, i32 %28, i32 %27
%30 = add nuw nsw i32 %22, 1
%31 = icmp sgt i32 %29, 1
br i1 %31, label %21, label %32
32: ; preds = %21
store i32 %29, i32* %3
%33 = icmp slt i32 %29, 0
br i1 %33, label %34, label %36
34: ; preds = %32
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %43
36: ; preds = %19, %32
%37 = phi i32 [ %30, %32 ], [ 0, %19 ]
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i32 %16, i32 %37)
br label %43
39: ; preds = %10
%40 = icmp slt i32 %16, 1
br i1 %40, label %41, label %43
41: ; preds = %39
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %39, %41, %34, %36
ret i32 0
}