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

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


x86 assembly (clang, -O0):
<__signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
call   1040 <sigemptyset@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rdx
mov    %rdx,%rsi
call   1030 <sigaction@plt>
cmp    $0x0,%rax
je     1174 <__signal+0x54>
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    117a <__signal+0x5a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__signal>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %esi,0x4(%rsp)
mov    %edx,(%rsp)
lea    0x8(%rsp),%rdi
call   1040 <sigemptyset@plt>
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rdx,%rsi
call   1030 <sigaction@plt>
test   %rax,%rax
je     1153 <__signal+0x33>
mov    0x2e8f(%rip),%rax
jmp    1158 <__signal+0x38>
lea    0x4(%rsp),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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