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

/* Variables and functions */
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
int sigaction(int, struct sigaction *, int /*<<< orphan*/ *);
int setsig(int sig, void (*handler)(int), int flags, sigset_t *mask) {
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = handler;
  if (mask)
    sa.sa_mask = *mask;
  sa.sa_flags = flags;
  return sigaction(sig, &sa, NULL);
}


x86 assembly (clang, -O0):
<setsig>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1162 <setsig+0x42>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setsig>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%r14d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %rbp,0x8(%rsp)
test   %rbx,%rbx
je     1157 <setsig+0x37>
mov    (%rbx),%eax
mov    %eax,0x14(%rsp)
mov    %r15d,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1030 <sigaction@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32, i32 }
define i32 @setsig(i32 %0, void (i32)* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca void (i32)*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.sigaction
store i32 %0, i32* %5
store void (i32)* %1, void (i32)** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%10 = call i32 @memset(%struct.sigaction* %9, i32 0, i32 16)
%11 = load void (i32)*, void (i32)** %6
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 0
store void (i32)* %11, void (i32)** %12
%13 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %4
%16 = load i32*, i32** %8
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 2
store i32 %17, i32* %18
br label %19
19: ; preds = %15, %4
%20 = load i32, i32* %7
%21 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* %5
%23 = call i32 @sigaction(i32 %22, %struct.sigaction* %9, i32* null)
ret i32 %23
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32, i32 }
define i32 @setsig(i32 %0, void (i32)* %1, i32 %2, i32* %3) {
%5 = alloca %struct.sigaction
%6 = bitcast %struct.sigaction* %5 to i8*
%7 = call i32 @memset(%struct.sigaction* %5, i32 0, i32 16)
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 0
store void (i32)* %1, void (i32)** %8
%9 = icmp eq i32* %3, null
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = load i32, i32* %3
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 2
store i32 %11, i32* %12
br label %13
13: ; preds = %10, %4
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 1
store i32 %2, i32* %14
%15 = call i32 @sigaction(i32 %0, %struct.sigaction* %5, i32* null)
ret i32 %15
}