#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t p1, p2;
  if ((p1 = fork()) == -1) {
    perror("Process Creation Fail.");
    exit(1);
  }
  if (p1 == 0) {
    puts("Child 1");
    return 0;
  }
  wait(0);
  if ((p2 = fork()) == -1) {
    perror("Process Creation Fail.");
    exit(1);
  }
  if (p2 == 0) {
    puts("Child 2");
    return 0;
  }
  wait(0);
  puts("Parent");
  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)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    118f <main+0x3f>
lea    0xe80(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11b1 <main+0x61>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    121d <main+0xcd>
xor    %edi,%edi
call   1060 <wait@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11e1 <main+0x91>
lea    0xe2e(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1203 <main+0xb3>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    121d <main+0xcd>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     1188 <main+0x38>
cmp    $0xffffffff,%eax
je     11a1 <main+0x51>
xor    %edi,%edi
call   1060 <wait@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     1191 <main+0x41>
cmp    $0xffffffff,%eax
je     11a1 <main+0x51>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xea1(%rip),%rdi
jmp    1198 <main+0x48>
lea    0xe88(%rip),%rdi
jmp    1198 <main+0x48>
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe58(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Process Creation Fail.\00"
@.str.1 = [8 x i8] c"Child 1\00"
@.str.2 = [8 x i8] c"Child 2\00"
@.str.3 = [7 x i8] c"Parent\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %6
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %33
18: ; preds = %13
%19 = call i32 @wait(i32 0)
%20 = call i32 (...) @fork()
store i32 %20, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %33
30: ; preds = %25
%31 = call i32 @wait(i32 0)
%32 = call i32 @puts(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %33
33: ; preds = %30, %28, %16
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [23 x i8] c"Process Creation Fail.\00"
@.str.1 = [8 x i8] c"Child 1\00"
@.str.2 = [8 x i8] c"Child 2\00"
@.str.3 = [7 x i8] c"Parent\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %7 [
i32 -1, label %4
i32 0, label %15
]
4: ; preds = %2
%5 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = tail call i32 @wait(i32 0)
%9 = tail call i32 (...) @fork()
switch i32 %9, label %13 [
i32 -1, label %10
i32 0, label %15
]
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %7
%14 = tail call i32 @wait(i32 0)
br label %15
15: ; preds = %7, %2, %13
%16 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), %13 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %7 ]
%17 = tail call i32 @puts(i8* dereferenceable(1) %16)
ret i32 0
}