#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WNOHANG;
int WTERMSIG(int);
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
pid_t pjf_waitany(int *code) {
  pid_t pid;
  int status;
  pid = waitpid(0, &status, WNOHANG);
  if (pid < 1)
    return 0;
  if (code)
    *code = (WIFEXITED(status))     ? WEXITSTATUS(status)
            : (WIFSIGNALED(status)) ? 200 + WTERMSIG(status)
                                    : -2;
  return pid;
}


x86 assembly (clang, -O0):
<pjf_waitany>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1060 <waitpid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    1189 <pjf_waitany+0x39>
movl   $0x0,-0x4(%rbp)
jmp    11ff <pjf_waitany+0xaf>
cmpq   $0x0,-0x10(%rbp)
je     11f9 <pjf_waitany+0xa9>
mov    -0x18(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11b6 <pjf_waitany+0x66>
mov    -0x18(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x1c(%rbp)
jmp    11f0 <pjf_waitany+0xa0>
mov    -0x18(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11dd <pjf_waitany+0x8d>
mov    -0x18(%rbp),%edi
call   1040 <WTERMSIG@plt>
add    $0xc8,%eax
mov    %eax,-0x20(%rbp)
jmp    11ea <pjf_waitany+0x9a>
mov    $0xfffffffe,%eax
mov    %eax,-0x20(%rbp)
jmp    11ea <pjf_waitany+0x9a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pjf_waitany>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e8a(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
lea    0x4(%rsp),%rsi
xor    %edi,%edi
call   1060 <waitpid@plt>
test   %eax,%eax
jle    11b4 <pjf_waitany+0x64>
mov    %eax,%ebx
test   %r14,%r14
je     11b4 <pjf_waitany+0x64>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     1192 <pjf_waitany+0x42>
call   1050 <WEXITSTATUS@plt>
jmp    11b1 <pjf_waitany+0x61>
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
je     11ac <pjf_waitany+0x5c>
mov    0x4(%rsp),%edi
call   1040 <WTERMSIG@plt>
add    $0xc8,%eax
jmp    11b1 <pjf_waitany+0x61>
mov    $0xfffffffe,%eax
mov    %eax,(%r14)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
define i32 @pjf_waitany(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
%6 = load i32, i32* @WNOHANG
%7 = call i32 @waitpid(i32 0, i32* %5, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %37
11: ; preds = %1
%12 = load i32*, i32** %3
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %35
14: ; preds = %11
%15 = load i32, i32* %5
%16 = call i64 @WIFEXITED(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %5
%20 = call i32 @WEXITSTATUS(i32 %19)
br label %32
21: ; preds = %14
%22 = load i32, i32* %5
%23 = call i64 @WIFSIGNALED(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* %5
%27 = call i32 @WTERMSIG(i32 %26)
%28 = add nsw i32 200, %27
br label %30
29: ; preds = %21
br label %30
30: ; preds = %29, %25
%31 = phi i32 [ %28, %25 ], [ -2, %29 ]
br label %32
32: ; preds = %30, %18
%33 = phi i32 [ %20, %18 ], [ %31, %30 ]
%34 = load i32*, i32** %3
store i32 %33, i32* %34
br label %35
35: ; preds = %32, %11
%36 = load i32, i32* %4
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %10
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@WNOHANG = global i32 0
define i32 @pjf_waitany(i32* writeonly %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @WNOHANG
%5 = call i32 @waitpid(i32 0, i32* %2, i32 %4)
%6 = icmp slt i32 %5, 1
br i1 %6, label %25, label %7
7: ; preds = %1
%8 = icmp eq i32* %0, null
br i1 %8, label %25, label %9
9: ; preds = %7
%10 = load i32, i32* %2
%11 = call i64 @WIFEXITED(i32 %10)
%12 = icmp eq i64 %11, 0
%13 = load i32, i32* %2
br i1 %12, label %16, label %14
14: ; preds = %9
%15 = call i32 @WEXITSTATUS(i32 %13)
br label %23
16: ; preds = %9
%17 = call i64 @WIFSIGNALED(i32 %13)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = load i32, i32* %2
%21 = call i32 @WTERMSIG(i32 %20)
%22 = add nsw i32 %21, 200
br label %23
23: ; preds = %19, %16, %14
%24 = phi i32 [ %15, %14 ], [ %22, %19 ], [ -2, %16 ]
store i32 %24, i32* %0
br label %25
25: ; preds = %7, %23, %1
%26 = phi i32 [ 0, %1 ], [ %5, %23 ], [ %5, %7 ]
ret i32 %26
}