#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 */

/* Variables and functions */
scalar_t__ ECHILD;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, char *);
char *strerror(scalar_t__);
int wait(int /*<<< orphan*/ *);
bool reap() {
  int harvest;
  while (true) {
    harvest = wait(NULL);
    if ((harvest == -1 && errno == ECHILD))
      return true;
    if (harvest == -1) {
      printf("failed to reap children: %s\n", strerror(errno));
      return false;
    }
  }
}


x86 assembly (clang, -O0):
<reap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
mov    %eax,%edi
call   1050 <wait@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1174 <reap+0x44>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
jne    1174 <reap+0x44>
movl   $0x1,-0x4(%rbp)
jmp    11ad <reap+0x7d>
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a8 <reap+0x78>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rdi
call   1040 <strerror@plt>
mov    %rax,%rsi
lea    0xe69(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ad <reap+0x7d>
jmp    1138 <reap+0x8>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reap>:
push   %rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1050 <wait@plt>
cmp    $0xffffffff,%eax
jne    1140 <reap+0x10>
mov    0x2e8d(%rip),%rax
mov    (%rax),%rdi
mov    0x2e7b(%rip),%rcx
mov    $0x1,%eax
cmp    (%rcx),%rdi
je     117d <reap+0x4d>
call   1040 <strerror@plt>
lea    0xe8d(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@errno = global i64 0
@ECHILD = global i64 0
@.str = [29 x i8] c"failed to reap children: %s\0A\00"
define i32 @reap() {
%1 = alloca i32
%2 = alloca i32
br label %3
3: ; preds = %0, %19
%4 = call i32 @wait(i32* null)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = load i64, i64* @errno
%9 = load i64, i64* @ECHILD
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %7
store i32 1, i32* %1
br label %20
12: ; preds = %7, %3
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = call i8* @strerror(i64 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 0, i32* %1
br label %20
19: ; preds = %12
br label %3
20: ; preds = %15, %11
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@errno = global i64 0
@ECHILD = global i64 0
@.str = [29 x i8] c"failed to reap children: %s\0A\00"
define i32 @reap() {
br label %1
1: ; preds = %1, %0
%2 = tail call i32 @wait(i32* null)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %1
4: ; preds = %1
%5 = load i64, i64* @errno
%6 = load i64, i64* @ECHILD
%7 = icmp eq i64 %5, %6
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i8* @strerror(i64 %5)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %11
11: ; preds = %4, %8
%12 = phi i32 [ 0, %8 ], [ 1, %4 ]
ret i32 %12
}