#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 */

/* Variables and functions */
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int wait4(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int pid, wpid;
  if (pid = fork()) {
    fprintf(stderr, "parent: I see kid with pid = %d\n", pid);
    while (!(wpid = wait4(pid, 0, WNOHANG, 0)))
      sleep(2);
    if (wpid == pid) {
      fprintf(stderr, "parent: waited on kid\n");
      exit(0);
    } else {
      perror("wait4 failed");
      exit(1);
    }
  } else {
    fprintf(stderr, "child: I'm here\n");
    sleep(5);
    fprintf(stderr, "child: exiting\n");
    exit(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   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     121f <main+0xbf>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x14(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1040 <wait4@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d0 <main+0x70>
jmp    11df <main+0x7f>
mov    $0x2,%edi
call   1070 <sleep@plt>
jmp    11a3 <main+0x43>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1209 <main+0xa9>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe28(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
mov    0x2e65(%rip),%r14
mov    (%r14),%edi
test   %eax,%eax
jne    119e <main+0x3e>
lea    0xec4(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
mov    (%r14),%edi
lea    0xeba(%rip),%rsi
jmp    11ec <main+0x8c>
mov    %eax,%ebx
lea    0xe59(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2e31(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    %ebx,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1040 <wait4@plt>
test   %eax,%eax
jne    11de <main+0x7e>
mov    $0x2,%edi
call   1070 <sleep@plt>
jmp    11c0 <main+0x60>
cmp    %ebx,%eax
jne    11fa <main+0x9a>
mov    (%r14),%edi
lea    0xe35(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"parent: I see kid with pid = %d\0A\00"
@WNOHANG = global i32 0
@.str.1 = [23 x i8] c"parent: waited on kid\0A\00"
@.str.2 = [13 x i8] c"wait4 failed\00"
@.str.3 = [17 x i8] c"child: I'm here\0A\00"
@.str.4 = [16 x i8] c"child: exiting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %6
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %33
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* %6
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %20, %10
%15 = load i32, i32* %6
%16 = load i32, i32* @WNOHANG
%17 = call i32 @wait4(i32 %15, i32 0, i32 %16, i32 0)
store i32 %17, i32* %7
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @sleep(i32 2)
br label %14
22: ; preds = %14
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %2
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @sleep(i32 5)
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"parent: I see kid with pid = %d\0A\00"
@WNOHANG = global i32 0
@.str.1 = [23 x i8] c"parent: waited on kid\0A\00"
@.str.2 = [13 x i8] c"wait4 failed\00"
@.str.3 = [17 x i8] c"child: I'm here\0A\00"
@.str.4 = [16 x i8] c"child: exiting\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
%5 = load i32, i32* @stderr
br i1 %4, label %26, label %6
6: ; preds = %2
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %3)
%8 = load i32, i32* @WNOHANG
%9 = tail call i32 @wait4(i32 %3, i32 0, i32 %8, i32 0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %6, %11
%12 = tail call i32 @sleep(i32 2)
%13 = load i32, i32* @WNOHANG
%14 = tail call i32 @wait4(i32 %3, i32 0, i32 %13, i32 0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %11, label %16
16: ; preds = %11, %6
%17 = phi i32 [ %9, %6 ], [ %14, %11 ]
%18 = icmp eq i32 %17, %3
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %16
%24 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%28 = tail call i32 @sleep(i32 5)
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @exit(i32 0)
unreachable
}