#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_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int si_pid;
};
typedef TYPE_1__ siginfo_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ P_ALL;
int WEXITED;
int WNOHANG;
int errno;
int /*<<< orphan*/ memset(TYPE_1__ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char const *, ...);
char *strerror(int);
int waitid(int /*<<< orphan*/, int /*<<< orphan*/, TYPE_1__ *, int);
pid_t try_waitid(const char *where) {
  siginfo_t si;
  int e;
  memset(&si, 0, sizeof si);
  e = waitid(P_ALL, 0, &si, WEXITED | WNOHANG);
  if (e == -1) {
    printf("%s: waitid() error (%d: %s)\n", where, errno, strerror(errno));
    return 0;
  } else if (e != 0) {
    printf("%s: unexpected return value from waitid: %d\n", where, e);
    return 0;
  } else if (si.si_pid == 0) {
    printf("%s: waitid() would block...\n", where);
    return 0;
  }
  printf("%s: reaped %d with waitid()\n", where, si.si_pid);
  return si.si_pid;
}


x86 assembly (clang, -O0):
<try_waitid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6b(%rip),%rax
or     (%rax),%ecx
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
call   1060 <waitid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d5 <try_waitid+0x95>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124d <try_waitid+0x10d>
cmpl   $0x0,-0x1c(%rbp)
je     1200 <try_waitid+0xc0>
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124d <try_waitid+0x10d>
cmpl   $0x0,-0x18(%rbp)
jne    1228 <try_waitid+0xe8>
mov    -0x10(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124d <try_waitid+0x10d>
jmp    122d <try_waitid+0xed>
jmp    1232 <try_waitid+0xf2>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_waitid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e79(%rip),%rax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %esi,%esi
mov    %rbp,%rdx
call   1060 <waitid@plt>
test   %eax,%eax
je     11b7 <try_waitid+0x77>
cmp    $0xffffffff,%eax
jne    11d6 <try_waitid+0x96>
mov    0x2e3c(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1050 <strerror@plt>
lea    0xe5c(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1200 <try_waitid+0xc0>
mov    0x8(%rsp),%edx
test   %edx,%edx
je     11ed <try_waitid+0xad>
lea    0xea1(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x8(%rsp),%ebp
jmp    1200 <try_waitid+0xc0>
lea    0xe40(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1200 <try_waitid+0xc0>
lea    0xe56(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32 }
@P_ALL = global i32 0
@WEXITED = global i32 0
@WNOHANG = global i32 0
@.str = [29 x i8] c"%s: waitid() error (%d: %s)\0A\00"
@errno = global i32 0
@.str.1 = [45 x i8] c"%s: unexpected return value from waitid: %d\0A\00"
@.str.2 = [29 x i8] c"%s: waitid() would block...\0A\00"
@.str.3 = [29 x i8] c"%s: reaped %d with waitid()\0A\00"
define i32 @try_waitid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.TYPE_4__
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i32 @memset(%struct.TYPE_4__* %4, i32 0, i32 4)
%7 = load i32, i32* @P_ALL
%8 = load i32, i32* @WEXITED
%9 = load i32, i32* @WNOHANG
%10 = or i32 %8, %9
%11 = call i32 @waitid(i32 %7, i32 0, %struct.TYPE_4__* %4, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = load i32, i32* @errno
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %16, i8* %18)
store i32 0, i32* %2
br label %43
20: ; preds = %1
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i8*, i8** %3
%25 = load i32, i32* %5
%26 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %24, i32 %25)
store i32 0, i32* %2
br label %43
27: ; preds = %20
%28 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 0
%29 = load i32, i32* %28
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i8*, i8** %3
%33 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 0, i32* %2
br label %43
34: ; preds = %27
br label %35
35: ; preds = %34
br label %36
36: ; preds = %35
%37 = load i8*, i8** %3
%38 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %37, i32 %39)
%41 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 0
%42 = load i32, i32* %41
store i32 %42, i32* %2
br label %43
43: ; preds = %36, %31, %23, %14
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32 }
@P_ALL = global i32 0
@WEXITED = global i32 0
@WNOHANG = global i32 0
@.str = [29 x i8] c"%s: waitid() error (%d: %s)\0A\00"
@errno = global i32 0
@.str.1 = [45 x i8] c"%s: unexpected return value from waitid: %d\0A\00"
@.str.2 = [29 x i8] c"%s: waitid() would block...\0A\00"
@.str.3 = [29 x i8] c"%s: reaped %d with waitid()\0A\00"
define i32 @try_waitid(i8* %0) {
%2 = alloca %struct.TYPE_4__
%3 = bitcast %struct.TYPE_4__* %2 to i8*
%4 = call i32 @memset(%struct.TYPE_4__* %2, i32 0, i32 4)
%5 = load i32, i32* @P_ALL
%6 = load i32, i32* @WEXITED
%7 = load i32, i32* @WNOHANG
%8 = or i32 %7, %6
%9 = call i32 @waitid(i32 %5, i32 0, %struct.TYPE_4__* %2, i32 %8)
switch i32 %9, label %14 [
i32 -1, label %10
i32 0, label %16
]
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %11, i8* %12)
br label %25
14: ; preds = %1
%15 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %9)
br label %25
16: ; preds = %1
%17 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 0
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %25
22: ; preds = %16
%23 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %18)
%24 = load i32, i32* %17
br label %25
25: ; preds = %22, %20, %14, %10
%26 = phi i32 [ 0, %10 ], [ 0, %14 ], [ 0, %20 ], [ %24, %22 ]
ret i32 %26
}