#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);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
int main() {
  pid_t pid;
  int sta;
  int i = -1;
  pid = fork();
  if (pid < 0) {
    printf("create child process fail\n");
    exit(1);
  }
  if (pid == 0) {
    printf("This is a child process\n");
  } else {
    sleep(1);
    printf("This is a parent process\n");
    printf("waiting for child process...\n");
    if (pid != wait(&sta)) {
      printf("wait error\n");
      exit(1);
    }
    if (WIFEXITED(sta)) {
      i = WEXITSTATUS(sta);
    }
    printf("child pid = %d\n", pid);
    printf("exit status =%d\n", i);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11b4 <main+0x44>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11d2 <main+0x62>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1271 <main+0x101>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x14(%rbp),%rdi
call   1080 <wait@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     1231 <main+0xc1>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
je     124e <main+0xde>
mov    -0x14(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1221 <main+0xa1>
je     11e4 <main+0x64>
mov    %rax,%rbx
mov    $0x1,%edi
call   1080 <sleep@plt>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
cmp    %rax,%rbx
jne    122a <main+0xaa>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     11f2 <main+0x72>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ebp
jmp    11f7 <main+0x77>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
jmp    1218 <main+0x98>
mov    $0xffffffff,%ebp
lea    0xe02(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe01(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe52(%rip),%rdi
jmp    1231 <main+0xb1>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"create child process fail\0A\00"
@.str.1 = [25 x i8] c"This is a child process\0A\00"
@.str.2 = [26 x i8] c"This is a parent process\0A\00"
@.str.3 = [30 x i8] c"waiting for child process...\0A\00"
@.str.4 = [12 x i8] c"wait error\0A\00"
@.str.5 = [16 x i8] c"child pid = %d\0A\00"
@.str.6 = [17 x i8] c"exit status =%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %4
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i64, i64* %2
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %38
16: ; preds = %11
%17 = call i32 @sleep(i32 1)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%20 = load i64, i64* %2
%21 = call i64 @wait(i32* %3)
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %16
%27 = load i32, i32* %3
%28 = call i64 @WIFEXITED(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %3
%32 = call i32 @WEXITSTATUS(i32 %31)
store i32 %32, i32* %4
br label %33
33: ; preds = %30, %26
%34 = load i64, i64* %2
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i64 %34)
%36 = load i32, i32* %4
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %33, %14
ret i32 0
}

LLVM IR (-O2):
@.str.5 = [16 x i8] c"child pid = %d\0A\00"
@.str.6 = [17 x i8] c"exit status =%d\0A\00"
@str = [25 x i8] c"This is a parent process\00"
@str.7 = [29 x i8] c"waiting for child process...\00"
@str.8 = [11 x i8] c"wait error\00"
@str.9 = [24 x i8] c"This is a child process\00"
@str.10 = [26 x i8] c"create child process fail\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.10, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %3, 0
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.9, i64 0, i64 0))
br label %32
12: ; preds = %8
%13 = tail call i32 @sleep(i32 1)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.7, i64 0, i64 0))
%16 = call i64 @wait(i32* %1)
%17 = icmp eq i64 %3, %16
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.8, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = load i32, i32* %1
%23 = call i64 @WIFEXITED(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = load i32, i32* %1
%27 = call i32 @WEXITSTATUS(i32 %26)
br label %28
28: ; preds = %25, %21
%29 = phi i32 [ %27, %25 ], [ -1, %21 ]
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i64 %3)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %29)
br label %32
32: ; preds = %28, %10
ret i32 0
}