#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 /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main() {
  int prPID = 0, chdStatus = 0;
  pid_t waitPID = 0;
  printf("Entering main function\n");
  if ((prPID = fork()) == 0) {
    printf("We are in child\n");
    sleep(5);
  } else {
    printf("We are in parent, child's PID: %d\n", prPID);
    waitPID = wait(&chdStatus);
    printf("waitPID: %d\tchdStatus: %d\n", waitPID, chdStatus);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jne    11a2 <main+0x62>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x5,%edi
call   1040 <sleep@plt>
jmp    11d3 <main+0x93>
mov    -0x8(%rbp),%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0xc(%rbp),%rdi
call   1050 <wait@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
movl   $0x0,0x4(%rsp)
lea    0xede(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     11a0 <main+0x50>
lea    0xe89(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x4(%rsp),%rdi
call   1060 <wait@plt>
mov    0x4(%rsp),%edx
lea    0xe8e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b6 <main+0x66>
lea    0xeae(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   1050 <sleep@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Entering main function\0A\00"
@.str.1 = [17 x i8] c"We are in child\0A\00"
@.str.2 = [35 x i8] c"We are in parent, child's PID: %d\0A\00"
@.str.3 = [27 x i8] c"waitPID: %d\09chdStatus: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (...) @fork()
store i32 %6, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @sleep(i32 5)
br label %18
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = call i32 @wait(i32* %3)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = load i32, i32* %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %16)
br label %18
18: ; preds = %11, %8
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [35 x i8] c"We are in parent, child's PID: %d\0A\00"
@.str.3 = [27 x i8] c"waitPID: %d\09chdStatus: %d\0A\00"
@str = [23 x i8] c"Entering main function\00"
@str.4 = [16 x i8] c"We are in child\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0))
%8 = tail call i32 @sleep(i32 5)
br label %14
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %4)
%11 = call i32 @wait(i32* %1)
%12 = load i32, i32* %1
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %11, i32 %12)
br label %14
14: ; preds = %9, %6
ret i32 0
}