#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__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
void reap(int pid) {
  int wpid, status;
  wpid = waitpid(pid, &status, 0);
  if (wpid < 0) {
    perror("waitpid");
    exit(1);
  }
  if (wpid != pid) {
    fprintf(stderr, "unexpected pid reaped: %d\n", wpid);
    exit(1);
  }
  if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
    fprintf(stderr, "child exited unhappily\n");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<reap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119c <reap+0x3c>
lea    0xe73(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     11cc <reap+0x6c>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%eax
je     11ef <reap+0x8f>
mov    -0xc(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     1210 <reap+0xb0>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reap>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %eax,%eax
js     11bd <reap+0x5d>
cmp    %ebx,%eax
jne    11d3 <reap+0x73>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %eax,%eax
je     119c <reap+0x3c>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
test   %rax,%rax
jne    119c <reap+0x3c>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe3c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e06(%rip),%rcx
mov    (%rcx),%edi
lea    0xe25(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"waitpid\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"unexpected pid reaped: %d\0A\00"
@.str.2 = [24 x i8] c"child exited unhappily\0A\00"
define void @reap(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @waitpid(i32 %5, i32* %4, i32 0)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = load i32, i32* %3
%14 = load i32, i32* %2
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* %3
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = load i32, i32* %4
%23 = call i32 @WIFEXITED(i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* %4
%27 = call i64 @WEXITSTATUS(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25, %21
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %25
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"waitpid\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"unexpected pid reaped: %d\0A\00"
@.str.2 = [24 x i8] c"child exited unhappily\0A\00"
define void @reap(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = icmp eq i32 %4, %0
br i1 %10, label %15, label %11
11: ; preds = %9
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = load i32, i32* %2
%17 = call i32 @WIFEXITED(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i32, i32* %2
%21 = call i64 @WEXITSTATUS(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %19, %15
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
ret void
}