#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ ECHILD;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ errno;
scalar_t__ pids_empty();
scalar_t__ pids_remove(int);
int waitpid(int, int *, int /*<<< orphan*/);
pid_t xwait(int block, int *status) {
  pid_t pid;
  if (pids_empty()) {
    errno = ECHILD;
    return (-1);
  }
  while ((pid = waitpid(-1, status, block ? 0 : WNOHANG)) > 0)
    if (pids_remove(pid))
      break;
  return (pid);
}


x86 assembly (clang, -O0):
<xwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <pids_empty@plt>
cmp    $0x0,%rax
je     116e <xwait+0x3e>
mov    0x2e79(%rip),%rax
mov    (%rax),%ecx
mov    0x2e78(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <xwait+0xaa>
jmp    1173 <xwait+0x43>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     118f <xwait+0x5f>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    119b <xwait+0x6b>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    $0xffffffff,%edi
call   1040 <waitpid@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    11d4 <xwait+0xa4>
mov    -0x14(%rbp),%edi
call   1050 <pids_remove@plt>
cmp    $0x0,%rax
je     11cf <xwait+0x9f>
jmp    11d4 <xwait+0xa4>
jmp    1173 <xwait+0x43>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xwait>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1030 <pids_empty@plt>
test   %rax,%rax
je     1161 <xwait+0x31>
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    0x2e80(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    1198 <xwait+0x68>
mov    0x2e80(%rip),%r15
nopl   0x0(%rax,%rax,1)
xor    %edx,%edx
test   %ebp,%ebp
jne    1179 <xwait+0x49>
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %r14,%rsi
call   1040 <waitpid@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1198 <xwait+0x68>
mov    %ebx,%edi
call   1050 <pids_remove@plt>
test   %rax,%rax
je     1170 <xwait+0x40>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ECHILD = global i32 0
@errno = global i32 0
@WNOHANG = global i32 0
define i32 @xwait(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%7 = call i64 (...) @pids_empty()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @ECHILD
store i32 %10, i32* @errno
store i32 -1, i32* %3
br label %31
11: ; preds = %2
br label %12
12: ; preds = %28, %11
%13 = load i32*, i32** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
br label %19
17: ; preds = %12
%18 = load i32, i32* @WNOHANG
br label %19
19: ; preds = %17, %16
%20 = phi i32 [ 0, %16 ], [ %18, %17 ]
%21 = call i32 @waitpid(i32 -1, i32* %13, i32 %20)
store i32 %21, i32* %6
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i32, i32* %6
%25 = call i64 @pids_remove(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
br label %29
28: ; preds = %23
br label %12
29: ; preds = %27, %19
%30 = load i32, i32* %6
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %9
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@ECHILD = global i32 0
@errno = global i32 0
@WNOHANG = global i32 0
define i32 @xwait(i32 %0, i32* %1) {
%3 = tail call i64 (...) @pids_empty()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = icmp eq i32 %0, 0
br label %9
7: ; preds = %2
%8 = load i32, i32* @ECHILD
store i32 %8, i32* @errno
br label %17
9: ; preds = %5, %14
%10 = load i32, i32* @WNOHANG
%11 = select i1 %6, i32 %10, i32 0
%12 = tail call i32 @waitpid(i32 -1, i32* %1, i32 %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = tail call i64 @pids_remove(i32 %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %9, label %17
17: ; preds = %9, %14, %7
%18 = phi i32 [ -1, %7 ], [ %12, %14 ], [ %12, %9 ]
ret i32 %18
}