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

/* Variables and functions */
int /*<<< orphan*/ SA_RESTART;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ SIG_UNBLOCK;
int /*<<< orphan*/ abortmsgerrno(char *);
int /*<<< orphan*/ forwardsignal(int);
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
scalar_t__ raise(int);
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
scalar_t__ sigaddset(int /*<<< orphan*/ *, int);
scalar_t__ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
void handlestopsignal(int sig) {
  sigset_t unblockset, oldset;
  struct sigaction sa, oldsa;
  if (sigemptyset(&unblockset) < 0)
    goto error;
  if (sigaddset(&unblockset, sig) < 0)
    goto error;
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = SIG_DFL;
  sa.sa_flags = SA_RESTART;
  if (sigemptyset(&sa.sa_mask) < 0)
    goto error;
  forwardsignal(sig);
  if (raise(sig) < 0)
    goto error;
  if (sigaction(sig, &sa, &oldsa) < 0)
    goto error;
  if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0)
    goto error;
  if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0)
    goto error;
  if (sigaction(sig, &oldsa, NULL) < 0)
    goto error;
  return;
error:
  abortmsgerrno("failed to handle stop signal");
}


x86 assembly (clang, -O0):
<handlestopsignal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1070 <sigemptyset@plt>
cmp    $0x0,%rax
jge    11a3 <handlestopsignal+0x23>
jmp    12aa <handlestopsignal+0x12a>
mov    -0x4(%rbp),%esi
lea    -0x8(%rbp),%rdi
call   10a0 <sigaddset@plt>
cmp    $0x0,%rax
jge    11be <handlestopsignal+0x3e>
jmp    12aa <handlestopsignal+0x12a>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <sigemptyset@plt>
cmp    $0x0,%rax
jge    11fe <handlestopsignal+0x7e>
jmp    12aa <handlestopsignal+0x12a>
mov    -0x4(%rbp),%edi
call   1080 <forwardsignal@plt>
mov    -0x4(%rbp),%edi
call   1040 <raise@plt>
cmp    $0x0,%rax
jge    121d <handlestopsignal+0x9d>
jmp    12aa <handlestopsignal+0x12a>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    123c <handlestopsignal+0xbc>
jmp    12aa <handlestopsignal+0x12a>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
jge    1261 <handlestopsignal+0xe1>
jmp    12aa <handlestopsignal+0x12a>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
jge    1286 <handlestopsignal+0x106>
jmp    12aa <handlestopsignal+0x12a>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    12a5 <handlestopsignal+0x125>
jmp    12aa <handlestopsignal+0x12a>
jmp    12b6 <handlestopsignal+0x136>
lea    0xd4f(%rip),%rdi
call   1090 <abortmsgerrno@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handlestopsignal>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
mov    %rsp,%rdi
call   1070 <sigemptyset@plt>
test   %rax,%rax
js     1259 <handlestopsignal+0xd9>
mov    %rsp,%rdi
mov    %ebx,%esi
call   10a0 <sigaddset@plt>
test   %rax,%rax
js     1259 <handlestopsignal+0xd9>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   1070 <sigemptyset@plt>
test   %rax,%rax
js     1259 <handlestopsignal+0xd9>
mov    %ebx,%edi
call   1080 <forwardsignal@plt>
mov    %ebx,%edi
call   1040 <raise@plt>
test   %rax,%rax
js     1259 <handlestopsignal+0xd9>
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1050 <sigaction@plt>
test   %rax,%rax
js     1259 <handlestopsignal+0xd9>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1030 <sigprocmask@plt>
test   %rax,%rax
js     1259 <handlestopsignal+0xd9>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
test   %rax,%rax
js     1259 <handlestopsignal+0xd9>
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <sigaction@plt>
test   %rax,%rax
jns    1265 <handlestopsignal+0xe5>
lea    0xda0(%rip),%rdi
call   1090 <abortmsgerrno@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sigaction = type { i32, i32, i32 }
@SIG_DFL = global i32 0
@SA_RESTART = global i32 0
@SIG_UNBLOCK = global i32 0
@SIG_SETMASK = global i32 0
@.str = [29 x i8] c"failed to handle stop signal\00"
define void @handlestopsignal(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sigaction
%6 = alloca %struct.sigaction
store i32 %0, i32* %2
%7 = call i64 @sigemptyset(i32* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
br label %53
10: ; preds = %1
%11 = load i32, i32* %2
%12 = call i64 @sigaddset(i32* %3, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
br label %53
15: ; preds = %10
%16 = call i32 @memset(%struct.sigaction* %5, i32 0, i32 12)
%17 = load i32, i32* @SIG_DFL
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @SA_RESTART
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 0
%22 = call i64 @sigemptyset(i32* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %15
br label %53
25: ; preds = %15
%26 = load i32, i32* %2
%27 = call i32 @forwardsignal(i32 %26)
%28 = load i32, i32* %2
%29 = call i64 @raise(i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
br label %53
32: ; preds = %25
%33 = load i32, i32* %2
%34 = call i64 @sigaction(i32 %33, %struct.sigaction* %5, %struct.sigaction* %6)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
br label %53
37: ; preds = %32
%38 = load i32, i32* @SIG_UNBLOCK
%39 = call i64 @sigprocmask(i32 %38, i32* %3, i32* %4)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %37
br label %53
42: ; preds = %37
%43 = load i32, i32* @SIG_SETMASK
%44 = call i64 @sigprocmask(i32 %43, i32* %4, i32* null)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42
br label %53
47: ; preds = %42
%48 = load i32, i32* %2
%49 = call i64 @sigaction(i32 %48, %struct.sigaction* %6, %struct.sigaction* null)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %47
br label %53
52: ; preds = %47
br label %55
53: ; preds = %51, %46, %41, %36, %31, %24, %14, %9
%54 = call i32 @abortmsgerrno(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %55
55: ; preds = %53, %52
ret void
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i32, i32 }
@SIG_DFL = global i32 0
@SA_RESTART = global i32 0
@SIG_UNBLOCK = global i32 0
@SIG_SETMASK = global i32 0
@.str = [29 x i8] c"failed to handle stop signal\00"
define void @handlestopsignal(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sigaction
%5 = alloca %struct.sigaction
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sigaction* %4 to i8*
%9 = bitcast %struct.sigaction* %5 to i8*
%10 = call i64 @sigemptyset(i32* %2)
%11 = icmp slt i64 %10, 0
br i1 %11, label %42, label %12
12: ; preds = %1
%13 = call i64 @sigaddset(i32* %2, i32 %0)
%14 = icmp slt i64 %13, 0
br i1 %14, label %42, label %15
15: ; preds = %12
%16 = call i32 @memset(%struct.sigaction* %4, i32 0, i32 12)
%17 = load i32, i32* @SIG_DFL
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @SA_RESTART
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%22 = call i64 @sigemptyset(i32* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %42, label %24
24: ; preds = %15
%25 = call i32 @forwardsignal(i32 %0)
%26 = call i64 @raise(i32 %0)
%27 = icmp slt i64 %26, 0
br i1 %27, label %42, label %28
28: ; preds = %24
%29 = call i64 @sigaction(i32 %0, %struct.sigaction* %4, %struct.sigaction* %5)
%30 = icmp slt i64 %29, 0
br i1 %30, label %42, label %31
31: ; preds = %28
%32 = load i32, i32* @SIG_UNBLOCK
%33 = call i64 @sigprocmask(i32 %32, i32* %2, i32* %3)
%34 = icmp slt i64 %33, 0
br i1 %34, label %42, label %35
35: ; preds = %31
%36 = load i32, i32* @SIG_SETMASK
%37 = call i64 @sigprocmask(i32 %36, i32* %3, i32* null)
%38 = icmp slt i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %35
%40 = call i64 @sigaction(i32 %0, %struct.sigaction* %5, %struct.sigaction* null)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39, %35, %31, %28, %24, %15, %12, %1
%43 = call i32 @abortmsgerrno(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %44
44: ; preds = %39, %42
ret void
}