#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 */
struct sigaction {
  void (*sa_handler)(int);
  int /*<<< orphan*/ sa_flags;
  int /*<<< orphan*/ sa_mask;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ SA_RESTART;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
scalar_t__ sigaction(int, struct sigaction *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strsignal(int);
void register_signal(int sig, void (*handler)(int)) {
  sigset_t block_mask;
  struct sigaction saction;
  sigfillset(&block_mask);
  saction.sa_handler = handler;
  saction.sa_mask = block_mask;
  saction.sa_flags = SA_RESTART;
  if (sigaction(sig, &saction, NULL) != 0) {
    fprintf(stderr,
            "Error: "
            "Failed to register signal handler for %s (%d)\n",
            strsignal(sig), sig);
  }
}


x86 assembly (clang, -O0):
<register_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <sigfillset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
je     11b5 <register_signal+0x75>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <strsignal@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
mov    -0x4(%rbp),%ecx
lea    0xe50(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<register_signal>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x4(%rsp),%rdi
call   1050 <sigfillset@plt>
mov    %rbx,0x8(%rsp)
mov    0x4(%rsp),%eax
mov    %eax,0x14(%rsp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <sigaction@plt>
test   %rax,%rax
je     11a5 <register_signal+0x65>
mov    0x2e4f(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1060 <strsignal@plt>
lea    0xe67(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
mov    %ebp,%ecx
call   1040 <fprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32, i32 }
@SA_RESTART = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"Error: Failed to register signal handler for %s (%d)\0A\00"
define void @register_signal(i32 %0, void (i32)* %1) {
%3 = alloca i32
%4 = alloca void (i32)*
%5 = alloca i32
%6 = alloca %struct.sigaction
store i32 %0, i32* %3
store void (i32)* %1, void (i32)** %4
%7 = call i32 @sigfillset(i32* %5)
%8 = load void (i32)*, void (i32)** %4
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
store void (i32)* %8, void (i32)** %9
%10 = load i32, i32* %5
%11 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @SA_RESTART
%13 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* %3
%15 = call i64 @sigaction(i32 %14, %struct.sigaction* %6, i32* null)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* %3
%20 = call i8* @strsignal(i32 %19)
%21 = load i32, i32* %3
%22 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21)
br label %23
23: ; preds = %17, %2
ret void
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32, i32 }
@SA_RESTART = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"Error: Failed to register signal handler for %s (%d)\0A\00"
define void @register_signal(i32 %0, void (i32)* %1) {
%3 = alloca i32
%4 = alloca %struct.sigaction
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = call i32 @sigfillset(i32* %3)
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store void (i32)* %1, void (i32)** %8
%9 = load i32, i32* %3
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @SA_RESTART
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i64 @sigaction(i32 %0, %struct.sigaction* %4, i32* null)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i8* @strsignal(i32 %0)
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %0)
br label %19
19: ; preds = %15, %2
ret void
}