#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);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(int);
int waitpid(int, int *, int /*<<< orphan*/);
int main(void) {
  pid_t childpid, pr;
  int status;
  childpid = fork();
  if (-1 == childpid) {
    perror("fork()");
    exit(EXIT_FAILURE);
  } else if (0 == childpid) {
    puts("In child process");
    sleep(1);
    printf("\tchild pid = %d\n", getpid());
    printf("\tchild ppid = %d\n", getppid());
    exit(EXIT_SUCCESS);
  } else {
    pr = waitpid(childpid, &status, 0);
    if (WIFEXITED(status)) {
      printf("the child process %d exit normally.\n", pr);
      printf("the return code is %d.\n", WEXITSTATUS(status));
    } else {
      printf("the child process %d exit abnormally.\n", pr);
    }
    puts("in parent");
    printf("\tparent pid = %d\n", getpid());
    printf("\tparent ppid = %d\n", getppid());
    printf("\tchild process exited with status %d \n", status);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    11f1 <main+0x41>
lea    0xe22(%rip),%rdi
call   1080 <perror@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
jne    124a <main+0x9a>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdf6(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <getppid@plt>
mov    %eax,%esi
lea    0xdf2(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1297 <main+0xe7>
mov    -0xc(%rbp),%esi
lea    0xdc4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdd3(%rip),%rdi
call   1050 <printf@plt>
jmp    12a6 <main+0xf6>
mov    -0xc(%rbp),%esi
lea    0xdd7(%rip),%rdi
call   1050 <printf@plt>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xde7(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <getppid@plt>
mov    %eax,%esi
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xde8(%rip),%rdi
call   1050 <printf@plt>
jmp    12f0 <main+0x140>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     11df <main+0x2f>
cmp    $0xffffffff,%eax
jne    121f <main+0x6f>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xe21(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe15(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <getppid@plt>
lea    0xe11(%rip),%rdi
mov    %eax,%esi
jmp    12ae <main+0xfe>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
jne    1246 <main+0x96>
lea    0xe34(%rip),%rdi
jmp    1266 <main+0xb6>
lea    0xdee(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%ebx
lea    0xdfa(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <getppid@plt>
lea    0xe1f(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0xc(%rsp),%esi
lea    0xe20(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"fork()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [17 x i8] c"In child process\00"
@.str.2 = [17 x i8] c"\09child pid = %d\0A\00"
@.str.3 = [18 x i8] c"\09child ppid = %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [37 x i8] c"the child process %d exit normally.\0A\00"
@.str.5 = [24 x i8] c"the return code is %d.\0A\00"
@.str.6 = [39 x i8] c"the child process %d exit abnormally.\0A\00"
@.str.7 = [10 x i8] c"in parent\00"
@.str.8 = [18 x i8] c"\09parent pid = %d\0A\00"
@.str.9 = [19 x i8] c"\09parent ppid = %d\0A\00"
@.str.10 = [39 x i8] c"\09child process exited with status %d \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 (...) @fork()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 -1, %6
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i32, i32* %2
%14 = icmp eq i32 0, %13
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @sleep(i32 1)
%18 = call i32 (...) @getpid()
%19 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = call i32 (...) @getppid()
%21 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @EXIT_SUCCESS
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %12
%25 = load i32, i32* %2
%26 = call i32 @waitpid(i32 %25, i32* %4, i32 0)
store i32 %26, i32* %3
%27 = load i32, i32* %4
%28 = call i64 @WIFEXITED(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %24
%31 = load i32, i32* %3
%32 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %4
%34 = call i32 @WEXITSTATUS(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %34)
br label %39
36: ; preds = %24
%37 = load i32, i32* %3
%38 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %36, %30
%40 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%41 = call i32 (...) @getpid()
%42 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i32 %41)
%43 = call i32 (...) @getppid()
%44 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %4
%46 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.10, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %39
br label %48
48: ; preds = %47
%49 = load i32, i32* @EXIT_SUCCESS
%50 = call i32 @exit(i32 %49)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"fork()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [17 x i8] c"In child process\00"
@.str.2 = [17 x i8] c"\09child pid = %d\0A\00"
@.str.3 = [18 x i8] c"\09child ppid = %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [37 x i8] c"the child process %d exit normally.\0A\00"
@.str.5 = [24 x i8] c"the return code is %d.\0A\00"
@.str.6 = [39 x i8] c"the child process %d exit abnormally.\0A\00"
@.str.7 = [10 x i8] c"in parent\00"
@.str.8 = [18 x i8] c"\09parent pid = %d\0A\00"
@.str.9 = [19 x i8] c"\09parent ppid = %d\0A\00"
@.str.10 = [39 x i8] c"\09child process exited with status %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %17 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %0
%18 = call i32 @waitpid(i32 %3, i32* %1, i32 0)
%19 = load i32, i32* %1
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %18)
%24 = load i32, i32* %1
%25 = call i32 @WEXITSTATUS(i32 %24)
br label %26
26: ; preds = %17, %22
%27 = phi i32 [ %25, %22 ], [ %18, %17 ]
%28 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), %22 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), %17 ]
%29 = call i32 @printf(i8* %28, i32 %27)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%31 = call i32 (...) @getpid()
%32 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i32 %31)
%33 = call i32 (...) @getppid()
%34 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %1
%36 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.10, i64 0, i64 0), i32 %35)
%37 = load i32, i32* @EXIT_SUCCESS
%38 = call i32 @exit(i32 %37)
unreachable
}