#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*/ pre_test(int);
int /*<<< orphan*/ printf(char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
void run_perf_test(int tasks) {
  pid_t pid[tasks];
  int i;
  assert(!pre_test(tasks));
  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)
mov    -0x4(%rbp),%edi
call   1060 <pre_test@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1243 <run_perf_test+0xd3>
mov    $0x0,%al
call   1090 <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    1200 <run_perf_test+0x90>
mov    -0x1c(%rbp),%edi
call   1030 <loop@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,4)
jne    122b <run_perf_test+0xbb>
mov    -0x1c(%rbp),%esi
lea    0xde4(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1230 <run_perf_test+0xc0>
jmp    1235 <run_perf_test+0xc5>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c3 <run_perf_test+0x53>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12a3 <run_perf_test+0x133>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1070 <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    124a <run_perf_test+0xda>
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,%r14d
mov    %edi,%r15d
mov    %rsp,%r12
lea    0xf(,%r15,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
call   1060 <pre_test@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
test   %r14d,%r14d
jle    1227 <run_perf_test+0xb7>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,(%r12,%rbx,4)
cmp    $0xffffffff,%eax
je     1236 <run_perf_test+0xc6>
test   %eax,%eax
je     124e <run_perf_test+0xde>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11c0 <run_perf_test+0x50>
test   %r14d,%r14d
jle    1227 <run_perf_test+0xb7>
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   1070 <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    11f0 <run_perf_test+0x80>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc3(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %ebx,%edi
call   1030 <loop@plt>
xor    %edi,%edi
call   1080 <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
%11 = load i32, i32* %2
%12 = call i32 @pre_test(i32 %11)
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
store i32 0, i32* %5
br label %17
17: ; preds = %47, %1
%18 = load i32, i32* %5
%19 = load i32, i32* %2
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %50
21: ; preds = %17
%22 = call i32 (...) @fork()
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %10, i64 %24
store i32 %22, i32* %25
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %10, i64 %27
%29 = load i32, i32* %28
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %21
%32 = load i32, i32* %5
%33 = call i32 @loop(i32 %32)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %21
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %10, i64 %37
%39 = load i32, i32* %38
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %35
%42 = load i32, i32* %5
%43 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %35
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 1
store i32 %49, i32* %5
br label %17
50: ; preds = %17
store i32 0, i32* %5
br label %51
51: ; preds = %72, %50
%52 = load i32, i32* %5
%53 = load i32, i32* %2
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %75
55: ; preds = %51
%56 = load i32, i32* %5
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %10, i64 %57
%59 = load i32, i32* %58
%60 = call i32 @waitpid(i32 %59, i32* %6, i32 0)
%61 = load i32, i32* %5
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %10, i64 %62
%64 = load i32, i32* %63
%65 = icmp eq i32 %60, %64
%66 = zext i1 %65 to i32
%67 = call i32 @assert(i32 %66)
%68 = load i32, i32* %6
%69 = icmp eq i32 %68, 0
%70 = zext i1 %69 to i32
%71 = call i32 @assert(i32 %70)
br label %72
72: ; preds = %55
%73 = load i32, i32* %5
%74 = add nsw i32 %73, 1
store i32 %74, i32* %5
br label %51
75: ; preds = %51
%76 = 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 = tail call i32 @pre_test(i32 %0)
%6 = icmp eq i32 %5, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = icmp sgt i32 %0, 0
br i1 %9, label %13, label %42
10: ; preds = %25
br i1 %9, label %11, label %42
11: ; preds = %10
%12 = bitcast i32* %2 to i8*
br label %28
13: ; preds = %1, %25
%14 = phi i64 [ %26, %25 ], [ 0, %1 ]
%15 = tail call i32 (...) @fork()
%16 = getelementptr inbounds i32, i32* %4, i64 %14
store i32 %15, i32* %16
switch i32 %15, label %25 [
i32 0, label %17
i32 -1, label %21
]
17: ; preds = %13
%18 = trunc i64 %14 to i32
%19 = tail call i32 @loop(i32 %18)
%20 = tail call i32 @exit(i32 0)
unreachable
21: ; preds = %13
%22 = trunc i64 %14 to i32
%23 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %13
%26 = add nuw nsw i64 %14, 1
%27 = icmp eq i64 %26, %3
br i1 %27, label %10, label %13
28: ; preds = %11, %28
%29 = phi i64 [ 0, %11 ], [ %40, %28 ]
%30 = getelementptr inbounds i32, i32* %4, i64 %29
%31 = load i32, i32* %30
%32 = call i32 @waitpid(i32 %31, i32* %2, i32 0)
%33 = icmp eq i32 %32, %31
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i32, i32* %2
%37 = icmp eq i32 %36, 0
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = add nuw nsw i64 %29, 1
%41 = icmp eq i64 %40, %3
br i1 %41, label %42, label %28
42: ; preds = %28, %1, %10
ret void
}