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

/* Variables and functions */
#define SIGHUP 130
#define SIGINT 129
#define SIGUSR1 128
int /*<<< orphan*/ do_sleep(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sigismember(int /*<<< orphan*/ *, int const);
int /*<<< orphan*/ sigpending(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void handle_signal(int signal) {
  const char *signal_name;
  sigset_t pending;
  switch (signal) {
  case SIGHUP:
    signal_name = "SIGHUP";
    break;
  case SIGUSR1:
    signal_name = "SIGUSR1";
    break;
  case SIGINT:
    printf("Caught SIGINT, exiting now\n");
    exit(0);
  default:
    fprintf(stderr, "Caught wrong signal: %d\n", signal);
    return;
  }
  printf("Caught %s, sleeping for ~3 seconds\n"
         "Try sending another SIGHUP / SIGINT / SIGALRM "
         "(or more) meanwhile\n",
         signal_name);
  do_sleep(3);
  printf("Done sleeping for %s\n", signal_name);
  sigpending(&pending);
  if (sigismember(&pending, SIGHUP)) {
    printf("A SIGHUP is waiting\n");
  }
  if (sigismember(&pending, SIGUSR1)) {
    printf("A SIGUSR1 is waiting\n");
  }
  printf("Done handling %s\n\n", signal_name);
}


x86 assembly (clang, -O0):
<handle_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0x80,%eax
je     11b7 <handle_signal+0x57>
jmp    1181 <handle_signal+0x21>
mov    -0x18(%rbp),%eax
sub    $0x81,%eax
je     11c7 <handle_signal+0x67>
jmp    1194 <handle_signal+0x34>
mov    -0x18(%rbp),%eax
sub    $0x82,%eax
jne    11dc <handle_signal+0x7c>
jmp    11a7 <handle_signal+0x47>
lea    0xe52(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    11f9 <handle_signal+0x99>
lea    0xe49(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    11f9 <handle_signal+0x99>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe3c(%rip),%rsi
call   1040 <fprintf@plt>
jmp    128e <handle_signal+0x12e>
mov    -0x10(%rbp),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x3,%edi
call   1050 <do_sleep@plt>
mov    -0x10(%rbp),%rsi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1060 <sigpending@plt>
lea    -0x14(%rbp),%rdi
mov    $0x82,%esi
call   1070 <sigismember@plt>
cmp    $0x0,%rax
je     1256 <handle_signal+0xf6>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x14(%rbp),%rdi
mov    $0x80,%esi
call   1070 <sigismember@plt>
cmp    $0x0,%rax
je     127c <handle_signal+0x11c>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_signal>:
push   %r14
push   %rbx
push   %rax
cmp    $0x80,%edi
je     11ae <handle_signal+0x3e>
mov    %edi,%edx
cmp    $0x82,%edi
je     11b7 <handle_signal+0x47>
cmp    $0x81,%edx
je     124e <handle_signal+0xde>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <fprintf@plt>
lea    0xe52(%rip),%r14
jmp    11be <handle_signal+0x4e>
lea    0xe42(%rip),%r14
lea    0xe63(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3,%edi
call   1060 <do_sleep@plt>
lea    0xeae(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sigpending@plt>
mov    %rbx,%rdi
mov    $0x82,%esi
call   1080 <sigismember@plt>
test   %rax,%rax
je     1215 <handle_signal+0xa5>
lea    0xed7(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
mov    $0x80,%esi
call   1080 <sigismember@plt>
test   %rax,%rax
je     1235 <handle_signal+0xc5>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
lea    0xe68(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"SIGHUP\00"
@.str.1 = [8 x i8] c"SIGUSR1\00"
@.str.2 = [28 x i8] c"Caught SIGINT, exiting now\0A\00"
@stderr = global i32 0
@.str.3 = [25 x i8] c"Caught wrong signal: %d\0A\00"
@.str.4 = [102 x i8] c"Caught %s, sleeping for ~3 seconds\0ATry sending another SIGHUP / SIGINT / SIGALRM (or more) meanwhile\0A\00"
@.str.5 = [22 x i8] c"Done sleeping for %s\0A\00"
@.str.6 = [21 x i8] c"A SIGHUP is waiting\0A\00"
@.str.7 = [22 x i8] c"A SIGUSR1 is waiting\0A\00"
@.str.8 = [19 x i8] c"Done handling %s\0A\0A\00"
define void @handle_signal(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
switch i32 %5, label %11 [
i32 130, label %6
i32 128, label %7
i32 129, label %8
]
6: ; preds = %1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %3
br label %15
7: ; preds = %1
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %15
8: ; preds = %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* %2
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %13)
br label %34
15: ; preds = %7, %6
%16 = load i8*, i8** %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([102 x i8], [102 x i8]* @.str.4, i64 0, i64 0), i8* %16)
%18 = call i32 @do_sleep(i32 3)
%19 = load i8*, i8** %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %19)
%21 = call i32 @sigpending(i32* %4)
%22 = call i64 @sigismember(i32* %4, i32 130)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %26
26: ; preds = %24, %15
%27 = call i64 @sigismember(i32* %4, i32 128)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
%32 = load i8*, i8** %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %31, %11
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"SIGHUP\00"
@.str.1 = [8 x i8] c"SIGUSR1\00"
@stderr = global i32 0
@.str.3 = [25 x i8] c"Caught wrong signal: %d\0A\00"
@.str.4 = [102 x i8] c"Caught %s, sleeping for ~3 seconds\0ATry sending another SIGHUP / SIGINT / SIGALRM (or more) meanwhile\0A\00"
@.str.5 = [22 x i8] c"Done sleeping for %s\0A\00"
@.str.8 = [19 x i8] c"Done handling %s\0A\0A\00"
@str = [27 x i8] c"Caught SIGINT, exiting now\00"
@str.9 = [21 x i8] c"A SIGUSR1 is waiting\00"
@str.10 = [20 x i8] c"A SIGHUP is waiting\00"
define void @handle_signal(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
switch i32 %0, label %8 [
i32 130, label %11
i32 128, label %4
i32 129, label %5
]
4: ; preds = %1
br label %11
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %28
11: ; preds = %1, %4
%12 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %1 ]
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([102 x i8], [102 x i8]* @.str.4, i64 0, i64 0), i8* %12)
%14 = tail call i32 @do_sleep(i32 3)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %12)
%16 = call i32 @sigpending(i32* %2)
%17 = call i64 @sigismember(i32* %2, i32 130)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %11
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0))
br label %21
21: ; preds = %19, %11
%22 = call i64 @sigismember(i32* %2, i32 128)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i8* %12)
br label %28
28: ; preds = %26, %8
ret void
}