#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*/ err_sys(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, int);
int random();
int /*<<< orphan*/ usleep(int);
int main() {
  int i;
  pid_t pid;
  int data[1000];
  for (i = 0; i < 1000; i++)
    data[i] = random() % 1000;
  for (i = 0; i < 1000; i++) {
    pid = fork();
    if (pid < 0)
      err_sys("fork error");
    else if (pid == 0) {
      usleep(data[i] * 100000);
      printf("%d\n", data[i]);
      exit(1);
    } else
      continue;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xfb0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x3e8,-0x8(%rbp)
jge    11ae <main+0x4e>
mov    $0x0,%al
call   1050 <random@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
movslq -0x8(%rbp),%rax
mov    %edx,-0xfb0(%rbp,%rax,4)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1179 <main+0x19>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3e8,-0x8(%rbp)
jge    1241 <main+0xe1>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11e9 <main+0x89>
lea    0xe21(%rip),%rdi
call   1030 <err_sys@plt>
jmp    122e <main+0xce>
cmpq   $0x0,-0x10(%rbp)
jne    1229 <main+0xc9>
movslq -0x8(%rbp),%rax
imul   $0x186a0,-0xfb0(%rbp,%rax,4),%edi
call   1080 <usleep@plt>
movslq -0x8(%rbp),%rax
mov    -0xfb0(%rbp,%rax,4),%esi
lea    0xdf1(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    1233 <main+0xd3>
jmp    1233 <main+0xd3>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11b5 <main+0x55>
xor    %eax,%eax
add    $0xfb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0xfa8,%rsp
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <random@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    $0x3e8,%rbx
jne    1170 <main+0x10>
lea    0xe54(%rip),%r14
xor    %ebx,%ebx
jmp    11c4 <main+0x64>
mov    %r14,%rdi
call   1030 <err_sys@plt>
add    $0x1,%rbx
cmp    $0x3e8,%ebx
je     11f8 <main+0x98>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11b0 <main+0x50>
jne    11b8 <main+0x58>
mov    (%rsp,%rbx,4),%ebx
imul   $0x186a0,%ebx,%edi
call   1080 <usleep@plt>
lea    0xe24(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0xfa8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca [1000 x i32]
%2 = bitcast [1000 x i32]* %1 to i8*
br label %3
3: ; preds = %0, %3
%4 = phi i64 [ 0, %0 ], [ %8, %3 ]
%5 = tail call i32 (...) @random()
%6 = srem i32 %5, 1000
%7 = getelementptr inbounds [1000 x i32], [1000 x i32]* %1, i64 0, i64 %4
store i32 %6, i32* %7
%8 = add nuw nsw i64 %4, 1
%9 = icmp eq i64 %8, 1000
br i1 %9, label %10, label %3
10: ; preds = %3, %26
%11 = phi i32 [ %27, %26 ], [ 0, %3 ]
%12 = tail call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %26
16: ; preds = %10
%17 = icmp eq i64 %12, 0
br i1 %17, label %18, label %26
18: ; preds = %16
%19 = zext i32 %11 to i64
%20 = getelementptr inbounds [1000 x i32], [1000 x i32]* %1, i64 0, i64 %19
%21 = load i32, i32* %20
%22 = mul nsw i32 %21, 100000
%23 = tail call i32 @usleep(i32 %22)
%24 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %16, %14
%27 = add nuw nsw i32 %11, 1
%28 = icmp eq i32 %27, 1000
br i1 %28, label %29, label %10
29: ; preds = %26
ret i32 0
}