#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;
};

/* Variables and functions */
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int install_sig_handler(int signo, int flags, struct sigaction *oldsate,
                        void (*sig_handler)(int)) {
  struct sigaction action;
  action.sa_handler = sig_handler;
  sigemptyset(&(action.sa_mask));
  action.sa_flags = flags;
  if (sigaction(signo, &action, oldsate) != 0) {
    return -1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<install_sig_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
add    $0xc,%rdi
call   1040 <sigemptyset@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%rsi
call   1030 <sigaction@plt>
cmp    $0x0,%rax
je     1177 <install_sig_handler+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    117e <install_sig_handler+0x5e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<install_sig_handler>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%ebx
mov    %rcx,(%rsp)
lea    0xc(%rsp),%rdi
call   1040 <sigemptyset@plt>
mov    %ebp,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %r14,%rdx
call   1030 <sigaction@plt>
neg    %rax
sbb    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32, i32 }
define i32 @install_sig_handler(i32 %0, i32 %1, %struct.sigaction* %2, void (i32)* %3) {
%5 = alloca %struct.sigaction
%6 = bitcast %struct.sigaction* %5 to i8*
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 0
store void (i32)* %3, void (i32)** %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 2
%9 = call i32 @sigemptyset(i32* %8)
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 1
store i32 %1, i32* %10
%11 = call i64 @sigaction(i32 %0, %struct.sigaction* %5, %struct.sigaction* %2)
%12 = icmp ne i64 %11, 0
%13 = sext i1 %12 to i32
ret i32 %13
}