#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 */
scalar_t__ ECHILD;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main(int argc, char **argv) {
  int i;
  int status;
  pid_t pid;
  for (i = 0; i < 5; i++) {
    pid = fork();
    if (pid < 0) {
      perror("fork() ");
      _exit(EXIT_FAILURE);
    } else if (pid == 0) {
      printf("This is child with pid %u and parent pid %u\n", getpid(),
             getppid());
      sleep(i);
      _exit(EXIT_SUCCESS);
    } else {
      continue;
    }
  }
  for (;;) {
    pid = wait(&status);
    if (pid == -1) {
      if (errno == ECHILD) {
        printf("No More children\n");
        exit(EXIT_SUCCESS);
      } else {
        perror("wait() ");
        exit(EXIT_FAILURE);
      }
    } else {
      printf("Wait child with pid %u and status %d\n", pid, status);
    }
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    123c <main+0xac>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e5 <main+0x55>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1229 <main+0x99>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <sleep@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    122e <main+0x9e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ad <main+0x1d>
jmp    1241 <main+0xb1>
lea    -0x18(%rbp),%rdi
call   10a0 <wait@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12a7 <main+0x117>
mov    0x2d82(%rip),%rax
mov    (%rax),%rax
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
jne    128d <main+0xfd>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xdb3(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1241 <main+0xb1>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     11f8 <main+0x58>
je     1219 <main+0x79>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     11f8 <main+0x58>
mov    $0x1,%ebx
je     1219 <main+0x79>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     11f8 <main+0x58>
mov    $0x2,%ebx
je     1219 <main+0x79>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     11f8 <main+0x58>
mov    $0x3,%ebx
je     1219 <main+0x79>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jns    1212 <main+0x72>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    $0x4,%ebx
jne    1250 <main+0xb0>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xdd8(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   10a0 <sleep@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
je     128d <main+0xed>
lea    0xdd7(%rip),%r14
lea    0xc(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    0xc(%rsp),%edx
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
jne    1270 <main+0xd0>
mov    0x2d4c(%rip),%rax
mov    (%rax),%rax
mov    0x2d32(%rip),%rcx
cmp    (%rcx),%rax
jne    12bd <main+0x11d>
lea    0xdb9(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xd71(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"fork() \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [45 x i8] c"This is child with pid %u and parent pid %u\0A\00"
@EXIT_SUCCESS = global i32 0
@errno = global i64 0
@ECHILD = global i64 0
@.str.2 = [18 x i8] c"No More children\0A\00"
@.str.3 = [8 x i8] c"wait() \00"
@.str.4 = [38 x i8] c"Wait child with pid %u and status %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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
br label %9
9: ; preds = %32, %2
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 5
br i1 %11, label %12, label %35
12: ; preds = %9
%13 = call i32 (...) @fork()
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @_exit(i32 %18)
unreachable
20: ; preds = %12
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = call i32 (...) @getpid()
%25 = call i32 (...) @getppid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32, i32* %6
%28 = call i32 @sleep(i32 %27)
%29 = load i32, i32* @EXIT_SUCCESS
%30 = call i32 @_exit(i32 %29)
unreachable
31: ; preds = %20
br label %32
32: ; preds = %31
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %9
35: ; preds = %9
br label %36
36: ; preds = %56, %35
%37 = call i32 @wait(i32* %7)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %52
40: ; preds = %36
%41 = load i64, i64* @errno
%42 = load i64, i64* @ECHILD
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %36
%53 = load i32, i32* %8
%54 = load i32, i32* %7
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %53, i32 %54)
br label %56
56: ; preds = %52
br label %36
}

LLVM IR (-O2):
@.str = [8 x i8] c"fork() \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [45 x i8] c"This is child with pid %u and parent pid %u\0A\00"
@EXIT_SUCCESS = global i32 0
@errno = global i64 0
@ECHILD = global i64 0
@.str.3 = [8 x i8] c"wait() \00"
@.str.4 = [38 x i8] c"Wait child with pid %u and status %d\0A\00"
@str = [17 x i8] c"No More children\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %36, %31, %26, %21, %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @_exit(i32 %9)
unreachable
11: ; preds = %2
%12 = icmp eq i32 %5, 0
br i1 %12, label %13, label %21
13: ; preds = %39, %34, %29, %24, %11
%14 = phi i32 [ 0, %11 ], [ 1, %24 ], [ 2, %29 ], [ 3, %34 ], [ 4, %39 ]
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (...) @getppid()
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16)
%18 = tail call i32 @sleep(i32 %14)
%19 = load i32, i32* @EXIT_SUCCESS
%20 = tail call i32 @_exit(i32 %19)
unreachable
21: ; preds = %11
%22 = tail call i32 (...) @fork()
%23 = icmp slt i32 %22, 0
br i1 %23, label %7, label %24
24: ; preds = %21
%25 = icmp eq i32 %22, 0
br i1 %25, label %13, label %26
26: ; preds = %24
%27 = tail call i32 (...) @fork()
%28 = icmp slt i32 %27, 0
br i1 %28, label %7, label %29
29: ; preds = %26
%30 = icmp eq i32 %27, 0
br i1 %30, label %13, label %31
31: ; preds = %29
%32 = tail call i32 (...) @fork()
%33 = icmp slt i32 %32, 0
br i1 %33, label %7, label %34
34: ; preds = %31
%35 = icmp eq i32 %32, 0
br i1 %35, label %13, label %36
36: ; preds = %34
%37 = tail call i32 (...) @fork()
%38 = icmp slt i32 %37, 0
br i1 %38, label %7, label %39
39: ; preds = %36
%40 = icmp eq i32 %37, 0
br i1 %40, label %13, label %41
41: ; preds = %39
%42 = call i32 @wait(i32* %3)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %56
44: ; preds = %56, %41
%45 = load i64, i64* @errno
%46 = load i64, i64* @ECHILD
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%50 = load i32, i32* @EXIT_SUCCESS
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %44
%53 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %41, %56
%57 = phi i32 [ %60, %56 ], [ %42, %41 ]
%58 = load i32, i32* %3
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %57, i32 %58)
%60 = call i32 @wait(i32* %3)
%61 = icmp eq i32 %60, -1
br i1 %61, label %44, label %56
}