#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WTERMSIG(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main(int argc, char *argv[]) {
  int i;
  for (i = 0; i < 5; i++) {
    pid_t pid = fork();
    if (pid == -1) {
      perror("fork");
      exit(EXIT_FAILURE);
    }
    if (pid == 0) {
      printf("start %ld\n", (long)getpid());
      sleep(3);
      printf("exit %ld\n", (long)getpid());
      exit(EXIT_SUCCESS);
    }
  }
  int status;
  pid_t pid;
  while ((pid = wait(&status)) != -1) {
    if (WIFEXITED(status)) {
      printf("PROCESS %ld: EXITED (%d)\n", (long)pid, WEXITSTATUS(status));
    } else if (WIFSIGNALED(status)) {
      printf("PROCESS %ld: SIGNALED (%d)\n", (long)pid, WTERMSIG(status));
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    126a <main+0xba>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1205 <main+0x55>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1257 <main+0xa7>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    125c <main+0xac>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11cd <main+0x1d>
jmp    126f <main+0xbf>
lea    -0x1c(%rbp),%rdi
call   10c0 <wait@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
je     12ff <main+0x14f>
mov    -0x1c(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12bf <main+0x10f>
movslq -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fa <main+0x14a>
mov    -0x1c(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12f5 <main+0x145>
movslq -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fa <main+0x14a>
jmp    126f <main+0xbf>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12de <main+0x12e>
cmp    $0xffffffff,%eax
je     12c4 <main+0x114>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     12c4 <main+0x114>
test   %eax,%eax
je     12de <main+0x12e>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     12c4 <main+0x114>
test   %eax,%eax
je     12de <main+0x12e>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     12c4 <main+0x114>
test   %eax,%eax
je     12de <main+0x12e>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     12c4 <main+0x114>
test   %eax,%eax
je     12de <main+0x12e>
lea    0x4(%rsp),%rdi
call   10c0 <wait@plt>
cmp    $0xffffffff,%eax
je     12b7 <main+0x107>
mov    %eax,%ebp
lea    0xded(%rip),%r14
lea    0x4(%rsp),%rbx
lea    0xdc7(%rip),%r15
jmp    128a <main+0xda>
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebp,%rbp
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   10c0 <wait@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12b7 <main+0x107>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
jne    1260 <main+0xb0>
mov    0x4(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     127b <main+0xcb>
movslq %ebp,%rbp
mov    0x4(%rsp),%edi
call   1060 <WTERMSIG@plt>
mov    %r14,%rdi
jmp    126f <main+0xbf>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd35(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd19(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd02(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"start %ld\0A\00"
@.str.2 = [10 x i8] c"exit %ld\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [26 x i8] c"PROCESS %ld: EXITED (%d)\0A\00"
@.str.4 = [28 x i8] c"PROCESS %ld: SIGNALED (%d)\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i32 (...) @fork()
switch i32 %4, label %17 [
i32 -1, label %5
i32 0, label %9
]
5: ; preds = %23, %21, %19, %17, %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %23, %21, %19, %17, %2
%10 = tail call i64 (...) @getpid()
%11 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64 %10)
%12 = tail call i32 @sleep(i32 3)
%13 = tail call i64 (...) @getpid()
%14 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %13)
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = tail call i32 (...) @fork()
switch i32 %18, label %19 [
i32 -1, label %5
i32 0, label %9
]
19: ; preds = %17
%20 = tail call i32 (...) @fork()
switch i32 %20, label %21 [
i32 -1, label %5
i32 0, label %9
]
21: ; preds = %19
%22 = tail call i32 (...) @fork()
switch i32 %22, label %23 [
i32 -1, label %5
i32 0, label %9
]
23: ; preds = %21
%24 = tail call i32 (...) @fork()
switch i32 %24, label %25 [
i32 -1, label %5
i32 0, label %9
]
25: ; preds = %23
%26 = bitcast i32* %3 to i8*
%27 = call i32 @wait(i32* %3)
%28 = icmp eq i32 %27, -1
br i1 %28, label %51, label %29
29: ; preds = %25, %48
%30 = phi i32 [ %49, %48 ], [ %27, %25 ]
%31 = load i32, i32* %3
%32 = call i64 @WIFEXITED(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %29
%35 = sext i32 %30 to i64
%36 = load i32, i32* %3
%37 = call i32 @WEXITSTATUS(i32 %36)
%38 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %35, i32 %37)
br label %48
39: ; preds = %29
%40 = load i32, i32* %3
%41 = call i64 @WIFSIGNALED(i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %48, label %43
43: ; preds = %39
%44 = sext i32 %30 to i64
%45 = load i32, i32* %3
%46 = call i32 @WTERMSIG(i32 %45)
%47 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i64 %44, i32 %46)
br label %48
48: ; preds = %39, %43, %34
%49 = call i32 @wait(i32* %3)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %29
51: ; preds = %48, %25
ret i32 0
}