#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);
int /*<<< orphan*/ printf(char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
void run_perf_test(int tasks, int flags) {
  pid_t pid[tasks];
  int i;
  for (i = 0; i < tasks; i++) {
    pid[i] = fork();
    if (pid[i] == 0) {
      loop(i, flags);
      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    %esi,-0x8(%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    121f <run_perf_test+0xbf>
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    11dc <run_perf_test+0x7c>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%esi
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    1207 <run_perf_test+0xa7>
mov    -0x1c(%rbp),%esi
lea    0xe08(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    120c <run_perf_test+0xac>
jmp    1211 <run_perf_test+0xb1>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119c <run_perf_test+0x3c>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    127f <run_perf_test+0x11f>
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    1226 <run_perf_test+0xc6>
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,%r12d
mov    %rsp,%r13
lea    0xf(,%r12,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
test   %edi,%edi
jle    1208 <run_perf_test+0xa8>
mov    %esi,%r14d
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,0x0(%r13,%rbx,4)
cmp    $0xffffffff,%eax
je     1217 <run_perf_test+0xb7>
test   %eax,%eax
je     122f <run_perf_test+0xcf>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11a0 <run_perf_test+0x40>
test   %r15d,%r15d
jle    1208 <run_perf_test+0xa8>
xor    %ebx,%ebx
lea    -0x2c(%rbp),%r14
nopl   0x0(%rax)
mov    0x0(%r13,%rbx,4),%r15d
mov    %r15d,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
cmp    %r15d,%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,%r12
jne    11d0 <run_perf_test+0x70>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde2(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %ebx,%edi
mov    %r14d,%esi
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, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %3
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i32, i64 %10
store i64 %10, i64* %6
store i32 0, i32* %7
br label %13
13: ; preds = %44, %2
%14 = load i32, i32* %7
%15 = load i32, i32* %3
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %47
17: ; preds = %13
%18 = call i32 (...) @fork()
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %12, i64 %20
store i32 %18, i32* %21
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %12, i64 %23
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %17
%28 = load i32, i32* %7
%29 = load i32, i32* %4
%30 = call i32 @loop(i32 %28, i32 %29)
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %17
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %12, i64 %34
%36 = load i32, i32* %35
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %32
%39 = load i32, i32* %7
%40 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %32
br label %43
43: ; preds = %42
br label %44
44: ; preds = %43
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
br label %13
47: ; preds = %13
store i32 0, i32* %7
br label %48
48: ; preds = %69, %47
%49 = load i32, i32* %7
%50 = load i32, i32* %3
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %72
52: ; preds = %48
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %12, i64 %54
%56 = load i32, i32* %55
%57 = call i32 @waitpid(i32 %56, i32* %8, i32 0)
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %12, i64 %59
%61 = load i32, i32* %60
%62 = icmp eq i32 %57, %61
%63 = zext i1 %62 to i32
%64 = call i32 @assert(i32 %63)
%65 = load i32, i32* %8
%66 = icmp eq i32 %65, 0
%67 = zext i1 %66 to i32
%68 = call i32 @assert(i32 %67)
br label %69
69: ; preds = %52
%70 = load i32, i32* %7
%71 = add nsw i32 %70, 1
store i32 %71, i32* %7
br label %48
72: ; preds = %48
%73 = load i8*, i8** %5
ret void
}

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