#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int /*<<< orphan*/ si_status;
  int /*<<< orphan*/ si_signo;
};
typedef TYPE_1__ siginfo_t;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ P_PID;
int WCONTINUED;
int WEXITED;
int WEXITSTATUS(int /*<<< orphan*/);
scalar_t__ WIFCONTINUED(int /*<<< orphan*/);
scalar_t__ WIFEXITED(int /*<<< orphan*/);
scalar_t__ WIFSTOPPED(int /*<<< orphan*/);
int WSTOPPED;
int WSTOPSIG(int /*<<< orphan*/);
int __WALL;
int __WNOTHREAD;
scalar_t__ errno;
int waitid(int /*<<< orphan*/, int /*<<< orphan*/, TYPE_1__ *, int);
int clone_reap(pid_t pid, int options) {
  int ret;
  siginfo_t info = {
      .si_signo = 0,
  };
again:
  ret = waitid(P_PID, pid, &info, options | __WALL | __WNOTHREAD);
  if (ret < 0) {
    if (errno == EINTR)
      goto again;
    return -1;
  }
  if (options & WEXITED) {
    if (WIFEXITED(info.si_status))
      return WEXITSTATUS(info.si_status);
  }
  if (options & WSTOPPED) {
    if (WIFSTOPPED(info.si_status))
      return WSTOPSIG(info.si_status);
  }
  if (options & WCONTINUED) {
    if (WIFCONTINUED(info.si_status))
      return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<clone_reap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%ecx
mov    0x2e14(%rip),%rax
or     (%rax),%ecx
mov    0x2e33(%rip),%rax
or     (%rax),%ecx
lea    -0x18(%rbp),%rdx
call   1080 <waitid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11f0 <clone_reap+0x80>
mov    0x2df4(%rip),%rax
mov    (%rax),%rax
mov    0x2dda(%rip),%rcx
cmp    (%rcx),%rax
jne    11e4 <clone_reap+0x74>
jmp    118e <clone_reap+0x1e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <clone_reap+0x137>
mov    -0xc(%rbp),%eax
mov    0x2dd6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     122c <clone_reap+0xbc>
mov    -0x18(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1227 <clone_reap+0xb7>
mov    -0x18(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    12a7 <clone_reap+0x137>
jmp    122c <clone_reap+0xbc>
mov    -0xc(%rbp),%eax
mov    0x2dba(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1268 <clone_reap+0xf8>
mov    -0x18(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1263 <clone_reap+0xf3>
mov    -0x18(%rbp),%edi
call   1090 <WSTOPSIG@plt>
mov    %eax,-0x4(%rbp)
jmp    12a7 <clone_reap+0x137>
jmp    1268 <clone_reap+0xf8>
mov    -0xc(%rbp),%eax
mov    0x2d2e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12a0 <clone_reap+0x130>
mov    -0x18(%rbp),%edi
call   1040 <WIFCONTINUED@plt>
cmp    $0x0,%rax
je     129b <clone_reap+0x12b>
movl   $0x0,-0x4(%rbp)
jmp    12a7 <clone_reap+0x137>
jmp    12a0 <clone_reap+0x130>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clone_reap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
movq   $0x0,(%rsp)
mov    0x2e62(%rip),%r13
mov    %rsp,%r14
mov    0x2e38(%rip),%r15
mov    0x2e21(%rip),%r12
nop
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%ecx
or     %ebx,%ecx
or     0x0(%r13),%ecx
mov    %ebp,%esi
mov    %r14,%rdx
call   1070 <waitid@plt>
test   %eax,%eax
jns    11c6 <clone_reap+0x66>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1190 <clone_reap+0x30>
mov    $0xffffffff,%eax
jmp    1227 <clone_reap+0xc7>
mov    0x2e03(%rip),%rax
test   %ebx,(%rax)
je     11e8 <clone_reap+0x88>
mov    (%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     11e8 <clone_reap+0x88>
mov    (%rsp),%edi
call   1050 <WEXITSTATUS@plt>
jmp    1227 <clone_reap+0xc7>
mov    0x2e01(%rip),%rax
test   %ebx,(%rax)
je     120a <clone_reap+0xaa>
mov    (%rsp),%edi
call   1040 <WIFSTOPPED@plt>
test   %rax,%rax
je     120a <clone_reap+0xaa>
mov    (%rsp),%edi
call   1080 <WSTOPSIG@plt>
jmp    1227 <clone_reap+0xc7>
mov    0x2d8f(%rip),%rax
test   %ebx,(%rax)
je     11bf <clone_reap+0x5f>
mov    (%rsp),%edi
call   1030 <WIFCONTINUED@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     11bf <clone_reap+0x5f>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@P_PID = global i32 0
@__WALL = global i32 0
@__WNOTHREAD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@WEXITED = global i32 0
@WSTOPPED = global i32 0
@WCONTINUED = global i32 0
define i32 @clone_reap(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.TYPE_3__
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = bitcast %struct.TYPE_3__* %7 to i8*
br label %9
9: ; preds = %24, %2
%10 = load i32, i32* @P_PID
%11 = load i32, i32* %4
%12 = load i32, i32* %5
%13 = load i32, i32* @__WALL
%14 = or i32 %12, %13
%15 = load i32, i32* @__WNOTHREAD
%16 = or i32 %14, %15
%17 = call i32 @waitid(i32 %10, i32 %11, %struct.TYPE_3__* %7, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %9
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %9
25: ; preds = %20
store i32 -1, i32* %3
br label %69
26: ; preds = %9
%27 = load i32, i32* %5
%28 = load i32, i32* @WEXITED
%29 = and i32 %27, %28
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %41
31: ; preds = %26
%32 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i64 @WIFEXITED(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @WEXITSTATUS(i32 %38)
store i32 %39, i32* %3
br label %69
40: ; preds = %31
br label %41
41: ; preds = %40, %26
%42 = load i32, i32* %5
%43 = load i32, i32* @WSTOPPED
%44 = and i32 %42, %43
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %41
%47 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i64 @WIFSTOPPED(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @WSTOPSIG(i32 %53)
store i32 %54, i32* %3
br label %69
55: ; preds = %46
br label %56
56: ; preds = %55, %41
%57 = load i32, i32* %5
%58 = load i32, i32* @WCONTINUED
%59 = and i32 %57, %58
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %68
61: ; preds = %56
%62 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i64 @WIFCONTINUED(i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %61
store i32 0, i32* %3
br label %69
67: ; preds = %61
br label %68
68: ; preds = %67, %56
store i32 -1, i32* %3
br label %69
69: ; preds = %68, %66, %51, %36, %25
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@P_PID = global i32 0
@__WALL = global i32 0
@__WNOTHREAD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@WEXITED = global i32 0
@WSTOPPED = global i32 0
@WCONTINUED = global i32 0
define i32 @clone_reap(i32 %0, i32 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.TYPE_3__*
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
br label %6
6: ; preds = %14, %2
%7 = load i32, i32* @P_PID
%8 = load i32, i32* @__WALL
%9 = or i32 %8, %1
%10 = load i32, i32* @__WNOTHREAD
%11 = or i32 %9, %10
%12 = call i32 @waitid(i32 %7, i32 %0, %struct.TYPE_3__* %4, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %6
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %6, label %52
18: ; preds = %6
%19 = load i32, i32* @WEXITED
%20 = and i32 %19, %1
%21 = icmp eq i32 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %18
%23 = bitcast i64* %3 to i32*
%24 = load i32, i32* %23
%25 = call i64 @WIFEXITED(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = load i32, i32* %23
%29 = call i32 @WEXITSTATUS(i32 %28)
br label %52
30: ; preds = %22, %18
%31 = load i32, i32* @WSTOPPED
%32 = and i32 %31, %1
%33 = icmp eq i32 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %30
%35 = bitcast i64* %3 to i32*
%36 = load i32, i32* %35
%37 = call i64 @WIFSTOPPED(i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %34
%40 = load i32, i32* %35
%41 = call i32 @WSTOPSIG(i32 %40)
br label %52
42: ; preds = %34, %30
%43 = load i32, i32* @WCONTINUED
%44 = and i32 %43, %1
%45 = icmp eq i32 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %42
%47 = bitcast i64* %3 to i32*
%48 = load i32, i32* %47
%49 = call i64 @WIFCONTINUED(i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %46, %42
br label %52
52: ; preds = %14, %46, %51, %39, %27
%53 = phi i32 [ %29, %27 ], [ %41, %39 ], [ -1, %51 ], [ 0, %46 ], [ -1, %14 ]
ret i32 %53
}