#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 */
scalar_t__ fork();
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(void) {
  int i;
  int j;
  pid_t pid;
  pid_t child_pid_array[5];
  int status;
  for (i = 0; i < 5; i++) {
    pid = fork();
    if (pid < 0) {
      perror("fork");
      return -1;
    } else if (pid == 0) {
      for (j = 0; j < i + 1; j++) {
        sleep(5);
        pid = getpid();
        printf("pid = %d\n", pid);
      }
      return 0;
    } else {
      child_pid_array[i] = pid;
    }
  }
  for (i = 0; i < 5; i++) {
    waitpid(child_pid_array[i], &status, 0);
    printf("finished! child_pid_array[%d] = %d\n", i, child_pid_array[i]);
    sleep(5);
  }
  printf("all finished!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    1237 <main+0xd7>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11ae <main+0x4e>
lea    0xe63(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <main+0x143>
cmpq   $0x0,-0x18(%rbp)
jne    1212 <main+0xb2>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1206 <main+0xa6>
mov    $0x5,%edi
call   1070 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c0 <main+0x60>
movl   $0x0,-0x4(%rbp)
jmp    12a3 <main+0x143>
mov    -0x18(%rbp),%rcx
movslq -0x8(%rbp),%rax
mov    %rcx,-0x40(%rbp,%rax,8)
jmp    1224 <main+0xc4>
jmp    1229 <main+0xc9>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1176 <main+0x16>
movl   $0x0,-0x8(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    128e <main+0x12e>
movslq -0x8(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
lea    -0x44(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x8(%rbp),%esi
movslq -0x8(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdx
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    123e <main+0xde>
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     12f9 <main+0x189>
je     12eb <main+0x17b>
mov    %rax,%rbp
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     12f9 <main+0x189>
je     12f2 <main+0x182>
mov    %rax,%r13
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     12f9 <main+0x189>
je     130c <main+0x19c>
mov    %rax,%r12
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     12f9 <main+0x189>
je     1313 <main+0x1a3>
mov    %rax,%r15
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     12f9 <main+0x189>
je     131a <main+0x1aa>
mov    %rax,%r14
lea    0x4(%rsp),%rsi
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
lea    0xdff(%rip),%rdi
xor    %esi,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    %r13,%rdi
lea    0x4(%rsp),%rbp
mov    %rbp,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
lea    0xdd0(%rip),%rdi
mov    $0x1,%esi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %rbp,%r13
xor    %edx,%edx
call   1060 <waitpid@plt>
lea    0xda0(%rip),%rbp
mov    %rbp,%rdi
mov    $0x2,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    %r15,%rdi
mov    %r13,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rbp,%rdi
mov    $0x3,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    %r14,%rdi
mov    %r13,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rbp,%rdi
mov    $0x4,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
lea    0xd4f(%rip),%rdi
call   1030 <puts@plt>
jmp    1353 <main+0x1e3>
mov    $0x1,%ebp
jmp    131f <main+0x1af>
mov    $0x2,%ebp
jmp    131f <main+0x1af>
lea    0xd00(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1353 <main+0x1e3>
mov    $0x3,%ebp
jmp    131f <main+0x1af>
mov    $0x4,%ebp
jmp    131f <main+0x1af>
mov    $0x5,%ebp
lea    0xcdf(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    $0x5,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%ebp
jne    1330 <main+0x1c0>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [10 x i8] c"pid = %d\0A\00"
@.str.2 = [36 x i8] c"finished! child_pid_array[%d] = %d\0A\00"
@str = [14 x i8] c"all finished!\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %33, %28, %23, %18, %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %55
7: ; preds = %0
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %18
9: ; preds = %36, %31, %26, %21, %7
%10 = phi i32 [ 1, %7 ], [ 2, %21 ], [ 3, %26 ], [ 4, %31 ], [ 5, %36 ]
br label %11
11: ; preds = %9, %11
%12 = phi i32 [ 0, %9 ], [ %16, %11 ]
%13 = tail call i32 @sleep(i32 5)
%14 = tail call i64 (...) @getpid()
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 %14)
%16 = add nuw nsw i32 %12, 1
%17 = icmp eq i32 %16, %10
br i1 %17, label %55, label %11
18: ; preds = %7
%19 = tail call i64 (...) @fork()
%20 = icmp slt i64 %19, 0
br i1 %20, label %5, label %21
21: ; preds = %18
%22 = icmp eq i64 %19, 0
br i1 %22, label %9, label %23
23: ; preds = %21
%24 = tail call i64 (...) @fork()
%25 = icmp slt i64 %24, 0
br i1 %25, label %5, label %26
26: ; preds = %23
%27 = icmp eq i64 %24, 0
br i1 %27, label %9, label %28
28: ; preds = %26
%29 = tail call i64 (...) @fork()
%30 = icmp slt i64 %29, 0
br i1 %30, label %5, label %31
31: ; preds = %28
%32 = icmp eq i64 %29, 0
br i1 %32, label %9, label %33
33: ; preds = %31
%34 = tail call i64 (...) @fork()
%35 = icmp slt i64 %34, 0
br i1 %35, label %5, label %36
36: ; preds = %33
%37 = icmp eq i64 %34, 0
br i1 %37, label %9, label %38
38: ; preds = %36
%39 = call i32 @waitpid(i64 %3, i32* %1, i32 0)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 0, i64 %3)
%41 = call i32 @sleep(i32 5)
%42 = call i32 @waitpid(i64 %19, i32* %1, i32 0)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 1, i64 %19)
%44 = call i32 @sleep(i32 5)
%45 = call i32 @waitpid(i64 %24, i32* %1, i32 0)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 2, i64 %24)
%47 = call i32 @sleep(i32 5)
%48 = call i32 @waitpid(i64 %29, i32* %1, i32 0)
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 3, i64 %29)
%50 = call i32 @sleep(i32 5)
%51 = call i32 @waitpid(i64 %34, i32* %1, i32 0)
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 4, i64 %34)
%53 = call i32 @sleep(i32 5)
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %55
55: ; preds = %11, %38, %5
%56 = phi i32 [ -1, %5 ], [ 0, %38 ], [ 0, %11 ]
ret i32 %56
}