#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 {
  void (*sa_handler)(int);
  int /*<<< orphan*/ sa_flags;
  int /*<<< orphan*/ sa_mask;
};

/* Variables and functions */
int /*<<< orphan*/ SA_NODEFER;
int /*<<< orphan*/ SA_RESETHAND;
int /*<<< orphan*/ SA_RESTART;
int SIGALRM;
void SIG_ERR(int);
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
void (*my_signal(int signo, void (*handler)(int)))(int) {
  struct sigaction old_handler, new_handler;
  new_handler.sa_handler = handler;
  sigemptyset(&new_handler.sa_mask);
  new_handler.sa_flags = 0;
  if (signo == SIGALRM) {
  } else {
    new_handler.sa_flags |= SA_RESTART;
  }
  new_handler.sa_flags |= SA_RESETHAND;
  new_handler.sa_flags |= SA_NODEFER;
  if (sigaction(signo, &new_handler, &old_handler) < 0) {
    return SIG_ERR;
  }
  return old_handler.sa_handler;
}


x86 assembly (clang, -O0):
<my_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x38(%rbp),%rdi
add    $0xc,%rdi
call   1040 <sigemptyset@plt>
movl   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e93(%rip),%rcx
cmp    (%rcx),%eax
jne    1162 <my_signal+0x42>
jmp    1171 <my_signal+0x51>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    11b9 <my_signal+0x99>
mov    0x2e18(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11c1 <my_signal+0xa1>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_signal>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    %rsi,(%rsp)
lea    0xc(%rsp),%rdi
call   1040 <sigemptyset@plt>
mov    0x2eac(%rip),%rcx
xor    %eax,%eax
cmp    %ebx,(%rcx)
je     114b <my_signal+0x2b>
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
mov    0x2e86(%rip),%rcx
or     (%rcx),%eax
mov    0x2e65(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1030 <sigaction@plt>
test   %rax,%rax
jns    117e <my_signal+0x5e>
mov    0x2e4c(%rip),%rax
jmp    1183 <my_signal+0x63>
mov    0x10(%rsp),%rax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32, i32 }
@SIGALRM = global i32 0
@SA_RESTART = global i32 0
@SA_RESETHAND = global i32 0
@SA_NODEFER = global i32 0
define void (i32)* @my_signal(i32 %0, void (i32)* %1) {
%3 = alloca void (i32)*
%4 = alloca i32
%5 = alloca void (i32)*
%6 = alloca %struct.sigaction
%7 = alloca %struct.sigaction
store i32 %0, i32* %4
store void (i32)* %1, void (i32)** %5
%8 = load void (i32)*, void (i32)** %5
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
store void (i32)* %8, void (i32)** %9
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%11 = call i32 @sigemptyset(i32* %10)
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
store i32 0, i32* %12
%13 = load i32, i32* %4
%14 = load i32, i32* @SIGALRM
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %2
br label %22
17: ; preds = %2
%18 = load i32, i32* @SA_RESTART
%19 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
%20 = load i32, i32* %19
%21 = or i32 %20, %18
store i32 %21, i32* %19
br label %22
22: ; preds = %17, %16
%23 = load i32, i32* @SA_RESETHAND
%24 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
%25 = load i32, i32* %24
%26 = or i32 %25, %23
store i32 %26, i32* %24
%27 = load i32, i32* @SA_NODEFER
%28 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
%29 = load i32, i32* %28
%30 = or i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* %4
%32 = call i64 @sigaction(i32 %31, %struct.sigaction* %7, %struct.sigaction* %6)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %22
store void (i32)* @SIG_ERR, void (i32)** %3
br label %38
35: ; preds = %22
%36 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
%37 = load void (i32)*, void (i32)** %36
store void (i32)* %37, void (i32)** %3
br label %38
38: ; preds = %35, %34
%39 = load void (i32)*, void (i32)** %3
ret void (i32)* %39
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32, i32 }
@SIGALRM = global i32 0
@SA_RESTART = global i32 0
@SA_RESETHAND = global i32 0
@SA_NODEFER = global i32 0
define void (i32)* @my_signal(i32 %0, void (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* %4, i64 0, i32 0
store void (i32)* %1, void (i32)** %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
%9 = call i32 @sigemptyset(i32* %8)
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
%11 = load i32, i32* @SIGALRM
%12 = icmp eq i32 %11, %0
%13 = load i32, i32* @SA_RESTART
%14 = select i1 %12, i32 0, i32 %13
%15 = load i32, i32* @SA_RESETHAND
%16 = or i32 %14, %15
%17 = load i32, i32* @SA_NODEFER
%18 = or i32 %17, %16
store i32 %18, i32* %10
%19 = call i64 @sigaction(i32 %0, %struct.sigaction* %4, %struct.sigaction* %3)
%20 = icmp slt i64 %19, 0
%21 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
%22 = load void (i32)*, void (i32)** %21
%23 = select i1 %20, void (i32)* @SIG_ERR, void (i32)* %22
ret void (i32)* %23
}