#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;
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
__sighandler_t my_signal(int sig, __sighandler_t handler) {
  struct sigaction act;
  struct sigaction oldact;
  act.sa_handler = handler;
  sigemptyset(&act.sa_mask);
  act.sa_flags = 0;
  if (sigaction(sig, &act, &oldact) < 0)
    return SIG_ERR;
  return oldact.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    $0x0,%rax
jge    1174 <my_signal+0x54>
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    117a <my_signal+0x5a>
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>
test   %rax,%rax
jns    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 i64 @sigaction(i32 %13, %struct.sigaction* %6, %struct.sigaction* %7)
%15 = icmp slt i64 %14, 0
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 i64 @sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%12 = icmp slt i64 %11, 0
%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
}