#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*/ printf(char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
void run_parallel(int tasks, void (*fn)(int i, void *data), void *data) {
  pid_t pid[tasks];
  int i;
  for (i = 0; i < tasks; i++) {
    pid[i] = fork();
    if (pid[i] == 0) {
      fn(i, data);
      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_parallel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1216 <run_parallel+0xc6>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,%edx
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
movslq -0x2c(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jne    11d3 <run_parallel+0x83>
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   *%rax
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,4)
jne    11fe <run_parallel+0xae>
mov    -0x2c(%rbp),%esi
lea    0xe11(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    1203 <run_parallel+0xb3>
jmp    1208 <run_parallel+0xb8>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1191 <run_parallel+0x41>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1276 <run_parallel+0x126>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x38(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0x30(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    121d <run_parallel+0xcd>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_parallel>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r13d
mov    %rsp,%r15
lea    0xf(,%r13,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
test   %edi,%edi
jle    11f7 <run_parallel+0xa7>
mov    %rdx,%r14
mov    %edi,%r12d
mov    %rsi,-0x38(%rbp)
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,(%r15,%rbx,4)
cmp    $0xffffffff,%eax
je     1206 <run_parallel+0xb6>
test   %eax,%eax
je     121e <run_parallel+0xce>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1190 <run_parallel+0x40>
test   %r12d,%r12d
jle    11f7 <run_parallel+0xa7>
xor    %ebx,%ebx
lea    -0x2c(%rbp),%r14
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%r12d
mov    %r12d,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
xor    %edi,%edi
cmp    %r12d,%eax
sete   %dil
call   1040 <assert@plt>
xor    %edi,%edi
cmpl   $0x0,-0x2c(%rbp)
sete   %dil
call   1040 <assert@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11c0 <run_parallel+0x70>
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   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   *-0x38(%rbp)
xor    %edi,%edi
call   1060 <exit@plt>

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

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