#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 /*<<< orphan*/ abort();
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ pre_exit(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ wait(int *);
int main(int argc, char *argv[]) {
  pid_t pid;
  int status;
  if ((pid = fork()) < 0) {
    printf("fork occurs error.\n");
    exit(0);
  } else if (pid == 0) {
    exit(7);
  }
  if (wait(&status) != pid) {
    printf("wait occurs error.\n");
  }
  pre_exit(status);
  if ((pid = fork()) < 0) {
    printf("fork occurs error.\n");
  } else if (pid == 0) {
    abort();
  }
  if (wait(&status) != pid) {
    printf("wait occurs error.\n");
  }
  pre_exit(status);
  if ((pid = fork()) < 0) {
    printf("fork occurs error.\n");
  } else if (pid == 0) {
    status /= 0;
  }
  if (wait(&status) != pid) {
    printf("wait occurs error.\n");
  }
  pre_exit(status);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    119e <main+0x3e>
lea    0xe6e(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <main+0x53>
mov    $0x7,%edi
call   1060 <exit@plt>
jmp    11b8 <main+0x58>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x18(%rbp),%rax
je     11d7 <main+0x77>
lea    0xe42(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <pre_exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1205 <main+0xa5>
lea    0xe05(%rip),%rdi
call   1050 <printf@plt>
jmp    121c <main+0xbc>
cmpq   $0x0,-0x18(%rbp)
jne    1217 <main+0xb7>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    121c <main+0xbc>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x18(%rbp),%rax
je     123b <main+0xdb>
lea    0xdde(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <pre_exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1269 <main+0x109>
lea    0xda1(%rip),%rdi
call   1050 <printf@plt>
jmp    1284 <main+0x124>
cmpq   $0x0,-0x18(%rbp)
jne    127f <main+0x11f>
mov    -0x1c(%rbp),%eax
xor    %ecx,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
jmp    1284 <main+0x124>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x18(%rbp),%rax
je     12a3 <main+0x143>
lea    0xd76(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <pre_exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     1231 <main+0xd1>
je     1244 <main+0xe4>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
je     1199 <main+0x39>
lea    0xe6c(%rip),%rdi
call   1040 <puts@plt>
mov    0xc(%rsp),%edi
call   1050 <pre_exit@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11ba <main+0x5a>
jne    11c6 <main+0x66>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
je     11e1 <main+0x81>
lea    0xe24(%rip),%rdi
call   1040 <puts@plt>
mov    0xc(%rsp),%edi
call   1050 <pre_exit@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    1205 <main+0xa5>
lea    0xe13(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
je     1220 <main+0xc0>
lea    0xde5(%rip),%rdi
call   1040 <puts@plt>
mov    0xc(%rsp),%edi
call   1050 <pre_exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xddb(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    $0x7,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"fork occurs error.\0A\00"
@.str.1 = [20 x i8] c"wait occurs error.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %2
%14 = load i64, i64* %6
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @exit(i32 7)
unreachable
18: ; preds = %13
br label %19
19: ; preds = %18
%20 = call i64 @wait(i32* %7)
%21 = load i64, i64* %6
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = load i32, i32* %7
%27 = call i32 @pre_exit(i32 %26)
%28 = call i64 (...) @fork()
store i64 %28, i64* %6
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %38
32: ; preds = %25
%33 = load i64, i64* %6
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (...) @abort()
unreachable
37: ; preds = %32
br label %38
38: ; preds = %37, %30
%39 = call i64 @wait(i32* %7)
%40 = load i64, i64* %6
%41 = icmp ne i64 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
%45 = load i32, i32* %7
%46 = call i32 @pre_exit(i32 %45)
%47 = call i64 (...) @fork()
store i64 %47, i64* %6
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %58
51: ; preds = %44
%52 = load i64, i64* %6
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* %7
%56 = sdiv i32 %55, 0
store i32 %56, i32* %7
br label %57
57: ; preds = %54, %51
br label %58
58: ; preds = %57, %49
%59 = call i64 @wait(i32* %7)
%60 = load i64, i64* %6
%61 = icmp ne i64 %59, %60
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %64
64: ; preds = %62, %58
%65 = load i32, i32* %7
%66 = call i32 @pre_exit(i32 %65)
ret i32 0
}

LLVM IR (-O2):
@str.5 = [19 x i8] c"wait occurs error.\00"
@str.6 = [19 x i8] c"fork occurs error.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = icmp eq i64 %5, 0
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 @exit(i32 7)
unreachable
14: ; preds = %10
%15 = call i64 @wait(i32* %3)
%16 = icmp eq i64 %15, %5
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = load i32, i32* %3
%21 = call i32 @pre_exit(i32 %20)
%22 = call i64 (...) @fork()
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %30
26: ; preds = %19
%27 = icmp eq i64 %22, 0
br i1 %27, label %28, label %30
28: ; preds = %26
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %26, %24
%31 = call i64 @wait(i32* %3)
%32 = icmp eq i64 %31, %22
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = load i32, i32* %3
%37 = call i32 @pre_exit(i32 %36)
%38 = call i64 (...) @fork()
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %42
42: ; preds = %35, %40
%43 = call i64 @wait(i32* %3)
%44 = icmp eq i64 %43, %38
br i1 %44, label %47, label %45
45: ; preds = %42
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
%48 = load i32, i32* %3
%49 = call i32 @pre_exit(i32 %48)
ret i32 0
}