#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 */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ err(int, char *);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int main() {
  printf(" -> parent\n");
  pid_t child = fork();
  if (child < 0)
    err(1, "fork");
  if (!child) {
    printf(" -> child 1\n");
    if (!fork()) {
      printf(" -> child 2\n");
      sleep(1);
      return 10;
    } else {
      while (true) {
        int status;
        int x = waitpid(0, &status, 0);
        if (WIFEXITED(status))
          return WEXITSTATUS(status);
      }
    }
  }
  while (true) {
    int status;
    int x = waitpid(0, &status, 0);
    if (x < 0)
      err(1, "waitpid");
    if (WIFEXITED(status)) {
      printf(" -> exiting\n");
      return WEXITSTATUS(status);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11b2 <main+0x42>
mov    $0x1,%edi
lea    0xe5f(%rip),%rsi
call   1040 <err@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1238 <main+0xc8>
lea    0xe4d(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    11fc <main+0x8c>
lea    0xe3d(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
movl   $0xa,-0x4(%rbp)
jmp    129b <main+0x12b>
jmp    1201 <main+0x91>
xor    %edx,%edx
lea    -0x14(%rbp),%rsi
mov    %edx,%edi
call   1060 <waitpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1233 <main+0xc3>
mov    -0x14(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    129b <main+0x12b>
jmp    1201 <main+0x91>
jmp    123d <main+0xcd>
xor    %edx,%edx
lea    -0x1c(%rbp),%rsi
mov    %edx,%edi
call   1060 <waitpid@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1268 <main+0xf8>
mov    $0x1,%edi
lea    0xdc8(%rip),%rsi
call   1040 <err@plt>
mov    -0x1c(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1296 <main+0x126>
lea    0xdb2(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    129b <main+0x12b>
jmp    123d <main+0xcd>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11f0 <main+0x80>
jne    121e <main+0xae>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     125e <main+0xee>
lea    0x8(%rsp),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    0x8(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
jne    11ea <main+0x7a>
lea    0x8(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    0x8(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     11d0 <main+0x60>
mov    0x8(%rsp),%edi
jmp    1253 <main+0xe3>
lea    0xe09(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
jmp    121e <main+0xae>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
jne    1243 <main+0xd3>
lea    0xc(%rsp),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %eax,%eax
jns    1210 <main+0xa0>
lea    0xdce(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
jmp    1210 <main+0xa0>
lea    0xde6(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdbf(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0xa,%eax
jmp    1258 <main+0xe8>

LLVM IR (-O0):
@.str = [12 x i8] c" -> parent\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [13 x i8] c" -> child 1\0A\00"
@.str.3 = [13 x i8] c" -> child 2\0A\00"
@.str.4 = [8 x i8] c"waitpid\00"
@.str.5 = [13 x i8] c" -> exiting\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = call i64 (...) @fork()
store i64 %8, i64* %2
%9 = load i64, i64* %2
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = load i64, i64* %2
%15 = icmp ne i64 %14, 0
br i1 %15, label %33, label %16
16: ; preds = %13
%17 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%18 = call i64 (...) @fork()
%19 = icmp ne i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @sleep(i32 1)
store i32 10, i32* %1
br label %49
23: ; preds = %16
br label %24
24: ; preds = %23, %32
%25 = call i32 @waitpid(i32 0, i32* %3, i32 0)
store i32 %25, i32* %4
%26 = load i32, i32* %3
%27 = call i64 @WIFEXITED(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* %3
%31 = call i32 @WEXITSTATUS(i32 %30)
store i32 %31, i32* %1
br label %49
32: ; preds = %24
br label %24
33: ; preds = %13
br label %34
34: ; preds = %33, %48
%35 = call i32 @waitpid(i32 0, i32* %5, i32 0)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = load i32, i32* %5
%42 = call i64 @WIFEXITED(i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%46 = load i32, i32* %5
%47 = call i32 @WEXITSTATUS(i32 %46)
store i32 %47, i32* %1
br label %49
48: ; preds = %40
br label %34
49: ; preds = %44, %29, %20
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"fork\00"
@.str.4 = [8 x i8] c"waitpid\00"
@str = [11 x i8] c" -> parent\00"
@str.6 = [12 x i8] c" -> child 1\00"
@str.7 = [12 x i8] c" -> child 2\00"
@str.8 = [12 x i8] c" -> exiting\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %10
8: ; preds = %0
%9 = icmp eq i64 %4, 0
br i1 %9, label %12, label %10
10: ; preds = %6, %8
%11 = bitcast i32* %2 to i8*
br label %33
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
%14 = tail call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = bitcast i32* %1 to i8*
%18 = call i32 @waitpid(i32 0, i32* %1, i32 0)
%19 = load i32, i32* %1
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %28, label %25
22: ; preds = %12
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
%24 = tail call i32 @sleep(i32 1)
br label %47
25: ; preds = %28, %16
%26 = load i32, i32* %1
%27 = call i32 @WEXITSTATUS(i32 %26)
br label %47
28: ; preds = %16, %28
%29 = call i32 @waitpid(i32 0, i32* %1, i32 0)
%30 = load i32, i32* %1
%31 = call i64 @WIFEXITED(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %28, label %25
33: ; preds = %10, %46
%34 = call i32 @waitpid(i32 0, i32* %2, i32 0)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = load i32, i32* %2
%40 = call i64 @WIFEXITED(i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0))
%44 = load i32, i32* %2
%45 = call i32 @WEXITSTATUS(i32 %44)
br label %47
46: ; preds = %38
br label %33
47: ; preds = %42, %25, %22
%48 = phi i32 [ 10, %22 ], [ %27, %25 ], [ %45, %42 ]
ret i32 %48
}