#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);
  scalar_t__ sa_flags;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int SIG_BLOCK;
void SIG_ERR(int);
int SIG_UNBLOCK;
int /*<<< orphan*/ perror(char *);
scalar_t__ sigaction(int, struct sigaction *, int /*<<< orphan*/ *);
scalar_t__ sigaddset(int /*<<< orphan*/ *, int);
scalar_t__ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ wrap_sig_mask(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int wrap_sig_setup(int signum, void (*sigfn)(int), int sign_block) {
  int ret = -1, how = (sign_block < 0) ? SIG_BLOCK : SIG_UNBLOCK;
  struct sigaction sa;
  sigset_t sigset;
  if (sign_block != 0) {
    if (sigemptyset(&sigset) != 0) {
      perror("sigemptyset()");
      goto fail;
    }
    if (sigaddset(&sigset, signum) != 0) {
      perror("sigaddset()");
      goto fail;
    }
    if (wrap_sig_mask(how, &sigset, NULL) != 0)
      goto fail;
  }
  if (sigfn != SIG_ERR) {
    sa.sa_flags = 0;
    sa.sa_handler = sigfn;
    if (sigaction(signum, &sa, NULL) != 0) {
      perror("sigaction()");
      goto fail;
    }
  }
  ret = 0;
fail:
  return ret;
}


x86 assembly (clang, -O0):
<wrap_sig_setup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1184 <wrap_sig_setup+0x34>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
jmp    1190 <wrap_sig_setup+0x40>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     120f <wrap_sig_setup+0xbf>
lea    -0x34(%rbp),%rdi
call   1040 <sigemptyset@plt>
cmp    $0x0,%rax
je     11c4 <wrap_sig_setup+0x74>
lea    0xe46(%rip),%rdi
call   1060 <perror@plt>
jmp    1267 <wrap_sig_setup+0x117>
mov    -0x4(%rbp),%esi
lea    -0x34(%rbp),%rdi
call   1070 <sigaddset@plt>
cmp    $0x0,%rax
je     11eb <wrap_sig_setup+0x9b>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
jmp    1267 <wrap_sig_setup+0x117>
mov    -0x1c(%rbp),%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <wrap_sig_mask@plt>
cmp    $0x0,%rax
je     120a <wrap_sig_setup+0xba>
jmp    1267 <wrap_sig_setup+0x117>
jmp    120f <wrap_sig_setup+0xbf>
mov    0x2dc2(%rip),%rax
cmp    %rax,-0x10(%rbp)
je     1260 <wrap_sig_setup+0x110>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
je     125b <wrap_sig_setup+0x10b>
lea    0xdc9(%rip),%rdi
call   1060 <perror@plt>
jmp    1267 <wrap_sig_setup+0x117>
jmp    1260 <wrap_sig_setup+0x110>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wrap_sig_setup>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
test   %edx,%edx
jns    116d <wrap_sig_setup+0x1d>
mov    0x2e67(%rip),%rax
jne    1176 <wrap_sig_setup+0x26>
jmp    11b1 <wrap_sig_setup+0x61>
mov    0x2e74(%rip),%rax
je     11b1 <wrap_sig_setup+0x61>
mov    (%rax),%ebp
lea    0xc(%rsp),%rdi
call   1040 <sigemptyset@plt>
test   %rax,%rax
jne    11eb <wrap_sig_setup+0x9b>
lea    0xc(%rsp),%rdi
mov    %r14d,%esi
call   1070 <sigaddset@plt>
test   %rax,%rax
jne    11f4 <wrap_sig_setup+0xa4>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <wrap_sig_mask@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1205 <wrap_sig_setup+0xb5>
cmp    0x2e20(%rip),%rbx
je     11e7 <wrap_sig_setup+0x97>
movq   $0x0,0x18(%rsp)
mov    %rbx,0x10(%rsp)
xor    %ebp,%ebp
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1030 <sigaction@plt>
test   %rax,%rax
je     1205 <wrap_sig_setup+0xb5>
lea    0xe35(%rip),%rdi
jmp    11fb <wrap_sig_setup+0xab>
xor    %ebp,%ebp
jmp    1205 <wrap_sig_setup+0xb5>
lea    0xe0e(%rip),%rdi
jmp    11fb <wrap_sig_setup+0xab>
lea    0xe13(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i64 }
@SIG_BLOCK = global i32 0
@SIG_UNBLOCK = global i32 0
@.str = [14 x i8] c"sigemptyset()\00"
@.str.1 = [12 x i8] c"sigaddset()\00"
@.str.2 = [12 x i8] c"sigaction()\00"
define i32 @wrap_sig_setup(i32 %0, void (i32)* %1, i32 %2) {
%4 = alloca i32
%5 = alloca void (i32)*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sigaction
%10 = alloca i32
store i32 %0, i32* %4
store void (i32)* %1, void (i32)** %5
store i32 %2, i32* %6
store i32 -1, i32* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i32, i32* @SIG_BLOCK
br label %17
15: ; preds = %3
%16 = load i32, i32* @SIG_UNBLOCK
br label %17
17: ; preds = %15, %13
%18 = phi i32 [ %14, %13 ], [ %16, %15 ]
store i32 %18, i32* %8
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %17
%22 = call i64 @sigemptyset(i32* %10)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %52
26: ; preds = %21
%27 = load i32, i32* %4
%28 = call i64 @sigaddset(i32* %10, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %52
32: ; preds = %26
%33 = load i32, i32* %8
%34 = call i64 @wrap_sig_mask(i32 %33, i32* %10, i32* null)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
br label %52
37: ; preds = %32
br label %38
38: ; preds = %37, %17
%39 = load void (i32)*, void (i32)** %5
%40 = icmp ne void (i32)* %39, @SIG_ERR
br i1 %40, label %41, label %51
41: ; preds = %38
%42 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 1
store i64 0, i64* %42
%43 = load void (i32)*, void (i32)** %5
%44 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 0
store void (i32)* %43, void (i32)** %44
%45 = load i32, i32* %4
%46 = call i64 @sigaction(i32 %45, %struct.sigaction* %9, i32* null)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %52
50: ; preds = %41
br label %51
51: ; preds = %50, %38
store i32 0, i32* %7
br label %52
52: ; preds = %51, %48, %36, %30, %24
%53 = load i32, i32* %7
ret i32 %53
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i64 }
@SIG_BLOCK = global i32 0
@SIG_UNBLOCK = global i32 0
@.str = [14 x i8] c"sigemptyset()\00"
@.str.1 = [12 x i8] c"sigaddset()\00"
@.str.2 = [12 x i8] c"sigaction()\00"
define i32 @wrap_sig_setup(i32 %0, void (i32)* %1, i32 %2) {
%4 = alloca %struct.sigaction
%5 = alloca i32
%6 = icmp slt i32 %2, 0
%7 = load i32, i32* @SIG_BLOCK
%8 = load i32, i32* @SIG_UNBLOCK
%9 = select i1 %6, i32 %7, i32 %8
%10 = bitcast %struct.sigaction* %4 to i8*
%11 = bitcast i32* %5 to i8*
%12 = icmp eq i32 %2, 0
br i1 %12, label %22, label %13
13: ; preds = %3
%14 = call i64 @sigemptyset(i32* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = call i64 @sigaddset(i32* %5, i32 %0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = call i64 @wrap_sig_mask(i32 %9, i32* %5, i32* null)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %19, %3
%23 = icmp eq void (i32)* %1, @SIG_ERR
br i1 %23, label %32, label %24
24: ; preds = %22
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store void (i32)* %1, void (i32)** %26
%27 = call i64 @sigaction(i32 %0, %struct.sigaction* %4, i32* null)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %24, %16, %13
%30 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), %13 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %16 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %24 ]
%31 = call i32 @perror(i8* %30)
br label %32
32: ; preds = %29, %22, %24, %19
%33 = phi i32 [ -1, %19 ], [ 0, %24 ], [ 0, %22 ], [ -1, %29 ]
ret i32 %33
}