#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_mask;
  int /*<<< orphan*/ sa_flags;
};

/* Variables and functions */
int /*<<< orphan*/ SA_RESTART;
void SIG_ERR(int);
int sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
void (*tst_setup_signal(int sig, void (*handler)(int)))(int) {
  struct sigaction my_act, old_act;
  int ret;
  my_act.sa_handler = handler;
  my_act.sa_flags = SA_RESTART;
  sigemptyset(&my_act.sa_mask);
  ret = sigaction(sig, &my_act, &old_act);
  if (ret == 0)
    return (old_act.sa_handler);
  else
    return (SIG_ERR);
}


x86 assembly (clang, -O0):
<tst_setup_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
call   1040 <sigemptyset@plt>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1030 <sigaction@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    117a <tst_setup_signal+0x5a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1185 <tst_setup_signal+0x65>
mov    0x2e57(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tst_setup_signal>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    %rsi,(%rsp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
call   1040 <sigemptyset@plt>
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1030 <sigaction@plt>
test   %eax,%eax
jne    115c <tst_setup_signal+0x3c>
mov    0x10(%rsp),%rax
jmp    1163 <tst_setup_signal+0x43>
mov    0x2e75(%rip),%rax
add    $0x20,%rsp
pop    %rbx
ret

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

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