#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
int waitchild(void) {
  int status;
  while (wait(&status) < 0)
    sleep(1);
  if (WIFEXITED(status))
    return WEXITSTATUS(status);
  if (WIFSIGNALED(status))
    return WTERMSIG(status);
  return 127;
}


x86 assembly (clang, -O0):
<waitchild>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
call   1080 <wait@plt>
cmp    $0x0,%rax
jge    118a <waitchild+0x2a>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1168 <waitchild+0x8>
mov    -0x8(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11ac <waitchild+0x4c>
mov    -0x8(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    11d5 <waitchild+0x75>
mov    -0x8(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11ce <waitchild+0x6e>
mov    -0x8(%rbp),%edi
call   1040 <WTERMSIG@plt>
mov    %eax,-0x4(%rbp)
jmp    11d5 <waitchild+0x75>
movl   $0x7f,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitchild>:
push   %rbx
sub    $0x10,%rsp
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
test   %rax,%rax
jns    1197 <waitchild+0x37>
lea    0xc(%rsp),%rbx
nopl   0x0(%rax)
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
call   1080 <wait@plt>
test   %rax,%rax
js     1180 <waitchild+0x20>
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     11b0 <waitchild+0x50>
call   1050 <WEXITSTATUS@plt>
jmp    11ca <waitchild+0x6a>
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
je     11c5 <waitchild+0x65>
mov    0xc(%rsp),%edi
call   1040 <WTERMSIG@plt>
jmp    11ca <waitchild+0x6a>
mov    $0x7f,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @waitchild() {
%1 = alloca i32
%2 = alloca i32
br label %3
3: ; preds = %6, %0
%4 = call i64 @wait(i32* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = call i32 @sleep(i32 1)
br label %3
8: ; preds = %3
%9 = load i32, i32* %2
%10 = call i64 @WIFEXITED(i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* %2
%14 = call i32 @WEXITSTATUS(i32 %13)
store i32 %14, i32* %1
br label %23
15: ; preds = %8
%16 = load i32, i32* %2
%17 = call i64 @WIFSIGNALED(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %2
%21 = call i32 @WTERMSIG(i32 %20)
store i32 %21, i32* %1
br label %23
22: ; preds = %15
store i32 127, i32* %1
br label %23
23: ; preds = %22, %19, %12
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
define i32 @waitchild() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i64 @wait(i32* %1)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0, %5
%6 = call i32 @sleep(i32 1)
%7 = call i64 @wait(i32* %1)
%8 = icmp slt i64 %7, 0
br i1 %8, label %5, label %9
9: ; preds = %5, %0
%10 = load i32, i32* %1
%11 = call i64 @WIFEXITED(i32 %10)
%12 = icmp eq i64 %11, 0
%13 = load i32, i32* %1
br i1 %12, label %16, label %14
14: ; preds = %9
%15 = call i32 @WEXITSTATUS(i32 %13)
br label %22
16: ; preds = %9
%17 = call i64 @WIFSIGNALED(i32 %13)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32, i32* %1
%21 = call i32 @WTERMSIG(i32 %20)
br label %22
22: ; preds = %16, %19, %14
%23 = phi i32 [ %15, %14 ], [ %21, %19 ], [ 127, %16 ]
ret i32 %23
}