#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);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int main() {
  pid_t p;
  int status = 0;
  p = fork();
  if (-1 == p) {
    printf("I am the parent. Fork was unsuccessful.\n");
  } else if (0 == p) {
    printf("I am the child. My pid is %d. My parent's pid is %d.\n", getpid(),
           getppid());
    sleep(1);
    exit(10);
  } else {
    printf("I am the parent. My pid is %d. Child pid is %d\n", getpid(), p);
    wait(&status);
    if (WIFEXITED(status) != 0)
      printf("Child process exited with exit code %d.\n", WEXITSTATUS(status));
    else
      printf("Child process exited normally.\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    11d1 <main+0x41>
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127e <main+0xee>
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
jne    1214 <main+0x84>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    $0xa,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xc(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0xc(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1266 <main+0xd6>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1274 <main+0xe4>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1279 <main+0xe9>
jmp    127e <main+0xee>
mov    -0x4(%rbp),%eax
add    $0x10,%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>
cmp    $0xffffffff,%eax
je     120b <main+0x6b>
mov    %eax,%ebx
xor    %eax,%eax
test   %ebx,%ebx
je     1228 <main+0x88>
call   1040 <getpid@plt>
lea    0xe69(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     1214 <main+0x74>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xe66(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1220 <main+0x80>
lea    0xe9c(%rip),%rdi
jmp    121b <main+0x7b>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xdc3(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    $0xa,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"I am the parent. Fork was unsuccessful.\0A\00"
@.str.1 = [54 x i8] c"I am the child. My pid is %d. My parent's pid is %d.\0A\00"
@.str.2 = [48 x i8] c"I am the parent. My pid is %d. Child pid is %d\0A\00"
@.str.3 = [41 x i8] c"Child process exited with exit code %d.\0A\00"
@.str.4 = [32 x i8] c"Child process exited normally.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 -1, %5
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %34
9: ; preds = %0
%10 = load i32, i32* %2
%11 = icmp eq i32 0, %10
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = call i32 (...) @getpid()
%14 = call i32 (...) @getppid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
%16 = call i32 @sleep(i32 1)
%17 = call i32 @exit(i32 10)
unreachable
18: ; preds = %9
%19 = call i32 (...) @getpid()
%20 = load i32, i32* %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
%22 = call i32 @wait(i32* %3)
%23 = load i32, i32* %3
%24 = call i64 @WIFEXITED(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %18
%27 = load i32, i32* %3
%28 = call i32 @WEXITSTATUS(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %28)
br label %32
30: ; preds = %18
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
br label %33
33: ; preds = %32
br label %34
34: ; preds = %33, %7
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str.1 = [54 x i8] c"I am the child. My pid is %d. My parent's pid is %d.\0A\00"
@.str.2 = [48 x i8] c"I am the parent. My pid is %d. Child pid is %d\0A\00"
@.str.3 = [41 x i8] c"Child process exited with exit code %d.\0A\00"
@str = [31 x i8] c"Child process exited normally.\00"
@str.5 = [40 x i8] c"I am the parent. Fork was unsuccessful.\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 (...) @fork()
switch i32 %3, label %12 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.5, i64 0, i64 0))
br label %25
6: ; preds = %0
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (...) @getppid()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %8)
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 @exit(i32 10)
unreachable
12: ; preds = %0
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %3)
%15 = call i32 @wait(i32* %1)
%16 = load i32, i32* %1
%17 = call i64 @WIFEXITED(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %12
%20 = load i32, i32* %1
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %25
23: ; preds = %12
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %19, %4
ret i32 0
}