#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 collatz(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getUserInput(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(scalar_t__);
int main() {
  pid_t pid;
  int num = 0;
  num = getUserInput(num);
  while (num > 0) {
    pid = fork();
    if (pid < 0) {
      fprintf(stderr, "Fork Failed");
      return 1;
    } else if (pid == 0) {
      int col = num;
      while (col > 1) {
        printf(" %d, ", col);
        col = collatz(col);
      }
      printf(" %d\n", col);
      return 0;
    } else {
      wait(pid);
      num = getUserInput(num);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <getUserInput@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1235 <main+0xd5>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11c2 <main+0x62>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123c <main+0xdc>
cmpq   $0x0,-0x10(%rbp)
jne    1217 <main+0xb7>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jle    11fc <main+0x9c>
mov    -0x18(%rbp),%esi
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1030 <collatz@plt>
mov    %eax,-0x18(%rbp)
jmp    11d3 <main+0x73>
mov    -0x18(%rbp),%esi
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    123c <main+0xdc>
mov    -0x10(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x14(%rbp),%edi
call   1060 <getUserInput@plt>
mov    %eax,-0x14(%rbp)
jmp    1230 <main+0xd0>
jmp    1181 <main+0x21>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %ebp,%ebp
xor    %edi,%edi
call   1060 <getUserInput@plt>
test   %eax,%eax
jle    1208 <main+0xa8>
mov    %eax,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11a5 <main+0x45>
je     11c1 <main+0x61>
mov    %rax,%rdi
call   1070 <wait@plt>
mov    %ebx,%edi
call   1060 <getUserInput@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1180 <main+0x20>
jmp    1206 <main+0xa6>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%ebp
jmp    1208 <main+0xa8>
mov    $0x1,%ebp
cmp    $0x2,%ebx
jb     11f8 <main+0x98>
lea    0xe3a(%rip),%r14
mov    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1030 <collatz@plt>
mov    %eax,%ebp
cmp    $0x1,%eax
jg     11e0 <main+0x80>
lea    0xe13(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"Fork Failed\00"
@.str.1 = [6 x i8] c" %d, \00"
@.str.2 = [5 x i8] c" %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @getUserInput(i32 %5)
store i32 %6, i32* %3
br label %7
7: ; preds = %39, %0
%8 = load i32, i32* %3
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %40
10: ; preds = %7
%11 = call i64 (...) @fork()
store i64 %11, i64* %2
%12 = load i64, i64* %2
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %41
17: ; preds = %10
%18 = load i64, i64* %2
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %33
20: ; preds = %17
%21 = load i32, i32* %3
store i32 %21, i32* %4
br label %22
22: ; preds = %25, %20
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 1
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %4
%29 = call i32 @collatz(i32 %28)
store i32 %29, i32* %4
br label %22
30: ; preds = %22
%31 = load i32, i32* %4
%32 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %31)
store i32 0, i32* %1
br label %41
33: ; preds = %17
%34 = load i64, i64* %2
%35 = call i32 @wait(i64 %34)
%36 = load i32, i32* %3
%37 = call i32 @getUserInput(i32 %36)
store i32 %37, i32* %3
br label %38
38: ; preds = %33
br label %39
39: ; preds = %38
br label %7
40: ; preds = %7
store i32 0, i32* %1
br label %41
41: ; preds = %40, %30, %14
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"Fork Failed\00"
@.str.1 = [6 x i8] c" %d, \00"
@.str.2 = [5 x i8] c" %d\0A\00"
define i32 @main() {
%1 = tail call i32 @getUserInput(i32 0)
%2 = icmp sgt i32 %1, 0
br i1 %2, label %3, label %26
3: ; preds = %0, %22
%4 = phi i32 [ %24, %22 ], [ %1, %0 ]
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %3
%11 = icmp eq i64 %5, 0
br i1 %11, label %12, label %22
12: ; preds = %10
%13 = icmp ugt i32 %4, 1
br i1 %13, label %14, label %19
14: ; preds = %12, %14
%15 = phi i32 [ %17, %14 ], [ %4, %12 ]
%16 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = tail call i32 @collatz(i32 %15)
%18 = icmp sgt i32 %17, 1
br i1 %18, label %14, label %19
19: ; preds = %14, %12
%20 = phi i32 [ 1, %12 ], [ %17, %14 ]
%21 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %26
22: ; preds = %10
%23 = tail call i32 @wait(i64 %5)
%24 = tail call i32 @getUserInput(i32 %4)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %3, label %26
26: ; preds = %22, %0, %19, %7
%27 = phi i32 [ 1, %7 ], [ 0, %19 ], [ 0, %0 ], [ 0, %22 ]
ret i32 %27
}