#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 EXIT_SUCCESS;
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  pid_t pid = -1;
  printf("Hello Fork - %d\n", getpid());
  switch ((pid = fork())) {
  case 0:
    printf("In the child[%d] - parent %d\n", getpid(), getppid());
    break;
  case -1:
    printf("Failed to fork\n");
    break;
  default:
    printf("Parent %d forked a child with pid %d\n", getpid(), pid);
    break;
  }
  printf("Good bye fork %d\n", getpid());
  return EXIT_SUCCESS;
}


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)
movl   $0xffffffff,-0x14(%rbp)
xor    %eax,%eax
mov    %al,-0x19(%rbp)
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x19(%rbp),%al
lea    0xe8d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x19(%rbp),%al
call   1060 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11cf <main+0x8f>
jmp    1196 <main+0x56>
mov    -0x18(%rbp),%eax
test   %eax,%eax
jne    11e2 <main+0xa2>
jmp    11a6 <main+0x66>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11fc <main+0xbc>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11fc <main+0xbc>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xea1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
cmp    $0xffffffff,%eax
je     1197 <main+0x47>
mov    %eax,%ebx
xor    %eax,%eax
test   %ebx,%ebx
jne    11a5 <main+0x55>
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe80(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
jmp    11b5 <main+0x65>
lea    0xec9(%rip),%rdi
call   1030 <puts@plt>
jmp    11bc <main+0x6c>
call   1040 <getpid@plt>
lea    0xe7e(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Hello Fork - %d\0A\00"
@.str.1 = [30 x i8] c"In the child[%d] - parent %d\0A\00"
@.str.2 = [16 x i8] c"Failed to fork\0A\00"
@.str.3 = [38 x i8] c"Parent %d forked a child with pid %d\0A\00"
@.str.4 = [18 x i8] c"Good bye fork %d\0A\00"
@EXIT_SUCCESS = global i32 0
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
store i32 -1, i32* %6
%7 = call i32 (...) @getpid()
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
switch i32 %9, label %16 [
i32 0, label %10
i32 -1, label %14
]
10: ; preds = %2
%11 = call i32 (...) @getpid()
%12 = call i32 (...) @getppid()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %12)
br label %20
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %20
16: ; preds = %2
%17 = call i32 (...) @getpid()
%18 = load i32, i32* %6
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %18)
br label %20
20: ; preds = %16, %14, %10
%21 = call i32 (...) @getpid()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @EXIT_SUCCESS
ret i32 %23
}

LLVM IR (-O2):
@.str = [17 x i8] c"Hello Fork - %d\0A\00"
@.str.1 = [30 x i8] c"In the child[%d] - parent %d\0A\00"
@.str.3 = [38 x i8] c"Parent %d forked a child with pid %d\0A\00"
@.str.4 = [18 x i8] c"Good bye fork %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [15 x i8] c"Failed to fork\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 (...) @fork()
switch i32 %5, label %12 [
i32 0, label %6
i32 -1, label %10
]
6: ; preds = %2
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (...) @getppid()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %8)
br label %15
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %15
12: ; preds = %2
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %13, i32 %5)
br label %15
15: ; preds = %12, %10, %6
%16 = tail call i32 (...) @getpid()
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @EXIT_SUCCESS
ret i32 %18
}