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

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ errno;
int sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int set_signal(int sig, void (*handler)(int)) {
  struct sigaction act;
  struct sigaction oact;
  int rc;
  sigemptyset(&act.sa_mask);
  act.sa_handler = handler;
  act.sa_flags = 0;
  rc = sigaction(sig, &act, &oact);
  if (rc == -1)
    syslog(LOG_ERR, "sigaction() = %s", strerror(errno));
  return rc;
}


x86 assembly (clang, -O0):
<set_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
call   1040 <sigemptyset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1030 <sigaction@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    11b4 <set_signal+0x74>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %eax,%edx
lea    0xe51(%rip),%rsi
call   1050 <syslog@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_signal>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x18(%rsp),%rdi
call   1040 <sigemptyset@plt>
mov    %rbx,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x20(%rsp),%rdx
mov    %ebp,%edi
call   1030 <sigaction@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11a2 <set_signal+0x62>
mov    0x2e4e(%rip),%rax
mov    (%rax),%ebp
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe67(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1050 <syslog@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i64, i32 }
@LOG_ERR = global i32 0
@.str = [17 x i8] c"sigaction() = %s\00"
@errno = global i32 0
define i32 @set_signal(i32 %0, void (i32)* %1) {
%3 = alloca %struct.sigaction
%4 = alloca %struct.sigaction
%5 = bitcast %struct.sigaction* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%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 i64 0, i64* %10
%11 = call i32 @sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @LOG_ERR
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @syslog(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %13, %2
ret i32 %11
}