#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*/ SIGKILL;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(void) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
  } else if (pid != 0) {
    sleep(2);
    exit(2);
  }
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
  } else if (pid != 0) {
    sleep(4);
    abort();
  }
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
  } else if (pid != 0) {
    execl("/bin/dd", "dd", "if=/etc/passwd", "of=/dev/null", NULL);
    exit(7);
  }
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
  } else if (pid != 0) {
    sleep(8);
    exit(0);
  }
  sleep(6);
  kill(getpid(), SIGKILL);
  exit(6);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11c3 <main+0x43>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     11e2 <main+0x62>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
jmp    11e7 <main+0x67>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    121b <main+0x9b>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     1237 <main+0xb7>
mov    $0x4,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    123c <main+0xbc>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1270 <main+0xf0>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xd9f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     12ab <main+0x12b>
lea    0xd8a(%rip),%rdi
lea    0xd88(%rip),%rsi
lea    0xd84(%rip),%rdx
lea    0xd8c(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
mov    $0x7,%edi
call   1070 <exit@plt>
jmp    12b0 <main+0x130>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    12e4 <main+0x164>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    0xd2b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     1300 <main+0x180>
mov    $0x8,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1305 <main+0x185>
mov    $0x6,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
mov    0x2cb9(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    $0x6,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    11ac <main+0x2c>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jne    1203 <main+0x83>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     118d <main+0xd>
jne    1217 <main+0x97>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     118d <main+0xd>
jne    1228 <main+0xa8>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     118d <main+0xd>
jne    1256 <main+0xd6>
mov    $0x6,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2de8(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1060 <kill@plt>
mov    $0x6,%edi
call   1070 <exit@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    $0x4,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xddd(%rip),%rdi
lea    0xddb(%rip),%rsi
lea    0xdd7(%rip),%rdx
lea    0xddf(%rip),%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
mov    $0x7,%edi
call   1070 <exit@plt>
mov    $0x8,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"fork error\0A\00"
@.str.1 = [8 x i8] c"/bin/dd\00"
@.str.2 = [3 x i8] c"dd\00"
@.str.3 = [15 x i8] c"if=/etc/passwd\00"
@.str.4 = [13 x i8] c"of=/dev/null\00"
@SIGKILL = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @sleep(i32 2)
%14 = call i32 @exit(i32 2)
unreachable
15: ; preds = %9
br label %16
16: ; preds = %15
%17 = call i64 (...) @fork()
store i64 %17, i64* %2
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = load i64, i64* %2
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @sleep(i32 4)
%28 = call i32 (...) @abort()
unreachable
29: ; preds = %23
br label %30
30: ; preds = %29
%31 = call i64 (...) @fork()
store i64 %31, i64* %2
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%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 = %30
%38 = load i64, i64* %2
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32* null)
%42 = call i32 @exit(i32 7)
unreachable
43: ; preds = %37
br label %44
44: ; preds = %43
%45 = call i64 (...) @fork()
store i64 %45, i64* %2
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%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 = %44
%52 = load i64, i64* %2
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = call i32 @sleep(i32 8)
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %51
br label %58
58: ; preds = %57
%59 = call i32 @sleep(i32 6)
%60 = call i32 (...) @getpid()
%61 = load i32, i32* @SIGKILL
%62 = call i32 @kill(i32 %60, i32 %61)
%63 = call i32 @exit(i32 6)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"fork error\0A\00"
@.str.1 = [8 x i8] c"/bin/dd\00"
@.str.2 = [3 x i8] c"dd\00"
@.str.3 = [15 x i8] c"if=/etc/passwd\00"
@.str.4 = [13 x i8] c"of=/dev/null\00"
@SIGKILL = global i32 0
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = icmp eq i64 %1, 0
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = tail call i32 @sleep(i32 2)
%11 = tail call i32 @exit(i32 2)
unreachable
12: ; preds = %7
%13 = tail call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = icmp eq i64 %13, 0
br i1 %20, label %24, label %21
21: ; preds = %19
%22 = tail call i32 @sleep(i32 4)
%23 = tail call i32 (...) @abort()
unreachable
24: ; preds = %19
%25 = tail call i64 (...) @fork()
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = icmp eq i64 %25, 0
br i1 %32, label %36, label %33
33: ; preds = %31
%34 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32* null)
%35 = tail call i32 @exit(i32 7)
unreachable
36: ; preds = %31
%37 = tail call i64 (...) @fork()
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %36
%44 = icmp eq i64 %37, 0
br i1 %44, label %48, label %45
45: ; preds = %43
%46 = tail call i32 @sleep(i32 8)
%47 = tail call i32 @exit(i32 0)
unreachable
48: ; preds = %43
%49 = tail call i32 @sleep(i32 6)
%50 = tail call i32 (...) @getpid()
%51 = load i32, i32* @SIGKILL
%52 = tail call i32 @kill(i32 %50, i32 %51)
%53 = tail call i32 @exit(i32 6)
unreachable
}