#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 atoi(char *);
int /*<<< orphan*/ child(char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ parent(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("Usage: %s [number].\n", argv[0]);
    return (-1);
  }
  int num = atoi(argv[1]);
  for (int i = 0; i < num; i += 3) {
    pid_t child_id;
    if ((child_id = fork()) == -1) {
      fprintf(stderr, "\e[1;31mfork() failed.\e[0m\n");
      exit(1);
    }
    if (child_id != 0) {
      parent(&child_id);
    } else {
      child(argv[1]);
    }
  }
  printf("Alpha: exiting.\n");
  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   $0x2,-0x8(%rbp)
je     11b1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <main+0xe3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    123e <main+0xce>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1206 <main+0x96>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     121e <main+0xae>
lea    -0x1c(%rbp),%rdi
call   1040 <parent@plt>
jmp    122b <main+0xbb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <child@plt>
jmp    1230 <main+0xc0>
mov    -0x18(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x18(%rbp)
jmp    11c8 <main+0x58>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
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   %rbx
push   %rax
mov    %rsi,%r15
cmp    $0x2,%edi
jne    11de <main+0x5e>
mov    0x8(%r15),%rdi
call   1080 <atoi@plt>
test   %eax,%eax
jle    11f6 <main+0x76>
mov    %eax,%ebp
lea    0x4(%rsp),%r14
xor    %ebx,%ebx
jmp    11c0 <main+0x40>
nopw   0x0(%rax,%rax,1)
mov    0x8(%r15),%rdi
call   1030 <child@plt>
add    $0x3,%ebx
cmp    %ebp,%ebx
jge    11f6 <main+0x76>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
je     11b0 <main+0x30>
cmp    $0xffffffff,%eax
je     120f <main+0x8f>
mov    %r14,%rdi
call   1040 <parent@plt>
jmp    11b9 <main+0x39>
mov    (%r15),%rsi
lea    0xe18(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
jmp    1204 <main+0x84>
lea    0xe33(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"Usage: %s [number].\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"\1B[1;31mfork() failed.\1B[0m\0A\00"
@.str.2 = [17 x i8] c"Alpha: exiting.\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 -1, i32* %3
br label %48
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %6
store i32 0, i32* %7
br label %21
21: ; preds = %43, %16
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %46
25: ; preds = %21
%26 = call i32 (...) @fork()
store i32 %26, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @parent(i32* %8)
br label %42
37: ; preds = %32
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 @child(i8* %40)
br label %42
42: ; preds = %37, %35
br label %43
43: ; preds = %42
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 3
store i32 %45, i32* %7
br label %21
46: ; preds = %21
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %48
48: ; preds = %46, %11
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [21 x i8] c"Usage: %s [number].\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"\1B[1;31mfork() failed.\1B[0m\0A\00"
@str = [16 x i8] c"Alpha: exiting.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %32
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = bitcast i32* %3 to i8*
br label %17
15: ; preds = %29, %8
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %32
17: ; preds = %13, %29
%18 = phi i32 [ 0, %13 ], [ %30, %29 ]
%19 = call i32 (...) @fork()
store i32 %19, i32* %3
switch i32 %19, label %24 [
i32 -1, label %20
i32 0, label %26
]
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = call i32 @parent(i32* %3)
br label %29
26: ; preds = %17
%27 = load i8*, i8** %9
%28 = call i32 @child(i8* %27)
br label %29
29: ; preds = %26, %24
%30 = add nuw nsw i32 %18, 3
%31 = icmp slt i32 %30, %11
br i1 %31, label %17, label %15
32: ; preds = %15, %5
%33 = phi i32 [ -1, %5 ], [ 0, %15 ]
ret i32 %33
}