#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;
char *strsignal(int);
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: ", stderr);
      fputs(strsignal(WTERMSIG(status)), stderr);
      fputs("\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   1090 <wait@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11a0 <reap_until+0x30>
lea    0xe65(%rip),%rdi
call   1070 <die@plt>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1255 <reap_until+0xe5>
mov    -0x14(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11f6 <reap_until+0x86>
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     11e6 <reap_until+0x76>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    0xe24(%rip),%rdi
call   1030 <fputs@plt>
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    125a <reap_until+0xea>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdi
call   1030 <fputs@plt>
mov    -0x14(%rbp),%edi
call   1040 <WTERMSIG@plt>
mov    %eax,%edi
call   1050 <strsignal@plt>
mov    %rax,%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
call   1030 <fputs@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
lea    0xde1(%rip),%rdi
call   1030 <fputs@plt>
mov    -0x14(%rbp),%edi
call   1040 <WTERMSIG@plt>
add    $0x80,%eax
mov    %eax,-0x4(%rbp)
jmp    125a <reap_until+0xea>
jmp    117c <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    0xe79(%rip),%r15
jmp    1195 <reap_until+0x25>
nopl   0x0(%rax)
cmp    %r12,%rbx
je     11af <reap_until+0x3f>
mov    %r14,%rdi
call   1090 <wait@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    1190 <reap_until+0x20>
mov    %r15,%rdi
call   1070 <die@plt>
jmp    1190 <reap_until+0x20>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11ea <reap_until+0x7a>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
test   %eax,%eax
je     11df <reap_until+0x6f>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0xe2b(%rip),%rdi
call   1030 <fputs@plt>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
jmp    1233 <reap_until+0xc3>
mov    0x2def(%rip),%rbx
mov    (%rbx),%esi
lea    0xe24(%rip),%rdi
call   1030 <fputs@plt>
mov    0x4(%rsp),%edi
call   1040 <WTERMSIG@plt>
mov    %eax,%edi
call   1050 <strsignal@plt>
mov    (%rbx),%esi
mov    %rax,%rdi
call   1030 <fputs@plt>
mov    (%rbx),%esi
lea    0xdfa(%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 = [25 x i8] c"child exited by signal: \00"
@.str.3 = [2 x i8] c"\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 = %43, %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 %43
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 %44
30: ; preds = %16
%31 = load i32, i32* @stderr
%32 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %4
%34 = call i32 @WTERMSIG(i32 %33)
%35 = call i8* @strsignal(i32 %34)
%36 = load i32, i32* @stderr
%37 = call i32 @fputs(i8* %35, i32 %36)
%38 = load i32, i32* @stderr
%39 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %4
%41 = call i32 @WTERMSIG(i32 %40)
%42 = add nsw i32 128, %41
store i32 %42, i32* %2
br label %44
43: ; preds = %12
br label %6
44: ; preds = %30, %27
%45 = load i32, i32* %2
ret i32 %45
}

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 = [25 x i8] c"child exited by signal: \00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @reap_until(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %38, %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 %38
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 %39
25: ; preds = %11
%26 = load i32, i32* @stderr
%27 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %2
%29 = call i32 @WTERMSIG(i32 %28)
%30 = call i8* @strsignal(i32 %29)
%31 = load i32, i32* @stderr
%32 = call i32 @fputs(i8* %30, i32 %31)
%33 = load i32, i32* @stderr
%34 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %2
%36 = call i32 @WTERMSIG(i32 %35)
%37 = add nsw i32 %36, 128
br label %39
38: ; preds = %9
br label %4
39: ; preds = %22, %25
%40 = phi i32 [ %37, %25 ], [ %24, %22 ]
ret i32 %40
}