#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(void) {
  pid_t pid;
  int rv;
  switch (pid = fork()) {
  case -1:
    perror("fork");
    exit(1);
  case 0:
    printf(" CHILD: This is the child process!\n");
    printf(" CHILD: My PID is %d\n", getpid());
    printf(" CHILD: My parent's PID is %d\n", getppid());
    printf(" CHILD: Enter my exit status (make it small): ");
    scanf(" %d", &rv);
    printf(" CHILD: I'm outta here!\n");
    exit(rv);
  default:
    printf("PARENT: This is the parent process!\n");
    printf("PARENT: My PID is %d\n", getpid());
    printf("PARENT: My child's PID is %d\n", pid);
    printf("PARENT: I'm now waiting for my child to exit()...\n");
    wait(&rv);
    printf("PARENT: My child's exit status is: %d\n", WEXITSTATUS(rv));
    printf("PARENT: I'm outta here!\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11cc <main+0x3c>
jmp    11bc <main+0x2c>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11e2 <main+0x52>
jmp    1252 <main+0xc2>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,%esi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe5a(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1050 <scanf@plt>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1090 <exit@plt>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xc(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0xc(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1235 <main+0x95>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12a3 <main+0x103>
lea    0xf3d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe94(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe9a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf2e(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe89(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf31(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdb6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <getppid@plt>
lea    0xdb5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdc4(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xde5(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1060 <scanf@plt>
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   10a0 <exit@plt>
lea    0xd56(%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 = [36 x i8] c" CHILD: This is the child process!\0A\00"
@.str.2 = [22 x i8] c" CHILD: My PID is %d\0A\00"
@.str.3 = [31 x i8] c" CHILD: My parent's PID is %d\0A\00"
@.str.4 = [47 x i8] c" CHILD: Enter my exit status (make it small): \00"
@.str.5 = [4 x i8] c" %d\00"
@.str.6 = [25 x i8] c" CHILD: I'm outta here!\0A\00"
@.str.7 = [37 x i8] c"PARENT: This is the parent process!\0A\00"
@.str.8 = [22 x i8] c"PARENT: My PID is %d\0A\00"
@.str.9 = [30 x i8] c"PARENT: My child's PID is %d\0A\00"
@.str.10 = [51 x i8] c"PARENT: I'm now waiting for my child to exit()...\0A\00"
@.str.11 = [39 x i8] c"PARENT: My child's exit status is: %d\0A\00"
@.str.12 = [25 x i8] c"PARENT: I'm outta here!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
switch i32 %4, label %19 [
i32 -1, label %5
i32 0, label %8
]
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 (...) @getpid()
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = call i32 (...) @getppid()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%17 = load i32, i32* %3
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0))
%21 = call i32 (...) @getpid()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0), i32 %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.10, i64 0, i64 0))
%26 = call i32 @wait(i32* %3)
%27 = load i32, i32* %3
%28 = call i32 @WEXITSTATUS(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.11, i64 0, i64 0), i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.12, i64 0, i64 0))
br label %31
31: ; preds = %19
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.2 = [22 x i8] c" CHILD: My PID is %d\0A\00"
@.str.3 = [31 x i8] c" CHILD: My parent's PID is %d\0A\00"
@.str.4 = [47 x i8] c" CHILD: Enter my exit status (make it small): \00"
@.str.5 = [4 x i8] c" %d\00"
@.str.8 = [22 x i8] c"PARENT: My PID is %d\0A\00"
@.str.9 = [30 x i8] c"PARENT: My child's PID is %d\0A\00"
@.str.11 = [39 x i8] c"PARENT: My child's exit status is: %d\0A\00"
@str = [35 x i8] c" CHILD: This is the child process!\00"
@str.13 = [24 x i8] c" CHILD: I'm outta here!\00"
@str.14 = [36 x i8] c"PARENT: This is the parent process!\00"
@str.15 = [50 x i8] c"PARENT: I'm now waiting for my child to exit()...\00"
@str.16 = [24 x i8] c"PARENT: I'm outta here!\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %18 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = tail call i32 (...) @getppid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
%14 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* %1)
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.13, i64 0, i64 0))
%16 = load i32, i32* %1
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %0
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.14, i64 0, i64 0))
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i32 %20)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0), i32 %3)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.15, i64 0, i64 0))
%24 = call i32 @wait(i32* %1)
%25 = load i32, i32* %1
%26 = call i32 @WEXITSTATUS(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.11, i64 0, i64 0), i32 %26)
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.16, i64 0, i64 0))
ret i32 0
}