#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 /*<<< orphan*/ sa_mask;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ sigaction(int, struct sigaction *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void catch_sig(int sig, void (*handler)(int)) {
  struct sigaction vec;
  memset(&vec, 0, sizeof(struct sigaction));
  vec.sa_handler = handler;
  sigfillset(&vec.sa_mask);
  if (sigaction(sig, &vec, NULL) < 0) {
    perror("sigaction");
    fprintf(stderr, "sig=%d\n", sig);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<catch_sig>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
call   1060 <sigfillset@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    11dc <catch_sig+0x7c>
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe3d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<catch_sig>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %rbx,(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <sigfillset@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
test   %rax,%rax
js     11a9 <catch_sig+0x49>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
mov    %r14d,%edx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32 }
@.str = [10 x i8] c"sigaction\00"
@stderr = global i32 0
@.str.1 = [8 x i8] c"sig=%d\0A\00"
define void @catch_sig(i32 %0, void (i32)* %1) {
%3 = alloca i32
%4 = alloca void (i32)*
%5 = alloca %struct.sigaction
store i32 %0, i32* %3
store void (i32)* %1, void (i32)** %4
%6 = call i32 @memset(%struct.sigaction* %5, i32 0, i32 16)
%7 = load void (i32)*, void (i32)** %4
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 0
store void (i32)* %7, void (i32)** %8
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 1
%10 = call i32 @sigfillset(i32* %9)
%11 = load i32, i32* %3
%12 = call i64 @sigaction(i32 %11, %struct.sigaction* %5, i32* null)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @stderr
%17 = load i32, i32* %3
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
ret void
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32 }
@.str = [10 x i8] c"sigaction\00"
@stderr = global i32 0
@.str.1 = [8 x i8] c"sig=%d\0A\00"
define void @catch_sig(i32 %0, void (i32)* %1) {
%3 = alloca %struct.sigaction
%4 = bitcast %struct.sigaction* %3 to i8*
%5 = call i32 @memset(%struct.sigaction* %3, i32 0, i32 16)
%6 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store void (i32)* %1, void (i32)** %6
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
%8 = call i32 @sigfillset(i32* %7)
%9 = call i64 @sigaction(i32 %0, %struct.sigaction* %3, i32* null)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
ret void
}