#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *, char const *, ...);
char *strerror(int /*<<< orphan*/);
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
pid_t try_waitpid(const char *where) {
  pid_t pid = waitpid(-1, NULL, WNOHANG);
  if (pid == 0) {
    printf("%s: waitpid() would block...\n", where);
    return 0;
  }
  if (pid < 0) {
    printf("%s: waitpid() returned error (%s)\n", where, strerror(errno));
    return 0;
  }
  printf("%s: reaped %d with waitpid()\n", where, pid);
  return pid;
}


x86 assembly (clang, -O0):
<try_waitpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <waitpid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1181 <try_waitpid+0x51>
mov    -0x10(%rbp),%rsi
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <try_waitpid+0xb2>
cmpq   $0x0,-0x18(%rbp)
jge    11c4 <try_waitpid+0x94>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <try_waitpid+0xb2>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_waitpid>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2eaa(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1040 <waitpid@plt>
test   %rax,%rax
je     115f <try_waitpid+0x2f>
js     1174 <try_waitpid+0x44>
mov    %rax,%rbx
lea    0xee4(%rip),%rdi
jmp    118b <try_waitpid+0x5b>
lea    0xe9a(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1198 <try_waitpid+0x68>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe95(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@.str = [30 x i8] c"%s: waitpid() would block...\0A\00"
@.str.1 = [35 x i8] c"%s: waitpid() returned error (%s)\0A\00"
@errno = global i32 0
@.str.2 = [30 x i8] c"%s: reaped %d with waitpid()\0A\00"
define i64 @try_waitpid(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %3
%5 = load i32, i32* @WNOHANG
%6 = call i64 @waitpid(i32 -1, i32* null, i32 %5)
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %10)
store i64 0, i64* %2
br label %25
12: ; preds = %1
%13 = load i64, i64* %4
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %18)
store i64 0, i64* %2
br label %25
20: ; preds = %12
%21 = load i8*, i8** %3
%22 = load i64, i64* %4
%23 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %21, i64 %22)
%24 = load i64, i64* %4
store i64 %24, i64* %2
br label %25
25: ; preds = %20, %15, %9
%26 = load i64, i64* %2
ret i64 %26
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [30 x i8] c"%s: waitpid() would block...\0A\00"
@.str.1 = [35 x i8] c"%s: waitpid() returned error (%s)\0A\00"
@errno = global i32 0
@.str.2 = [30 x i8] c"%s: reaped %d with waitpid()\0A\00"
define i64 @try_waitpid(i8* %0) {
%2 = load i32, i32* @WNOHANG
%3 = tail call i64 @waitpid(i32 -1, i32* null, i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %15
7: ; preds = %1
%8 = icmp slt i64 %3, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %11)
br label %15
13: ; preds = %7
%14 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %3)
br label %15
15: ; preds = %13, %9, %5
%16 = phi i64 [ 0, %5 ], [ 0, %9 ], [ %3, %13 ]
ret i64 %16
}