#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 /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  int iter;
  int child;
  int status;
  if (argc != 2) {
    printf("Usage: %s count\n", argv[0]);
    exit(1);
  }
  iter = atoi(argv[1]);
  while (iter-- > 0) {
    if ((child = fork()) == 0) {
      exit(0);
    } else if (child < 0) {
      printf("Fork failed at iteration %d\n", iter);
      perror("Reason");
      exit(2);
    } else
      wait(&status);
    if (status != 0) {
      printf("Bad wait status: 0x%x\n", status);
      exit(2);
    }
  }
  exit(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     119f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    1246 <main+0xe6>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jne    11dd <main+0x7d>
xor    %edi,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jge    120e <main+0xae>
mov    -0x14(%rbp),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe2f(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
jmp    121c <main+0xbc>
cmpl   $0x0,-0x1c(%rbp)
je     1241 <main+0xe1>
mov    -0x1c(%rbp),%esi
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
jmp    11af <main+0x4f>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11bc <main+0x5c>
mov    0x8(%rsi),%rdi
call   1050 <atoi@plt>
mov    %eax,%ebx
lea    0x4(%rsp),%r14
nopl   0x0(%rax)
test   %ebx,%ebx
jle    11d7 <main+0x77>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11d7 <main+0x77>
js     11de <main+0x7e>
mov    %r14,%rdi
call   1070 <wait@plt>
mov    0x4(%rsp),%esi
add    $0xffffffff,%ebx
test   %esi,%esi
je     1180 <main+0x20>
lea    0xe8a(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
mov    (%rsi),%rsi
lea    0xe3a(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>
add    $0xffffffff,%ebx
lea    0xe29(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe36(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"Usage: %s count\0A\00"
@.str.1 = [29 x i8] c"Fork failed at iteration %d\0A\00"
@.str.2 = [7 x i8] c"Reason\00"
@.str.3 = [23 x i8] c"Bad wait status: 0x%x\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 %17
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 ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %6
br label %22
22: ; preds = %49, %17
%23 = load i32, i32* %6
%24 = add nsw i32 %23, -1
store i32 %24, i32* %6
%25 = icmp sgt i32 %23, 0
br i1 %25, label %26, label %50
26: ; preds = %22
%27 = call i32 (...) @fork()
store i32 %27, i32* %7
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %26
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i32, i32* %6
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 2)
unreachable
39: ; preds = %31
%40 = call i32 @wait(i32* %8)
br label %41
41: ; preds = %39
br label %42
42: ; preds = %41
%43 = load i32, i32* %8
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = load i32, i32* %8
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = call i32 @exit(i32 2)
unreachable
49: ; preds = %42
br label %22
50: ; preds = %22
%51 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [17 x i8] c"Usage: %s count\0A\00"
@.str.1 = [29 x i8] c"Fork failed at iteration %d\0A\00"
@.str.2 = [7 x i8] c"Reason\00"
@.str.3 = [23 x i8] c"Bad wait status: 0x%x\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
br label %14
14: ; preds = %29, %10
%15 = phi i32 [ %13, %10 ], [ %16, %29 ]
%16 = add nsw i32 %15, -1
%17 = icmp sgt i32 %15, 0
br i1 %17, label %18, label %36
18: ; preds = %14
%19 = call i32 (...) @fork()
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %18
%24 = icmp slt i32 %19, 0
br i1 %24, label %25, label %29
25: ; preds = %23
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %23
%30 = call i32 @wait(i32* %3)
%31 = load i32, i32* %3
%32 = icmp eq i32 %31, 0
br i1 %32, label %14, label %33
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%35 = call i32 @exit(i32 2)
unreachable
36: ; preds = %14
%37 = call i32 @exit(i32 0)
unreachable
}