#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 */
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ wait();
int main() {
  pid_t pid;
  pid = fork();
  if (pid == 0)
    printf("Process 2 = %d, parent = %d.\n", getpid(), getppid());
  else if (pid > 0) {
    printf("Process 1 = %d, parent = %d.\n", getpid(), getppid());
    wait();
    pid = fork();
    if (pid > 0)
      wait();
    else if (pid == 0)
      printf("Process 3 = %d, parent = %d.\n", getpid(), getppid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119c <main+0x4c>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
jmp    122e <main+0xde>
cmpq   $0x0,-0x10(%rbp)
jle    1229 <main+0xd9>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <wait@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    11f2 <main+0xa2>
mov    $0x0,%al
call   1060 <wait@plt>
jmp    1224 <main+0xd4>
cmpq   $0x0,-0x10(%rbp)
jne    121f <main+0xcf>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
jmp    1224 <main+0xd4>
jmp    1229 <main+0xd9>
jmp    122e <main+0xde>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     119b <main+0x4b>
jle    11bb <main+0x6b>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xea8(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <wait@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jle    11bf <main+0x6f>
xor    %eax,%eax
call   1060 <wait@plt>
jmp    11bb <main+0x6b>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe4e(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
jne    11bb <main+0x6b>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe64(%rip),%rdi
jmp    11b2 <main+0x62>

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

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