#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*/ sigaddset(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
void (*bsd_signal(int sig, void (*func)(int)))(int) {
  struct sigaction act, oact;
  act.sa_handler = func;
  act.sa_flags = SA_RESTART;
  sigemptyset(&act.sa_mask);
  sigaddset(&act.sa_mask, sig);
  if (sigaction(sig, &act, &oact) == -1)
    return (SIG_ERR);
  return (oact.sa_handler);
}


x86 assembly (clang, -O0):
<bsd_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    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
call   1040 <sigemptyset@plt>
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
mov    -0xc(%rbp),%esi
call   1050 <sigaddset@plt>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    1199 <bsd_signal+0x69>
mov    0x2e48(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a1 <bsd_signal+0x71>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bsd_signal>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
mov    %rsi,0x8(%rsp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <sigemptyset@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <sigaddset@plt>
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    1181 <bsd_signal+0x51>
mov    0x2e59(%rip),%rax
jmp    1186 <bsd_signal+0x56>
mov    0x18(%rsp),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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