#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 esh_command {
  int dummy;
};
typedef int /*<<< orphan*/ siginfo_t;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int SIGCHLD;
int WNOHANG;
int WUNTRACED;
int /*<<< orphan*/ assert(int);
struct esh_command *esh_get_cmd_from_pid(int /*<<< orphan*/);
int /*<<< orphan*/ esh_signal_cleanup(struct esh_command *, int);
int /*<<< orphan*/ waitpid(int, int *, int);
void esh_signal_handler_chld(int sig, siginfo_t *info, void *_ctxt) {
  assert(sig == SIGCHLD);
  int status;
  pid_t pid;
  while ((pid = waitpid(-1, &status, WUNTRACED | WNOHANG)) > 0) {
    struct esh_command *cmd = esh_get_cmd_from_pid(pid);
    if (cmd) {
      esh_signal_cleanup(cmd, status);
      ;
    }
  }
}


x86 assembly (clang, -O0):
<esh_signal_handler_chld>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
mov    0x2e6c(%rip),%rax
or     (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x1c(%rbp),%rsi
call   1060 <waitpid@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    11c0 <esh_signal_handler_chld+0x80>
mov    -0x20(%rbp),%edi
call   1040 <esh_get_cmd_from_pid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11bb <esh_signal_handler_chld+0x7b>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <esh_signal_cleanup@plt>
jmp    116c <esh_signal_handler_chld+0x2c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<esh_signal_handler_chld>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e90(%rip),%rcx
xor    %eax,%eax
cmp    %edi,(%rcx)
sete   %al
mov    %eax,%edi
call   1030 <assert@plt>
mov    0x2e6b(%rip),%r14
mov    0x2e7c(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
call   1060 <waitpid@plt>
test   %eax,%eax
jle    11c1 <esh_signal_handler_chld+0x81>
lea    0xc(%rsp),%rbx
jmp    11a7 <esh_signal_handler_chld+0x67>
nopl   0x0(%rax)
mov    (%r15),%edx
or     (%r14),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1060 <waitpid@plt>
test   %eax,%eax
jle    11c1 <esh_signal_handler_chld+0x81>
mov    %eax,%edi
call   1040 <esh_get_cmd_from_pid@plt>
test   %rax,%rax
je     1190 <esh_signal_handler_chld+0x50>
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   1050 <esh_signal_cleanup@plt>
jmp    1190 <esh_signal_handler_chld+0x50>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.esh_command = type { i32 }
@SIGCHLD = global i32 0
@WUNTRACED = global i32 0
@WNOHANG = global i32 0
define void @esh_signal_handler_chld(i32 %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.esh_command*
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%10 = load i32, i32* %4
%11 = load i32, i32* @SIGCHLD
%12 = icmp eq i32 %10, %11
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
br label %15
15: ; preds = %30, %3
%16 = load i32, i32* @WUNTRACED
%17 = load i32, i32* @WNOHANG
%18 = or i32 %16, %17
%19 = call i32 @waitpid(i32 -1, i32* %7, i32 %18)
store i32 %19, i32* %8
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %15
%22 = load i32, i32* %8
%23 = call %struct.esh_command* @esh_get_cmd_from_pid(i32 %22)
store %struct.esh_command* %23, %struct.esh_command** %9
%24 = load %struct.esh_command*, %struct.esh_command** %9
%25 = icmp ne %struct.esh_command* %24, null
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load %struct.esh_command*, %struct.esh_command** %9
%28 = load i32, i32* %7
%29 = call i32 @esh_signal_cleanup(%struct.esh_command* %27, i32 %28)
br label %30
30: ; preds = %26, %21
br label %15
31: ; preds = %15
ret void
}

LLVM IR (-O2):
%struct.esh_command = type { i32 }
@SIGCHLD = global i32 0
@WUNTRACED = global i32 0
@WNOHANG = global i32 0
define void @esh_signal_handler_chld(i32 %0, i32* nocapture readnone %1, i8* nocapture readnone %2) {
%4 = alloca i32
%5 = load i32, i32* @SIGCHLD
%6 = icmp eq i32 %5, %0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = bitcast i32* %4 to i8*
%10 = load i32, i32* @WUNTRACED
%11 = load i32, i32* @WNOHANG
%12 = or i32 %11, %10
%13 = call i32 @waitpid(i32 -1, i32* %4, i32 %12)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %3, %22
%16 = phi i32 [ %26, %22 ], [ %13, %3 ]
%17 = call %struct.esh_command* @esh_get_cmd_from_pid(i32 %16)
%18 = icmp eq %struct.esh_command* %17, null
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i32, i32* %4
%21 = call i32 @esh_signal_cleanup(%struct.esh_command* %17, i32 %20)
br label %22
22: ; preds = %19, %15
%23 = load i32, i32* @WUNTRACED
%24 = load i32, i32* @WNOHANG
%25 = or i32 %24, %23
%26 = call i32 @waitpid(i32 -1, i32* %4, i32 %25)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %15, label %28
28: ; preds = %22, %3
ret void
}