#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_FAILURE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
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) {
    sleep(15);
    return 3;
  } else {
    while (!waitpid(-1, &status, WNOHANG)) {
      sleep(3);
      printf("sleep 3sec.\n");
    }
    if (WIFEXITED(status))
      printf("Child send: %d\n", WEXITSTATUS(status));
  }
  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   1060 <perror@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11e7 <main+0x67>
mov    $0xf,%edi
call   1090 <sleep@plt>
movl   $0x3,-0x4(%rbp)
jmp    1270 <main+0xf0>
jmp    11ec <main+0x6c>
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x14(%rbp),%rsi
call   1050 <waitpid@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1218 <main+0x98>
jmp    1235 <main+0xb5>
mov    $0x3,%edi
call   1090 <sleep@plt>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ec <main+0x6c>
mov    -0x14(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     125f <main+0xdf>
mov    -0x14(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1264 <main+0xe4>
jmp    1269 <main+0xe9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1250 <main+0xc0>
je     1231 <main+0xa1>
mov    0x2e32(%rip),%r15
mov    (%r15),%edx
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
call   1060 <waitpid@plt>
test   %eax,%eax
jne    1206 <main+0x76>
lea    0xe4b(%rip),%r14
lea    0xc(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1060 <waitpid@plt>
test   %eax,%eax
je     11e0 <main+0x50>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     1242 <main+0xb2>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xdea(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1244 <main+0xb4>
mov    $0xf,%edi
call   10a0 <sleep@plt>
mov    $0x3,%ebx
jmp    1244 <main+0xb4>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xda9(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"fork() failed\00"
@EXIT_FAILURE = global i32 0
@WNOHANG = global i32 0
@.str.1 = [13 x i8] c"sleep 3sec.\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 %20
18: ; preds = %15
%19 = call i32 @sleep(i32 15)
store i32 3, i32* %3
br label %40
20: ; preds = %15
br label %21
21: ; preds = %26, %20
%22 = load i32, i32* @WNOHANG
%23 = call i32 @waitpid(i32 -1, i32* %6, i32 %22)
%24 = icmp ne i32 %23, 0
%25 = xor i1 %24, true
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @sleep(i32 3)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %21
29: ; preds = %21
%30 = load i32, i32* %6
%31 = call i64 @WIFEXITED(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* %6
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %29
br label %38
38: ; preds = %37
br label %39
39: ; preds = %38
store i32 0, i32* %3
br label %40
40: ; preds = %39, %18
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [14 x i8] c"fork() failed\00"
@EXIT_FAILURE = global i32 0
@WNOHANG = global i32 0
@.str.2 = [16 x i8] c"Child send: %d\0A\00"
@str = [12 x i8] c"sleep 3sec.\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 %17, label %13
13: ; preds = %11
%14 = load i32, i32* @WNOHANG
%15 = call i32 @waitpid(i32 -1, i32* %3, i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %25
17: ; preds = %11
%18 = tail call i32 @sleep(i32 15)
br label %33
19: ; preds = %13, %19
%20 = call i32 @sleep(i32 3)
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%22 = load i32, i32* @WNOHANG
%23 = call i32 @waitpid(i32 -1, i32* %3, i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %19, label %25
25: ; preds = %19, %13
%26 = load i32, i32* %3
%27 = call i64 @WIFEXITED(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i32, i32* %3
%31 = call i32 @WEXITSTATUS(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %25, %29, %17
%34 = phi i32 [ 3, %17 ], [ 0, %29 ], [ 0, %25 ]
ret i32 %34
}