#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 */
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
scalar_t__ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ work(long);
int test(long process_count, long loop_count) {
  long i;
  pid_t pid;
  for (i = 0; i < process_count; i++) {
    pid = fork();
    if (pid < 0) {
      printf("fork fail\n");
    } else if (pid == 0) {
      work(loop_count);
      return 0;
    }
  }
  for (i = 0; i < process_count; i++) {
    pid = wait(NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    11c8 <test+0x88>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    118d <test+0x4d>
lea    0xe7d(%rip),%rdi
call   1040 <printf@plt>
jmp    11b2 <test+0x72>
cmpq   $0x0,-0x28(%rbp)
jne    11ad <test+0x6d>
mov    -0x18(%rbp),%rdi
call   1030 <work@plt>
movl   $0x0,-0x4(%rbp)
jmp    1203 <test+0xc3>
jmp    11b2 <test+0x72>
jmp    11b7 <test+0x77>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1158 <test+0x18>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    11fc <test+0xbc>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <wait@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11d0 <test+0x90>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
jle    119d <test+0x5d>
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xea6(%rip),%r15
mov    %rdi,%rbx
jmp    116e <test+0x2e>
nop
mov    %r15,%rdi
call   1030 <puts@plt>
add    $0xffffffffffffffff,%rbx
je     1186 <test+0x46>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     1160 <test+0x20>
jne    1168 <test+0x28>
mov    %r14,%rdi
call   1040 <work@plt>
jmp    119d <test+0x5d>
test   %r12,%r12
jle    119d <test+0x5d>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1050 <wait@plt>
add    $0xffffffffffffffff,%r12
jne    1190 <test+0x50>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"fork fail\0A\00"
define i32 @test(i64 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i64 %0, i64* %4
store i64 %1, i64* %5
store i64 0, i64* %6
br label %8
8: ; preds = %26, %2
%9 = load i64, i64* %6
%10 = load i64, i64* %4
%11 = icmp slt i64 %9, %10
br i1 %11, label %12, label %29
12: ; preds = %8
%13 = call i64 (...) @fork()
store i64 %13, i64* %7
%14 = load i64, i64* %7
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %25
18: ; preds = %12
%19 = load i64, i64* %7
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i64, i64* %5
%23 = call i32 @work(i64 %22)
store i32 0, i32* %3
br label %40
24: ; preds = %18
br label %25
25: ; preds = %24, %16
br label %26
26: ; preds = %25
%27 = load i64, i64* %6
%28 = add nsw i64 %27, 1
store i64 %28, i64* %6
br label %8
29: ; preds = %8
store i64 0, i64* %6
br label %30
30: ; preds = %36, %29
%31 = load i64, i64* %6
%32 = load i64, i64* %4
%33 = icmp slt i64 %31, %32
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = call i64 @wait(i32* null)
store i64 %35, i64* %7
br label %36
36: ; preds = %34
%37 = load i64, i64* %6
%38 = add nsw i64 %37, 1
store i64 %38, i64* %6
br label %30
39: ; preds = %30
store i32 0, i32* %3
br label %40
40: ; preds = %39, %21
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@str = [10 x i8] c"fork fail\00"
define i32 @test(i64 %0, i64 %1) {
%3 = icmp sgt i64 %0, 0
br i1 %3, label %5, label %23
4: ; preds = %15
br i1 %3, label %18, label %23
5: ; preds = %2, %15
%6 = phi i64 [ %16, %15 ], [ 0, %2 ]
%7 = tail call i64 (...) @fork()
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %15
11: ; preds = %5
%12 = icmp eq i64 %7, 0
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @work(i64 %1)
br label %23
15: ; preds = %9, %11
%16 = add nuw nsw i64 %6, 1
%17 = icmp eq i64 %16, %0
br i1 %17, label %4, label %5
18: ; preds = %4, %18
%19 = phi i64 [ %21, %18 ], [ 0, %4 ]
%20 = tail call i64 @wait(i32* null)
%21 = add nuw nsw i64 %19, 1
%22 = icmp eq i64 %21, %0
br i1 %22, label %23, label %18
23: ; preds = %18, %2, %4, %13
ret i32 0
}