#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_flags;
  int /*<<< orphan*/ sa_mask;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ SA_RESTART;
void SIG_ERR(int);
int sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
void (*xsignal(int signum, void (*handler)(int)))(int) {
  struct sigaction sa = {0}, sa_old;
  sigemptyset(&sa.sa_mask);
  sa.sa_handler = handler;
  sa.sa_flags = SA_RESTART;
  if (sigaction(signum, &sa, &sa_old) == -1)
    return SIG_ERR;
  else
    return sa_old.sa_handler;
}


x86 assembly (clang, -O0):
<xsignal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rdi
add    $0xc,%rdi
call   1050 <sigemptyset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    1199 <xsignal+0x69>
mov    0x2e48(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a1 <xsignal+0x71>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xsignal>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xc(%rsp),%rdi
call   1040 <sigemptyset@plt>
mov    %rbx,(%rsp)
mov    0x2e98(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rsp,%rsi
lea    0x20(%rsp),%rdx
mov    %ebp,%edi
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    1173 <xsignal+0x53>
mov    0x2e67(%rip),%rax
jmp    1178 <xsignal+0x58>
mov    0x20(%rsp),%rax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32, i32, i32 }
@SA_RESTART = global i32 0
define void (i32)* @xsignal(i32 %0, void (i32)* %1) {
%3 = alloca void (i32)*
%4 = alloca i32
%5 = alloca void (i32)*
%6 = alloca %struct.sigaction
%7 = alloca %struct.sigaction
store i32 %0, i32* %4
store void (i32)* %1, void (i32)** %5
%8 = bitcast %struct.sigaction* %6 to i8*
%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 = load i32, i32* @SA_RESTART
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* %4
%16 = call i32 @sigaction(i32 %15, %struct.sigaction* %6, %struct.sigaction* %7)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %2
store void (i32)* @SIG_ERR, void (i32)** %3
br label %22
19: ; preds = %2
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%21 = load void (i32)*, void (i32)** %20
store void (i32)* %21, void (i32)** %3
br label %22
22: ; preds = %19, %18
%23 = load void (i32)*, void (i32)** %3
ret void (i32)* %23
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32, i32, i32 }
@SA_RESTART = global i32 0
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* %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 = load i32, i32* @SA_RESTART
%11 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = call i32 @sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%13 = icmp eq i32 %12, -1
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%15 = load void (i32)*, void (i32)** %14
%16 = select i1 %13, void (i32)* @SIG_ERR, void (i32)* %15
ret void (i32)* %16
}