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

/* Variables and functions */
int /*<<< orphan*/ err_sys(char *, int);
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
scalar_t__ sigemptyset(int /*<<< orphan*/ *);
void (*xsignal(int signo, void (*handler)(int)))(int) {
  struct sigaction old_sa, sa = {.sa_handler = handler, .sa_flags = 0};
  if (sigemptyset(&sa.sa_mask) < 0 || sigaction(signo, &sa, &old_sa) < 0)
    err_sys("xsignal(%d) failed", signo);
  return old_sa.sa_handler;
}


x86 assembly (clang, -O0):
<xsignal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   1050 <sigemptyset@plt>
cmp    $0x0,%rax
jl     1186 <xsignal+0x56>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    1195 <xsignal+0x65>
mov    -0x4(%rbp),%esi
lea    0xe70(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xsignal>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    %rsi,(%rsp)
lea    0x8(%rsp),%rdi
movq   $0x0,0x8(%rsp)
call   1050 <sigemptyset@plt>
test   %rax,%rax
js     1167 <xsignal+0x37>
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1030 <sigaction@plt>
test   %rax,%rax
jns    1175 <xsignal+0x45>
lea    0xe92(%rip),%rdi
mov    %ebx,%esi
call   1040 <err_sys@plt>
mov    0x10(%rsp),%rax
add    $0x20,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32, i32 }
@.str = [19 x i8] c"xsignal(%d) failed\00"
define void (i32)* @xsignal(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* %4, i64 0, i32 0
store void (i32)* %1, void (i32)** %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
store i32 0, i32* %9
%10 = call i64 @sigemptyset(i32* %8)
%11 = icmp slt i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = call i64 @sigaction(i32 %0, %struct.sigaction* %4, %struct.sigaction* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12, %2
%16 = call i32 @err_sys(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %17
17: ; preds = %15, %12
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
%19 = load void (i32)*, void (i32)** %18
ret void (i32)* %19
}