#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main() {
  pid_t c_pid;
  int i;
  for (i = 0; i < 10; ++i) {
    switch (c_pid = fork()) {
    case -1:
      perror("fork failed!\n");
      return 0;
    case 0:
      return 0;
      break;
    default:
      printf("fork %d!\n", i + 1);
      break;
    }
  }
  while (1)
    ;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    11c6 <main+0x96>
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     117d <main+0x4d>
jmp    116d <main+0x3d>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     1195 <main+0x65>
jmp    11a1 <main+0x71>
lea    0xe7c(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <main+0xa0>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <main+0xa0>
mov    -0xc(%rbp),%esi
add    $0x1,%esi
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
jmp    11b8 <main+0x88>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1146 <main+0x16>
jmp    11cb <main+0x9b>
jmp    11cb <main+0x9b>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     12be <main+0x18e>
cmp    $0xffffffff,%eax
je     12b2 <main+0x182>
lea    0xebe(%rip),%rdi
mov    $0x1,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     12be <main+0x18e>
cmp    $0xffffffff,%eax
je     12b2 <main+0x182>
lea    0xe95(%rip),%rdi
mov    $0x2,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     12be <main+0x18e>
cmp    $0xffffffff,%eax
je     12b2 <main+0x182>
lea    0xe6c(%rip),%rdi
mov    $0x3,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     12be <main+0x18e>
cmp    $0xffffffff,%eax
je     12b2 <main+0x182>
lea    0xe43(%rip),%rdi
mov    $0x4,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     12be <main+0x18e>
cmp    $0xffffffff,%eax
je     12b2 <main+0x182>
lea    0xe1a(%rip),%rdi
mov    $0x5,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     12be <main+0x18e>
cmp    $0xffffffff,%eax
je     12b2 <main+0x182>
lea    0xdf1(%rip),%rdi
mov    $0x6,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     12be <main+0x18e>
cmp    $0xffffffff,%eax
je     12b2 <main+0x182>
lea    0xdcc(%rip),%rdi
mov    $0x7,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     12be <main+0x18e>
cmp    $0xffffffff,%eax
je     12b2 <main+0x182>
lea    0xdab(%rip),%rdi
mov    $0x8,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     12be <main+0x18e>
cmp    $0xffffffff,%eax
je     12b2 <main+0x182>
lea    0xd8a(%rip),%rdi
mov    $0x9,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     12be <main+0x18e>
cmp    $0xffffffff,%eax
je     12b2 <main+0x182>
lea    0xd69(%rip),%rdi
mov    $0xa,%esi
call   1030 <printf@plt>
nop
jmp    12b0 <main+0x180>
lea    0xd47(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"fork failed!\0A\00"
@.str.1 = [10 x i8] c"fork %d!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
br label %4
4: ; preds = %17, %0
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 10
br i1 %6, label %7, label %20
7: ; preds = %4
%8 = call i32 (...) @fork()
store i32 %8, i32* %2
switch i32 %8, label %12 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %7
%10 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %22
11: ; preds = %7
store i32 0, i32* %1
br label %22
12: ; preds = %7
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 1
%15 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12
br label %17
17: ; preds = %16
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
store i32 %19, i32* %3
br label %4
20: ; preds = %4
br label %21
21: ; preds = %20, %21
br label %21
22: ; preds = %11, %9
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [14 x i8] c"fork failed!\0A\00"
@.str.1 = [10 x i8] c"fork %d!\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %4 [
i32 -1, label %2
i32 0, label %34
]
2: ; preds = %28, %25, %22, %19, %16, %13, %10, %7, %4, %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %34
4: ; preds = %0
%5 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 1)
%6 = tail call i32 (...) @fork()
switch i32 %6, label %7 [
i32 -1, label %2
i32 0, label %34
]
7: ; preds = %4
%8 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 2)
%9 = tail call i32 (...) @fork()
switch i32 %9, label %10 [
i32 -1, label %2
i32 0, label %34
]
10: ; preds = %7
%11 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 3)
%12 = tail call i32 (...) @fork()
switch i32 %12, label %13 [
i32 -1, label %2
i32 0, label %34
]
13: ; preds = %10
%14 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 4)
%15 = tail call i32 (...) @fork()
switch i32 %15, label %16 [
i32 -1, label %2
i32 0, label %34
]
16: ; preds = %13
%17 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 5)
%18 = tail call i32 (...) @fork()
switch i32 %18, label %19 [
i32 -1, label %2
i32 0, label %34
]
19: ; preds = %16
%20 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 6)
%21 = tail call i32 (...) @fork()
switch i32 %21, label %22 [
i32 -1, label %2
i32 0, label %34
]
22: ; preds = %19
%23 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 7)
%24 = tail call i32 (...) @fork()
switch i32 %24, label %25 [
i32 -1, label %2
i32 0, label %34
]
25: ; preds = %22
%26 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 8)
%27 = tail call i32 (...) @fork()
switch i32 %27, label %28 [
i32 -1, label %2
i32 0, label %34
]
28: ; preds = %25
%29 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 9)
%30 = tail call i32 (...) @fork()
switch i32 %30, label %31 [
i32 -1, label %2
i32 0, label %34
]
31: ; preds = %28
%32 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 10)
br label %33
33: ; preds = %31, %33
br label %33
34: ; preds = %0, %4, %7, %10, %13, %16, %19, %22, %25, %28, %2
ret i32 0
}