#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 */
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  char c;
  int waitCounter = 4;
  int i;
  pid_t pid;
  pid_t pid1;
  pid_t pid2;
  pid_t pid3;
  for (i = 0; i <= 4; i++) {
    pid = fork();
    if (pid < 0) {
      fprintf(stderr, "Fork Failed");
      exit(-1);
    }
    if (pid == 0) {
      int num = i;
      char buf[5];
      sprintf(buf, "%d", i);
    }
  }
  while (waitCounter > 0) {
    wait(NULL);
    waitCounter--;
  }
  printf("\nChild processes Complete.\n");
  printf("Parent pid: ");
  printf("%d", getpid());
  exit(0);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x4,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
jg     1203 <main+0x93>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11cc <main+0x5c>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11f0 <main+0x80>
mov    -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x39(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe21(%rip),%rsi
call   1060 <sprintf@plt>
jmp    11f5 <main+0x85>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    118d <main+0x1d>
jmp    1208 <main+0x98>
cmpl   $0x0,-0xc(%rbp)
jle    1229 <main+0xb9>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1208 <main+0x98>
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     121e <main+0x9e>
jne    11a6 <main+0x26>
lea    0xe72(%rip),%rsi
lea    0x3(%rsp),%rdi
xor    %edx,%edx
call   1070 <sprintf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     121e <main+0x9e>
jne    11ca <main+0x4a>
lea    0xe51(%rip),%rsi
lea    0x3(%rsp),%rdi
mov    $0x1,%edx
call   1070 <sprintf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     121e <main+0x9e>
jne    11ee <main+0x6e>
lea    0xe2d(%rip),%rsi
lea    0x3(%rsp),%rdi
mov    $0x2,%edx
call   1070 <sprintf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     121e <main+0x9e>
jne    1212 <main+0x92>
lea    0xe09(%rip),%rsi
lea    0x3(%rsp),%rdi
mov    $0x3,%edx
call   1070 <sprintf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    123d <main+0xbd>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jne    1255 <main+0xd5>
lea    0xdc6(%rip),%rsi
lea    0x3(%rsp),%rdi
mov    $0x4,%edx
call   1070 <sprintf@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %edi,%edi
call   1090 <wait@plt>
lea    0xda4(%rip),%rdi
call   1030 <puts@plt>
lea    0xd8b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd73(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"Fork Failed\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [28 x i8] c"\0AChild processes Complete.\0A\00"
@.str.3 = [13 x i8] c"Parent pid: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca [5 x i8]
store i32 0, i32* %1
store i32 4, i32* %3
store i32 0, i32* %4
br label %11
11: ; preds = %31, %0
%12 = load i32, i32* %4
%13 = icmp sle i32 %12, 4
br i1 %13, label %14, label %34
14: ; preds = %11
%15 = call i64 (...) @fork()
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %14
%23 = load i64, i64* %5
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* %4
store i32 %26, i32* %9
%27 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 0
%28 = load i32, i32* %4
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %25, %22
br label %31
31: ; preds = %30
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
br label %11
34: ; preds = %11
br label %35
35: ; preds = %38, %34
%36 = load i32, i32* %3
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = call i32 @wait(i32* null)
%40 = load i32, i32* %3
%41 = add nsw i32 %40, -1
store i32 %41, i32* %3
br label %35
42: ; preds = %35
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 (...) @getpid()
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %45)
%47 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"Fork Failed\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [13 x i8] c"Parent pid: \00"
@str = [27 x i8] c"\0AChild processes Complete.\00"
define i32 @main() {
%1 = alloca [5 x i8]
%2 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %34, %27, %20, %13, %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = icmp eq i64 %3, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 0)
br label %13
13: ; preds = %9, %11
%14 = tail call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %5, label %16
16: ; preds = %13
%17 = icmp eq i64 %14, 0
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %20
20: ; preds = %18, %16
%21 = tail call i64 (...) @fork()
%22 = icmp slt i64 %21, 0
br i1 %22, label %5, label %23
23: ; preds = %20
%24 = icmp eq i64 %21, 0
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
br label %27
27: ; preds = %25, %23
%28 = tail call i64 (...) @fork()
%29 = icmp slt i64 %28, 0
br i1 %29, label %5, label %30
30: ; preds = %27
%31 = icmp eq i64 %28, 0
br i1 %31, label %32, label %34
32: ; preds = %30
%33 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 3)
br label %34
34: ; preds = %32, %30
%35 = tail call i64 (...) @fork()
%36 = icmp slt i64 %35, 0
br i1 %36, label %5, label %37
37: ; preds = %34
%38 = icmp eq i64 %35, 0
br i1 %38, label %39, label %41
39: ; preds = %37
%40 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 4)
br label %41
41: ; preds = %39, %37
%42 = tail call i32 @wait(i32* null)
%43 = tail call i32 @wait(i32* null)
%44 = tail call i32 @wait(i32* null)
%45 = tail call i32 @wait(i32* null)
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%48 = tail call i32 (...) @getpid()
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = tail call i32 @exit(i32 0)
unreachable
}