#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 /*<<< orphan*/ child();
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  pid_t pid;
  int counter;
  long i = 0;
  counter = 0;
  while (1) {
    if ((pid = fork()) == -1) {
      perror("fork() failed");
      while (1) {
        i = i + 1;
        sleep(1);
      }
    } else if (pid == 0) {
      child();
    }
    printf("create %d child process, pid = %d\n", ++counter, pid);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11b3 <main+0x63>
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    1194 <main+0x44>
cmpl   $0x0,-0x14(%rbp)
jne    11c4 <main+0x74>
mov    $0x0,%al
call   1030 <child@plt>
jmp    11c9 <main+0x79>
mov    -0x18(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
mov    -0x14(%rbp),%edx
lea    0xe32(%rip),%rdi
call   1040 <printf@plt>
jmp    1175 <main+0x25>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x1,%ebx
lea    0xeae(%rip),%r14
jmp    1186 <main+0x36>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <child@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1040 <printf@plt>
add    $0x1,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1170 <main+0x20>
cmp    $0xffffffff,%ebp
jne    1177 <main+0x27>
lea    0xe61(%rip),%rdi
call   1050 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11b0 <main+0x60>

LLVM IR (-O0):
@.str = [14 x i8] c"fork() failed\00"
@.str.1 = [35 x i8] c"create %d child process, pid = %d\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 i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %8
store i32 0, i32* %7
br label %9
9: ; preds = %2, %24
%10 = call i32 (...) @fork()
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %14
%15 = load i64, i64* %8
%16 = add nsw i64 %15, 1
store i64 %16, i64* %8
%17 = call i32 @sleep(i32 1)
br label %14
18: ; preds = %9
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 (...) @child()
br label %23
23: ; preds = %21, %18
br label %24
24: ; preds = %23
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
%27 = load i32, i32* %6
%28 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
br label %9
}

LLVM IR (-O2):
@.str = [14 x i8] c"fork() failed\00"
@.str.1 = [35 x i8] c"create %d child process, pid = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
br label %3
3: ; preds = %12, %2
%4 = phi i32 [ 0, %2 ], [ %13, %12 ]
%5 = tail call i32 (...) @fork()
switch i32 %5, label %12 [
i32 -1, label %6
i32 0, label %10
]
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %8, %6
%9 = tail call i32 @sleep(i32 1)
br label %8
10: ; preds = %3
%11 = tail call i32 (...) @child()
br label %12
12: ; preds = %3, %10
%13 = add nuw nsw i32 %4, 1
%14 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %5)
br label %3
}