#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 */
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int real_waitpid(int, int *, int);
int /*<<< orphan*/ reap_common(int);
pid_t waitpid(pid_t pid, int *status, int options) {
  pid_t ret;
  ret = real_waitpid(pid, status, options);
  if (ret < 1) {
    return ret;
  }
  if (WIFEXITED(*status) == 0 && WIFSIGNALED(*status) == 0) {
    return ret;
  }
  reap_common(ret);
  return ret;
}


x86 assembly (clang, -O0):
<waitpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <real_waitpid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jge    1179 <waitpid+0x39>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <waitpid+0x7c>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
jne    11ae <waitpid+0x6e>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
jne    11ae <waitpid+0x6e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <waitpid+0x7c>
mov    -0x18(%rbp),%edi
call   1050 <reap_common@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitpid>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
call   1040 <real_waitpid@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1173 <waitpid+0x33>
mov    (%r14),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
jne    116c <waitpid+0x2c>
mov    (%r14),%edi
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
je     1173 <waitpid+0x33>
mov    %ebx,%edi
call   1050 <reap_common@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @waitpid(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i32*, i32** %6
%11 = load i32, i32* %7
%12 = call i32 @real_waitpid(i32 %9, i32* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* %8
store i32 %16, i32* %4
br label %33
17: ; preds = %3
%18 = load i32*, i32** %6
%19 = load i32, i32* %18
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = load i32*, i32** %6
%24 = load i32, i32* %23
%25 = call i64 @WIFSIGNALED(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = load i32, i32* %8
store i32 %28, i32* %4
br label %33
29: ; preds = %22, %17
%30 = load i32, i32* %8
%31 = call i32 @reap_common(i32 %30)
%32 = load i32, i32* %8
store i32 %32, i32* %4
br label %33
33: ; preds = %29, %27, %15
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
define i32 @waitpid(i32 %0, i32* %1, i32 %2) {
%4 = tail call i32 @real_waitpid(i32 %0, i32* %1, i32 %2)
%5 = icmp slt i32 %4, 1
br i1 %5, label %16, label %6
6: ; preds = %3
%7 = load i32, i32* %1
%8 = tail call i64 @WIFEXITED(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load i32, i32* %1
%12 = tail call i64 @WIFSIGNALED(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10, %6
%15 = tail call i32 @reap_common(i32 %4)
br label %16
16: ; preds = %10, %3, %14
ret i32 %4
}