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

/* Variables and functions */
int /*<<< orphan*/ SIG_ERR;
int sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
__sighandler_t my_signal(int sig, __sighandler_t handler) {
  struct sigaction nact;
  struct sigaction oact;
  nact.sa_handler = handler;
  sigemptyset(&nact.sa_mask);
  nact.sa_flags = 0;
  if (sigaction(sig, &nact, &oact) == -1)
    return SIG_ERR;
  return oact.sa_handler;
}


x86 assembly (clang, -O0):
<my_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
call   1040 <sigemptyset@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    1173 <my_signal+0x53>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1179 <my_signal+0x59>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_signal>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebp
mov    %esi,0x8(%rsp)
lea    0x18(%rsp),%rdi
call   1040 <sigemptyset@plt>
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x20(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    115f <my_signal+0x3f>
mov    0x2e81(%rip),%rbx
mov    (%rbx),%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

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

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