#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 fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int *);
int main(void) {
  pid_t child;
  int status;
  printf("I am the parent, my PID is %d\n", getpid());
  printf("My parent's PID is %d\n", getppid());
  printf("I am going to create a new process...\n");
  child = fork();
  if (child == -1) {
    printf("fork() failed.");
    return (-1);
  } else if (child == 0) {
    printf("I am the child, my PID is %d\n", getpid());
    printf("My parent's PID is %d\n", getppid());
  } else {
    wait(&status);
    printf("I am the parent, my PID is still %d\n", getpid());
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,%esi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c9 <main+0x79>
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <main+0xe2>
cmpl   $0x0,-0x8(%rbp)
jne    1206 <main+0xb6>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1226 <main+0xd6>
lea    -0xc(%rbp),%rdi
call   1060 <wait@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122b <main+0xdb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe93(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xeec(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11c6 <main+0x66>
cmp    $0xffffffff,%eax
jne    11ed <main+0x8d>
lea    0xe7e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1210 <main+0xb0>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe71(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe34(%rip),%rdi
jmp    1207 <main+0xa7>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe5c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [31 x i8] c"I am the parent, my PID is %d\0A\00"
@.str.1 = [23 x i8] c"My parent's PID is %d\0A\00"
@.str.2 = [39 x i8] c"I am going to create a new process...\0A\00"
@.str.3 = [15 x i8] c"fork() failed.\00"
@.str.4 = [30 x i8] c"I am the child, my PID is %d\0A\00"
@.str.5 = [37 x i8] c"I am the parent, my PID is still %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @getpid()
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 (...) @getppid()
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 (...) @fork()
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %28
14: ; preds = %0
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %18)
%20 = call i32 (...) @getppid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %26
22: ; preds = %14
%23 = call i32 @wait(i32* %3)
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %22, %17
br label %27
27: ; preds = %26
store i32 0, i32* %1
br label %28
28: ; preds = %27, %12
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [31 x i8] c"I am the parent, my PID is %d\0A\00"
@.str.1 = [23 x i8] c"My parent's PID is %d\0A\00"
@.str.3 = [15 x i8] c"fork() failed.\00"
@.str.4 = [30 x i8] c"I am the child, my PID is %d\0A\00"
@.str.5 = [37 x i8] c"I am the parent, my PID is still %d\0A\00"
@str = [38 x i8] c"I am going to create a new process...\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 (...) @getppid()
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 (...) @fork()
switch i32 %8, label %16 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %20
11: ; preds = %0
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %12)
%14 = tail call i32 (...) @getppid()
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %20
16: ; preds = %0
%17 = call i32 @wait(i32* %1)
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %16, %11, %9
%21 = phi i32 [ -1, %9 ], [ 0, %11 ], [ 0, %16 ]
ret i32 %21
}