#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 /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getppid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  pid_t proc_id = fork();
  if (proc_id == -1) {
    fprintf(stderr, "Fork failure\n");
    exit(1);
  }
  if (proc_id == 0) {
    fprintf(stdout, "CHILD  : Parent PID=%ld\n", (long)getppid());
    fprintf(stdout, "CHILD  : sleep...\n");
    sleep(2);
    fprintf(stdout, "CHILD  : wake up...\n");
    fprintf(stdout, "CHILD  : Parent PID=%ld (init)\n", (long)getppid());
    exit(0);
  } else {
    sleep(1);
    fprintf(stdout, "PARENT : goodbye\n");
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119b <main+0x4b>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1232 <main+0xe2>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getppid@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x2,%edi
call   1060 <sleep@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getppid@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     1189 <main+0x39>
cmp    $0xffffffff,%eax
jne    11fd <main+0xad>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x2e48(%rip),%r14
mov    (%r14),%ebx
xor    %eax,%eax
call   1040 <getppid@plt>
lea    0xe6d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    (%r14),%edi
lea    0xe70(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x2,%edi
call   1060 <sleep@plt>
mov    (%r14),%edi
lea    0xe68(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    (%r14),%ebx
xor    %eax,%eax
call   1040 <getppid@plt>
lea    0xe65(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [14 x i8] c"Fork failure\0A\00"
@stdout = global i32 0
@.str.1 = [25 x i8] c"CHILD : Parent PID=%ld\0A\00"
@.str.2 = [19 x i8] c"CHILD : sleep...\0A\00"
@.str.3 = [21 x i8] c"CHILD : wake up...\0A\00"
@.str.4 = [32 x i8] c"CHILD : Parent PID=%ld (init)\0A\00"
@.str.5 = [18 x i8] c"PARENT : goodbye\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %14
%18 = load i32, i32* @stdout
%19 = call i64 (...) @getppid()
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %19)
%21 = load i32, i32* @stdout
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @sleep(i32 2)
%24 = load i32, i32* @stdout
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @stdout
%27 = call i64 (...) @getppid()
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i64 %27)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %14
%31 = call i32 @sleep(i32 1)
%32 = load i32, i32* @stdout
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%34 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"Fork failure\0A\00"
@stdout = global i32 0
@.str.1 = [25 x i8] c"CHILD : Parent PID=%ld\0A\00"
@.str.2 = [19 x i8] c"CHILD : sleep...\0A\00"
@.str.3 = [21 x i8] c"CHILD : wake up...\0A\00"
@.str.4 = [32 x i8] c"CHILD : Parent PID=%ld (init)\0A\00"
@.str.5 = [18 x i8] c"PARENT : goodbye\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %21 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = load i32, i32* @stdout
%10 = tail call i64 (...) @getppid()
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %10)
%12 = load i32, i32* @stdout
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @sleep(i32 2)
%15 = load i32, i32* @stdout
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%17 = load i32, i32* @stdout
%18 = tail call i64 (...) @getppid()
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i64 %18)
%20 = tail call i32 @exit(i32 0)
unreachable
21: ; preds = %2
%22 = tail call i32 @sleep(i32 1)
%23 = load i32, i32* @stdout
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%25 = tail call i32 @exit(i32 0)
unreachable
}