#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__ EINTR;
int /*<<< orphan*/ WNOHANG;
scalar_t__ errno;
int waitpid(int, int *, int /*<<< orphan*/);
void child_terminated_handler(int sig __attribute__((unused))) {
  int status, child_pid;
  for (;;) {
    child_pid = waitpid(-1, &status, WNOHANG);
    if (child_pid <= 0) {
      if (child_pid == -1) {
        if (errno == EINTR)
          continue;
      }
      break;
    }
  }
}


x86 assembly (clang, -O0):
<child_terminated_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2ec6(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1030 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     1172 <child_terminated_handler+0x62>
cmpl   $0xffffffff,-0xc(%rbp)
jne    116d <child_terminated_handler+0x5d>
mov    0x2e88(%rip),%rax
mov    (%rax),%rax
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%rax
jne    1168 <child_terminated_handler+0x58>
jmp    111b <child_terminated_handler+0xb>
jmp    116d <child_terminated_handler+0x5d>
jmp    1177 <child_terminated_handler+0x67>
jmp    111b <child_terminated_handler+0xb>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child_terminated_handler>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2ec9(%rip),%r14
lea    0x4(%rsp),%rbx
mov    0x2ead(%rip),%r15
mov    0x2e9e(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1030 <waitpid@plt>
test   %eax,%eax
jg     1140 <child_terminated_handler+0x30>
cmp    $0xffffffff,%eax
jne    1162 <child_terminated_handler+0x52>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1140 <child_terminated_handler+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @child_terminated_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %20, %17, %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 sle i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %5
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
br label %5
18: ; preds = %13
br label %19
19: ; preds = %18, %10
br label %21
20: ; preds = %5
br label %5
21: ; preds = %19
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @child_terminated_handler(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %14, %1
%5 = load i32, i32* @WNOHANG
%6 = call i32 @waitpid(i32 -1, i32* %2, i32 %5)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %14
8: ; preds = %4
%9 = icmp eq i32 %6, -1
br i1 %9, label %10, label %15
10: ; preds = %8
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10, %4
br label %4
15: ; preds = %8, %10
ret void
}