#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_SUCCESS;
int /*<<< orphan*/ childs_play();
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    err_sys("fork error");
  } else if (pid == 0) {
    childs_play();
    exit(EXIT_SUCCESS);
  }
  printf("Created child with pid %d\n", pid);
  sleep(4);
  printf("Shoo away!\n");
  if (waitpid(pid, NULL, 0) != pid) {
    err_sys("waitpid error");
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11a5 <main+0x35>
lea    0xe65(%rip),%rdi
call   1030 <err_sys@plt>
jmp    11ca <main+0x5a>
cmpq   $0x0,-0x10(%rbp)
jne    11c5 <main+0x55>
mov    $0x0,%al
call   1040 <childs_play@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11ca <main+0x5a>
mov    -0x10(%rbp),%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x4,%edi
call   1080 <sleep@plt>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    -0x10(%rbp),%rax
je     1219 <main+0xa9>
lea    0xe1e(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    119e <main+0x1e>
lea    0xe69(%rip),%rdi
call   1040 <err_sys@plt>
jmp    11a9 <main+0x29>
jne    11a9 <main+0x29>
xor    %eax,%eax
call   1050 <childs_play@plt>
jmp    11ed <main+0x6d>
lea    0xe5b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x4,%edi
call   1090 <sleep@plt>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    %rbx,%rax
je     11ed <main+0x6d>
lea    0xe3e(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [27 x i8] c"Created child with pid %d\0A\00"
@.str.2 = [12 x i8] c"Shoo away!\0A\00"
@.str.3 = [14 x i8] c"waitpid error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %15
7: ; preds = %0
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = call i32 (...) @childs_play()
%12 = load i32, i32* @EXIT_SUCCESS
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %7
br label %15
15: ; preds = %14, %5
%16 = load i64, i64* %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %16)
%18 = call i32 @sleep(i32 4)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%20 = load i64, i64* %2
%21 = call i64 @waitpid(i64 %20, i32* null, i32 0)
%22 = load i64, i64* %2
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %15
%27 = load i32, i32* @EXIT_SUCCESS
%28 = call i32 @exit(i32 %27)
unreachable
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [27 x i8] c"Created child with pid %d\0A\00"
@.str.3 = [14 x i8] c"waitpid error\00"
@str = [11 x i8] c"Shoo away!\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %11
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %7, label %11
7: ; preds = %5
%8 = tail call i32 (...) @childs_play()
%9 = load i32, i32* @EXIT_SUCCESS
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %5, %3
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %1)
%13 = tail call i32 @sleep(i32 4)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%15 = tail call i64 @waitpid(i64 %1, i32* null, i32 0)
%16 = icmp eq i64 %15, %1
br i1 %16, label %19, label %17
17: ; preds = %11
%18 = tail call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = load i32, i32* @EXIT_SUCCESS
%21 = tail call i32 @exit(i32 %20)
unreachable
}