#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 scalar_t__ pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int main() {
  pid_t childpid;
  int retval;
  int status;
  childpid = fork();
  if (childpid >= 0) {
    if (childpid == 0) {
      printf("CHILD: I am the child process!\n");
      printf("CHILD: Here's my PID: %d\n", getpid());
      printf("CHILD: My parent's PID is: %d\n", getppid());
      printf("CHILD: The value of my copy of childpid is: %d\n", childpid);
      printf("CHILD: Sleeping for 1 second...\n");
      sleep(5);
      printf("CHILD: Enter an exit value (0 to 255): ");
      scanf(" %d", &retval);
      printf("CHILD: Goodbye!\n");
      exit(retval);
    } else {
      printf("PARENT: I am the parent process!\n");
      printf("PARENT: Here's my PID: %d\n", getpid());
      printf("PARENT: The value of my copy of childpid is %d\n", childpid);
      printf("PARENT: I will now wait for my child to exit.\n");
      wait(&status);
      printf("PARENT: Child's exit code is: %d\n", WEXITSTATUS(status));
      printf("PARENT: Goodbye!\n");
      exit(0);
    }
  } else {
    perror("fork error");
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jl     12e5 <main+0x145>
cmpq   $0x0,-0x10(%rbp)
jne    126a <main+0xca>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,%esi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe87(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1050 <scanf@plt>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <exit@plt>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x18(%rbp),%rdi
call   10b0 <wait@plt>
mov    -0x18(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xece(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xecb(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     125f <main+0xaf>
jne    1272 <main+0xc2>
lea    0xf9b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe1b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <getppid@plt>
lea    0xe1e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe2d(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf70(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
lea    0xe37(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe51(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1060 <scanf@plt>
lea    0xf5b(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   10a0 <exit@plt>
lea    0xe9c(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    %rax,%rbx
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe06(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe11(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
call   10c0 <wait@plt>
mov    0x8(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe11(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7d(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"CHILD: I am the child process!\0A\00"
@.str.1 = [26 x i8] c"CHILD: Here's my PID: %d\0A\00"
@.str.2 = [31 x i8] c"CHILD: My parent's PID is: %d\0A\00"
@.str.3 = [48 x i8] c"CHILD: The value of my copy of childpid is: %d\0A\00"
@.str.4 = [33 x i8] c"CHILD: Sleeping for 1 second...\0A\00"
@.str.5 = [40 x i8] c"CHILD: Enter an exit value (0 to 255): \00"
@.str.6 = [4 x i8] c" %d\00"
@.str.7 = [17 x i8] c"CHILD: Goodbye!\0A\00"
@.str.8 = [34 x i8] c"PARENT: I am the parent process!\0A\00"
@.str.9 = [27 x i8] c"PARENT: Here's my PID: %d\0A\00"
@.str.10 = [48 x i8] c"PARENT: The value of my copy of childpid is %d\0A\00"
@.str.11 = [47 x i8] c"PARENT: I will now wait for my child to exit.\0A\00"
@.str.12 = [34 x i8] c"PARENT: Child's exit code is: %d\0A\00"
@.str.13 = [18 x i8] c"PARENT: Goodbye!\0A\00"
@.str.14 = [11 x i8] c"fork error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp sge i64 %6, 0
br i1 %7, label %8, label %39
8: ; preds = %0
%9 = load i64, i64* %2
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %26
11: ; preds = %8
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 (...) @getppid()
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i64, i64* %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i64 %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 @sleep(i32 5)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
%22 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32* %3)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%24 = load i32, i32* %3
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
%28 = call i32 (...) @getpid()
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), i32 %28)
%30 = load i64, i64* %2
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.10, i64 0, i64 0), i64 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.11, i64 0, i64 0))
%33 = call i32 @wait(i32* %4)
%34 = load i32, i32* %4
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.12, i64 0, i64 0), i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.13, i64 0, i64 0))
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %0
%40 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.14, i64 0, i64 0))
%41 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [26 x i8] c"CHILD: Here's my PID: %d\0A\00"
@.str.2 = [31 x i8] c"CHILD: My parent's PID is: %d\0A\00"
@.str.3 = [48 x i8] c"CHILD: The value of my copy of childpid is: %d\0A\00"
@.str.5 = [40 x i8] c"CHILD: Enter an exit value (0 to 255): \00"
@.str.6 = [4 x i8] c" %d\00"
@.str.9 = [27 x i8] c"PARENT: Here's my PID: %d\0A\00"
@.str.10 = [48 x i8] c"PARENT: The value of my copy of childpid is %d\0A\00"
@.str.12 = [34 x i8] c"PARENT: Child's exit code is: %d\0A\00"
@.str.14 = [11 x i8] c"fork error\00"
@str = [33 x i8] c"PARENT: I am the parent process!\00"
@str.15 = [46 x i8] c"PARENT: I will now wait for my child to exit.\00"
@str.16 = [17 x i8] c"PARENT: Goodbye!\00"
@str.17 = [31 x i8] c"CHILD: I am the child process!\00"
@str.18 = [32 x i8] c"CHILD: Sleeping for 1 second...\00"
@str.19 = [16 x i8] c"CHILD: Goodbye!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp sgt i64 %5, -1
br i1 %6, label %7, label %35
7: ; preds = %0
%8 = icmp eq i64 %5, 0
br i1 %8, label %9, label %23
9: ; preds = %7
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.17, i64 0, i64 0))
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i64 0)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.18, i64 0, i64 0))
%17 = tail call i32 @sleep(i32 5)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
%19 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32* %1)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.19, i64 0, i64 0))
%21 = load i32, i32* %1
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %7
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), i32 %25)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.10, i64 0, i64 0), i64 %5)
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.15, i64 0, i64 0))
%29 = call i32 @wait(i32* %2)
%30 = load i32, i32* %2
%31 = call i32 @WEXITSTATUS(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.12, i64 0, i64 0), i32 %31)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.16, i64 0, i64 0))
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %0
%36 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.14, i64 0, i64 0))
%37 = tail call i32 @exit(i32 0)
unreachable
}