#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 */
struct tree_node {
  int dummy;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGCONT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ explain_wait_status(scalar_t__, int);
scalar_t__ fork();
int /*<<< orphan*/ fork_procs(struct tree_node *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct tree_node *get_tree_from_file(char *);
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ show_pstree(scalar_t__);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ wait_for_ready_children(int);
int main(int argc, char *argv[]) {
  pid_t pid;
  int status;
  struct tree_node *root;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <tree_file>\n", argv[0]);
    exit(1);
  }
  root = get_tree_from_file(argv[1]);
  pid = fork();
  if (pid < 0) {
    perror("main: fork");
    exit(1);
  }
  if (pid == 0) {
    fork_procs(root);
    exit(1);
  }
  wait_for_ready_children(1);
  show_pstree(pid);
  kill(pid, SIGCONT);
  wait(&status);
  explain_wait_status(pid, status);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11f6 <main+0x46>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <get_tree_from_file@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1233 <main+0x83>
lea    0xdf3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1251 <main+0xa1>
mov    -0x28(%rbp),%rdi
call   10a0 <fork_procs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x1,%edi
call   1040 <wait_for_ready_children@plt>
mov    -0x18(%rbp),%rdi
call   1070 <show_pstree@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
lea    -0x1c(%rbp),%rdi
call   10b0 <wait@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   10d0 <explain_wait_status@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    1219 <main+0x69>
mov    0x8(%rsi),%rdi
call   1030 <get_tree_from_file@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     123b <main+0x8b>
je     1251 <main+0xa1>
mov    %rax,%rbx
mov    $0x1,%edi
call   1040 <wait_for_ready_children@plt>
mov    %rbx,%rdi
call   1070 <show_pstree@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <kill@plt>
lea    0x4(%rsp),%rdi
call   10b0 <wait@plt>
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
call   10d0 <explain_wait_status@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdd4(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdd5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %r14,%rdi
call   10a0 <fork_procs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.tree_node = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s <tree_file>\0A\00"
@.str.1 = [11 x i8] c"main: fork\00"
@SIGCONT = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.tree_node*
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 ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call %struct.tree_node* @get_tree_from_file(i8* %21)
store %struct.tree_node* %22, %struct.tree_node** %8
%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 = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %18
%30 = load i64, i64* %6
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load %struct.tree_node*, %struct.tree_node** %8
%34 = call i32 @fork_procs(%struct.tree_node* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %29
%37 = call i32 @wait_for_ready_children(i32 1)
%38 = load i64, i64* %6
%39 = call i32 @show_pstree(i64 %38)
%40 = load i64, i64* %6
%41 = load i32, i32* @SIGCONT
%42 = call i32 @kill(i64 %40, i32 %41)
%43 = call i32 @wait(i32* %7)
%44 = load i64, i64* %6
%45 = load i32, i32* %7
%46 = call i32 @explain_wait_status(i64 %44, i32 %45)
ret i32 0
}

LLVM IR (-O2):
%struct.tree_node = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s <tree_file>\0A\00"
@.str.1 = [11 x i8] c"main: fork\00"
@SIGCONT = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call %struct.tree_node* @get_tree_from_file(i8* %13)
%15 = tail call i64 (...) @fork()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = icmp eq i64 %15, 0
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = tail call i32 @fork_procs(%struct.tree_node* %14)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %20
%26 = tail call i32 @wait_for_ready_children(i32 1)
%27 = tail call i32 @show_pstree(i64 %15)
%28 = load i32, i32* @SIGCONT
%29 = tail call i32 @kill(i64 %15, i32 %28)
%30 = call i32 @wait(i32* %3)
%31 = load i32, i32* %3
%32 = call i32 @explain_wait_status(i64 %15, i32 %31)
ret i32 0
}