#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*/ 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 wpid;
  fprintf(stderr, "wait on init\n");
  while (!(wpid = wait4(1, 0, WNOHANG, 0)))
    sleep(2);
  if (wpid == 1)
    fprintf(stderr, "parent: waited on init\n");
  else
    perror("wait4 failed");
}


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    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0xe9a(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%edi
xor    %ecx,%ecx
mov    %ecx,%esi
call   1040 <wait4@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    119a <main+0x5a>
jmp    11a9 <main+0x69>
mov    $0x2,%edi
call   1060 <sleep@plt>
jmp    116b <main+0x2b>
cmpl   $0x1,-0x14(%rbp)
jne    11cd <main+0x8d>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11d9 <main+0x99>
lea    0xe52(%rip),%rdi
call   1050 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%r14
mov    (%r14),%edi
lea    0xeab(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e87(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%edx
mov    $0x1,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1040 <wait4@plt>
test   %eax,%eax
jne    1190 <main+0x50>
mov    $0x2,%edi
call   1060 <sleep@plt>
jmp    1170 <main+0x30>
cmp    $0x1,%eax
jne    11ae <main+0x6e>
mov    (%r14),%edi
lea    0xe6f(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
jmp    11a4 <main+0x64>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [14 x i8] c"wait on init\0A\00"
@WNOHANG = global i32 0
@.str.1 = [24 x i8] c"parent: waited on init\0A\00"
@.str.2 = [13 x i8] c"wait4 failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %14, %2
%10 = load i32, i32* @WNOHANG
%11 = call i32 @wait4(i32 1, i32 0, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = icmp ne i32 %11, 0
%13 = xor i1 %12, true
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @sleep(i32 2)
br label %9
16: ; preds = %9
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"wait on init\0A\00"
@WNOHANG = global i32 0
@.str.1 = [24 x i8] c"parent: waited on init\0A\00"
@.str.2 = [13 x i8] c"wait4 failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @stderr
%4 = tail call i32 @fprintf(i32 %3, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %8, %2
%6 = load i32, i32* @WNOHANG
%7 = tail call i32 @wait4(i32 1, i32 0, i32 %6, i32 0)
switch i32 %7, label %13 [
i32 0, label %8
i32 1, label %10
]
8: ; preds = %5
%9 = tail call i32 @sleep(i32 2)
br label %5
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %15
13: ; preds = %5
%14 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
ret i32 0
}