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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
scalar_t__ SIG_ERR;
scalar_t__ SIG_IGN;
int /*<<< orphan*/ errno;
scalar_t__ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ signal(int, scalar_t__);
int sigaction(int _sig, const struct sigaction *_act, struct sigaction *_oact) {
  if (_oact) {
    _oact->sa_flags = 0;
    _oact->sa_handler = signal(_sig, SIG_IGN);
    if (sigemptyset(&_oact->sa_mask) != 0 || _oact->sa_handler == SIG_ERR) {
      errno = EINVAL;
      return -1;
    }
    signal(_sig, _oact->sa_handler);
  }
  if (_act) {
    if (signal(_sig, _act->sa_handler) == SIG_ERR) {
      errno = EINVAL;
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<sigaction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c1 <sigaction+0xa1>
mov    -0x18(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x8(%rbp),%edi
mov    0x2e94(%rip),%rax
mov    (%rax),%rsi
call   1030 <signal@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
add    $0x8,%rdi
call   1040 <sigemptyset@plt>
cmp    $0x0,%rax
jne    1194 <sigaction+0x74>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jne    11b2 <sigaction+0x92>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <sigaction+0xf5>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1030 <signal@plt>
cmpq   $0x0,-0x10(%rbp)
je     120e <sigaction+0xee>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1030 <signal@plt>
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
jne    1209 <sigaction+0xe9>
mov    0x2dd6(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <sigaction+0xf5>
jmp    120e <sigaction+0xee>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigaction>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
test   %rdx,%rdx
je     1171 <sigaction+0x51>
mov    %rdx,%rbx
movq   $0x0,0x10(%rdx)
mov    0x2ea8(%rip),%rax
mov    (%rax),%rsi
mov    %ebp,%edi
call   1030 <signal@plt>
mov    %rax,(%rbx)
lea    0x8(%rbx),%rdi
call   1040 <sigemptyset@plt>
test   %rax,%rax
jne    118e <sigaction+0x6e>
mov    (%rbx),%rsi
mov    0x2e73(%rip),%rax
cmp    (%rax),%rsi
je     118e <sigaction+0x6e>
mov    %ebp,%edi
call   1030 <signal@plt>
xor    %ebx,%ebx
test   %r14,%r14
je     11a5 <sigaction+0x85>
mov    (%r14),%rsi
mov    %ebp,%edi
call   1030 <signal@plt>
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
jne    11a5 <sigaction+0x85>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    0x2e32(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { i64, i32, i64 }
@SIG_IGN = global i64 0
@SIG_ERR = global i64 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @sigaction(i32 %0, %struct.sigaction* %1, %struct.sigaction* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sigaction*
%7 = alloca %struct.sigaction*
store i32 %0, i32* %5
store %struct.sigaction* %1, %struct.sigaction** %6
store %struct.sigaction* %2, %struct.sigaction** %7
%8 = load %struct.sigaction*, %struct.sigaction** %7
%9 = icmp ne %struct.sigaction* %8, null
br i1 %9, label %10, label %36
10: ; preds = %3
%11 = load %struct.sigaction*, %struct.sigaction** %7
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %11, i32 0, i32 2
store i64 0, i64* %12
%13 = load i32, i32* %5
%14 = load i64, i64* @SIG_IGN
%15 = call i64 @signal(i32 %13, i64 %14)
%16 = load %struct.sigaction*, %struct.sigaction** %7
%17 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %16, i32 0, i32 0
store i64 %15, i64* %17
%18 = load %struct.sigaction*, %struct.sigaction** %7
%19 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %18, i32 0, i32 1
%20 = call i64 @sigemptyset(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %10
%23 = load %struct.sigaction*, %struct.sigaction** %7
%24 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @SIG_ERR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %22, %10
%29 = load i32, i32* @EINVAL
store i32 %29, i32* @errno
store i32 -1, i32* %4
br label %51
30: ; preds = %22
%31 = load i32, i32* %5
%32 = load %struct.sigaction*, %struct.sigaction** %7
%33 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %32, i32 0, i32 0
%34 = load i64, i64* %33
%35 = call i64 @signal(i32 %31, i64 %34)
br label %36
36: ; preds = %30, %3
%37 = load %struct.sigaction*, %struct.sigaction** %6
%38 = icmp ne %struct.sigaction* %37, null
br i1 %38, label %39, label %50
39: ; preds = %36
%40 = load i32, i32* %5
%41 = load %struct.sigaction*, %struct.sigaction** %6
%42 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = call i64 @signal(i32 %40, i64 %43)
%45 = load i64, i64* @SIG_ERR
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = load i32, i32* @EINVAL
store i32 %48, i32* @errno
store i32 -1, i32* %4
br label %51
49: ; preds = %39
br label %50
50: ; preds = %49, %36
store i32 0, i32* %4
br label %51
51: ; preds = %50, %47, %28
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
%struct.sigaction = type { i64, i32, i64 }
@SIG_IGN = global i64 0
@SIG_ERR = global i64 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @sigaction(i32 %0, %struct.sigaction* %1, %struct.sigaction* %2) {
%4 = icmp eq %struct.sigaction* %2, null
br i1 %4, label %19, label %5
5: ; preds = %3
%6 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 2
store i64 0, i64* %6
%7 = load i64, i64* @SIG_IGN
%8 = tail call i64 @signal(i32 %0, i64 %7)
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 0
store i64 %8, i64* %9
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 1
%11 = tail call i64 @sigemptyset(i32* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %5
%14 = load i64, i64* %9
%15 = load i64, i64* @SIG_ERR
%16 = icmp eq i64 %14, %15
br i1 %16, label %27, label %17
17: ; preds = %13
%18 = tail call i64 @signal(i32 %0, i64 %14)
br label %19
19: ; preds = %17, %3
%20 = icmp eq %struct.sigaction* %1, null
br i1 %20, label %29, label %21
21: ; preds = %19
%22 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 0
%23 = load i64, i64* %22
%24 = tail call i64 @signal(i32 %0, i64 %23)
%25 = load i64, i64* @SIG_ERR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %21, %5, %13
%28 = load i32, i32* @EINVAL
store i32 %28, i32* @errno
br label %29
29: ; preds = %27, %19, %21
%30 = phi i32 [ 0, %21 ], [ 0, %19 ], [ -1, %27 ]
ret i32 %30
}