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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int NSIG;
int SA_RESTART;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ __sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
__sighandler_t __sysv_signal(int sig, __sighandler_t handler) {
  struct sigaction act, oact;
  if (handler == SIG_ERR || sig < 1 || sig >= NSIG) {
    __set_errno(EINVAL);
    return SIG_ERR;
  }
  act.sa_handler = handler;
  __sigemptyset(&act.sa_mask);
  act.sa_flags = (0 | 0 | 0) & ~SA_RESTART;
  if (sigaction(sig, &act, &oact) < 0)
    return SIG_ERR;
  return oact.sa_handler;
}


x86 assembly (clang, -O0):
<__sysv_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e8e(%rip),%rcx
cmp    (%rcx),%rax
je     116f <__sysv_signal+0x3f>
cmpl   $0x1,-0xc(%rbp)
jl     116f <__sysv_signal+0x3f>
mov    -0xc(%rbp),%eax
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%eax
jl     1190 <__sysv_signal+0x60>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11ec <__sysv_signal+0xbc>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
add    $0xc,%rdi
call   1050 <__sigemptyset@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    $0x0,%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1040 <sigaction@plt>
cmp    $0x0,%rax
jge    11e4 <__sysv_signal+0xb4>
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11ec <__sysv_signal+0xbc>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__sysv_signal>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2ea2(%rip),%r14
test   %edi,%edi
jle    1188 <__sysv_signal+0x58>
cmp    %rsi,(%r14)
je     1188 <__sysv_signal+0x58>
mov    %edi,%ebx
mov    0x2e80(%rip),%rax
cmp    %edi,(%rax)
jle    1188 <__sysv_signal+0x58>
mov    %rsi,0x8(%rsp)
lea    0x14(%rsp),%rdi
call   1050 <__sigemptyset@plt>
movl   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1040 <sigaction@plt>
test   %rax,%rax
js     1196 <__sysv_signal+0x66>
mov    0x18(%rsp),%rax
jmp    1199 <__sysv_signal+0x69>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    (%r14),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sigaction = type { i64, i32, i32 }
@SIG_ERR = global i64 0
@NSIG = global i32 0
@EINVAL = global i32 0
@SA_RESTART = global i32 0
define i64 @__sysv_signal(i32 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.sigaction
%7 = alloca %struct.sigaction
store i32 %0, i32* %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = load i64, i64* @SIG_ERR
%10 = icmp eq i64 %8, %9
br i1 %10, label %18, label %11
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 1
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = load i32, i32* %4
%16 = load i32, i32* @NSIG
%17 = icmp sge i32 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %14, %11, %2
%19 = load i32, i32* @EINVAL
%20 = call i32 @__set_errno(i32 %19)
%21 = load i64, i64* @SIG_ERR
store i64 %21, i64* %3
br label %39
22: ; preds = %14
%23 = load i64, i64* %5
%24 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
store i64 %23, i64* %24
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 2
%26 = call i32 @__sigemptyset(i32* %25)
%27 = load i32, i32* @SA_RESTART
%28 = xor i32 %27, -1
%29 = and i32 0, %28
%30 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* %4
%32 = call i64 @sigaction(i32 %31, %struct.sigaction* %6, %struct.sigaction* %7)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %22
%35 = load i64, i64* @SIG_ERR
store i64 %35, i64* %3
br label %39
36: ; preds = %22
%37 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%38 = load i64, i64* %37
store i64 %38, i64* %3
br label %39
39: ; preds = %36, %34, %18
%40 = load i64, i64* %3
ret i64 %40
}

LLVM IR (-O2):
%struct.sigaction = type { i64, i32, i32 }
@SIG_ERR = global i64 0
@NSIG = global i32 0
@EINVAL = global i32 0
@SA_RESTART = global i32 0
define i64 @__sysv_signal(i32 %0, i64 %1) {
%3 = alloca %struct.sigaction
%4 = alloca %struct.sigaction
%5 = bitcast %struct.sigaction* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = load i64, i64* @SIG_ERR
%8 = icmp ne i64 %7, %1
%9 = icmp sgt i32 %0, 0
%10 = and i1 %9, %8
%11 = load i32, i32* @NSIG
%12 = icmp sgt i32 %11, %0
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* @EINVAL
%16 = tail call i32 @__set_errno(i32 %15)
%17 = load i64, i64* @SIG_ERR
br label %30
18: ; preds = %2
%19 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store i64 %1, i64* %19
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
%21 = call i32 @__sigemptyset(i32* %20)
%22 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i32 0, i32* %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 = %18
%26 = load i64, i64* @SIG_ERR
br label %30
27: ; preds = %18
%28 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%29 = load i64, i64* %28
br label %30
30: ; preds = %27, %25, %14
%31 = phi i64 [ %17, %14 ], [ %26, %25 ], [ %29, %27 ]
ret i64 %31
}