#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ pr_exit(int);
int /*<<< orphan*/ stderr;
scalar_t__ wait(int *);
int main() {
  pid_t pid;
  int status;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error");
    return 0;
  } else if (pid == 0)
    exit(7);
  if (wait(&status) != pid) {
    fprintf(stderr, "wait error");
    return 0;
  }
  pr_exit(status);
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error");
    return 0;
  } else if (pid == 0) {
    abort();
  }
  if (wait(&status) != pid) {
    fprintf(stderr, "wait error");
    return 0;
  }
  pr_exit(status);
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error");
    return 0;
  } else if (pid == 0) {
    status /= 0;
  }
  if (wait(&status) != pid) {
    fprintf(stderr, "wait error");
    return 0;
  }
  pr_exit(status);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11a5 <main+0x45>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1318 <main+0x1b8>
cmpq   $0x0,-0x10(%rbp)
jne    11ba <main+0x5a>
mov    $0x7,%edi
call   1060 <exit@plt>
jmp    11bf <main+0x5f>
lea    -0x14(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x10(%rbp),%rax
je     11f3 <main+0x93>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1318 <main+0x1b8>
mov    -0x14(%rbp),%edi
call   1050 <pr_exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1231 <main+0xd1>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1318 <main+0x1b8>
cmpq   $0x0,-0x10(%rbp)
jne    1243 <main+0xe3>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1248 <main+0xe8>
lea    -0x14(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x10(%rbp),%rax
je     127c <main+0x11c>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1318 <main+0x1b8>
mov    -0x14(%rbp),%edi
call   1050 <pr_exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    12ba <main+0x15a>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1318 <main+0x1b8>
cmpq   $0x0,-0x10(%rbp)
jne    12d0 <main+0x170>
mov    -0x14(%rbp),%eax
xor    %ecx,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
jmp    12d5 <main+0x175>
lea    -0x14(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x10(%rbp),%rax
je     1309 <main+0x1a9>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
lea    0xd13(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1318 <main+0x1b8>
mov    -0x14(%rbp),%edi
call   1050 <pr_exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%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     11eb <main+0x8b>
je     1208 <main+0xa8>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
jne    11d9 <main+0x79>
mov    0xc(%rsp),%edi
call   1050 <pr_exit@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11eb <main+0x8b>
je     1212 <main+0xb2>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
jne    11d9 <main+0x79>
mov    0xc(%rsp),%edi
call   1050 <pr_exit@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11eb <main+0x8b>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
je     1219 <main+0xb9>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
jmp    11fb <main+0x9b>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x7,%edi
call   1060 <exit@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0xc(%rsp),%edi
call   1050 <pr_exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [11 x i8] c"fork error\00"
@.str.1 = [11 x i8] c"wait error\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 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %54
8: ; preds = %0
%9 = icmp eq i64 %3, 0
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i32 @exit(i32 7)
unreachable
12: ; preds = %8
%13 = call i64 @wait(i32* %1)
%14 = icmp eq i64 %13, %3
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %54
18: ; preds = %12
%19 = load i32, i32* %1
%20 = call i32 @pr_exit(i32 %19)
%21 = call i64 (...) @fork()
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %54
26: ; preds = %18
%27 = icmp eq i64 %21, 0
br i1 %27, label %28, label %30
28: ; preds = %26
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %26
%31 = call i64 @wait(i32* %1)
%32 = icmp eq i64 %31, %21
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %54
36: ; preds = %30
%37 = load i32, i32* %1
%38 = call i32 @pr_exit(i32 %37)
%39 = call i64 (...) @fork()
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %54
44: ; preds = %36
%45 = call i64 @wait(i32* %1)
%46 = icmp eq i64 %45, %39
br i1 %46, label %50, label %47
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %54
50: ; preds = %44
%51 = load i32, i32* %1
%52 = call i32 @pr_exit(i32 %51)
%53 = call i32 @exit(i32 0)
unreachable
54: ; preds = %47, %41, %33, %23, %15, %5
ret i32 0
}