#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 */
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int /*<<< orphan*/ *);
int main() {
  pid_t pc, pr;
  int i = 0;
  for (; i < 5; ++i) {
    pc = fork();
    if (pc < 0)
      printf("error ocurred!\n");
    else if (pc == 0) {
      printf("This is child process with pid of %d\n", getpid());
      while (1) {
        printf("%d\n", getpid());
        sleep(1);
      }
    } else {
      printf("I catched a child process with pid of %d\n", pr);
    }
  }
  while (wait(NULL))
    ;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jge    120b <main+0xbb>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1199 <main+0x49>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f8 <main+0xa8>
cmpq   $0x0,-0x10(%rbp)
jne    11e1 <main+0x91>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    11bb <main+0x6b>
mov    -0x18(%rbp),%rsi
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f8 <main+0xa8>
jmp    11fd <main+0xad>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1166 <main+0x16>
jmp    1210 <main+0xc0>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
cmp    $0x0,%rax
je     1228 <main+0xd8>
jmp    1210 <main+0xc0>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     1183 <main+0x23>
je     122f <main+0xcf>
lea    0xeac(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    118f <main+0x2f>
lea    0xec6(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11b1 <main+0x51>
je     122f <main+0xcf>
lea    0xe7e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11bd <main+0x5d>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11db <main+0x7b>
je     122f <main+0xcf>
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11e7 <main+0x87>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     1205 <main+0xa5>
je     122f <main+0xcf>
lea    0xe2a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1211 <main+0xb1>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     126f <main+0x10f>
je     122f <main+0xcf>
lea    0xe00(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1280 <main+0x120>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdc3(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdff(%rip),%rbx
nopl   (%rax)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    1250 <main+0xf0>
lea    0xdda(%rip),%rdi
call   1030 <puts@plt>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1070 <wait@plt>
test   %rax,%rax
jne    1280 <main+0x120>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [16 x i8] c"error ocurred!\0A\00"
@.str.1 = [38 x i8] c"This is child process with pid of %d\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [42 x i8] c"I catched a child process with pid of %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
br label %5
5: ; preds = %29, %0
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 5
br i1 %7, label %8, label %32
8: ; preds = %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %2
%10 = load i64, i64* %2
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %28
14: ; preds = %8
%15 = load i64, i64* %2
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %17, %20
%21 = call i32 (...) @getpid()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i32 @sleep(i32 1)
br label %20
24: ; preds = %14
%25 = load i64, i64* %3
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 %25)
br label %27
27: ; preds = %24
br label %28
28: ; preds = %27, %12
br label %29
29: ; preds = %28
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %5
32: ; preds = %5
br label %33
33: ; preds = %36, %32
%34 = call i64 @wait(i32* null)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
br label %33
37: ; preds = %33
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [38 x i8] c"This is child process with pid of %d\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [42 x i8] c"I catched a child process with pid of %d\0A\00"
@str = [15 x i8] c"error ocurred!\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %16
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %7, label %14
7: ; preds = %46, %37, %28, %19, %5
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %10, %7
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = tail call i32 @sleep(i32 1)
br label %10
14: ; preds = %5
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 undef)
br label %16
16: ; preds = %3, %14
%17 = tail call i64 (...) @fork()
%18 = icmp slt i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = icmp eq i64 %17, 0
br i1 %20, label %7, label %21
21: ; preds = %19
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 undef)
br label %25
23: ; preds = %16
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %21
%26 = tail call i64 (...) @fork()
%27 = icmp slt i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = icmp eq i64 %26, 0
br i1 %29, label %7, label %30
30: ; preds = %28
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 undef)
br label %34
32: ; preds = %25
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %34
34: ; preds = %32, %30
%35 = tail call i64 (...) @fork()
%36 = icmp slt i64 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = icmp eq i64 %35, 0
br i1 %38, label %7, label %39
39: ; preds = %37
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 undef)
br label %43
41: ; preds = %34
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %43
43: ; preds = %41, %39
%44 = tail call i64 (...) @fork()
%45 = icmp slt i64 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %43
%47 = icmp eq i64 %44, 0
br i1 %47, label %7, label %48
48: ; preds = %46
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 undef)
br label %52
50: ; preds = %43
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %52
52: ; preds = %50, %48
br label %53
53: ; preds = %52, %53
%54 = tail call i64 @wait(i32* null)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %53
56: ; preds = %53
ret i32 0
}