#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 {
  scalar_t__ sa_handler;
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_mask;
};
typedef int /*<<< orphan*/ sigset_t;
typedef scalar_t__ __sighandler_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int NSIG;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ SIG_UNBLOCK;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
scalar_t__ __sigaddset(int /*<<< orphan*/ *, int);
scalar_t__ __sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
scalar_t__ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
__sighandler_t sigset(int sig, __sighandler_t disp) {
  struct sigaction act, oact;
  sigset_t set;
  if (disp == SIG_ERR || sig < 1 || sig >= NSIG) {
    __set_errno(EINVAL);
    return SIG_ERR;
  }
  act.sa_handler = disp;
  if (__sigemptyset(&act.sa_mask) < 0)
    return SIG_ERR;
  act.sa_flags = 0;
  if (sigaction(sig, &act, &oact) < 0)
    return SIG_ERR;
  if (__sigemptyset(&set) < 0)
    return SIG_ERR;
  if (__sigaddset(&set, sig) < 0)
    return SIG_ERR;
  if (sigprocmask(SIG_UNBLOCK, &set, NULL) < 0)
    return SIG_ERR;
  return oact.sa_handler;
}


x86 assembly (clang, -O0):
<sigset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
je     118f <sigset+0x3f>
cmpl   $0x1,-0xc(%rbp)
jl     118f <sigset+0x3f>
mov    -0xc(%rbp),%eax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%eax
jl     11b0 <sigset+0x60>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12a1 <sigset+0x151>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x10,%rdi
call   1060 <__sigemptyset@plt>
cmp    $0x0,%rax
jge    11e2 <sigset+0x92>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12a1 <sigset+0x151>
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    1217 <sigset+0xc7>
mov    0x2dcd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12a1 <sigset+0x151>
lea    -0x4c(%rbp),%rdi
call   1060 <__sigemptyset@plt>
cmp    $0x0,%rax
jge    123d <sigset+0xed>
mov    0x2da7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12a1 <sigset+0x151>
mov    -0xc(%rbp),%esi
lea    -0x4c(%rbp),%rdi
call   1070 <__sigaddset@plt>
cmp    $0x0,%rax
jge    1266 <sigset+0x116>
mov    0x2d7e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12a1 <sigset+0x151>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    -0x4c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigprocmask@plt>
cmp    $0x0,%rax
jge    1299 <sigset+0x149>
mov    0x2d4b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12a1 <sigset+0x151>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigset>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x2e7a(%rip),%r14
test   %edi,%edi
jle    11f0 <sigset+0xa0>
cmp    %rsi,(%r14)
je     11f0 <sigset+0xa0>
mov    %edi,%ebx
mov    0x2e50(%rip),%rax
cmp    %edi,(%rax)
jle    11f0 <sigset+0xa0>
mov    %rsi,0x8(%rsp)
lea    0x18(%rsp),%rdi
call   1060 <__sigemptyset@plt>
test   %rax,%rax
js     11fe <sigset+0xae>
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x20(%rsp),%rdx
mov    %ebx,%edi
call   1050 <sigaction@plt>
test   %rax,%rax
js     11fe <sigset+0xae>
lea    0x4(%rsp),%rdi
call   1060 <__sigemptyset@plt>
test   %rax,%rax
js     11fe <sigset+0xae>
lea    0x4(%rsp),%rdi
mov    %ebx,%esi
call   1070 <__sigaddset@plt>
test   %rax,%rax
js     11fe <sigset+0xae>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigprocmask@plt>
test   %rax,%rax
js     11fe <sigset+0xae>
mov    0x20(%rsp),%rax
jmp    1201 <sigset+0xb1>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    (%r14),%rax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sigaction = type { i64, i64, i32 }
@SIG_ERR = global i64 0
@NSIG = global i32 0
@EINVAL = global i32 0
@SIG_UNBLOCK = global i32 0
define i64 @sigset(i32 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.sigaction
%7 = alloca %struct.sigaction
%8 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
%9 = load i64, i64* %5
%10 = load i64, i64* @SIG_ERR
%11 = icmp eq i64 %9, %10
br i1 %11, label %19, label %12
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 1
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = load i32, i32* %4
%17 = load i32, i32* @NSIG
%18 = icmp sge i32 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %15, %12, %2
%20 = load i32, i32* @EINVAL
%21 = call i32 @__set_errno(i32 %20)
%22 = load i64, i64* @SIG_ERR
store i64 %22, i64* %3
br label %58
23: ; preds = %15
%24 = load i64, i64* %5
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
store i64 %24, i64* %25
%26 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 2
%27 = call i64 @__sigemptyset(i32* %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = load i64, i64* @SIG_ERR
store i64 %30, i64* %3
br label %58
31: ; preds = %23
%32 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
store i64 0, i64* %32
%33 = load i32, i32* %4
%34 = call i64 @sigaction(i32 %33, %struct.sigaction* %6, %struct.sigaction* %7)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = load i64, i64* @SIG_ERR
store i64 %37, i64* %3
br label %58
38: ; preds = %31
%39 = call i64 @__sigemptyset(i32* %8)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = load i64, i64* @SIG_ERR
store i64 %42, i64* %3
br label %58
43: ; preds = %38
%44 = load i32, i32* %4
%45 = call i64 @__sigaddset(i32* %8, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = load i64, i64* @SIG_ERR
store i64 %48, i64* %3
br label %58
49: ; preds = %43
%50 = load i32, i32* @SIG_UNBLOCK
%51 = call i64 @sigprocmask(i32 %50, i32* %8, i32* null)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = load i64, i64* @SIG_ERR
store i64 %54, i64* %3
br label %58
55: ; preds = %49
%56 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%57 = load i64, i64* %56
store i64 %57, i64* %3
br label %58
58: ; preds = %55, %53, %47, %41, %36, %29, %19
%59 = load i64, i64* %3
ret i64 %59
}

LLVM IR (-O2):
%struct.sigaction = type { i64, i64, i32 }
@SIG_ERR = global i64 0
@NSIG = global i32 0
@EINVAL = global i32 0
@SIG_UNBLOCK = global i32 0
define i64 @sigset(i32 %0, i64 %1) {
%3 = alloca %struct.sigaction
%4 = alloca %struct.sigaction
%5 = alloca i32
%6 = bitcast %struct.sigaction* %3 to i8*
%7 = bitcast %struct.sigaction* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = load i64, i64* @SIG_ERR
%10 = icmp ne i64 %9, %1
%11 = icmp sgt i32 %0, 0
%12 = and i1 %11, %10
%13 = load i32, i32* @NSIG
%14 = icmp sgt i32 %13, %0
%15 = select i1 %12, i1 %14, i1 false
br i1 %15, label %20, label %16
16: ; preds = %2
%17 = load i32, i32* @EINVAL
%18 = tail call i32 @__set_errno(i32 %17)
%19 = load i64, i64* @SIG_ERR
br label %52
20: ; preds = %2
%21 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store i64 %1, i64* %21
%22 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
%23 = call i64 @__sigemptyset(i32* %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = load i64, i64* @SIG_ERR
br label %52
27: ; preds = %20
%28 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i64 0, i64* %28
%29 = call i64 @sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i64, i64* @SIG_ERR
br label %52
33: ; preds = %27
%34 = call i64 @__sigemptyset(i32* %5)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = load i64, i64* @SIG_ERR
br label %52
38: ; preds = %33
%39 = call i64 @__sigaddset(i32* %5, i32 %0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = load i64, i64* @SIG_ERR
br label %52
43: ; preds = %38
%44 = load i32, i32* @SIG_UNBLOCK
%45 = call i64 @sigprocmask(i32 %44, i32* %5, i32* null)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = load i64, i64* @SIG_ERR
br label %52
49: ; preds = %43
%50 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%51 = load i64, i64* %50
br label %52
52: ; preds = %49, %47, %41, %36, %31, %25, %16
%53 = phi i64 [ %19, %16 ], [ %26, %25 ], [ %32, %31 ], [ %37, %36 ], [ %42, %41 ], [ %48, %47 ], [ %51, %49 ]
ret i64 %53
}