#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 */

/* Variables and functions */
int atoi(char *);
int execve(char *, char **, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc < 3) {
    fprintf(stderr, "forker count /path/to/exe [ args ]\n");
    return 1;
  }
  int count = atoi(argv[1]);
  int i;
  for (i = 0; i < count; i++) {
    int pid = fork();
    if (pid == -1) {
      fprintf(stderr, "fork fault\n");
      return 2;
    }
    if (pid == 0) {
      int r = execve(argv[2], argv + 2, 0);
      if (r == -1) {
        fprintf(stderr, "exec fault\n");
        return 3;
      }
    }
  }
  for (i = 0; i < count; i++) {
    wait(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1191 <main+0x41>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127f <main+0x12f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1250 <main+0x100>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e9 <main+0x99>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    127f <main+0x12f>
cmpl   $0x0,-0x1c(%rbp)
jne    123d <main+0xed>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
xor    %edx,%edx
call   1030 <execve@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1238 <main+0xe8>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    127f <main+0x12f>
jmp    123d <main+0xed>
jmp    1242 <main+0xf2>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11a8 <main+0x58>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1278 <main+0x128>
xor    %edi,%edi
call   1060 <wait@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1257 <main+0x107>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x3,%edi
jge    117f <main+0x2f>
mov    $0x1,%ebp
lea    0xe97(%rip),%r14
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   1040 <fprintf@plt>
jmp    11fc <main+0xac>
mov    %rsi,%r15
mov    0x8(%rsi),%rdi
call   1050 <atoi@plt>
test   %eax,%eax
jle    11de <main+0x8e>
mov    %eax,%ebx
add    $0x10,%r15
lea    0xe88(%rip),%r14
lea    0xe8d(%rip),%r12
mov    %eax,%ebp
jmp    11c7 <main+0x77>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <execve@plt>
cmp    $0xffffffff,%eax
je     1207 <main+0xb7>
add    $0xffffffff,%ebp
je     11e2 <main+0x92>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     11b0 <main+0x60>
cmp    $0xffffffff,%eax
jne    11c2 <main+0x72>
mov    $0x2,%ebp
jmp    1169 <main+0x19>
xor    %ebp,%ebp
jmp    11fc <main+0xac>
xor    %ebp,%ebp
test   %ebx,%ebx
jle    11fc <main+0xac>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1060 <wait@plt>
add    $0xffffffff,%ebx
jne    11f0 <main+0xa0>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x3,%ebp
mov    %r12,%r14
jmp    1169 <main+0x19>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"forker count /path/to/exe [ args ]\0A\00"
@.str.1 = [12 x i8] c"fork fault\0A\00"
@.str.2 = [12 x i8] c"exec fault\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %62
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %6
store i32 0, i32* %7
br label %20
20: ; preds = %48, %15
%21 = load i32, i32* %7
%22 = load i32, i32* %6
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %51
24: ; preds = %20
%25 = call i32 (...) @fork()
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %62
31: ; preds = %24
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %47
34: ; preds = %31
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = call i32 @execve(i8* %37, i8** %39, i32 0)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %34
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %3
br label %62
46: ; preds = %34
br label %47
47: ; preds = %46, %31
br label %48
48: ; preds = %47
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %20
51: ; preds = %20
store i32 0, i32* %7
br label %52
52: ; preds = %58, %51
%53 = load i32, i32* %7
%54 = load i32, i32* %6
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %61
56: ; preds = %52
%57 = call i32 @wait(i32 0)
br label %58
58: ; preds = %56
%59 = load i32, i32* %7
%60 = add nsw i32 %59, 1
store i32 %60, i32* %7
br label %52
61: ; preds = %52
store i32 0, i32* %3
br label %62
62: ; preds = %61, %43, %28, %12
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"forker count /path/to/exe [ args ]\0A\00"
@.str.1 = [12 x i8] c"fork fault\0A\00"
@.str.2 = [12 x i8] c"exec fault\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %27, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %32
9: ; preds = %4
%10 = getelementptr inbounds i8*, i8** %1, i64 2
br label %12
11: ; preds = %19
br i1 %8, label %22, label %32
12: ; preds = %9, %19
%13 = phi i32 [ 0, %9 ], [ %20, %19 ]
%14 = tail call i32 (...) @fork()
switch i32 %14, label %19 [
i32 -1, label %27
i32 0, label %15
]
15: ; preds = %12
%16 = load i8*, i8** %10
%17 = tail call i32 @execve(i8* %16, i8** %10, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %27, label %19
19: ; preds = %15, %12
%20 = add nuw nsw i32 %13, 1
%21 = icmp eq i32 %20, %7
br i1 %21, label %11, label %12
22: ; preds = %11, %22
%23 = phi i32 [ %25, %22 ], [ 0, %11 ]
%24 = tail call i32 @wait(i32 0)
%25 = add nuw nsw i32 %23, 1
%26 = icmp eq i32 %25, %7
br i1 %26, label %32, label %22
27: ; preds = %15, %12, %2
%28 = phi i8* [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %12 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %15 ]
%29 = phi i32 [ 1, %2 ], [ 2, %12 ], [ 3, %15 ]
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* %28)
br label %32
32: ; preds = %22, %27, %4, %11
%33 = phi i32 [ 0, %11 ], [ 0, %4 ], [ %29, %27 ], [ 0, %22 ]
ret i32 %33
}