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

/* Variables and functions */
scalar_t__ ECHILD;
int WNOHANG;
int WUNTRACED;
scalar_t__ errno;
int /*<<< orphan*/ error_msg(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int);
void sig_handler(int signal) {
  pid_t pid;
  while ((pid = waitpid(-1, NULL, WNOHANG | WUNTRACED)) > 0) {
    printf("Child Process [%d]: Terminated", (int)pid);
  }
  if (errno != ECHILD)
    error_msg("waitpid() error");
}


x86 assembly (clang, -O0):
<sig_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%edx
mov    0x2e8d(%rip),%rax
or     (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <waitpid@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jle    1180 <sig_handler+0x50>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
jmp    113b <sig_handler+0xb>
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
je     11a6 <sig_handler+0x76>
lea    0xe7e(%rip),%rdi
call   1030 <error_msg@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_handler>:
push   %r15
push   %r14
push   %rbx
mov    0x2eac(%rip),%r14
mov    0x2e95(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1050 <waitpid@plt>
test   %rax,%rax
jle    1191 <sig_handler+0x61>
lea    0xe9f(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    (%r15),%edx
or     (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1050 <waitpid@plt>
test   %rax,%rax
jg     1170 <sig_handler+0x40>
mov    0x2e38(%rip),%rax
mov    (%rax),%rax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <sig_handler+0x7d>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe6b(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <error_msg@plt>

LLVM IR (-O0):
@WNOHANG = global i32 0
@WUNTRACED = global i32 0
@.str = [31 x i8] c"Child Process [%d]: Terminated\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.1 = [16 x i8] c"waitpid() error\00"
define void @sig_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i64
store i32 %0, i32* %2
br label %4
4: ; preds = %10, %1
%5 = load i32, i32* @WNOHANG
%6 = load i32, i32* @WUNTRACED
%7 = or i32 %5, %6
%8 = call i64 @waitpid(i32 -1, i32* null, i32 %7)
store i64 %8, i64* %3
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = load i64, i64* %3
%12 = trunc i64 %11 to i32
%13 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %4
14: ; preds = %4
%15 = load i64, i64* @errno
%16 = load i64, i64* @ECHILD
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @error_msg(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@WUNTRACED = global i32 0
@.str = [31 x i8] c"Child Process [%d]: Terminated\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.1 = [16 x i8] c"waitpid() error\00"
define void @sig_handler(i32 %0) {
%2 = load i32, i32* @WNOHANG
%3 = load i32, i32* @WUNTRACED
%4 = or i32 %3, %2
%5 = tail call i64 @waitpid(i32 -1, i32* null, i32 %4)
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %1, %7
%8 = phi i64 [ %14, %7 ], [ %5, %1 ]
%9 = trunc i64 %8 to i32
%10 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @WNOHANG
%12 = load i32, i32* @WUNTRACED
%13 = or i32 %12, %11
%14 = tail call i64 @waitpid(i32 -1, i32* null, i32 %13)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %7, label %16
16: ; preds = %7, %1
%17 = load i64, i64* @errno
%18 = load i64, i64* @ECHILD
%19 = icmp eq i64 %17, %18
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 @error_msg(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
ret void
}