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

/* Variables and functions */
#define SIGCHLD 128
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
void signal_handler(int signum) {
  int status;
  pid_t pid;
  switch (signum) {
  case SIGCHLD:
    while (0 < (pid = waitpid(0, &status, WNOHANG))) {
      fprintf(stderr, "\n[+] *****detect process %d exit*****\n", pid);
    }
  default:
    break;
  }
}


x86 assembly (clang, -O0):
<signal_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x80,%eax
jne    1188 <signal_handler+0x68>
jmp    113e <signal_handler+0x1e>
jmp    1143 <signal_handler+0x23>
mov    0x2e9e(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1040 <waitpid@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    1183 <signal_handler+0x63>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe87(%rip),%rsi
call   1030 <fprintf@plt>
jmp    1143 <signal_handler+0x23>
jmp    1188 <signal_handler+0x68>
jmp    118d <signal_handler+0x6d>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal_handler>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x80,%edi
jne    117f <signal_handler+0x5f>
mov    0x2eb1(%rip),%r15
mov    (%r15),%edx
lea    0x4(%rsp),%rsi
xor    %edi,%edi
call   1040 <waitpid@plt>
test   %eax,%eax
jle    117f <signal_handler+0x5f>
mov    0x2e87(%rip),%r12
lea    0xea8(%rip),%r14
lea    0x4(%rsp),%rbx
nopl   (%rax)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <fprintf@plt>
mov    (%r15),%edx
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <waitpid@plt>
test   %eax,%eax
jg     1160 <signal_handler+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"\0A[+] *****detect process %d exit*****\0A\00"
define void @signal_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
switch i32 %5, label %16 [
i32 128, label %6
]
6: ; preds = %1
br label %7
7: ; preds = %11, %6
%8 = load i32, i32* @WNOHANG
%9 = call i32 @waitpid(i32 0, i32* %3, i32 %8)
store i32 %9, i32* %4
%10 = icmp slt i32 0, %9
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = load i32, i32* %4
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %7
15: ; preds = %7
br label %16
16: ; preds = %1, %15
br label %17
17: ; preds = %16
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"\0A[+] *****detect process %d exit*****\0A\00"
define void @signal_handler(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = icmp eq i32 %0, 128
br i1 %4, label %5, label %16
5: ; preds = %1
%6 = load i32, i32* @WNOHANG
%7 = call i32 @waitpid(i32 0, i32* %2, i32 %6)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %5, %9
%10 = phi i32 [ %14, %9 ], [ %7, %5 ]
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %10)
%13 = load i32, i32* @WNOHANG
%14 = call i32 @waitpid(i32 0, i32* %2, i32 %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %9, label %16
16: ; preds = %9, %5, %1
ret void
}