#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 int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ num1;

/* Variables and functions */
int EXIT_FAILURE;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ explain_wait_status(int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct tree_node *get_tree_from_file(char *);
int /*<<< orphan*/ make_proc_tree(struct tree_node *, int *);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read(int, int *, int);
int /*<<< orphan*/ show_pstree(int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int fd[2];
  int num1;
  pid_t pid;
  int status;
  struct tree_node *root;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <input_tree_file>\n\n", argv[0]);
    exit(1);
  }
  root = get_tree_from_file(argv[1]);
  if (pipe(fd)) {
    perror("pipe");
    return EXIT_FAILURE;
  }
  pid = make_proc_tree(root, fd);
  sleep(3);
  show_pstree(pid);
  pid = wait(&status);
  explain_wait_status(pid, status);
  if (read(fd[0], &num1, sizeof(num1)) < 0) {
    perror("read pipe");
    exit(-1);
  }
  printf("\nFinal output => %d \n", num1);
  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)
je     1206 <main+0x46>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe09(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <get_tree_from_file@plt>
mov    %rax,-0x30(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     1247 <main+0x87>
lea    0xdf2(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c7 <main+0x107>
mov    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <make_proc_tree@plt>
mov    %eax,-0x20(%rbp)
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    -0x20(%rbp),%edi
call   1090 <show_pstree@plt>
lea    -0x24(%rbp),%rdi
call   10d0 <wait@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
call   10e0 <explain_wait_status@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    12b1 <main+0xf1>
lea    0xd7c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%esi
lea    0xd6d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    1262 <main+0xa2>
mov    0x8(%rsi),%rdi
call   1030 <get_tree_from_file@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    124b <main+0x8b>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <make_proc_tree@plt>
mov    %eax,%ebx
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    %ebx,%edi
call   1090 <show_pstree@plt>
mov    %rsp,%rdi
call   10d0 <wait@plt>
mov    (%rsp),%esi
mov    %eax,%edi
call   10e0 <explain_wait_status@plt>
mov    0x8(%rsp),%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
test   %rax,%rax
js     1284 <main+0xc4>
mov    0x4(%rsp),%esi
lea    0xdea(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdd1(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
jmp    1245 <main+0x85>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd8b(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd93(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.tree_node = type { i32 }
@stderr = global i32 0
@.str = [30 x i8] c"Usage: %s <input_tree_file>\0A\0A\00"
@.str.1 = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [10 x i8] c"read pipe\00"
@.str.3 = [22 x i8] c"\0AFinal output => %d \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.tree_node*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call %struct.tree_node* @get_tree_from_file(i8* %23)
store %struct.tree_node* %24, %struct.tree_node** %10
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%26 = call i64 @pipe(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
store i32 %30, i32* %3
br label %52
31: ; preds = %20
%32 = load %struct.tree_node*, %struct.tree_node** %10
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%34 = call i32 @make_proc_tree(%struct.tree_node* %32, i32* %33)
store i32 %34, i32* %8
%35 = call i32 @sleep(i32 3)
%36 = load i32, i32* %8
%37 = call i32 @show_pstree(i32 %36)
%38 = call i32 @wait(i32* %9)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = load i32, i32* %9
%41 = call i32 @explain_wait_status(i32 %39, i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i64 @read(i32 %43, i32* %7, i32 4)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %31
%47 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %31
%50 = load i32, i32* %7
%51 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %50)
store i32 0, i32* %3
br label %52
52: ; preds = %49, %28
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.tree_node = type { i32 }
@stderr = global i32 0
@.str = [30 x i8] c"Usage: %s <input_tree_file>\0A\0A\00"
@.str.1 = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [10 x i8] c"read pipe\00"
@.str.3 = [22 x i8] c"\0AFinal output => %d \0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = bitcast [2 x i32]* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = icmp eq i32 %0, 2
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call %struct.tree_node* @get_tree_from_file(i8* %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%20 = call i64 @pipe(i32* %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
br label %41
25: ; preds = %15
%26 = call i32 @make_proc_tree(%struct.tree_node* %18, i32* %19)
%27 = call i32 @sleep(i32 3)
%28 = call i32 @show_pstree(i32 %26)
%29 = call i32 @wait(i32* %5)
%30 = load i32, i32* %5
%31 = call i32 @explain_wait_status(i32 %29, i32 %30)
%32 = load i32, i32* %19
%33 = call i64 @read(i32 %32, i32* %4, i32 4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %25
%39 = load i32, i32* %4
%40 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %38, %22
%42 = phi i32 [ %24, %22 ], [ 0, %38 ]
ret i32 %42
}