#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  pid_t pid;
  int n;
  if (argc != 2) {
    printf("This program expects exactly 1 parameter not: %d\n", argc);
    return 0;
  }
  n = atoi(argv[1]);
  if (n < 1) {
    printf("The collatz conjecture only works for numbers > 0.\n");
    return 0;
  }
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork Failed");
    return 1;
  } else if (pid == 0) {
    while (n != 1) {
      if (n & 1)
        n = 3 * n + 1;
      else
        n >>= 1;
      printf("%d\n", n);
    }
    exit(0);
  } else {
    wait(NULL);
    printf("Child Complete\n");
  }
  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)
je     119d <main+0x3d>
mov    -0x8(%rbp),%esi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1289 <main+0x129>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    11d1 <main+0x71>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1289 <main+0x129>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1208 <main+0xa8>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1289 <main+0x129>
cmpq   $0x0,-0x18(%rbp)
jne    1266 <main+0x106>
jmp    1218 <main+0xb8>
cmpl   $0x1,-0x1c(%rbp)
je     125f <main+0xff>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1240 <main+0xe0>
imul   $0x3,-0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1249 <main+0xe9>
mov    -0x1c(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1218 <main+0xb8>
xor    %edi,%edi
call   1060 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1282 <main+0x122>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11ad <main+0x3d>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
test   %eax,%eax
jle    11c3 <main+0x53>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11d3 <main+0x63>
je     11f7 <main+0x87>
xor    %ebx,%ebx
xor    %edi,%edi
call   1080 <wait@plt>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
jmp    11ed <main+0x7d>
mov    %edi,%eax
lea    0xe4a(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11ed <main+0x7d>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    11ed <main+0x7d>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
cmp    $0x1,%ebx
je     1222 <main+0xb2>
lea    0xe2b(%rip),%r14
mov    %ebx,%eax
sar    %eax
test   $0x1,%bl
lea    0x1(%rbx,%rbx,2),%ebx
cmove  %eax,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x1,%ebx
jne    1203 <main+0x93>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [50 x i8] c"This program expects exactly 1 parameter not: %d\0A\00"
@.str.1 = [52 x i8] c"The collatz conjecture only works for numbers > 0.\0A\00"
@stderr = global i32 0
@.str.2 = [12 x i8] c"Fork Failed\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [16 x i8] c"Child Complete\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* %4
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 0, i32* %3
br label %57
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %57
22: ; preds = %13
%23 = call i64 (...) @fork()
store i64 %23, i64* %6
%24 = load i64, i64* %6
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %57
29: ; preds = %22
%30 = load i64, i64* %6
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %52
32: ; preds = %29
br label %33
33: ; preds = %47, %32
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %50
36: ; preds = %33
%37 = load i32, i32* %7
%38 = and i32 %37, 1
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i32, i32* %7
%42 = mul nsw i32 3, %41
%43 = add nsw i32 %42, 1
store i32 %43, i32* %7
br label %47
44: ; preds = %36
%45 = load i32, i32* %7
%46 = ashr i32 %45, 1
store i32 %46, i32* %7
br label %47
47: ; preds = %44, %40
%48 = load i32, i32* %7
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %48)
br label %33
50: ; preds = %33
%51 = call i32 @exit(i32 0)
unreachable
52: ; preds = %29
%53 = call i32 @wait(i32* null)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %52
br label %56
56: ; preds = %55
store i32 0, i32* %3
br label %57
57: ; preds = %56, %26, %20, %10
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str = [50 x i8] c"This program expects exactly 1 parameter not: %d\0A\00"
@stderr = global i32 0
@.str.2 = [12 x i8] c"Fork Failed\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@str = [15 x i8] c"Child Complete\00"
@str.5 = [51 x i8] c"The collatz conjecture only works for numbers > 0.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %38
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.5, i64 0, i64 0))
br label %38
13: ; preds = %6
%14 = tail call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %38
19: ; preds = %13
%20 = icmp eq i64 %14, 0
br i1 %20, label %21, label %35
21: ; preds = %19
%22 = icmp eq i32 %9, 1
br i1 %22, label %33, label %23
23: ; preds = %21, %23
%24 = phi i32 [ %30, %23 ], [ %9, %21 ]
%25 = and i32 %24, 1
%26 = icmp eq i32 %25, 0
%27 = mul nsw i32 %24, 3
%28 = add nsw i32 %27, 1
%29 = ashr i32 %24, 1
%30 = select i1 %26, i32 %29, i32 %28
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = icmp eq i32 %30, 1
br i1 %32, label %33, label %23
33: ; preds = %23, %21
%34 = tail call i32 @exit(i32 0)
unreachable
35: ; preds = %19
%36 = tail call i32 @wait(i32* null)
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %35, %16, %11, %4
%39 = phi i32 [ 0, %4 ], [ 0, %11 ], [ 1, %16 ], [ 0, %35 ]
ret i32 %39
}