#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 /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ loop(int);
int /*<<< orphan*/ printf(char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
void run_perf_test(int tasks) {
  pid_t pid[tasks];
  int i;
  for (i = 0; i < tasks; i++) {
    pid[i] = fork();
    if (pid[i] == 0) {
      loop(i);
      exit(0);
    } else if (pid[i] == -1) {
      printf("couldn't spawn #%d process\n", i);
      exit(1);
    }
  }
  for (i = 0; i < tasks; i++) {
    int status;
    assert(waitpid(pid[i], &status, 0) == pid[i]);
    assert(status == 0);
  }
}


x86 assembly (clang, -O0):
<run_perf_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1219 <run_perf_test+0xb9>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
movslq -0x1c(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jne    11d6 <run_perf_test+0x76>
mov    -0x1c(%rbp),%edi
call   1030 <loop@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,4)
jne    1201 <run_perf_test+0xa1>
mov    -0x1c(%rbp),%esi
lea    0xe0e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1206 <run_perf_test+0xa6>
jmp    120b <run_perf_test+0xab>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1199 <run_perf_test+0x39>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1279 <run_perf_test+0x119>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    -0x28(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x0,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1220 <run_perf_test+0xc0>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_perf_test>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
mov    %rsp,%r12
lea    0xf(,%r15,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
test   %edi,%edi
jle    11f7 <run_perf_test+0x97>
mov    %edi,%r14d
xor    %ebx,%ebx
nop
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,(%r12,%rbx,4)
cmp    $0xffffffff,%eax
je     1206 <run_perf_test+0xa6>
test   %eax,%eax
je     121e <run_perf_test+0xbe>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1190 <run_perf_test+0x30>
test   %r14d,%r14d
jle    11f7 <run_perf_test+0x97>
xor    %ebx,%ebx
lea    -0x2c(%rbp),%r14
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbx,4),%r13d
mov    %r13d,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
cmp    %r13d,%eax
sete   %dil
call   1050 <assert@plt>
xor    %edi,%edi
cmpl   $0x0,-0x2c(%rbp)
sete   %dil
call   1050 <assert@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11c0 <run_perf_test+0x60>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf3(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %ebx,%edi
call   1030 <loop@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [28 x i8] c"couldn't spawn #%d process\0A\00"
define void @run_perf_test(i32 %0) {
%2 = alloca i32
%3 = zext i32 %0 to i64
%4 = alloca i32, i64 %3
%5 = icmp sgt i32 %0, 0
br i1 %5, label %9, label %38
6: ; preds = %21
br i1 %5, label %7, label %38
7: ; preds = %6
%8 = bitcast i32* %2 to i8*
br label %24
9: ; preds = %1, %21
%10 = phi i64 [ %22, %21 ], [ 0, %1 ]
%11 = tail call i32 (...) @fork()
%12 = getelementptr inbounds i32, i32* %4, i64 %10
store i32 %11, i32* %12
switch i32 %11, label %21 [
i32 0, label %13
i32 -1, label %17
]
13: ; preds = %9
%14 = trunc i64 %10 to i32
%15 = tail call i32 @loop(i32 %14)
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %9
%18 = trunc i64 %10 to i32
%19 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %9
%22 = add nuw nsw i64 %10, 1
%23 = icmp eq i64 %22, %3
br i1 %23, label %6, label %9
24: ; preds = %7, %24
%25 = phi i64 [ 0, %7 ], [ %36, %24 ]
%26 = getelementptr inbounds i32, i32* %4, i64 %25
%27 = load i32, i32* %26
%28 = call i32 @waitpid(i32 %27, i32* %2, i32 0)
%29 = icmp eq i32 %28, %27
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32, i32* %2
%33 = icmp eq i32 %32, 0
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = add nuw nsw i64 %25, 1
%37 = icmp eq i64 %36, %3
br i1 %37, label %38, label %24
38: ; preds = %24, %1, %6
ret void
}