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

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
scalar_t__ sigaction(int, struct sigaction *, int /*<<< orphan*/ *);
scalar_t__ sigfillset(int /*<<< orphan*/ *);
int linux_sigaction(int signum, void (*sig_func)(int)) {
  sigset_t sigmask_all;
  struct sigaction sigact = {
      .sa_handler = sig_func,
      .sa_mask = {{0}},
      .sa_flags = 0,
  };
  if (sigfillset(&sigmask_all)) {
    printf("Error: sigfillset failed.\n");
    return -1;
  }
  sigact.sa_mask = sigmask_all;
  if (sigaction(signum, &sigact, NULL)) {
    printf("Error: sigaction failed.\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<linux_sigaction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <sigfillset@plt>
cmp    $0x0,%rax
je     1180 <linux_sigaction+0x50>
lea    0xe91(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <linux_sigaction+0x8f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
je     11b8 <linux_sigaction+0x88>
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <linux_sigaction+0x8f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linux_sigaction>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    %rsi,0x8(%rsp)
movl   $0x0,0x14(%rsp)
lea    0x4(%rsp),%rdi
call   1050 <sigfillset@plt>
test   %rax,%rax
je     115d <linux_sigaction+0x2d>
lea    0xebe(%rip),%rdi
jmp    1181 <linux_sigaction+0x51>
mov    0x4(%rsp),%eax
mov    %eax,0x10(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <sigaction@plt>
test   %rax,%rax
je     118b <linux_sigaction+0x5b>
lea    0xe7f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32, i32 }
@.str = [27 x i8] c"Error: sigfillset failed.\0A\00"
@.str.1 = [26 x i8] c"Error: sigaction failed.\0A\00"
define i32 @linux_sigaction(i32 %0, void (i32)* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca void (i32)*
%6 = alloca i32
%7 = alloca %struct.sigaction
store i32 %0, i32* %4
store void (i32)* %1, void (i32)** %5
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%9 = load void (i32)*, void (i32)** %5
store void (i32)* %9, void (i32)** %8
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
store i32 0, i32* %11
%12 = call i64 @sigfillset(i32* %6)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %25
16: ; preds = %2
%17 = load i32, i32* %6
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* %4
%20 = call i64 @sigaction(i32 %19, %struct.sigaction* %7, i32* null)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %25
24: ; preds = %16
store i32 0, i32* %3
br label %25
25: ; preds = %24, %22, %14
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32, i32 }
@str = [25 x i8] c"Error: sigaction failed.\00"
@str.2 = [26 x i8] c"Error: sigfillset failed.\00"
define i32 @linux_sigaction(i32 %0, void (i32)* %1) {
%3 = alloca i32
%4 = alloca %struct.sigaction
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store void (i32)* %1, void (i32)** %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
store i32 0, i32* %8
%9 = call i64 @sigfillset(i32* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
%13 = load i32, i32* %3
store i32 %13, i32* %12
%14 = call i64 @sigaction(i32 %0, %struct.sigaction* %4, i32* null)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11, %2
%17 = phi i8* [ getelementptr inbounds ([26 x i8], [26 x i8]* @str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0), %11 ]
%18 = call i32 @puts(i8* dereferenceable(1) %17)
br label %19
19: ; preds = %16, %11
%20 = phi i32 [ 0, %11 ], [ -1, %16 ]
ret i32 %20
}