#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 wait(int *);
void fork10() {
  pid_t pid[5];
  int i, 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 = wait(&child_status);
    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):
<fork10>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x24(%rbp)
cmpl   $0x5,-0x24(%rbp)
jge    11af <fork10+0x4f>
mov    $0x0,%al
call   1080 <fork@plt>
movslq -0x24(%rbp),%rcx
mov    %eax,-0x20(%rbp,%rcx,4)
cmp    $0x0,%eax
jne    119c <fork10+0x3c>
mov    -0x24(%rbp),%edi
add    $0x64,%edi
call   1060 <exit@plt>
jmp    11a1 <fork10+0x41>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116f <fork10+0xf>
movl   $0x0,-0x24(%rbp)
cmpl   $0x5,-0x24(%rbp)
jge    1228 <fork10+0xc8>
lea    -0x28(%rbp),%rdi
call   1070 <wait@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1204 <fork10+0xa4>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    -0x30(%rbp),%esi
mov    %eax,%edx
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1215 <fork10+0xb5>
mov    -0x2c(%rbp),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    121a <fork10+0xba>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b6 <fork10+0x56>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork10>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1319 <fork10+0x1b9>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1323 <fork10+0x1c3>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     132d <fork10+0x1cd>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1337 <fork10+0x1d7>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1341 <fork10+0x1e1>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
je     11e7 <fork10+0x87>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xe26(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11f7 <fork10+0x97>
lea    0xe3b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
je     122e <fork10+0xce>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xddf(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    123e <fork10+0xde>
lea    0xdf4(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
je     1275 <fork10+0x115>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xd98(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1285 <fork10+0x125>
lea    0xdad(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
je     12bc <fork10+0x15c>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xd51(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    12cc <fork10+0x16c>
lea    0xd66(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
je     1303 <fork10+0x1a3>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xd0a(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1313 <fork10+0x1b3>
lea    0xd1f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x64,%edi
call   1060 <exit@plt>
mov    $0x65,%edi
call   1060 <exit@plt>
mov    $0x66,%edi
call   1060 <exit@plt>
mov    $0x67,%edi
call   1060 <exit@plt>
mov    $0x68,%edi
call   1060 <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 @fork10() {
%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 = %40, %22
%24 = load i32, i32* %2
%25 = icmp slt i32 %24, 5
br i1 %25, label %26, label %43
26: ; preds = %23
%27 = call i32 @wait(i32* %3)
store i32 %27, i32* %4
%28 = load i32, i32* %3
%29 = call i64 @WIFEXITED(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load i32, i32* %4
%33 = load i32, i32* %3
%34 = call i32 @WEXITSTATUS(i32 %33)
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %32, i32 %34)
br label %39
36: ; preds = %26
%37 = load i32, i32* %4
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %36, %31
br label %40
40: ; preds = %39
%41 = load i32, i32* %2
%42 = add nsw i32 %41, 1
store i32 %42, i32* %2
br label %23
43: ; 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 @fork10() {
%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 @wait(i32* %1)
%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 @wait(i32* %1)
%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 @wait(i32* %1)
%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 @wait(i32* %1)
%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 @wait(i32* %1)
%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
}