#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);
int WTERMSIG(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ wait(int *);
int reap_until(pid_t until_pid) {
  for (;;) {
    int status;
    pid_t pid = wait(&status);
    if (pid < 0) {
      die("wait");
    }
    if (pid == until_pid) {
      if (WIFEXITED(status)) {
        if (WEXITSTATUS(status) != 0) {
          fputs("child exited with error\n", stderr);
        }
        return WEXITSTATUS(status);
      }
      fputs("child exited by signal\n", stderr);
      return 128 + WTERMSIG(status);
    }
  }
}


x86 assembly (clang, -O0):
<reap_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1080 <wait@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1190 <reap_until+0x30>
lea    0xe75(%rip),%rdi
call   1060 <die@plt>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1210 <reap_until+0xb0>
mov    -0x14(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e6 <reap_until+0x86>
mov    -0x14(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     11d6 <reap_until+0x76>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   1030 <fputs@plt>
mov    -0x14(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    1215 <reap_until+0xb5>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe28(%rip),%rdi
call   1030 <fputs@plt>
mov    -0x14(%rbp),%edi
call   1040 <WTERMSIG@plt>
add    $0x80,%eax
mov    %eax,-0x4(%rbp)
jmp    1215 <reap_until+0xb5>
jmp    116c <reap_until+0xc>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reap_until>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
lea    0x4(%rsp),%r14
lea    0xe89(%rip),%r15
jmp    1185 <reap_until+0x25>
nopl   0x0(%rax)
cmp    %r12,%rbx
je     119f <reap_until+0x3f>
mov    %r14,%rdi
call   1080 <wait@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    1180 <reap_until+0x20>
mov    %r15,%rdi
call   1060 <die@plt>
jmp    1180 <reap_until+0x20>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     11da <reap_until+0x7a>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
test   %eax,%eax
je     11cf <reap_until+0x6f>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rdi
call   1030 <fputs@plt>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
jmp    11fb <reap_until+0x9b>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   1030 <fputs@plt>
mov    0x4(%rsp),%edi
call   1040 <WTERMSIG@plt>
sub    $0xffffff80,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"wait\00"
@.str.1 = [25 x i8] c"child exited with error\0A\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"child exited by signal\0A\00"
define i32 @reap_until(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
store i64 %0, i64* %3
br label %6
6: ; preds = %36, %1
%7 = call i64 @wait(i32* %4)
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = load i64, i64* %5
%14 = load i64, i64* %3
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %36
16: ; preds = %12
%17 = load i32, i32* %4
%18 = call i64 @WIFEXITED(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i32, i32* %4
%22 = call i32 @WEXITSTATUS(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %20
%28 = load i32, i32* %4
%29 = call i32 @WEXITSTATUS(i32 %28)
store i32 %29, i32* %2
br label %37
30: ; preds = %16
%31 = load i32, i32* @stderr
%32 = call i32 @fputs(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %4
%34 = call i32 @WTERMSIG(i32 %33)
%35 = add nsw i32 128, %34
store i32 %35, i32* %2
br label %37
36: ; preds = %12
br label %6
37: ; preds = %30, %27
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [5 x i8] c"wait\00"
@.str.1 = [25 x i8] c"child exited with error\0A\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"child exited by signal\0A\00"
define i32 @reap_until(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %31, %1
%5 = call i64 @wait(i32* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %4
%10 = icmp eq i64 %5, %0
br i1 %10, label %11, label %31
11: ; preds = %9
%12 = load i32, i32* %2
%13 = call i64 @WIFEXITED(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %25, label %15
15: ; preds = %11
%16 = load i32, i32* %2
%17 = call i32 @WEXITSTATUS(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %19, %15
%23 = load i32, i32* %2
%24 = call i32 @WEXITSTATUS(i32 %23)
br label %32
25: ; preds = %11
%26 = load i32, i32* @stderr
%27 = call i32 @fputs(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %2
%29 = call i32 @WTERMSIG(i32 %28)
%30 = add nsw i32 %29, 128
br label %32
31: ; preds = %9
br label %4
32: ; preds = %22, %25
%33 = phi i32 [ %30, %25 ], [ %24, %22 ]
ret i32 %33
}