#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 WEXITSTATUS(int);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  pid_t pid;
  int returnValue = 0;
  pid = fork();
  switch (pid) {
  case -1:
    perror("fork");
    exit(1);
    break;
  case 0:
    printf("\tCHILD: We are inside child process!\n");
    printf("\tCHILD DATA:\n");
    printf("\t\tPID: %d\n", getpid());
    printf("\t\tParent PID: %d\n", getppid());
    printf("Just for fun, type a small number:\n");
    scanf(" %d", &returnValue);
    break;
  default:
    printf("\tPARENT: I am the parent process\n");
    printf("\tPARENT DATA:\n");
    printf("\t\tPID: %d\n", getpid());
    printf("\t\tMY CHILD: %d\n", pid);
    printf("=> Waiting for my child...\n");
    wait(&returnValue);
    printf("PARENT: Child exit status: %d\n", WEXITSTATUS(returnValue));
    printf("PARENT: Child said: %d\n", returnValue);
    printf("PARENT: bye\n");
    break;
  }
  return 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)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11db <main+0x4b>
jmp    11cb <main+0x3b>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11f1 <main+0x61>
jmp    125e <main+0xce>
lea    0xe1e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2a(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <scanf@plt>
jmp    12f0 <main+0x160>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x18(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x18(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1255 <main+0xb5>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12c0 <main+0x120>
lea    0xef4(%rip),%rdi
call   1030 <puts@plt>
lea    0xf09(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe18(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe29(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xee4(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe0a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%esi
lea    0xe15(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
jmp    12b8 <main+0x118>
lea    0xe11(%rip),%rdi
call   1030 <puts@plt>
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd8a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <getppid@plt>
lea    0xd7e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdfd(%rip),%rdi
call   1030 <puts@plt>
lea    0xd74(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1060 <scanf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd39(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [38 x i8] c"\09CHILD: We are inside child process!\0A\00"
@.str.2 = [14 x i8] c"\09CHILD DATA:\0A\00"
@.str.3 = [11 x i8] c"\09\09PID: %d\0A\00"
@.str.4 = [18 x i8] c"\09\09Parent PID: %d\0A\00"
@.str.5 = [36 x i8] c"Just for fun, type a small number:\0A\00"
@.str.6 = [4 x i8] c" %d\00"
@.str.7 = [34 x i8] c"\09PARENT: I am the parent process\0A\00"
@.str.8 = [15 x i8] c"\09PARENT DATA:\0A\00"
@.str.9 = [16 x i8] c"\09\09MY CHILD: %d\0A\00"
@.str.10 = [28 x i8] c"=> Waiting for my child...\0A\00"
@.str.11 = [31 x i8] c"PARENT: Child exit status: %d\0A\00"
@.str.12 = [24 x i8] c"PARENT: Child said: %d\0A\00"
@.str.13 = [13 x i8] c"PARENT: bye\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%8 = call i32 (...) @fork()
store i32 %8, i32* %6
%9 = load i32, i32* %6
switch i32 %9, label %22 [
i32 -1, label %10
i32 0, label %13
]
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 (...) @getpid()
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = call i32 (...) @getppid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%21 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32* %7)
br label %37
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %6
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
%30 = call i32 @wait(i32* %7)
%31 = load i32, i32* %7
%32 = call i32 @WEXITSTATUS(i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.11, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.12, i64 0, i64 0), i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.13, i64 0, i64 0))
br label %37
37: ; preds = %22, %13
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.3 = [11 x i8] c"\09\09PID: %d\0A\00"
@.str.4 = [18 x i8] c"\09\09Parent PID: %d\0A\00"
@.str.6 = [4 x i8] c" %d\00"
@.str.9 = [16 x i8] c"\09\09MY CHILD: %d\0A\00"
@.str.11 = [31 x i8] c"PARENT: Child exit status: %d\0A\00"
@.str.12 = [24 x i8] c"PARENT: Child said: %d\0A\00"
@str = [37 x i8] c"\09CHILD: We are inside child process!\00"
@str.14 = [13 x i8] c"\09CHILD DATA:\00"
@str.15 = [35 x i8] c"Just for fun, type a small number:\00"
@str.16 = [33 x i8] c"\09PARENT: I am the parent process\00"
@str.17 = [14 x i8] c"\09PARENT DATA:\00"
@str.18 = [27 x i8] c"=> Waiting for my child...\00"
@str.19 = [12 x i8] c"PARENT: bye\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = tail call i32 (...) @fork()
switch i32 %5, label %18 [
i32 -1, label %6
i32 0, label %9
]
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.14, i64 0, i64 0))
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = tail call i32 (...) @getppid()
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.15, i64 0, i64 0))
%17 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32* %3)
br label %32
18: ; preds = %2
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.16, i64 0, i64 0))
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.17, i64 0, i64 0))
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i32 %5)
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.18, i64 0, i64 0))
%25 = call i32 @wait(i32* %3)
%26 = load i32, i32* %3
%27 = call i32 @WEXITSTATUS(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.11, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %3
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.12, i64 0, i64 0), i32 %29)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.19, i64 0, i64 0))
br label %32
32: ; preds = %18, %9
ret i32 0
}