#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ explain_wait_status(scalar_t__, int);
scalar_t__ fork();
int /*<<< orphan*/ fork_procs();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ show_pstree(scalar_t__);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
int main(void) {
  pid_t pid;
  int status;
  pid = fork();
  if (pid < 0) {
    perror("main: fork");
    exit(1);
  }
  if (pid == 0) {
    fork_procs();
    exit(1);
  }
  sleep(3);
  show_pstree(pid);
  pid = wait(&status);
  explain_wait_status(pid, status);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11bb <main+0x3b>
lea    0xe54(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11d7 <main+0x57>
mov    $0x0,%al
call   1060 <fork_procs@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    $0x3,%edi
call   1070 <sleep@plt>
mov    -0x10(%rbp),%rdi
call   1030 <show_pstree@plt>
lea    -0x14(%rbp),%rdi
call   1080 <wait@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10a0 <explain_wait_status@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11c6 <main+0x46>
je     11dc <main+0x5c>
mov    %rax,%rbx
mov    $0x3,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
call   1030 <show_pstree@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   10a0 <explain_wait_status@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe33(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %eax,%eax
call   1060 <fork_procs@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@.str = [11 x i8] c"main: fork\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %3, 0
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = tail call i32 (...) @fork_procs()
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %8
%14 = tail call i32 @sleep(i32 3)
%15 = tail call i32 @show_pstree(i64 %3)
%16 = call i64 @wait(i32* %1)
%17 = load i32, i32* %1
%18 = call i32 @explain_wait_status(i64 %16, i32 %17)
ret i32 0
}