#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ progress(char *, scalar_t__);
scalar_t__ waitpid(scalar_t__, int *, int);
int reap_child(pid_t kid, int opt) {
  int r = 0;
  progress("parent: checking on child %d to exit..\n", kid);
  pid_t p = waitpid(kid, &r, opt);
  if (p == 0)
    return 0;
  if (p == (pid_t)-1)
    error(1, errno, "waitpid on %d failed", kid);
  if (WIFEXITED(r)) {
    int x = WEXITSTATUS(r);
    if (x != 0)
      error(1, 0, "kid exited with non-zero code %d", x);
    return 1;
  } else if (WIFSIGNALED(r)) {
    int sig = WTERMSIG(r);
    error(1, 0, "kid caught signal %d and aborted", sig);
  }
  return 0;
}


x86 assembly (clang, -O0):
<reap_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe6f(%rip),%rdi
call   1030 <progress@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x18(%rbp),%rsi
call   1080 <waitpid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c1 <reap_child+0x51>
movl   $0x0,-0x4(%rbp)
jmp    1274 <reap_child+0x104>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11ec <reap_child+0x7c>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    $0x1,%edi
lea    0xe41(%rip),%rdx
call   1070 <error@plt>
mov    -0x18(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1235 <reap_child+0xc5>
mov    -0x18(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1229 <reap_child+0xb9>
mov    -0x24(%rbp),%ecx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe19(%rip),%rdx
call   1070 <error@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <reap_child+0x104>
mov    -0x18(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1268 <reap_child+0xf8>
mov    -0x18(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xdfb(%rip),%rdx
call   1070 <error@plt>
jmp    126d <reap_child+0xfd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reap_child>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
movl   $0x0,0x4(%rsp)
lea    0xe79(%rip),%rdi
mov    %rbx,%rsi
call   1030 <progress@plt>
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1080 <waitpid@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1221 <reap_child+0xb1>
cmp    $0xffffffffffffffff,%rax
jne    11c7 <reap_child+0x57>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdx
mov    $0x1,%edi
mov    %ebx,%ecx
call   1070 <error@plt>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     11f0 <reap_child+0x80>
call   1060 <WEXITSTATUS@plt>
test   %eax,%eax
je     121c <reap_child+0xac>
lea    0xe54(%rip),%rdx
mov    $0x1,%ebp
jmp    120c <reap_child+0x9c>
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     1221 <reap_child+0xb1>
mov    0x4(%rsp),%edi
call   1050 <WTERMSIG@plt>
lea    0xe54(%rip),%rdx
xor    %ebp,%ebp
mov    $0x1,%edi
xor    %esi,%esi
mov    %eax,%ecx
call   1070 <error@plt>
jmp    1221 <reap_child+0xb1>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"parent: checking on child %d to exit..\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"waitpid on %d failed\00"
@.str.2 = [33 x i8] c"kid exited with non-zero code %d\00"
@.str.3 = [33 x i8] c"kid caught signal %d and aborted\00"
define i32 @reap_child(i64 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i64 %0, i64* %4
store i32 %1, i32* %5
store i32 0, i32* %6
%10 = load i64, i64* %4
%11 = call i32 @progress(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i64 %10)
%12 = load i64, i64* %4
%13 = load i32, i32* %5
%14 = call i64 @waitpid(i64 %12, i32* %6, i32 %13)
store i64 %14, i64* %7
%15 = load i64, i64* %7
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 0, i32* %3
br label %50
18: ; preds = %2
%19 = load i64, i64* %7
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = load i64, i64* %4
%24 = trunc i64 %23 to i32
%25 = call i32 @error(i32 1, i32 %22, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %21, %18
%27 = load i32, i32* %6
%28 = call i64 @WIFEXITED(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = load i32, i32* %6
%32 = call i32 @WEXITSTATUS(i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* %8
%37 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %35, %30
store i32 1, i32* %3
br label %50
39: ; preds = %26
%40 = load i32, i32* %6
%41 = call i64 @WIFSIGNALED(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %39
%44 = load i32, i32* %6
%45 = call i32 @WTERMSIG(i32 %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %43, %39
br label %49
49: ; preds = %48
store i32 0, i32* %3
br label %50
50: ; preds = %49, %38, %17
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [40 x i8] c"parent: checking on child %d to exit..\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"waitpid on %d failed\00"
@.str.2 = [33 x i8] c"kid exited with non-zero code %d\00"
@.str.3 = [33 x i8] c"kid caught signal %d and aborted\00"
define i32 @reap_child(i64 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = tail call i32 @progress(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i64 %0)
%6 = call i64 @waitpid(i64 %0, i32* %3, i32 %1)
switch i64 %6, label %11 [
i64 0, label %28
i64 -1, label %7
]
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = trunc i64 %0 to i32
%10 = call i32 @error(i32 1, i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %2, %7
%12 = load i32, i32* %3
%13 = call i64 @WIFEXITED(i32 %12)
%14 = icmp eq i64 %13, 0
%15 = load i32, i32* %3
br i1 %14, label %21, label %16
16: ; preds = %11
%17 = call i32 @WEXITSTATUS(i32 %15)
%18 = icmp eq i32 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %16
%20 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %28
21: ; preds = %11
%22 = call i64 @WIFSIGNALED(i32 %15)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = load i32, i32* %3
%26 = call i32 @WTERMSIG(i32 %25)
%27 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %24, %21, %16, %19, %2
%29 = phi i32 [ 0, %2 ], [ 1, %19 ], [ 1, %16 ], [ 0, %21 ], [ 0, %24 ]
ret i32 %29
}