#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_NOTICE;
#define SIGCHLD 129
#define SIGTERM 128
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ signal(int, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void signal_handler(int sig) {
  int status;
  switch (sig) {
  case SIGCHLD:
    while (waitpid(-1, &status, WNOHANG) > 0) {
    };
    return;
  case SIGTERM:
    signal(sig, SIG_IGN);
    syslog(LOG_NOTICE, "exit on SIGTERM");
    exit(EXIT_SUCCESS);
  }
}


x86 assembly (clang, -O0):
<signal_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
sub    $0x80,%eax
je     11a3 <signal_handler+0x63>
jmp    1161 <signal_handler+0x21>
mov    -0xc(%rbp),%eax
sub    $0x81,%eax
jne    11d7 <signal_handler+0x97>
jmp    1174 <signal_handler+0x34>
jmp    1179 <signal_handler+0x39>
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1050 <waitpid@plt>
cmp    $0x0,%eax
jle    119e <signal_handler+0x5e>
jmp    1179 <signal_handler+0x39>
jmp    11d7 <signal_handler+0x97>
mov    -0x4(%rbp),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
call   1030 <signal@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1040 <syslog@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal_handler>:
push   %r14
push   %rbx
push   %rax
cmp    $0x81,%edi
je     118a <signal_handler+0x4a>
cmp    $0x80,%edi
jne    11b4 <signal_handler+0x74>
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    $0x80,%edi
call   1030 <signal@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
call   1040 <syslog@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2e57(%rip),%r14
lea    0x4(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1050 <waitpid@plt>
test   %eax,%eax
jg     11a0 <signal_handler+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@SIG_IGN = global i32 0
@LOG_NOTICE = global i32 0
@.str = [16 x i8] c"exit on SIGTERM\00"
@EXIT_SUCCESS = global i32 0
define void @signal_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
switch i32 %4, label %20 [
i32 129, label %5
i32 128, label %12
]
5: ; preds = %1
br label %6
6: ; preds = %10, %5
%7 = load i32, i32* @WNOHANG
%8 = call i32 @waitpid(i32 -1, i32* %3, i32 %7)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %6
br label %6
11: ; preds = %6
br label %20
12: ; preds = %1
%13 = load i32, i32* %2
%14 = load i32, i32* @SIG_IGN
%15 = call i32 @signal(i32 %13, i32 %14)
%16 = load i32, i32* @LOG_NOTICE
%17 = call i32 @syslog(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_SUCCESS
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %11, %1
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@SIG_IGN = global i32 0
@LOG_NOTICE = global i32 0
@.str = [16 x i8] c"exit on SIGTERM\00"
@EXIT_SUCCESS = global i32 0
define void @signal_handler(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
switch i32 %0, label %15 [
i32 129, label %4
i32 128, label %8
]
4: ; preds = %1, %4
%5 = load i32, i32* @WNOHANG
%6 = call i32 @waitpid(i32 -1, i32* %2, i32 %5)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %4, label %15
8: ; preds = %1
%9 = load i32, i32* @SIG_IGN
%10 = tail call i32 @signal(i32 128, i32 %9)
%11 = load i32, i32* @LOG_NOTICE
%12 = tail call i32 @syslog(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_SUCCESS
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %4, %1
ret void
}