#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;
};

/* Variables and functions */
int /*<<< orphan*/ errno;
scalar_t__ sigaction(int, struct sigaction *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, int, int /*<<< orphan*/);
int setup_signal(int signum, void (*handler)(int)) {
  struct sigaction sigact;
  sigact.sa_flags = 0;
  sigact.sa_handler = handler;
  sigemptyset(&sigact.sa_mask);
  if (sigaction(signum, &sigact, NULL) < 0) {
    warn("could not set handler for signal %d: %s", signum, strerror(errno));
    return -1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<setup_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
call   1050 <sigemptyset@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
cmp    $0x0,%rax
jge    11b7 <setup_signal+0x77>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
lea    0xe5a(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <setup_signal+0x7e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_signal>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
movq   $0x0,0x10(%rsp)
mov    %rsi,(%rsp)
lea    0x8(%rsp),%rdi
call   1050 <sigemptyset@plt>
xor    %ebx,%ebx
mov    %rsp,%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <sigaction@plt>
test   %rax,%rax
jns    1195 <setup_signal+0x55>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe79(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1030 <warn@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32, i64 }
@.str = [40 x i8] c"could not set handler for signal %d: %s\00"
@errno = global i32 0
define i32 @setup_signal(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 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 2
store i64 0, i64* %7
%8 = load void (i32)*, void (i32)** %5
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
store void (i32)* %8, void (i32)** %9
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
%11 = call i32 @sigemptyset(i32* %10)
%12 = load i32, i32* %4
%13 = call i64 @sigaction(i32 %12, %struct.sigaction* %6, i32* null)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* %4
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 @warn(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %18)
store i32 -1, i32* %3
br label %21
20: ; preds = %2
store i32 0, i32* %3
br label %21
21: ; preds = %20, %15
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32, i64 }
@.str = [40 x i8] c"could not set handler for signal %d: %s\00"
@errno = global i32 0
define i32 @setup_signal(i32 %0, void (i32)* %1) {
%3 = alloca %struct.sigaction
%4 = bitcast %struct.sigaction* %3 to i8*
%5 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
store i64 0, i64* %5
%6 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store void (i32)* %1, void (i32)** %6
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
%8 = call i32 @sigemptyset(i32* %7)
%9 = call i64 @sigaction(i32 %0, %struct.sigaction* %3, i32* null)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = call i32 @warn(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %13)
br label %15
15: ; preds = %2, %11
%16 = phi i32 [ -1, %11 ], [ 0, %2 ]
ret i32 %16
}