#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ printf(char *, int, ...);
int waitpid(int, int *, int /*<<< orphan*/);
void fork11() {
  pid_t pid[5];
  int i;
  int child_status;
  for (i = 0; i < 5; i++) {
    if ((pid[i] = fork()) == 0)
      exit(100 + i);
  }
  for (i = 0; i < 5; i++) {
    pid_t wpid = waitpid(pid[i], &child_status, 0);
    if (WIFEXITED(child_status))
      printf("Child %d terminated with exit status %d\n", wpid,
             WEXITSTATUS(child_status));
    else
      printf("Child %d terminate abnormally\n", wpid);
  }
}


x86 assembly (clang, -O0):
<fork11>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x24(%rbp)
cmpl   $0x5,-0x24(%rbp)
jge    11af <fork11+0x4f>
mov    $0x0,%al
call   1080 <fork@plt>
movslq -0x24(%rbp),%rcx
mov    %eax,-0x20(%rbp,%rcx,4)
cmp    $0x0,%eax
jne    119c <fork11+0x3c>
mov    -0x24(%rbp),%edi
add    $0x64,%edi
call   1070 <exit@plt>
jmp    11a1 <fork11+0x41>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116f <fork11+0xf>
movl   $0x0,-0x24(%rbp)
cmpl   $0x5,-0x24(%rbp)
jge    1232 <fork11+0xd2>
movslq -0x24(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     120e <fork11+0xae>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    -0x30(%rbp),%esi
mov    %eax,%edx
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    121f <fork11+0xbf>
mov    -0x2c(%rbp),%esi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1224 <fork11+0xc4>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b6 <fork11+0x56>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork11>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     134b <fork11+0x1eb>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1355 <fork11+0x1f5>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     135f <fork11+0x1ff>
mov    %eax,%r12d
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1369 <fork11+0x209>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1373 <fork11+0x213>
mov    %eax,%r14d
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     11ff <fork11+0x9f>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xe0e(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    120f <fork11+0xaf>
lea    0xe23(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     124a <fork11+0xea>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xdc3(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    125a <fork11+0xfa>
lea    0xdd8(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %r12d,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     1296 <fork11+0x136>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xd77(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    12a6 <fork11+0x146>
lea    0xd8c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %r15d,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     12e2 <fork11+0x182>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xd2b(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    12f2 <fork11+0x192>
lea    0xd40(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     132e <fork11+0x1ce>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xcdf(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    133e <fork11+0x1de>
lea    0xcf4(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x64,%edi
call   1070 <exit@plt>
mov    $0x65,%edi
call   1070 <exit@plt>
mov    $0x66,%edi
call   1070 <exit@plt>
mov    $0x67,%edi
call   1070 <exit@plt>
mov    $0x68,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"Child %d terminated with exit status %d\0A\00"
@.str.1 = [31 x i8] c"Child %d terminate abnormally\0A\00"
define void @fork11() {
%1 = alloca [5 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %2
br label %5
5: ; preds = %19, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 5
br i1 %7, label %8, label %22
8: ; preds = %5
%9 = call i32 (...) @fork()
%10 = load i32, i32* %2
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [5 x i32], [5 x i32]* %1, i64 0, i64 %11
store i32 %9, i32* %12
%13 = icmp eq i32 %9, 0
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i32, i32* %2
%16 = add nsw i32 100, %15
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %8
br label %19
19: ; preds = %18
%20 = load i32, i32* %2
%21 = add nsw i32 %20, 1
store i32 %21, i32* %2
br label %5
22: ; preds = %5
store i32 0, i32* %2
br label %23
23: ; preds = %44, %22
%24 = load i32, i32* %2
%25 = icmp slt i32 %24, 5
br i1 %25, label %26, label %47
26: ; preds = %23
%27 = load i32, i32* %2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [5 x i32], [5 x i32]* %1, i64 0, i64 %28
%30 = load i32, i32* %29
%31 = call i32 @waitpid(i32 %30, i32* %3, i32 0)
store i32 %31, i32* %4
%32 = load i32, i32* %3
%33 = call i64 @WIFEXITED(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %26
%36 = load i32, i32* %4
%37 = load i32, i32* %3
%38 = call i32 @WEXITSTATUS(i32 %37)
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %36, i32 %38)
br label %43
40: ; preds = %26
%41 = load i32, i32* %4
%42 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %40, %35
br label %44
44: ; preds = %43
%45 = load i32, i32* %2
%46 = add nsw i32 %45, 1
store i32 %46, i32* %2
br label %23
47: ; preds = %23
ret void
}

LLVM IR (-O2):
@.str = [41 x i8] c"Child %d terminated with exit status %d\0A\00"
@.str.1 = [31 x i8] c"Child %d terminate abnormally\0A\00"
define void @fork11() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %17, %14, %11, %8, %0
%6 = phi i32 [ 100, %0 ], [ 101, %8 ], [ 102, %11 ], [ 103, %14 ], [ 104, %17 ]
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i32 (...) @fork()
%10 = icmp eq i32 %9, 0
br i1 %10, label %5, label %11
11: ; preds = %8
%12 = tail call i32 (...) @fork()
%13 = icmp eq i32 %12, 0
br i1 %13, label %5, label %14
14: ; preds = %11
%15 = tail call i32 (...) @fork()
%16 = icmp eq i32 %15, 0
br i1 %16, label %5, label %17
17: ; preds = %14
%18 = tail call i32 (...) @fork()
%19 = icmp eq i32 %18, 0
br i1 %19, label %5, label %20
20: ; preds = %17
%21 = call i32 @waitpid(i32 %3, i32* %1, i32 0)
%22 = load i32, i32* %1
%23 = call i64 @WIFEXITED(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %20
%26 = load i32, i32* %1
%27 = call i32 @WEXITSTATUS(i32 %26)
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %27)
br label %31
29: ; preds = %20
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %31
31: ; preds = %29, %25
%32 = call i32 @waitpid(i32 %9, i32* %1, i32 0)
%33 = load i32, i32* %1
%34 = call i64 @WIFEXITED(i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %31
%37 = load i32, i32* %1
%38 = call i32 @WEXITSTATUS(i32 %37)
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %32, i32 %38)
br label %42
40: ; preds = %31
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %42
42: ; preds = %40, %36
%43 = call i32 @waitpid(i32 %12, i32* %1, i32 0)
%44 = load i32, i32* %1
%45 = call i64 @WIFEXITED(i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %42
%48 = load i32, i32* %1
%49 = call i32 @WEXITSTATUS(i32 %48)
%50 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %43, i32 %49)
br label %53
51: ; preds = %42
%52 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %43)
br label %53
53: ; preds = %51, %47
%54 = call i32 @waitpid(i32 %15, i32* %1, i32 0)
%55 = load i32, i32* %1
%56 = call i64 @WIFEXITED(i32 %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %62, label %58
58: ; preds = %53
%59 = load i32, i32* %1
%60 = call i32 @WEXITSTATUS(i32 %59)
%61 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %54, i32 %60)
br label %64
62: ; preds = %53
%63 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %54)
br label %64
64: ; preds = %62, %58
%65 = call i32 @waitpid(i32 %18, i32* %1, i32 0)
%66 = load i32, i32* %1
%67 = call i64 @WIFEXITED(i32 %66)
%68 = icmp eq i64 %67, 0
br i1 %68, label %73, label %69
69: ; preds = %64
%70 = load i32, i32* %1
%71 = call i32 @WEXITSTATUS(i32 %70)
%72 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %65, i32 %71)
br label %75
73: ; preds = %64
%74 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %65)
br label %75
75: ; preds = %73, %69
ret void
}