#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 */
#define SIGINT 130
#define SIGQUIT 129
#define SIGTERM 128
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ shutdown();
char *signum_to_signame(int);
int /*<<< orphan*/ stdout;
char *strsignal(int);
void sig_handler(int sig) {
  const char *signame = signum_to_signame(sig);
  printf("Signal [%d] %s, %s\n", sig, signame, strsignal(sig));
  switch (sig) {
  case SIGTERM:
    shutdown();
    printf("Caught SIGTERM, exiting.\n");
    exit(0);
    break;
  case SIGQUIT:
    shutdown();
    printf("Caught SIGQUIT, exiting.\n");
    exit(0);
    break;
  case SIGINT:
    shutdown();
    printf("Caught SIGINT, exiting.\n");
    exit(0);
    break;
  }
  fflush(stdout);
}


x86 assembly (clang, -O0):
<sig_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <signum_to_signame@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    %edi,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
call   1060 <strsignal@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe65(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0x80,%eax
je     11de <sig_handler+0x7e>
jmp    11b8 <sig_handler+0x58>
mov    -0x14(%rbp),%eax
sub    $0x81,%eax
je     11fa <sig_handler+0x9a>
jmp    11cb <sig_handler+0x6b>
mov    -0x14(%rbp),%eax
sub    $0x82,%eax
je     1216 <sig_handler+0xb6>
jmp    1232 <sig_handler+0xd2>
mov    $0x0,%al
call   1030 <shutdown@plt>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1030 <shutdown@plt>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1030 <shutdown@plt>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_handler>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
call   1080 <signum_to_signame@plt>
mov    %rax,%r14
mov    %ebx,%edi
call   1070 <strsignal@plt>
lea    0xe74(%rip),%rdi
mov    %ebx,%esi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x82,%ebx
je     11c8 <sig_handler+0x58>
cmp    $0x81,%ebx
je     11d8 <sig_handler+0x68>
cmp    $0x80,%ebx
je     11e8 <sig_handler+0x78>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <fflush@plt>
xor    %eax,%eax
call   1040 <shutdown@plt>
lea    0xe3e(%rip),%rdi
jmp    11f6 <sig_handler+0x86>
xor    %eax,%eax
call   1040 <shutdown@plt>
lea    0xe46(%rip),%rdi
jmp    11f6 <sig_handler+0x86>
xor    %eax,%eax
call   1040 <shutdown@plt>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Signal [%d] %s, %s\0A\00"
@.str.1 = [26 x i8] c"Caught SIGTERM, exiting.\0A\00"
@.str.2 = [26 x i8] c"Caught SIGQUIT, exiting.\0A\00"
@.str.3 = [25 x i8] c"Caught SIGINT, exiting.\0A\00"
@stdout = global i32 0
define void @sig_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i8* @signum_to_signame(i32 %4)
store i8* %5, i8** %3
%6 = load i32, i32* %2
%7 = load i8*, i8** %3
%8 = load i32, i32* %2
%9 = call i8* @strsignal(i32 %8)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %6, i8* %7, i8* %9)
%11 = load i32, i32* %2
switch i32 %11, label %24 [
i32 128, label %12
i32 129, label %16
i32 130, label %20
]
12: ; preds = %1
%13 = call i32 (...) @shutdown()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %1
%17 = call i32 (...) @shutdown()
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %1
%21 = call i32 (...) @shutdown()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %1
%25 = load i32, i32* @stdout
%26 = call i32 @fflush(i32 %25)
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"Signal [%d] %s, %s\0A\00"
@stdout = global i32 0
@str = [24 x i8] c"Caught SIGINT, exiting.\00"
@str.4 = [25 x i8] c"Caught SIGQUIT, exiting.\00"
@str.5 = [25 x i8] c"Caught SIGTERM, exiting.\00"
define void @sig_handler(i32 %0) {
%2 = tail call i8* @signum_to_signame(i32 %0)
%3 = tail call i8* @strsignal(i32 %0)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %2, i8* %3)
switch i32 %0, label %17 [
i32 128, label %5
i32 129, label %9
i32 130, label %13
]
5: ; preds = %1
%6 = tail call i32 (...) @shutdown()
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %1
%10 = tail call i32 (...) @shutdown()
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.4, i64 0, i64 0))
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %1
%14 = tail call i32 (...) @shutdown()
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %1
%18 = load i32, i32* @stdout
%19 = tail call i32 @fflush(i32 %18)
ret void
}