#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_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main(void) {
  printf("Begin. This is PID: %d\n", getpid());
  printf("pid_t is %zu bytes\n", sizeof(pid_t));
  pid_t forkReturn = fork();
  if (forkReturn == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  } else if (forkReturn == 0) {
    printf(" CHILD: I have PID %d\n", getpid());
    sleep(1);
    printf(" CHILD: I am exiting.\n");
    exit(EXIT_SUCCESS);
  } else {
    printf("PARENT: I have PID %d\n", getpid());
    printf("PARENT: My child has PID %d\n", forkReturn);
    printf("PARENT: It is good practice to wait for child to exit.\n");
    printf("PARENT: Waiting...\n");
    int childStatus = 0;
    pid_t waitReturned = wait(&childStatus);
    if (waitReturned == -1) {
      perror("wait");
      exit(EXIT_FAILURE);
    } else {
      printf("wait() says that PID %d has exited.\n", waitReturned);
    }
    if (WIFEXITED(childStatus))
      printf("PARENT: The child exited NORMALLY with return value %u\n",
             WEXITSTATUS(childStatus));
    else if (WIFSIGNALED(childStatus))
      printf("PARENT: The child was terminated due to an UNHANDLED SIGNAL %u\n",
             WTERMSIG(childStatus));
    else if (WIFSTOPPED(childStatus))
      printf("PARENT: Child was STOPPED by signal %u\n", WSTOPSIG(childStatus));
    else
      printf("Other error.\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe1b(%rip),%rdi
mov    $0x4,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1237 <main+0x67>
lea    0xe08(%rip),%rdi
call   1090 <perror@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    127e <main+0xae>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
lea    -0xc(%rbp),%rdi
call   10e0 <wait@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12f9 <main+0x129>
lea    0xdf9(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%esi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1339 <main+0x169>
mov    -0xc(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13af <main+0x1df>
mov    -0xc(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1368 <main+0x198>
mov    -0xc(%rbp),%edi
call   1070 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13aa <main+0x1da>
mov    -0xc(%rbp),%edi
call   1060 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1397 <main+0x1c7>
mov    -0xc(%rbp),%edi
call   10d0 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13a5 <main+0x1d5>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13aa <main+0x1da>
jmp    13af <main+0x1df>
jmp    13b4 <main+0x1e4>
jmp    13b9 <main+0x1e9>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe0d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe15(%rip),%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     1308 <main+0x128>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1343 <main+0x163>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe11(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe18(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xeef(%rip),%rdi
call   1030 <puts@plt>
lea    0xf1a(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   10f0 <wait@plt>
cmp    $0xffffffff,%eax
je     134c <main+0x16c>
lea    0xdf7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   10b0 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     12b3 <main+0xd3>
call   1090 <WEXITSTATUS@plt>
lea    0xdf5(%rip),%rdi
jmp    12e9 <main+0x109>
call   1060 <WIFSIGNALED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     12cf <main+0xef>
call   1080 <WTERMSIG@plt>
lea    0xe11(%rip),%rdi
jmp    12e9 <main+0x109>
call   1070 <WIFSTOPPED@plt>
test   %rax,%rax
je     12fa <main+0x11a>
mov    0xc(%rsp),%edi
call   10e0 <WSTOPSIG@plt>
lea    0xe35(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
jmp    12f2 <main+0x112>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd1b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xce2(%rip),%rdi
jmp    1353 <main+0x173>
lea    0xd29(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Begin. This is PID: %d\0A\00"
@.str.1 = [20 x i8] c"pid_t is %zu bytes\0A\00"
@.str.2 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [23 x i8] c" CHILD: I have PID %d\0A\00"
@.str.4 = [23 x i8] c" CHILD: I am exiting.\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [23 x i8] c"PARENT: I have PID %d\0A\00"
@.str.6 = [29 x i8] c"PARENT: My child has PID %d\0A\00"
@.str.7 = [56 x i8] c"PARENT: It is good practice to wait for child to exit.\0A\00"
@.str.8 = [20 x i8] c"PARENT: Waiting...\0A\00"
@.str.9 = [5 x i8] c"wait\00"
@.str.10 = [37 x i8] c"wait() says that PID %d has exited.\0A\00"
@.str.11 = [56 x i8] c"PARENT: The child exited NORMALLY with return value %u\0A\00"
@.str.12 = [64 x i8] c"PARENT: The child was terminated due to an UNHANDLED SIGNAL %u\0A\00"
@.str.13 = [40 x i8] c"PARENT: Child was STOPPED by signal %u\0A\00"
@.str.14 = [14 x i8] c"Other error.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @getpid()
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i64 4)
%8 = call i32 (...) @fork()
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = call i32 (...) @getpid()
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = call i32 @sleep(i32 1)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %15
%26 = call i32 (...) @getpid()
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %2
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.7, i64 0, i64 0))
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %3
%32 = call i32 @wait(i32* %3)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %25
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %25
%40 = load i32, i32* %4
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.10, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %39
%43 = load i32, i32* %3
%44 = call i64 @WIFEXITED(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = load i32, i32* %3
%48 = call i32 @WEXITSTATUS(i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.11, i64 0, i64 0), i32 %48)
br label %70
50: ; preds = %42
%51 = load i32, i32* %3
%52 = call i64 @WIFSIGNALED(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = load i32, i32* %3
%56 = call i32 @WTERMSIG(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.12, i64 0, i64 0), i32 %56)
br label %69
58: ; preds = %50
%59 = load i32, i32* %3
%60 = call i64 @WIFSTOPPED(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %58
%63 = load i32, i32* %3
%64 = call i32 @WSTOPSIG(i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.13, i64 0, i64 0), i32 %64)
br label %68
66: ; preds = %58
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.14, i64 0, i64 0))
br label %68
68: ; preds = %66, %62
br label %69
69: ; preds = %68, %54
br label %70
70: ; preds = %69, %46
br label %71
71: ; preds = %70
br label %72
72: ; preds = %71
%73 = load i32, i32* %1
ret i32 %73
}

LLVM IR (-O2):
@.str = [24 x i8] c"Begin. This is PID: %d\0A\00"
@.str.1 = [20 x i8] c"pid_t is %zu bytes\0A\00"
@.str.2 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [23 x i8] c" CHILD: I have PID %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [23 x i8] c"PARENT: I have PID %d\0A\00"
@.str.6 = [29 x i8] c"PARENT: My child has PID %d\0A\00"
@.str.9 = [5 x i8] c"wait\00"
@.str.10 = [37 x i8] c"wait() says that PID %d has exited.\0A\00"
@.str.11 = [56 x i8] c"PARENT: The child exited NORMALLY with return value %u\0A\00"
@.str.12 = [64 x i8] c"PARENT: The child was terminated due to an UNHANDLED SIGNAL %u\0A\00"
@.str.13 = [40 x i8] c"PARENT: Child was STOPPED by signal %u\0A\00"
@str = [55 x i8] c"PARENT: It is good practice to wait for child to exit.\00"
@str.15 = [19 x i8] c"PARENT: Waiting...\00"
@str.16 = [13 x i8] c"Other error.\00"
@str.17 = [22 x i8] c" CHILD: I am exiting.\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @getpid()
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i64 4)
%5 = tail call i32 (...) @fork()
switch i32 %5, label %17 [
i32 -1, label %6
i32 0, label %10
]
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = tail call i32 @sleep(i32 1)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.17, i64 0, i64 0))
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %0
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %5)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.15, i64 0, i64 0))
%23 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%24 = call i32 @wait(i32* %1)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %17
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.10, i64 0, i64 0), i32 %24)
%32 = load i32, i32* %1
%33 = call i64 @WIFEXITED(i32 %32)
%34 = icmp eq i64 %33, 0
%35 = load i32, i32* %1
br i1 %34, label %39, label %36
36: ; preds = %30
%37 = call i32 @WEXITSTATUS(i32 %35)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.11, i64 0, i64 0), i32 %37)
br label %55
39: ; preds = %30
%40 = call i64 @WIFSIGNALED(i32 %35)
%41 = icmp eq i64 %40, 0
%42 = load i32, i32* %1
br i1 %41, label %46, label %43
43: ; preds = %39
%44 = call i32 @WTERMSIG(i32 %42)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @.str.12, i64 0, i64 0), i32 %44)
br label %55
46: ; preds = %39
%47 = call i64 @WIFSTOPPED(i32 %42)
%48 = icmp eq i64 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %46
%50 = load i32, i32* %1
%51 = call i32 @WSTOPSIG(i32 %50)
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.13, i64 0, i64 0), i32 %51)
br label %55
53: ; preds = %46
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.16, i64 0, i64 0))
br label %55
55: ; preds = %43, %53, %49, %36
ret i32 0
}