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

/* Variables and functions */
int sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
void (*catchsignal(int signum, void (*handler)()))() {
  struct sigaction new_sa;
  struct sigaction old_sa;
  new_sa.sa_handler = handler;
  sigemptyset(&new_sa.sa_mask);
  new_sa.sa_flags = 0;
  if (sigaction(signum, &new_sa, &old_sa) == -1)
    return ((void (*)()) - 1);
  return (old_sa.sa_handler);
}


x86 assembly (clang, -O0):
<catchsignal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x10,%rdi
call   1040 <sigemptyset@plt>
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    1175 <catchsignal+0x55>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    117d <catchsignal+0x5d>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<catchsignal>:
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
mov    %rsi,(%rsp)
lea    0x10(%rsp),%rdi
call   1040 <sigemptyset@plt>
movq   $0x0,0x8(%rsp)
mov    %rsp,%rsi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1030 <sigaction@plt>
mov    %eax,%ecx
mov    $0xffffffffffffffff,%rax
cmp    $0xffffffff,%ecx
je     1160 <catchsignal+0x40>
mov    0x18(%rsp),%rax
add    $0x30,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.sigaction = type { void (...)*, i64, i32 }
define void (...)* @catchsignal(i32 %0, void (...)* %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 (...)* %1, void (...)** %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
%9 = call i32 @sigemptyset(i32* %8)
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i64 0, i64* %10
%11 = call i32 @sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%12 = icmp eq i32 %11, -1
%13 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%14 = load void (...)*, void (...)** %13
%15 = select i1 %12, void (...)* inttoptr (i64 -1 to void (...)*), void (...)* %14
ret void (...)* %15
}