#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 /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ msg;

/* Variables and functions */
scalar_t__ ECHILD;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ STDOUT_FILENO;
int WNOHANG;
int WUNTRACED;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char const *, int);
void sig_handler(int signal) {
  pid_t pid;
  const char msg[] = "Child Terminated";
  while ((pid = waitpid(-1, NULL, WNOHANG | WUNTRACED)) > 0)
    write(STDOUT_FILENO, msg, sizeof(msg) - 1);
  if (errno != ECHILD)
    exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<sig_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0xebe(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xebb(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xeb9(%rip),%al
mov    %al,-0x10(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%edx
mov    0x2e66(%rip),%rax
or     (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    11a2 <sig_handler+0x72>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
jmp    115a <sig_handler+0x2a>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%rax
je     11ca <sig_handler+0x9a>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_handler>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movaps 0xebe(%rip),%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
mov    0x2e8e(%rip),%r14
mov    0x2e77(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1040 <waitpid@plt>
test   %eax,%eax
jle    11a7 <sig_handler+0x77>
mov    0x2e7a(%rip),%r12
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    (%r12),%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1030 <write@plt>
mov    (%r15),%edx
or     (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1040 <waitpid@plt>
test   %eax,%eax
jg     1180 <sig_handler+0x50>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    11c9 <sig_handler+0x99>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@__const.sig_handler.msg = [17 x i8] c"Child Terminated\00"
@WNOHANG = global i32 0
@WUNTRACED = global i32 0
@STDOUT_FILENO = global i32 0
@errno = global i64 0
@ECHILD = global i64 0
@EXIT_FAILURE = global i32 0
define void @sig_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [17 x i8]
store i32 %0, i32* %2
%5 = bitcast [17 x i8]* %4 to i8*
br label %6
6: ; preds = %12, %1
%7 = load i32, i32* @WNOHANG
%8 = load i32, i32* @WUNTRACED
%9 = or i32 %7, %8
%10 = call i32 @waitpid(i32 -1, i32* null, i32 %9)
store i32 %10, i32* %3
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %6
%13 = load i32, i32* @STDOUT_FILENO
%14 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%15 = call i32 @write(i32 %13, i8* %14, i32 16)
br label %6
16: ; preds = %6
%17 = load i64, i64* @errno
%18 = load i64, i64* @ECHILD
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %16
ret void
}

LLVM IR (-O2):
@__const.sig_handler.msg = [17 x i8] c"Child Terminated\00"
@WNOHANG = global i32 0
@WUNTRACED = global i32 0
@STDOUT_FILENO = global i32 0
@errno = global i64 0
@ECHILD = global i64 0
@EXIT_FAILURE = global i32 0
define void @sig_handler(i32 %0) {
%2 = alloca [17 x i8]
%3 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @WNOHANG
%5 = load i32, i32* @WUNTRACED
%6 = or i32 %5, %4
%7 = tail call i32 @waitpid(i32 -1, i32* null, i32 %6)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %1, %9
%10 = load i32, i32* @STDOUT_FILENO
%11 = call i32 @write(i32 %10, i8* %3, i32 16)
%12 = load i32, i32* @WNOHANG
%13 = load i32, i32* @WUNTRACED
%14 = or i32 %13, %12
%15 = tail call i32 @waitpid(i32 -1, i32* null, i32 %14)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %9, label %17
17: ; preds = %9, %1
%18 = load i64, i64* @errno
%19 = load i64, i64* @ECHILD
%20 = icmp eq i64 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %17
ret void
}