#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_mask;
  scalar_t__ sa_flags;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ SIG_UNBLOCK;
int /*<<< orphan*/ sigaction(int, struct sigaction *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int install_handler(int signum, void (*handler)(int)) {
  struct sigaction action;
  sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask, signum);
  sigprocmask(SIG_UNBLOCK, &mask, NULL);
  action.sa_handler = handler;
  action.sa_flags = 0;
  sigemptyset(&action.sa_mask);
  sigaction(signum, &action, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<install_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x2c(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    -0x4(%rbp),%esi
lea    -0x2c(%rbp),%rdi
call   1060 <sigaddset@plt>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
call   1050 <sigemptyset@plt>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<install_handler>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sigemptyset@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <sigaddset@plt>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    %r14,0x8(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rdi
call   1050 <sigemptyset@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <sigaction@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32, i64 }
@SIG_UNBLOCK = global i32 0
define i32 @install_handler(i32 %0, void (i32)* %1) {
%3 = alloca i32
%4 = alloca void (i32)*
%5 = alloca %struct.sigaction
%6 = alloca i32
store i32 %0, i32* %3
store void (i32)* %1, void (i32)** %4
%7 = call i32 @sigemptyset(i32* %6)
%8 = load i32, i32* %3
%9 = call i32 @sigaddset(i32* %6, i32 %8)
%10 = load i32, i32* @SIG_UNBLOCK
%11 = call i32 @sigprocmask(i32 %10, i32* %6, i32* null)
%12 = load void (i32)*, void (i32)** %4
%13 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 0
store void (i32)* %12, void (i32)** %13
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 2
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 1
%16 = call i32 @sigemptyset(i32* %15)
%17 = load i32, i32* %3
%18 = call i32 @sigaction(i32 %17, %struct.sigaction* %5, i32* null)
ret i32 0
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32, i64 }
@SIG_UNBLOCK = global i32 0
define i32 @install_handler(i32 %0, void (i32)* %1) {
%3 = alloca %struct.sigaction
%4 = alloca i32
%5 = bitcast %struct.sigaction* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = call i32 @sigemptyset(i32* %4)
%8 = call i32 @sigaddset(i32* %4, i32 %0)
%9 = load i32, i32* @SIG_UNBLOCK
%10 = call i32 @sigprocmask(i32 %9, i32* %4, i32* null)
%11 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store void (i32)* %1, void (i32)** %11
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
store i64 0, i64* %12
%13 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
%14 = call i32 @sigemptyset(i32* %13)
%15 = call i32 @sigaction(i32 %0, %struct.sigaction* %3, i32* null)
ret i32 0
}