#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGCONT;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ fork();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int start_process(unsigned long);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int do_test(unsigned long nr_procs, unsigned long nr_bytes) {
  pid_t procs[nr_procs];
  unsigned long i;
  int dummy;
  for (i = 0; i < nr_procs; i++) {
    switch ((procs[i] = fork())) {
    case -1:
      kill(0, SIGKILL);
      perror("fork()");
      return -1;
    case 0:
      return start_process(nr_bytes);
    default:
      waitpid(procs[i], &dummy, WUNTRACED);
      break;
    }
  }
  kill(0, SIGCONT);
  for (i = 0; i < nr_procs; i++)
    waitpid(procs[i], &dummy, 0);
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    124d <do_test+0xfd>
xor    %eax,%eax
call   1070 <fork@plt>
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%rdx
mov    %eax,(%rcx,%rdx,4)
mov    %eax,%ecx
mov    %ecx,-0x44(%rbp)
sub    $0xffffffff,%eax
je     11d3 <do_test+0x83>
jmp    11c3 <do_test+0x73>
mov    -0x44(%rbp),%eax
test   %eax,%eax
je     1202 <do_test+0xb2>
jmp    121a <do_test+0xca>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1040 <kill@plt>
lea    0xe16(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12a8 <do_test+0x158>
mov    -0x18(%rbp),%rdi
call   1030 <start_process@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12a8 <do_test+0x158>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rsi
call   1050 <waitpid@plt>
jmp    123c <do_test+0xec>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1190 <do_test+0x40>
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1040 <kill@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    129a <do_test+0x14a>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x34(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1265 <do_test+0x115>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%r13
lea    0xf(,%rdi,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
test   %rdi,%rdi
je     11fa <do_test+0xaa>
mov    %rdi,%r12
mov    %rsi,-0x38(%rbp)
mov    0x2e4f(%rip),%rbx
lea    -0x2c(%rbp),%r15
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,0x0(%r13,%r14,4)
test   %eax,%eax
je     120f <do_test+0xbf>
cmp    $0xffffffff,%eax
je     122d <do_test+0xdd>
mov    (%rbx),%edx
mov    %eax,%edi
mov    %r15,%rsi
call   1050 <waitpid@plt>
add    $0x1,%r14
cmp    %r14,%r12
jne    1190 <do_test+0x40>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edi,%edi
call   1040 <kill@plt>
test   %r12,%r12
je     121b <do_test+0xcb>
xor    %r14d,%r14d
lea    -0x2c(%rbp),%r15
xor    %ebx,%ebx
nop
mov    0x0(%r13,%rbx,4),%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11e0 <do_test+0x90>
jmp    121b <do_test+0xcb>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edi,%edi
call   1040 <kill@plt>
jmp    121b <do_test+0xcb>
mov    -0x38(%rbp),%rdi
call   1030 <start_process@plt>
mov    %eax,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1040 <kill@plt>
lea    0xdbc(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r14d
jmp    121b <do_test+0xcb>

LLVM IR (-O0):
@SIGKILL = global i32 0
@.str = [7 x i8] c"fork()\00"
@WUNTRACED = global i32 0
@SIGCONT = global i32 0
define i32 @do_test(i64 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i64 %0, i64* %4
store i64 %1, i64* %5
%11 = load i64, i64* %4
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i32, i64 %11
store i64 %11, i64* %7
store i64 0, i64* %8
br label %14
14: ; preds = %36, %2
%15 = load i64, i64* %8
%16 = load i64, i64* %4
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %39
18: ; preds = %14
%19 = call i32 (...) @fork()
%20 = load i64, i64* %8
%21 = getelementptr inbounds i32, i32* %13, i64 %20
store i32 %19, i32* %21
switch i32 %19, label %29 [
i32 -1, label %22
i32 0, label %26
]
22: ; preds = %18
%23 = load i32, i32* @SIGKILL
%24 = call i32 @kill(i32 0, i32 %23)
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %10
br label %55
26: ; preds = %18
%27 = load i64, i64* %5
%28 = call i32 @start_process(i64 %27)
store i32 %28, i32* %3
store i32 1, i32* %10
br label %55
29: ; preds = %18
%30 = load i64, i64* %8
%31 = getelementptr inbounds i32, i32* %13, i64 %30
%32 = load i32, i32* %31
%33 = load i32, i32* @WUNTRACED
%34 = call i32 @waitpid(i32 %32, i32* %9, i32 %33)
br label %35
35: ; preds = %29
br label %36
36: ; preds = %35
%37 = load i64, i64* %8
%38 = add i64 %37, 1
store i64 %38, i64* %8
br label %14
39: ; preds = %14
%40 = load i32, i32* @SIGCONT
%41 = call i32 @kill(i32 0, i32 %40)
store i64 0, i64* %8
br label %42
42: ; preds = %51, %39
%43 = load i64, i64* %8
%44 = load i64, i64* %4
%45 = icmp ult i64 %43, %44
br i1 %45, label %46, label %54
46: ; preds = %42
%47 = load i64, i64* %8
%48 = getelementptr inbounds i32, i32* %13, i64 %47
%49 = load i32, i32* %48
%50 = call i32 @waitpid(i32 %49, i32* %9, i32 0)
br label %51
51: ; preds = %46
%52 = load i64, i64* %8
%53 = add i64 %52, 1
store i64 %53, i64* %8
br label %42
54: ; preds = %42
store i32 0, i32* %3
store i32 1, i32* %10
br label %55
55: ; preds = %54, %26, %22
%56 = load i8*, i8** %6
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@SIGKILL = global i32 0
@.str = [7 x i8] c"fork()\00"
@WUNTRACED = global i32 0
@SIGCONT = global i32 0
define i32 @do_test(i64 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32, i64 %0
%5 = bitcast i32* %3 to i8*
%6 = icmp eq i64 %0, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @SIGCONT
%9 = call i32 @kill(i32 0, i32 %8)
br label %35
10: ; preds = %2, %20
%11 = phi i64 [ %23, %20 ], [ 0, %2 ]
%12 = call i32 (...) @fork()
%13 = getelementptr inbounds i32, i32* %4, i64 %11
store i32 %12, i32* %13
switch i32 %12, label %20 [
i32 -1, label %14
i32 0, label %18
]
14: ; preds = %10
%15 = load i32, i32* @SIGKILL
%16 = call i32 @kill(i32 0, i32 %15)
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %35
18: ; preds = %10
%19 = call i32 @start_process(i64 %1)
br label %35
20: ; preds = %10
%21 = load i32, i32* @WUNTRACED
%22 = call i32 @waitpid(i32 %12, i32* %3, i32 %21)
%23 = add nuw i64 %11, 1
%24 = icmp eq i64 %23, %0
br i1 %24, label %25, label %10
25: ; preds = %20
%26 = load i32, i32* @SIGCONT
%27 = call i32 @kill(i32 0, i32 %26)
br i1 %6, label %35, label %28
28: ; preds = %25, %28
%29 = phi i64 [ %33, %28 ], [ 0, %25 ]
%30 = getelementptr inbounds i32, i32* %4, i64 %29
%31 = load i32, i32* %30
%32 = call i32 @waitpid(i32 %31, i32* %3, i32 0)
%33 = add nuw i64 %29, 1
%34 = icmp eq i64 %33, %0
br i1 %34, label %35, label %28
35: ; preds = %28, %7, %25, %18, %14
%36 = phi i32 [ %19, %18 ], [ -1, %14 ], [ 0, %25 ], [ 0, %7 ], [ 0, %28 ]
ret i32 %36
}