#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*/ WNOHANG;
int /*<<< orphan*/ exit(int);
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;
  pid = fork();
  if (0 > pid) {
    printf("fail to fork\n");
    exit(1);
  } else if (0 == pid) {
    printf("the child\n");
    sleep(5);
    exit(0);
  } else {
    printf("the parent\n");
    if (waitpid(pid, NULL, WNOHANG) == 0)
      printf("the child is not available now\n");
  }
  printf("no waiting, parent done\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jle    118c <main+0x3c>
lea    0xe83(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11b5 <main+0x65>
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
mov    $0x5,%edi
call   1060 <sleep@plt>
xor    %edi,%edi
call   1050 <exit@plt>
lea    0xe5d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <waitpid@plt>
cmp    $0x0,%rax
jne    11ed <main+0x9d>
lea    0xe3d(%rip),%rdi
call   1030 <printf@plt>
jmp    11f2 <main+0xa2>
jmp    11f7 <main+0xa7>
lea    0xe47(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11a2 <main+0x52>
je     11b8 <main+0x68>
mov    %rax,%rbx
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <waitpid@plt>
test   %rax,%rax
jne    1192 <main+0x42>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   1060 <sleep@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"fail to fork\0A\00"
@.str.1 = [11 x i8] c"the child\0A\00"
@.str.2 = [12 x i8] c"the parent\0A\00"
@WNOHANG = global i32 0
@.str.3 = [32 x i8] c"the child is not available now\0A\00"
@.str.4 = [25 x i8] c"no waiting, parent done\0A\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 = load i64, i64* %2
%5 = icmp sgt i64 0, %4
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 0, %10
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @sleep(i32 5)
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %9
%17 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%18 = load i64, i64* %2
%19 = load i32, i32* @WNOHANG
%20 = call i64 @waitpid(i64 %18, i32* null, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
br label %25
25: ; preds = %24
br label %26
26: ; preds = %25
%27 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@str = [11 x i8] c"the parent\00"
@str.5 = [24 x i8] c"no waiting, parent done\00"
@str.6 = [31 x i8] c"the child is not available now\00"
@str.7 = [10 x i8] c"the child\00"
@str.8 = [13 x i8] c"fail to fork\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %12
8: ; preds = %6
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.7, i64 0, i64 0))
%10 = tail call i32 @sleep(i32 5)
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %6
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%14 = load i32, i32* @WNOHANG
%15 = tail call i64 @waitpid(i64 %1, i32* null, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.6, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.5, i64 0, i64 0))
ret i32 0
}