#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 const *argv[]) {
  pid_t pid;
  int status;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
  } else if (pid == 0) {
    exit(7);
  }
  if (wait(&status) != pid) {
    fprintf(stderr, "wait error\n");
    exit(1);
  }
  pr_exit(status);
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
  } else if (pid == 0) {
    abort();
  }
  if (wait(&status) != pid) {
    fprintf(stderr, "wait error\n");
    exit(1);
  }
  pr_exit(status);
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
  } else if (pid == 0) {
    status /= 0;
  }
  if (wait(&status) != pid) {
    fprintf(stderr, "wait error\n");
    exit(1);
  }
  pr_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    11aa <main+0x4a>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11bf <main+0x5f>
mov    $0x7,%edi
call   1060 <exit@plt>
jmp    11c4 <main+0x64>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x18(%rbp),%rax
je     11f6 <main+0x96>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@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    1232 <main+0xd2>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1244 <main+0xe4>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1249 <main+0xe9>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x18(%rbp),%rax
je     127b <main+0x11b>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@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    12b7 <main+0x157>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xd58(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12cd <main+0x16d>
mov    -0x1c(%rbp),%eax
xor    %ecx,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
jmp    12d2 <main+0x172>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
cmp    -0x18(%rbp),%rax
je     1304 <main+0x1a4>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
lea    0xd17(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <pr_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     11ee <main+0x8e>
je     121f <main+0xbf>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
jne    1200 <main+0xa0>
mov    0xc(%rsp),%edi
call   1050 <pr_exit@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11ee <main+0x8e>
je     1229 <main+0xc9>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
jne    1200 <main+0xa0>
mov    0xc(%rsp),%edi
call   1050 <pr_exit@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11ee <main+0x8e>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    %rbx,%rax
jne    1200 <main+0xa0>
mov    0xc(%rsp),%edi
call   1050 <pr_exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
jmp    1210 <main+0xb0>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0x7,%edi
call   1060 <exit@plt>
xor    %eax,%eax
call   1030 <abort@plt>

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

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