#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int p1, p2, p3, p4, p5, p6, p7, p8, p9, status;
  p1 = fork();
  p2 = fork();
  p3 = fork();
  printf("Process %d, with parent %d.\n", getpid(), getppid());
  if (p3 != 0)
    waitpid(p3, &status, 0);
  if (p2 != 0)
    waitpid(p2, &status, 0);
  if (p1 != 0)
    waitpid(p1, &status, 0);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x3c(%rbp),%esi
mov    %eax,%edx
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     11ce <main+0x6e>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmpl   $0x0,-0x18(%rbp)
je     11e6 <main+0x86>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmpl   $0x0,-0x14(%rbp)
je     11fe <main+0x9e>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe65(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
test   %ebx,%ebx
jne    11b9 <main+0x59>
test   %r15d,%r15d
jne    11cc <main+0x6c>
test   %r14d,%r14d
jne    11e0 <main+0x80>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %r15d,%r15d
je     11ad <main+0x4d>
lea    0x4(%rsp),%rsi
mov    %r15d,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %r14d,%r14d
je     11b2 <main+0x52>
lea    0x4(%rsp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"Process %d, with parent %d.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = call i32 (...) @fork()
store i32 %16, i32* %6
%17 = call i32 (...) @fork()
store i32 %17, i32* %7
%18 = call i32 (...) @fork()
store i32 %18, i32* %8
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getppid()
%21 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %20)
%22 = load i32, i32* %8
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = load i32, i32* %8
%26 = call i32 @waitpid(i32 %25, i32* %15, i32 0)
br label %27
27: ; preds = %24, %2
%28 = load i32, i32* %7
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* %7
%32 = call i32 @waitpid(i32 %31, i32* %15, i32 0)
br label %33
33: ; preds = %30, %27
%34 = load i32, i32* %6
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* %6
%38 = call i32 @waitpid(i32 %37, i32* %15, i32 0)
br label %39
39: ; preds = %36, %33
%40 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [29 x i8] c"Process %d, with parent %d.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
%6 = tail call i32 (...) @fork()
%7 = tail call i32 (...) @fork()
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (...) @getppid()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9)
%11 = icmp eq i32 %7, 0
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = call i32 @waitpid(i32 %7, i32* %3, i32 0)
br label %14
14: ; preds = %12, %2
%15 = icmp eq i32 %6, 0
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = call i32 @waitpid(i32 %6, i32* %3, i32 0)
br label %18
18: ; preds = %16, %14
%19 = icmp eq i32 %5, 0
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = call i32 @waitpid(i32 %5, i32* %3, i32 0)
br label %22
22: ; preds = %20, %18
%23 = call i32 @exit(i32 0)
unreachable
}