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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ sigaction(int, struct sigaction *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int init_signal_handler(int signum, void (*handlerfunc)(int)) {
  struct sigaction act;
  memset(&act, 0, sizeof(struct sigaction));
  act.sa_flags = 0;
  sigemptyset(&act.sa_mask);
  act.sa_handler = handlerfunc;
  act.sa_restorer = NULL;
  if ((sigaction(signum, &act, NULL)) < 0) {
    perror("sigaction");
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<init_signal_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
movq   $0x0,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x10,%rdi
call   1050 <sigemptyset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    11bb <init_signal_handler+0x7b>
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11c7 <init_signal_handler+0x87>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_signal_handler>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rdi
call   1050 <sigemptyset@plt>
mov    %rbx,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
test   %rax,%rax
js     11a2 <init_signal_handler+0x62>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e2b(%rip),%rax
jmp    1197 <init_signal_handler+0x57>

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32*, i32, i64 }
@.str = [10 x i8] c"sigaction\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @init_signal_handler(i32 %0, void (i32)* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca void (i32)*
%6 = alloca %struct.sigaction
store i32 %0, i32* %4
store void (i32)* %1, void (i32)** %5
%7 = call i32 @memset(%struct.sigaction* %6, i32 0, i32 32)
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 3
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 2
%10 = call i32 @sigemptyset(i32* %9)
%11 = load void (i32)*, void (i32)** %5
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
store void (i32)* %11, void (i32)** %12
%13 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
store i32* null, i32** %13
%14 = load i32, i32* %4
%15 = call i64 @sigaction(i32 %14, %struct.sigaction* %6, i32* null)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
store i32 %19, i32* %3
br label %22
20: ; preds = %2
%21 = load i32, i32* @EXIT_SUCCESS
store i32 %21, i32* %3
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32*, i32, i64 }
@.str = [10 x i8] c"sigaction\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @init_signal_handler(i32 %0, void (i32)* %1) {
%3 = alloca %struct.sigaction
%4 = bitcast %struct.sigaction* %3 to i8*
%5 = call i32 @memset(%struct.sigaction* %3, i32 0, i32 32)
%6 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 3
store i64 0, i64* %6
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
%8 = call i32 @sigemptyset(i32* %7)
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store void (i32)* %1, void (i32)** %9
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i32* null, i32** %10
%11 = call i64 @sigaction(i32 %0, %struct.sigaction* %3, i32* null)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %2, %13
%16 = phi i32* [ @EXIT_FAILURE, %13 ], [ @EXIT_SUCCESS, %2 ]
%17 = load i32, i32* %16
ret i32 %17
}