#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 fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ system(char *);
int main() {
  for (int i = 0; i < 10; i++) {
    pid_t pid = fork();
    if (pid == -1) {
      fprintf(stderr, "Can't fork process\n");
      return 1;
    }
    if (!pid) {
      system("ls");
    }
    if (pid) {
      printf("Current fork's PID: %i\n", pid);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    11d7 <main+0x97>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1195 <main+0x55>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11d7 <main+0x97>
cmpl   $0x0,-0xc(%rbp)
jne    11ab <main+0x6b>
lea    0xe6e(%rip),%rdi
call   1030 <system@plt>
cmpl   $0x0,-0xc(%rbp)
je     11c4 <main+0x84>
mov    -0xc(%rbp),%esi
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
jmp    11c9 <main+0x89>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1156 <main+0x16>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x1c7>
test   %eax,%eax
jne    1163 <main+0x23>
lea    0xeb8(%rip),%rdi
call   1030 <system@plt>
jmp    1171 <main+0x31>
lea    0xead(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x1c7>
test   %eax,%eax
jne    1193 <main+0x53>
lea    0xe88(%rip),%rdi
call   1030 <system@plt>
jmp    11a1 <main+0x61>
lea    0xe7d(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x1c7>
test   %eax,%eax
jne    11c3 <main+0x83>
lea    0xe58(%rip),%rdi
call   1030 <system@plt>
jmp    11d1 <main+0x91>
lea    0xe4d(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x1c7>
test   %eax,%eax
jne    11f3 <main+0xb3>
lea    0xe28(%rip),%rdi
call   1030 <system@plt>
jmp    1201 <main+0xc1>
lea    0xe1d(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x1c7>
test   %eax,%eax
jne    1223 <main+0xe3>
lea    0xdf8(%rip),%rdi
call   1030 <system@plt>
jmp    1231 <main+0xf1>
lea    0xded(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x1c7>
test   %eax,%eax
jne    1253 <main+0x113>
lea    0xdc8(%rip),%rdi
call   1030 <system@plt>
jmp    1261 <main+0x121>
lea    0xdbd(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x1c7>
test   %eax,%eax
jne    1283 <main+0x143>
lea    0xd98(%rip),%rdi
call   1030 <system@plt>
jmp    1291 <main+0x151>
lea    0xd8d(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x1c7>
test   %eax,%eax
jne    12af <main+0x16f>
lea    0xd6c(%rip),%rdi
call   1030 <system@plt>
jmp    12bd <main+0x17d>
lea    0xd61(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x1c7>
test   %eax,%eax
jne    12db <main+0x19b>
lea    0xd40(%rip),%rdi
call   1030 <system@plt>
jmp    12e9 <main+0x1a9>
lea    0xd35(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x1c7>
test   %eax,%eax
jne    1323 <main+0x1e3>
lea    0xd14(%rip),%rdi
call   1030 <system@plt>
jmp    1331 <main+0x1f1>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
lea    0xce9(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
pop    %rcx
ret
lea    0xced(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Can't fork process\0A\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [24 x i8] c"Current fork's PID: %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
br label %4
4: ; preds = %26, %0
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 10
br i1 %6, label %7, label %29
7: ; preds = %4
%8 = call i32 (...) @fork()
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %29
14: ; preds = %7
%15 = load i32, i32* %3
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = load i32, i32* %3
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* %3
%24 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %19
br label %26
26: ; preds = %25
%27 = load i32, i32* %2
%28 = add nsw i32 %27, 1
store i32 %28, i32* %2
br label %4
29: ; preds = %11, %4
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Can't fork process\0A\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [24 x i8] c"Current fork's PID: %i\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %4 [
i32 -1, label %6
i32 0, label %2
]
2: ; preds = %0
%3 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %9
4: ; preds = %0
%5 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %1)
br label %9
6: ; preds = %57, %51, %45, %39, %33, %27, %21, %15, %9, %0
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %63
9: ; preds = %2, %4
%10 = tail call i32 (...) @fork()
switch i32 %10, label %13 [
i32 -1, label %6
i32 0, label %11
]
11: ; preds = %9
%12 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %15
13: ; preds = %9
%14 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %15
15: ; preds = %13, %11
%16 = tail call i32 (...) @fork()
switch i32 %16, label %19 [
i32 -1, label %6
i32 0, label %17
]
17: ; preds = %15
%18 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %21
19: ; preds = %15
%20 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %21
21: ; preds = %19, %17
%22 = tail call i32 (...) @fork()
switch i32 %22, label %25 [
i32 -1, label %6
i32 0, label %23
]
23: ; preds = %21
%24 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %27
25: ; preds = %21
%26 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %27
27: ; preds = %25, %23
%28 = tail call i32 (...) @fork()
switch i32 %28, label %31 [
i32 -1, label %6
i32 0, label %29
]
29: ; preds = %27
%30 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %33
31: ; preds = %27
%32 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %28)
br label %33
33: ; preds = %31, %29
%34 = tail call i32 (...) @fork()
switch i32 %34, label %37 [
i32 -1, label %6
i32 0, label %35
]
35: ; preds = %33
%36 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %39
37: ; preds = %33
%38 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %34)
br label %39
39: ; preds = %37, %35
%40 = tail call i32 (...) @fork()
switch i32 %40, label %43 [
i32 -1, label %6
i32 0, label %41
]
41: ; preds = %39
%42 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %45
43: ; preds = %39
%44 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %40)
br label %45
45: ; preds = %43, %41
%46 = tail call i32 (...) @fork()
switch i32 %46, label %49 [
i32 -1, label %6
i32 0, label %47
]
47: ; preds = %45
%48 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %51
49: ; preds = %45
%50 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %46)
br label %51
51: ; preds = %49, %47
%52 = tail call i32 (...) @fork()
switch i32 %52, label %55 [
i32 -1, label %6
i32 0, label %53
]
53: ; preds = %51
%54 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %57
55: ; preds = %51
%56 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %52)
br label %57
57: ; preds = %55, %53
%58 = tail call i32 (...) @fork()
switch i32 %58, label %61 [
i32 -1, label %6
i32 0, label %59
]
59: ; preds = %57
%60 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %63
61: ; preds = %57
%62 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %58)
br label %63
63: ; preds = %59, %61, %6
%64 = phi i32 [ 1, %6 ], [ 0, %61 ], [ 0, %59 ]
ret i32 %64
}