#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int WNOHANG;
int WUNTRACED;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ kwaitpid(int *, int /*<<< orphan*/, int,
                            int /*<<< orphan*/ *, int /*<<< orphan*/ *);
pid_t __libc_waitpid(pid_t pid, int *stat_loc, int options) {
  if ((options & ~(WNOHANG | WUNTRACED)) != 0) {
    __set_errno(EINVAL);
    return (pid_t)-1;
  }
  return kwaitpid(stat_loc, pid, options, NULL, NULL);
}


x86 assembly (clang, -O0):
<__libc_waitpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2eac(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e93(%rip),%rdx
or     (%rdx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     116f <__libc_waitpid+0x4f>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1189 <__libc_waitpid+0x69>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   1040 <kwaitpid@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__libc_waitpid>:
push   %rax
mov    %edi,%eax
mov    0x2ebe(%rip),%rcx
mov    0x2ea7(%rip),%rdi
mov    (%rdi),%edi
or     (%rcx),%edi
not    %edi
test   %edx,%edi
je     1150 <__libc_waitpid+0x30>
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
mov    %rsi,%rdi
mov    %eax,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
pop    %rax
jmp    1040 <kwaitpid@plt>

LLVM IR (-O0):
@WNOHANG = global i32 0
@WUNTRACED = global i32 0
@EINVAL = global i32 0
define i32 @__libc_waitpid(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%8 = load i32, i32* %7
%9 = load i32, i32* @WNOHANG
%10 = load i32, i32* @WUNTRACED
%11 = or i32 %9, %10
%12 = xor i32 %11, -1
%13 = and i32 %8, %12
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* @EINVAL
%17 = call i32 @__set_errno(i32 %16)
store i32 -1, i32* %4
br label %23
18: ; preds = %3
%19 = load i32*, i32** %6
%20 = load i32, i32* %5
%21 = load i32, i32* %7
%22 = call i32 @kwaitpid(i32* %19, i32 %20, i32 %21, i32* null, i32* null)
store i32 %22, i32* %4
br label %23
23: ; preds = %18, %15
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@WUNTRACED = global i32 0
@EINVAL = global i32 0
define i32 @__libc_waitpid(i32 %0, i32* %1, i32 %2) {
%4 = load i32, i32* @WNOHANG
%5 = load i32, i32* @WUNTRACED
%6 = or i32 %5, %4
%7 = xor i32 %6, -1
%8 = and i32 %7, %2
%9 = icmp eq i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = load i32, i32* @EINVAL
%12 = tail call i32 @__set_errno(i32 %11)
br label %15
13: ; preds = %3
%14 = tail call i32 @kwaitpid(i32* %1, i32 %0, i32 %2, i32* null, i32* null)
br label %15
15: ; preds = %13, %10
%16 = phi i32 [ -1, %10 ], [ %14, %13 ]
ret i32 %16
}