#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*/);
scalar_t__ __sigaction(int, struct sigaction *, struct sigaction *);
scalar_t__ __sigemptyset(int /*<<< orphan*/ *);
__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;
  if (__sigemptyset(&act.sa_mask) < 0)
    return SIG_ERR;
  act.sa_flags = 0 | 0 | 0;
  act.sa_flags &= ~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    1210 <__sysv_signal+0xe0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
add    $0xc,%rdi
call   1040 <__sigemptyset@plt>
cmp    $0x0,%rax
jge    11c2 <__sysv_signal+0x92>
mov    0x2e22(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1210 <__sysv_signal+0xe0>
movl   $0x0,-0x20(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1050 <__sigaction@plt>
cmp    $0x0,%rax
jge    1208 <__sysv_signal+0xd8>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1210 <__sysv_signal+0xe0>
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    118d <__sysv_signal+0x5d>
cmp    %rsi,(%r14)
je     118d <__sysv_signal+0x5d>
mov    %edi,%ebx
mov    0x2e80(%rip),%rax
cmp    %edi,(%rax)
jle    118d <__sysv_signal+0x5d>
mov    %rsi,0x8(%rsp)
lea    0x14(%rsp),%rdi
call   1040 <__sigemptyset@plt>
test   %rax,%rax
js     119b <__sysv_signal+0x6b>
movl   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1050 <__sigaction@plt>
test   %rax,%rax
js     119b <__sysv_signal+0x6b>
mov    0x18(%rsp),%rax
jmp    119e <__sysv_signal+0x6e>
mov    0x2e44(%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 %45
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 i64 @__sigemptyset(i32* %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i64, i64* @SIG_ERR
store i64 %29, i64* %3
br label %45
30: ; preds = %22
%31 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
store i32 0, i32* %31
%32 = load i32, i32* @SA_RESTART
%33 = xor i32 %32, -1
%34 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
%35 = load i32, i32* %34
%36 = and i32 %35, %33
store i32 %36, i32* %34
%37 = load i32, i32* %4
%38 = call i64 @__sigaction(i32 %37, %struct.sigaction* %6, %struct.sigaction* %7)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %30
%41 = load i64, i64* @SIG_ERR
store i64 %41, i64* %3
br label %45
42: ; preds = %30
%43 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%44 = load i64, i64* %43
store i64 %44, i64* %3
br label %45
45: ; preds = %42, %40, %28, %18
%46 = load i64, i64* %3
ret i64 %46
}

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 %34
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 i64 @__sigemptyset(i32* %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = load i64, i64* @SIG_ERR
br label %34
25: ; preds = %18
%26 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i32 0, i32* %26
%27 = call i64 @__sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i64, i64* @SIG_ERR
br label %34
31: ; preds = %25
%32 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%33 = load i64, i64* %32
br label %34
34: ; preds = %31, %29, %23, %14
%35 = phi i64 [ %17, %14 ], [ %24, %23 ], [ %30, %29 ], [ %33, %31 ]
ret i64 %35
}