#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();
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  pid_t f[3], w;
  int status, i;
  for (i = 0; i < 3; i++) {
    f[i] = fork();
  }
  sleep(10);
  for (i = 0; i < 3; i++) {
    if (f[i] != 0)
      w = waitpid(f[i], &status, 0);
    printf("%d\n", w);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x3,-0x30(%rbp)
jge    1181 <main+0x41>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,%rcx
movslq -0x30(%rbp),%rax
mov    %rcx,-0x20(%rbp,%rax,8)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1156 <main+0x16>
mov    $0xa,%edi
call   1050 <sleep@plt>
movl   $0x0,-0x30(%rbp)
cmpl   $0x3,-0x30(%rbp)
jge    11e2 <main+0xa2>
movslq -0x30(%rbp),%rax
cmpq   $0x0,-0x20(%rbp,%rax,8)
je     11c4 <main+0x84>
movslq -0x30(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe31(%rip),%rdi
call   1030 <printf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1192 <main+0x52>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1060 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <fork@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1060 <fork@plt>
mov    %rax,%r14
mov    $0xa,%edi
call   1050 <sleep@plt>
test   %rbx,%rbx
je     118a <main+0x4a>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %rax,%rbx
jmp    118a <main+0x4a>
lea    0xe6f(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
test   %r15,%r15
je     11b0 <main+0x70>
lea    0xc(%rsp),%rsi
mov    %r15,%rdi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %rax,%rbx
lea    0xe49(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
test   %r14,%r14
je     11d6 <main+0x96>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %rax,%rbx
lea    0xe23(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = tail call i64 (...) @fork()
%5 = tail call i64 (...) @fork()
%6 = tail call i32 @sleep(i32 10)
%7 = icmp eq i64 %3, 0
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = call i64 @waitpid(i64 %3, i32* %1, i32 0)
br label %10
10: ; preds = %8, %0
%11 = phi i64 [ %9, %8 ], [ undef, %0 ]
%12 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %11)
%13 = icmp eq i64 %4, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = call i64 @waitpid(i64 %4, i32* %1, i32 0)
br label %16
16: ; preds = %14, %10
%17 = phi i64 [ %15, %14 ], [ %11, %10 ]
%18 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %17)
%19 = icmp eq i64 %5, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i64 @waitpid(i64 %5, i32* %1, i32 0)
br label %22
22: ; preds = %20, %16
%23 = phi i64 [ %21, %20 ], [ %17, %16 ]
%24 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %23)
ret i32 0
}