#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*/ siginfo_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ idtype_t;
typedef int /*<<< orphan*/ id_t;

/* Variables and functions */
scalar_t__ _wait6(int /*<<< orphan*/, int /*<<< orphan*/, int *, int,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int __waitid(idtype_t idtype, id_t id, siginfo_t *info, int flags) {
  int status;
  pid_t ret;
  ret = _wait6(idtype, id, &status, flags, NULL, info);
  if (ret == 0 && info != NULL)
    memset(info, 0, sizeof(*info));
  else if (ret > 0)
    ret = 0;
  return (ret);
}


x86 assembly (clang, -O0):
<__waitid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%r9
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1040 <_wait6@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117f <__waitid+0x5f>
cmpq   $0x0,-0x10(%rbp)
je     117f <__waitid+0x5f>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
jmp    1197 <__waitid+0x77>
cmpq   $0x0,-0x20(%rbp)
jle    1192 <__waitid+0x72>
movq   $0x0,-0x20(%rbp)
jmp    1197 <__waitid+0x77>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__waitid>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
lea    0x4(%rsp),%rdx
xor    %r8d,%r8d
mov    %r14,%r9
call   1040 <_wait6@plt>
test   %r14,%r14
je     1154 <__waitid+0x34>
test   %rax,%rax
jne    1154 <__waitid+0x34>
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
jmp    115d <__waitid+0x3d>
mov    %rax,%rbx
sar    $0x3f,%rbx
and    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @__waitid(i32 %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%11 = load i32, i32* %5
%12 = load i32, i32* %6
%13 = load i32, i32* %8
%14 = load i32*, i32** %7
%15 = call i64 @_wait6(i32 %11, i32 %12, i32* %9, i32 %13, i32* null, i32* %14)
store i64 %15, i64* %10
%16 = load i64, i64* %10
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %4
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32*, i32** %7
%23 = call i32 @memset(i32* %22, i32 0, i32 4)
br label %29
24: ; preds = %18, %4
%25 = load i64, i64* %10
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
store i64 0, i64* %10
br label %28
28: ; preds = %27, %24
br label %29
29: ; preds = %28, %21
%30 = load i64, i64* %10
%31 = trunc i64 %30 to i32
ret i32 %31
}

LLVM IR (-O2):
define i32 @__waitid(i32 %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i64 @_wait6(i32 %0, i32 %1, i32* %5, i32 %3, i32* null, i32* %2)
%8 = icmp eq i64 %7, 0
%9 = icmp ne i32* %2, null
%10 = and i1 %9, %8
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = call i32 @memset(i32* %2, i32 0, i32 4)
br label %17
13: ; preds = %4
%14 = icmp slt i64 %7, 0
%15 = select i1 %14, i64 %7, i64 0
%16 = trunc i64 %15 to i32
br label %17
17: ; preds = %13, %11
%18 = phi i32 [ 0, %11 ], [ %16, %13 ]
ret i32 %18
}