#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__ WIFSIGNALED(int);
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ WTERMSIG(int);
int /*<<< orphan*/ delete_job(int);
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, int, char *);
char *strsignal(int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
void reap_child(int signo) {
  for (;;) {
    int status;
    int ret = waitpid(-1, &status, WNOHANG);
    if (ret == 0) {
      break;
    } else if (ret < 0 && errno == ECHILD) {
      break;
    } else if (ret > 0) {
      if (WIFSIGNALED(status))
        printf("Job %d terminated by signal: %s\n", ret,
               strsignal(WTERMSIG(status)));
      delete_job(ret);
    } else {
      ;
    }
  }
}


x86 assembly (clang, -O0):
<reap_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1070 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1194 <reap_child+0x34>
jmp    1221 <reap_child+0xc1>
cmpl   $0x0,-0xc(%rbp)
jge    11bd <reap_child+0x5d>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
jne    11bd <reap_child+0x5d>
jmp    1221 <reap_child+0xc1>
cmpl   $0x0,-0xc(%rbp)
jle    120d <reap_child+0xad>
mov    -0x8(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1200 <reap_child+0xa0>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,%edi
call   1060 <strsignal@plt>
mov    -0x10(%rbp),%esi
mov    %rax,%rdx
lea    0xe05(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1080 <delete_job@plt>
jmp    1212 <reap_child+0xb2>
jmp    1212 <reap_child+0xb2>
jmp    1217 <reap_child+0xb7>
jmp    121c <reap_child+0xbc>
jmp    116b <reap_child+0xb>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reap_child>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e76(%rip),%r15
mov    (%r15),%edx
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
call   1070 <waitpid@plt>
test   %eax,%eax
je     1208 <reap_child+0xa8>
mov    %eax,%ebp
mov    0x2e43(%rip),%r12
mov    0x2e34(%rip),%r13
lea    0x4(%rsp),%rbx
lea    0xe58(%rip),%r14
jmp    11d0 <reap_child+0x70>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1208 <reap_child+0xa8>
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1070 <waitpid@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1208 <reap_child+0xa8>
test   %ebp,%ebp
js     11b0 <reap_child+0x50>
mov    0x4(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     11ff <reap_child+0x9f>
mov    0x4(%rsp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,%edi
call   1060 <strsignal@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1030 <printf@plt>
mov    %ebp,%edi
call   1080 <delete_job@plt>
jmp    11ba <reap_child+0x5a>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@errno = global i64 0
@ECHILD = global i64 0
@.str = [33 x i8] c"Job %d terminated by signal: %s\0A\00"
define void @reap_child(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %38, %1
%6 = load i32, i32* @WNOHANG
%7 = call i32 @waitpid(i32 -1, i32* %3, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %5
br label %39
11: ; preds = %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @ECHILD
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
br label %39
19: ; preds = %14, %11
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %19
%23 = load i32, i32* %3
%24 = call i64 @WIFSIGNALED(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i32, i32* %4
%28 = load i32, i32* %3
%29 = call i32 @WTERMSIG(i32 %28)
%30 = call i8* @strsignal(i32 %29)
%31 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %27, i8* %30)
br label %32
32: ; preds = %26, %22
%33 = load i32, i32* %4
%34 = call i32 @delete_job(i32 %33)
br label %36
35: ; preds = %19
br label %36
36: ; preds = %35, %32
br label %37
37: ; preds = %36
br label %38
38: ; preds = %37
br label %5
39: ; preds = %18, %10
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@errno = global i64 0
@ECHILD = global i64 0
@.str = [33 x i8] c"Job %d terminated by signal: %s\0A\00"
define void @reap_child(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @WNOHANG
%5 = call i32 @waitpid(i32 -1, i32* %2, i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %29, label %7
7: ; preds = %1, %25
%8 = phi i32 [ %27, %25 ], [ %5, %1 ]
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @ECHILD
%13 = icmp eq i64 %11, %12
br i1 %13, label %29, label %25
14: ; preds = %7
%15 = load i32, i32* %2
%16 = call i64 @WIFSIGNALED(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %14
%19 = load i32, i32* %2
%20 = call i32 @WTERMSIG(i32 %19)
%21 = call i8* @strsignal(i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %8, i8* %21)
br label %23
23: ; preds = %18, %14
%24 = call i32 @delete_job(i32 %8)
br label %25
25: ; preds = %10, %23
%26 = load i32, i32* @WNOHANG
%27 = call i32 @waitpid(i32 -1, i32* %2, i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %7
29: ; preds = %10, %25, %1
ret void
}