#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 */
int ECHILD;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ STDOUT_FILENO;
int WNOHANG;
int WUNTRACED;
int 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) {
  int sverrno = errno;
  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);
  errno = sverrno;
}


x86 assembly (clang, -O0):
<sig_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0xeb2(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xeaf(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xead(%rip),%al
mov    %al,-0x10(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
mov    0x2e5a(%rip),%rax
or     (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    11ae <sig_handler+0x7e>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
jmp    1166 <sig_handler+0x36>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%eax
je     11d4 <sig_handler+0xa4>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%ecx
mov    0x2dea(%rip),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_handler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e83(%rip),%r14
mov    (%r14),%r15d
movaps 0xeb1(%rip),%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
mov    0x2e81(%rip),%r12
mov    0x2e6a(%rip),%r13
mov    0x0(%r13),%edx
or     (%r12),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1040 <waitpid@plt>
test   %eax,%eax
jle    11b8 <sig_handler+0x88>
mov    0x2e6b(%rip),%rbp
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1030 <write@plt>
mov    0x0(%r13),%edx
or     (%r12),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1040 <waitpid@plt>
test   %eax,%eax
jg     1190 <sig_handler+0x60>
mov    (%r14),%eax
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%eax
jne    11d8 <sig_handler+0xa8>
mov    %r15d,(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

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

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