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

/* Variables and functions */
scalar_t__ SIG_ERR;
int /*<<< orphan*/ sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
__sighandler_t _signal(int signum, __sighandler_t handler, int flags) {
  struct sigaction sa;
  __sighandler_t result = SIG_ERR;
  sigemptyset(&sa.sa_mask);
  sa.sa_handler = handler;
  sa.sa_flags = flags;
  if (!sigaction(signum, &sa, &sa))
    result = (__sighandler_t)sa.sa_handler;
  return result;
}


x86 assembly (clang, -O0):
<_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x10,%rdi
call   1040 <sigemptyset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rdx
mov    %rdx,%rsi
call   1030 <sigaction@plt>
cmp    $0x0,%eax
jne    117b <_signal+0x5b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_signal>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2ea7(%rip),%rax
mov    (%rax),%r14
lea    0x10(%rsp),%rdi
call   1040 <sigemptyset@plt>
mov    %rbx,0x8(%rsp)
mov    %ebp,(%rsp)
mov    %rsp,%rdx
mov    %r15d,%edi
mov    %rdx,%rsi
call   1030 <sigaction@plt>
test   %eax,%eax
jne    1165 <_signal+0x45>
mov    0x8(%rsp),%r14
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { i32, i64, i32 }
@SIG_ERR = global i64 0
define i64 @_signal(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.sigaction
%8 = alloca i64
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%9 = load i64, i64* @SIG_ERR
store i64 %9, i64* %8
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%11 = call i32 @sigemptyset(i32* %10)
%12 = load i64, i64* %5
%13 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
store i64 %12, i64* %13
%14 = load i32, i32* %6
%15 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %4
%17 = call i32 @sigaction(i32 %16, %struct.sigaction* %7, %struct.sigaction* %7)
%18 = icmp ne i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %3
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
%21 = load i64, i64* %20
store i64 %21, i64* %8
br label %22
22: ; preds = %19, %3
%23 = load i64, i64* %8
ret i64 %23
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i64, i32 }
@SIG_ERR = global i64 0
define i64 @_signal(i32 %0, i64 %1, i32 %2) {
%4 = alloca %struct.sigaction
%5 = bitcast %struct.sigaction* %4 to i8*
%6 = load i64, i64* @SIG_ERR
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
%8 = call i32 @sigemptyset(i32* %7)
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i64 %1, i64* %9
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store i32 %2, i32* %10
%11 = call i32 @sigaction(i32 %0, %struct.sigaction* %4, %struct.sigaction* %4)
%12 = icmp eq i32 %11, 0
%13 = load i64, i64* %9
%14 = select i1 %12, i64 %13, i64 %6
ret i64 %14
}