#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;
};
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ act;
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*/);
int /*<<< orphan*/ __sigaddset(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ __sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ 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;
  }
  memset(&act, 0, sizeof(act));
  act.sa_handler = disp;
  if (sigaction(sig, &act, &oact) < 0)
    return SIG_ERR;
  __sigemptyset(&set);
  __sigaddset(&set, sig);
  sigprocmask(SIG_UNBLOCK, &set, NULL);
  return oact.sa_handler;
}


x86 assembly (clang, -O0):
<sigset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
je     119f <sigset+0x3f>
cmpl   $0x1,-0xc(%rbp)
jl     119f <sigset+0x3f>
mov    -0xc(%rbp),%eax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%eax
jl     11c0 <sigset+0x60>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1238 <sigset+0xd8>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    1205 <sigset+0xa5>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1238 <sigset+0xd8>
lea    -0x2c(%rbp),%rdi
call   1070 <__sigemptyset@plt>
mov    -0xc(%rbp),%esi
lea    -0x2c(%rbp),%rdi
call   1080 <__sigaddset@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigprocmask@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigset>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e66(%rip),%r12
test   %edi,%edi
jle    11e7 <sigset+0x87>
mov    %rsi,%r14
cmp    %rsi,(%r12)
je     11e7 <sigset+0x87>
mov    %edi,%ebx
mov    0x2e40(%rip),%rax
cmp    %edi,(%rax)
jle    11e7 <sigset+0x87>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    %r14,0x8(%rsp)
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %r15,%rsi
call   1050 <sigaction@plt>
test   %rax,%rax
js     11f5 <sigset+0x95>
lea    0x14(%rsp),%r14
mov    %r14,%rdi
call   1070 <__sigemptyset@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1080 <__sigaddset@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <sigprocmask@plt>
mov    (%rsp),%rax
jmp    11f9 <sigset+0x99>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    (%r12),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sigaction = type { i64 }
@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 %40
23: ; preds = %15
%24 = call i32 @memset(%struct.sigaction* %6, i32 0, i32 8)
%25 = load i64, i64* %5
%26 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
store i64 %25, i64* %26
%27 = load i32, i32* %4
%28 = call i64 @sigaction(i32 %27, %struct.sigaction* %6, %struct.sigaction* %7)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = load i64, i64* @SIG_ERR
store i64 %31, i64* %3
br label %40
32: ; preds = %23
%33 = call i32 @__sigemptyset(i32* %8)
%34 = load i32, i32* %4
%35 = call i32 @__sigaddset(i32* %8, i32 %34)
%36 = load i32, i32* @SIG_UNBLOCK
%37 = call i32 @sigprocmask(i32 %36, i32* %8, i32* null)
%38 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%39 = load i64, i64* %38
store i64 %39, i64* %3
br label %40
40: ; preds = %32, %30, %19
%41 = load i64, i64* %3
ret i64 %41
}

LLVM IR (-O2):
%struct.sigaction = type { i64 }
@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 %34
20: ; preds = %2
%21 = call i32 @memset(%struct.sigaction* %3, i32 0, i32 8)
%22 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store i64 %1, i64* %22
%23 = call i64 @sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = load i64, i64* @SIG_ERR
br label %34
27: ; preds = %20
%28 = call i32 @__sigemptyset(i32* %5)
%29 = call i32 @__sigaddset(i32* %5, i32 %0)
%30 = load i32, i32* @SIG_UNBLOCK
%31 = call i32 @sigprocmask(i32 %30, i32* %5, i32* null)
%32 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%33 = load i64, i64* %32
br label %34
34: ; preds = %27, %25, %16
%35 = phi i64 [ %19, %16 ], [ %26, %25 ], [ %33, %27 ]
ret i64 %35
}