#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 EXIT_FAILURE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int status;
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork() failed");
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    return 3;
  } else {
    printf("Child PID: %ld\n", (long)pid);
    pid = fork();
    if (pid < 0) {
      perror("fork() failed");
      exit(EXIT_FAILURE);
    } else if (pid == 0) {
      exit(7);
    } else {
      printf("Child PID: %ld\n", (long)pid);
      wait(&status);
      if (WIFEXITED(status))
        printf("Child send: %d\n", WEXITSTATUS(status));
      wait(&status);
      if (WIFEXITED(status))
        printf("Child send: %d\n", WEXITSTATUS(status));
      sleep(30);
    }
  }
  return 0;
}


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)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11c6 <main+0x46>
lea    0xe4d(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11dd <main+0x5d>
movl   $0x3,-0x4(%rbp)
jmp    12c8 <main+0x148>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xe24(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    121f <main+0x9f>
lea    0xdf4(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1234 <main+0xb4>
mov    $0x7,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xdcd(%rip),%rdi
call   1030 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1090 <wait@plt>
mov    -0x14(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1277 <main+0xf7>
mov    -0x14(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdac(%rip),%rdi
call   1030 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1090 <wait@plt>
mov    -0x14(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12a8 <main+0x128>
mov    -0x14(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd7b(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1e,%edi
call   1080 <sleep@plt>
jmp    12b7 <main+0x137>
jmp    12bc <main+0x13c>
jmp    12c1 <main+0x141>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     123c <main+0xbc>
je     1235 <main+0xb5>
lea    0xe70(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     123c <main+0xbc>
je     1256 <main+0xd6>
lea    0xe4c(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     11f8 <main+0x78>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xe2d(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     1227 <main+0xa7>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xdfe(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1e,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    $0x3,%eax
pop    %rcx
ret
lea    0xdbd(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    $0x7,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"fork() failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"Child PID: %ld\0A\00"
@.str.2 = [16 x i8] c"Child send: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %7
%9 = load i64, i64* %7
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i64, i64* %7
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 3, i32* %3
br label %62
19: ; preds = %15
%20 = load i64, i64* %7
%21 = trunc i64 %20 to i32
%22 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i64 (...) @fork()
store i64 %23, i64* %7
%24 = load i64, i64* %7
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %19
%31 = load i64, i64* %7
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @exit(i32 7)
unreachable
35: ; preds = %30
%36 = load i64, i64* %7
%37 = trunc i64 %36 to i32
%38 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = call i32 @wait(i32* %6)
%40 = load i32, i32* %6
%41 = call i64 @WIFEXITED(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = load i32, i32* %6
%45 = call i32 @WEXITSTATUS(i32 %44)
%46 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %43, %35
%48 = call i32 @wait(i32* %6)
%49 = load i32, i32* %6
%50 = call i64 @WIFEXITED(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %47
%53 = load i32, i32* %6
%54 = call i32 @WEXITSTATUS(i32 %53)
%55 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %52, %47
%57 = call i32 @sleep(i32 30)
br label %58
58: ; preds = %56
br label %59
59: ; preds = %58
br label %60
60: ; preds = %59
br label %61
61: ; preds = %60
store i32 0, i32* %3
br label %62
62: ; preds = %61, %18
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [14 x i8] c"fork() failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"Child PID: %ld\0A\00"
@.str.2 = [16 x i8] c"Child send: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 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 i64 %5, 0
br i1 %12, label %48, label %13
13: ; preds = %11
%14 = trunc i64 %5 to i32
%15 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = tail call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %13
%23 = icmp eq i64 %16, 0
br i1 %23, label %24, label %26
24: ; preds = %22
%25 = tail call i32 @exit(i32 7)
unreachable
26: ; preds = %22
%27 = trunc i64 %16 to i32
%28 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = call i32 @wait(i32* %3)
%30 = load i32, i32* %3
%31 = call i64 @WIFEXITED(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %26
%34 = load i32, i32* %3
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %26
%38 = call i32 @wait(i32* %3)
%39 = load i32, i32* %3
%40 = call i64 @WIFEXITED(i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %37
%43 = load i32, i32* %3
%44 = call i32 @WEXITSTATUS(i32 %43)
%45 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %42, %37
%47 = call i32 @sleep(i32 30)
br label %48
48: ; preds = %11, %46
%49 = phi i32 [ 0, %46 ], [ 3, %11 ]
ret i32 %49
}