#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(int argc, char **argv) {
  pid_t pid;
  int status;
  if ((pid = fork()) < 0)
    fprintf(stderr, "fork error");
  else if (pid == 0)
    exit(7);
  if (wait(&status) != pid)
    fprintf(stderr, "wait error");
  pr_exit(status);
  if ((pid = fork()) < 0)
    fprintf(stderr, "fork error");
  else if (pid == 0)
    abort();
  if (wait(&status) != pid)
    fprintf(stderr, "wait error");
  pr_exit(status);
  if ((pid = fork()) < 0)
    fprintf(stderr, "fork error");
  else if (pid == 0)
    status /= 0;
  if (wait(&status) != pid)
    fprintf(stderr, "wait error");
  pr_exit(status);
  exit(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    11a5 <main+0x45>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11bf <main+0x5f>
cmpq   $0x0,-0x18(%rbp)
jne    11ba <main+0x5a>
mov    $0x7,%edi
call   1060 <exit@plt>
jmp    11bf <main+0x5f>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x18(%rbp),%rax
je     11e7 <main+0x87>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <pr_exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    121e <main+0xbe>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1235 <main+0xd5>
cmpq   $0x0,-0x18(%rbp)
jne    1230 <main+0xd0>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1235 <main+0xd5>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x18(%rbp),%rax
je     125d <main+0xfd>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <pr_exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1294 <main+0x134>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
call   1040 <fprintf@plt>
jmp    12af <main+0x14f>
cmpq   $0x0,-0x18(%rbp)
jne    12aa <main+0x14a>
mov    -0x1c(%rbp),%eax
xor    %ecx,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
jmp    12af <main+0x14f>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x18(%rbp),%rax
je     12d7 <main+0x177>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
lea    0xd39(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <pr_exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    118b <main+0x2b>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1197 <main+0x37>
jne    1197 <main+0x37>
mov    $0x7,%edi
call   1060 <exit@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
je     11bb <main+0x5b>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1040 <fprintf@plt>
mov    0xc(%rsp),%edi
call   1050 <pr_exit@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11ea <main+0x8a>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11f3 <main+0x93>
jne    11f3 <main+0x93>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
je     1217 <main+0xb7>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   1040 <fprintf@plt>
mov    0xc(%rsp),%edi
call   1050 <pr_exit@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    1244 <main+0xe4>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
je     1268 <main+0x108>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
call   1040 <fprintf@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(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 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %19
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, %10
%20 = call i64 @wait(i32* %7)
%21 = load i64, i64* %6
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %23, %19
%27 = load i32, i32* %7
%28 = call i32 @pr_exit(i32 %27)
%29 = call i64 (...) @fork()
store i64 %29, i64* %6
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %40
34: ; preds = %26
%35 = load i64, i64* %6
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 (...) @abort()
unreachable
39: ; preds = %34
br label %40
40: ; preds = %39, %31
%41 = call i64 @wait(i32* %7)
%42 = load i64, i64* %6
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %47
47: ; preds = %44, %40
%48 = load i32, i32* %7
%49 = call i32 @pr_exit(i32 %48)
%50 = call i64 (...) @fork()
store i64 %50, i64* %6
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %62
55: ; preds = %47
%56 = load i64, i64* %6
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i32, i32* %7
%60 = sdiv i32 %59, 0
store i32 %60, i32* %7
br label %61
61: ; preds = %58, %55
br label %62
62: ; preds = %61, %52
%63 = call i64 @wait(i32* %7)
%64 = load i64, i64* %6
%65 = icmp ne i64 %63, %64
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = load i32, i32* @stderr
%68 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %69
69: ; preds = %66, %62
%70 = load i32, i32* %7
%71 = call i32 @pr_exit(i32 %70)
%72 = call i32 @exit(i32 0)
unreachable
}

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