#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 */
struct rusage {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ exit(scalar_t__);
int wait3(int *, int /*<<< orphan*/, struct rusage *);
void sighandle_sigchld(int signo) {
  int status;
  pid_t pid;
  while (1) {
    status = 0;
    pid = wait3(&status, WNOHANG, (struct rusage *)NULL);
    if (pid == 0) {
      return;
    } else if (pid == -1) {
      return;
    } else {
      if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
        exit(WEXITSTATUS(status));
      }
    }
  }
}


x86 assembly (clang, -O0):
<sighandle_sigchld>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <wait3@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    117a <sighandle_sigchld+0x3a>
jmp    11cc <sighandle_sigchld+0x8c>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1189 <sighandle_sigchld+0x49>
jmp    11cc <sighandle_sigchld+0x8c>
mov    -0x8(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11bd <sighandle_sigchld+0x7d>
mov    -0x8(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     11bd <sighandle_sigchld+0x7d>
mov    -0x8(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %rax,%rdi
call   1060 <exit@plt>
jmp    11c2 <sighandle_sigchld+0x82>
jmp    11c7 <sighandle_sigchld+0x87>
jmp    114b <sighandle_sigchld+0xb>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sighandle_sigchld>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%r14
lea    0x4(%rsp),%rbx
movl   $0x0,0x4(%rsp)
mov    (%r14),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1030 <wait3@plt>
add    $0x1,%eax
cmp    $0x2,%eax
jb     119a <sighandle_sigchld+0x5a>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
je     1150 <sighandle_sigchld+0x10>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
test   %rax,%rax
je     1150 <sighandle_sigchld+0x10>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %rax,%rdi
call   1060 <exit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.rusage = type { i32 }
@WNOHANG = 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, %29
store i32 0, i32* %3
%6 = load i32, i32* @WNOHANG
%7 = call i32 @wait3(i32* %3, i32 %6, %struct.rusage* null)
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 %30
11: ; preds = %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %11
br label %30
15: ; preds = %11
%16 = load i32, i32* %3
%17 = call i64 @WIFEXITED(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i32, i32* %3
%21 = call i64 @WEXITSTATUS(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* %3
%25 = call i64 @WEXITSTATUS(i32 %24)
%26 = call i32 @exit(i64 %25)
unreachable
27: ; preds = %19, %15
br label %28
28: ; preds = %27
br label %29
29: ; preds = %28
br label %5
30: ; preds = %14, %10
ret void
}

LLVM IR (-O2):
%struct.rusage = type { i32 }
@WNOHANG = global i32 0
define void @sighandle_sigchld(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %15, %1
store i32 0, i32* %2
%5 = load i32, i32* @WNOHANG
%6 = call i32 @wait3(i32* %2, i32 %5, %struct.rusage* null)
switch i32 %6, label %7 [
i32 0, label %20
i32 -1, label %20
]
7: ; preds = %4
%8 = load i32, i32* %2
%9 = call i64 @WIFEXITED(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = load i32, i32* %2
%13 = call i64 @WEXITSTATUS(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11, %7
br label %4
16: ; preds = %11
%17 = load i32, i32* %2
%18 = call i64 @WEXITSTATUS(i32 %17)
%19 = call i32 @exit(i64 %18)
unreachable
20: ; preds = %4, %4
ret void
}