#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 */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
int main() {
  int N = 3;
  int x = 0;
  pid_t y = 0;
  for (int i = 0; i < N; i++) {
    pid_t pid = fork();
    if (pid == 0) {
      sleep(1 + i);
      printf("%d\n", i);
      exit(0);
    }
    y = wait(&x);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x3,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11da <main+0x8a>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bf <main+0x6f>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <sleep@plt>
mov    -0x1c(%rbp),%esi
lea    0xe4d(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <exit@plt>
lea    -0xc(%rbp),%rdi
call   1060 <wait@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117c <main+0x2c>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11b5 <main+0x65>
lea    0xc(%rsp),%rdi
call   1060 <wait@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11a9 <main+0x59>
lea    0xc(%rsp),%rdi
call   1060 <wait@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11b0 <main+0x60>
lea    0xc(%rsp),%rdi
call   1060 <wait@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x1,%ebx
jmp    11b5 <main+0x65>
mov    $0x2,%ebx
lea    0x1(%rbx),%edi
call   1050 <sleep@plt>
lea    0xe3c(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
store i32 0, i32* %1
store i32 3, i32* %2
store i32 0, i32* %3
store i64 0, i64* %4
store i32 0, i32* %5
br label %7
7: ; preds = %24, %0
%8 = load i32, i32* %5
%9 = load i32, i32* %2
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %27
11: ; preds = %7
%12 = call i64 (...) @fork()
store i64 %12, i64* %6
%13 = load i64, i64* %6
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = load i32, i32* %5
%17 = add nsw i32 1, %16
%18 = call i32 @sleep(i32 %17)
%19 = load i32, i32* %5
%20 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %11
%23 = call i64 @wait(i32* %3)
store i64 %23, i64* %4
br label %24
24: ; preds = %22
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %7
27: ; preds = %7
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*
store i32 0, i32* %1
%3 = call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %15, %11, %0
%6 = phi i32 [ 0, %0 ], [ 1, %11 ], [ 2, %15 ]
%7 = add nuw nsw i32 %6, 1
%8 = call i32 @sleep(i32 %7)
%9 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %6)
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = call i64 @wait(i32* %1)
%13 = call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %5, label %15
15: ; preds = %11
%16 = call i64 @wait(i32* %1)
%17 = call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %5, label %19
19: ; preds = %15
%20 = call i64 @wait(i32* %1)
ret i32 0
}