#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 */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
void main() {
  pid_t pid = fork();
  if (pid != 0) {
    wait(0);
    printf("PID = %d\n", getpid());
  } else {
    fork();
    wait(0);
    fork();
    wait(0);
    printf("PID = %d\n", getpid());
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     118f <main+0x3f>
xor    %edi,%edi
call   1060 <wait@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
jmp    11c7 <main+0x77>
mov    $0x0,%al
call   1070 <fork@plt>
xor    %edi,%edi
call   1060 <wait@plt>
mov    $0x0,%al
call   1070 <fork@plt>
xor    %edi,%edi
call   1060 <wait@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     117a <main+0x2a>
xor    %edi,%edi
call   1060 <wait@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8e(%rip),%rdi
mov    %eax,%esi
pop    %rax
jmp    1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
xor    %edi,%edi
call   1060 <wait@plt>
xor    %eax,%eax
call   1070 <fork@plt>
xor    %edi,%edi
call   1060 <wait@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe5c(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"PID = %d\0A\00"
define void @main() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = load i64, i64* %1
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = call i32 @wait(i32 0)
%7 = call i32 (...) @getpid()
%8 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %17
9: ; preds = %0
%10 = call i64 (...) @fork()
%11 = call i32 @wait(i32 0)
%12 = call i64 (...) @fork()
%13 = call i32 @wait(i32 0)
%14 = call i32 (...) @getpid()
%15 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %5
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"PID = %d\0A\00"
define void @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %7, label %3
3: ; preds = %0
%4 = tail call i32 @wait(i32 0)
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
ret void
7: ; preds = %0
%8 = tail call i64 (...) @fork()
%9 = tail call i32 @wait(i32 0)
%10 = tail call i64 (...) @fork()
%11 = tail call i32 @wait(i32 0)
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = tail call i32 @exit(i32 0)
unreachable
}