#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 int pid_t;

/* Variables and functions */
scalar_t__ ECHILD;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ WNOHANG;
scalar_t__ errno;
int /*<<< orphan*/ signal(int /*<<< orphan*/, void (*)(int));
int waitpid(int, int *, int /*<<< orphan*/);
void sighandle_SIGCHLD(int arg) {
  pid_t pid;
  int status;
  while (1) {
    pid = waitpid(-1, &status, WNOHANG);
    if (pid == 0) {
      break;
    } else if (pid == -1 && errno == ECHILD) {
      break;
    }
  }
  signal(SIGCHLD, sighandle_SIGCHLD);
}


x86 assembly (clang, -O0):
<sighandle_SIGCHLD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0xc(%rbp),%rsi
call   1040 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1154 <sighandle_SIGCHLD+0x34>
jmp    1187 <sighandle_SIGCHLD+0x67>
cmpl   $0xffffffff,-0x8(%rbp)
jne    117d <sighandle_SIGCHLD+0x5d>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
jne    117d <sighandle_SIGCHLD+0x5d>
jmp    1187 <sighandle_SIGCHLD+0x67>
jmp    1182 <sighandle_SIGCHLD+0x62>
jmp    112b <sighandle_SIGCHLD+0xb>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rsi
call   1030 <signal@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sighandle_SIGCHLD>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2eb9(%rip),%r14
lea    0x4(%rsp),%rbx
mov    0x2e95(%rip),%r15
mov    0x2e86(%rip),%r12
jmp    1159 <sighandle_SIGCHLD+0x39>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15),%rax
cmp    (%r12),%rax
je     1172 <sighandle_SIGCHLD+0x52>
mov    (%r14),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1150 <sighandle_SIGCHLD+0x30>
test   %eax,%eax
jne    1159 <sighandle_SIGCHLD+0x39>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rsi
call   1030 <signal@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@errno = global i64 0
@ECHILD = global i64 0
@SIGCHLD = global i32 0
define void @sighandle_SIGCHLD(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %1, %20
%6 = load i32, i32* @WNOHANG
%7 = call i32 @waitpid(i32 -1, i32* %4, i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %5
br label %21
11: ; preds = %5
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, -1
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 %21
19: ; preds = %14, %11
br label %20
20: ; preds = %19
br label %5
21: ; preds = %18, %10
%22 = load i32, i32* @SIGCHLD
%23 = call i32 @signal(i32 %22, void (i32)* @sighandle_SIGCHLD)
ret void
}

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