#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);
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_mask;
};
typedef int /*<<< orphan*/ act;

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGIO;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ SIGUSR2;
void SIG_IGN(int);
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
int sigaction(int, struct sigaction *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int install_sighandler(int signal, int ignore, void (*handler)(int)) {
  struct sigaction act;
  memset(&act, 0, sizeof(act));
  do {
    sigemptyset((&act.sa_mask));
    sigaddset((&act.sa_mask), SIGCHLD);
    sigaddset((&act.sa_mask), SIGUSR1);
    sigaddset((&act.sa_mask), SIGUSR2);
    sigaddset((&act.sa_mask), SIGIO);
    sigaddset((&act.sa_mask), SIGPIPE);
    sigaddset((&act.sa_mask), SIGINT);
    sigaddset((&act.sa_mask), SIGTERM);
  } while (0);
  act.sa_flags = 0;
  act.sa_handler = ignore ? SIG_IGN : handler;
  return sigaction(signal, &act, NULL);
}


x86 assembly (clang, -O0):
<install_sighandler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
call   1050 <sigemptyset@plt>
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1060 <sigaddset@plt>
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1060 <sigaddset@plt>
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1060 <sigaddset@plt>
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   1060 <sigaddset@plt>
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
call   1060 <sigaddset@plt>
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
call   1060 <sigaddset@plt>
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1060 <sigaddset@plt>
movq   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     122b <install_sighandler+0xeb>
mov    0x2da6(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    1233 <install_sighandler+0xf3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<install_sighandler>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %edi,%r15d
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
lea    0x18(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <sigemptyset@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <sigaddset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <sigaddset@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <sigaddset@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <sigaddset@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <sigaddset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <sigaddset@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <sigaddset@plt>
movq   $0x0,0x10(%rsp)
test   %r12d,%r12d
je     1202 <install_sighandler+0xc2>
mov    0x2dc6(%rip),%rbx
mov    %rbx,0x8(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i64, i32 }
@SIGCHLD = global i32 0
@SIGUSR1 = global i32 0
@SIGUSR2 = global i32 0
@SIGIO = global i32 0
@SIGPIPE = global i32 0
@SIGINT = global i32 0
@SIGTERM = global i32 0
define i32 @install_sighandler(i32 %0, i32 %1, void (i32)* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca void (i32)*
%7 = alloca %struct.sigaction
store i32 %0, i32* %4
store i32 %1, i32* %5
store void (i32)* %2, void (i32)** %6
%8 = call i32 @memset(%struct.sigaction* %7, i32 0, i32 24)
br label %9
9: ; preds = %3
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%11 = call i32 @sigemptyset(i32* %10)
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%13 = load i32, i32* @SIGCHLD
%14 = call i32 @sigaddset(i32* %12, i32 %13)
%15 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%16 = load i32, i32* @SIGUSR1
%17 = call i32 @sigaddset(i32* %15, i32 %16)
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%19 = load i32, i32* @SIGUSR2
%20 = call i32 @sigaddset(i32* %18, i32 %19)
%21 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%22 = load i32, i32* @SIGIO
%23 = call i32 @sigaddset(i32* %21, i32 %22)
%24 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%25 = load i32, i32* @SIGPIPE
%26 = call i32 @sigaddset(i32* %24, i32 %25)
%27 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%28 = load i32, i32* @SIGINT
%29 = call i32 @sigaddset(i32* %27, i32 %28)
%30 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%31 = load i32, i32* @SIGTERM
%32 = call i32 @sigaddset(i32* %30, i32 %31)
br label %33
33: ; preds = %9
%34 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
store i64 0, i64* %34
%35 = load i32, i32* %5
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
br label %40
38: ; preds = %33
%39 = load void (i32)*, void (i32)** %6
br label %40
40: ; preds = %38, %37
%41 = phi void (i32)* [ @SIG_IGN, %37 ], [ %39, %38 ]
%42 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
store void (i32)* %41, void (i32)** %42
%43 = load i32, i32* %4
%44 = call i32 @sigaction(i32 %43, %struct.sigaction* %7, i32* null)
ret i32 %44
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i64, i32 }
@SIGCHLD = global i32 0
@SIGUSR1 = global i32 0
@SIGUSR2 = global i32 0
@SIGIO = global i32 0
@SIGPIPE = global i32 0
@SIGINT = global i32 0
@SIGTERM = global i32 0
define i32 @install_sighandler(i32 %0, i32 %1, void (i32)* %2) {
%4 = alloca %struct.sigaction
%5 = bitcast %struct.sigaction* %4 to i8*
%6 = call i32 @memset(%struct.sigaction* %4, i32 0, i32 24)
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
%8 = call i32 @sigemptyset(i32* %7)
%9 = load i32, i32* @SIGCHLD
%10 = call i32 @sigaddset(i32* %7, i32 %9)
%11 = load i32, i32* @SIGUSR1
%12 = call i32 @sigaddset(i32* %7, i32 %11)
%13 = load i32, i32* @SIGUSR2
%14 = call i32 @sigaddset(i32* %7, i32 %13)
%15 = load i32, i32* @SIGIO
%16 = call i32 @sigaddset(i32* %7, i32 %15)
%17 = load i32, i32* @SIGPIPE
%18 = call i32 @sigaddset(i32* %7, i32 %17)
%19 = load i32, i32* @SIGINT
%20 = call i32 @sigaddset(i32* %7, i32 %19)
%21 = load i32, i32* @SIGTERM
%22 = call i32 @sigaddset(i32* %7, i32 %21)
%23 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i64 0, i64* %23
%24 = icmp eq i32 %1, 0
%25 = select i1 %24, void (i32)* %2, void (i32)* @SIG_IGN
%26 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store void (i32)* %25, void (i32)** %26
%27 = call i32 @sigaction(i32 %0, %struct.sigaction* %4, i32* null)
ret i32 %27
}