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

/* Variables and functions */
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_SETMASK;
int errno;
int /*<<< orphan*/ free(void *);
int pthread_sigmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                    int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
void htx_free(void *ptr) {
  sigset_t newMask, oldMask;
  int ret = 0, skip_reset = 0, old_errno;
  sigemptyset(&oldMask);
  sigfillset(&newMask);
  old_errno = errno;
  ret = pthread_sigmask(SIG_BLOCK, &newMask, &oldMask);
  if (ret == -1) {
    errno = old_errno;
    skip_reset = 1;
  }
  free(ptr);
  if (skip_reset == 0) {
    old_errno = errno;
    ret = pthread_sigmask(SIG_SETMASK, &oldMask, NULL);
    if (ret == -1) {
      errno = old_errno;
    }
  }
}


x86 assembly (clang, -O0):
<htx_free>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <sigemptyset@plt>
lea    -0xc(%rbp),%rdi
call   1050 <sigfillset@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1060 <pthread_sigmask@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ae <htx_free+0x6e>
mov    -0x1c(%rbp),%ecx
mov    0x2e33(%rip),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1201 <htx_free+0xc1>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <pthread_sigmask@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fc <htx_free+0xbc>
mov    -0x1c(%rbp),%ecx
mov    0x2dde(%rip),%rax
mov    %ecx,(%rax)
jmp    1201 <htx_free+0xc1>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htx_free>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%r15
mov    %r15,%rdi
call   1040 <sigemptyset@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sigfillset@plt>
mov    0x2e68(%rip),%r12
mov    (%r12),%ebp
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1060 <pthread_sigmask@plt>
cmp    $0xffffffff,%eax
je     11b9 <htx_free+0x79>
mov    %r14,%rdi
call   1030 <free@plt>
mov    (%r12),%ebx
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1060 <pthread_sigmask@plt>
cmp    $0xffffffff,%eax
jne    11c5 <htx_free+0x85>
mov    %ebx,(%r12)
jmp    11c5 <htx_free+0x85>
mov    %ebp,(%r12)
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
define void @htx_free(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
store i32 0, i32* %6
%8 = call i32 @sigemptyset(i32* %4)
%9 = call i32 @sigfillset(i32* %3)
%10 = load i32, i32* @errno
store i32 %10, i32* %7
%11 = load i32, i32* @SIG_BLOCK
%12 = call i32 @pthread_sigmask(i32 %11, i32* %3, i32* %4)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = load i32, i32* %7
store i32 %16, i32* @errno
store i32 1, i32* %6
br label %17
17: ; preds = %15, %1
%18 = load i8*, i8** %2
%19 = call i32 @free(i8* %18)
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %17
%23 = load i32, i32* @errno
store i32 %23, i32* %7
%24 = load i32, i32* @SIG_SETMASK
%25 = call i32 @pthread_sigmask(i32 %24, i32* %4, i32* null)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i32, i32* %7
store i32 %29, i32* @errno
br label %30
30: ; preds = %28, %22
br label %31
31: ; preds = %30, %17
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
define void @htx_free(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = call i32 @sigemptyset(i32* %3)
%7 = call i32 @sigfillset(i32* %2)
%8 = load i32, i32* @errno
%9 = load i32, i32* @SIG_BLOCK
%10 = call i32 @pthread_sigmask(i32 %9, i32* %2, i32* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
store i32 %8, i32* @errno
%13 = call i32 @free(i8* %0)
br label %21
14: ; preds = %1
%15 = call i32 @free(i8* %0)
%16 = load i32, i32* @errno
%17 = load i32, i32* @SIG_SETMASK
%18 = call i32 @pthread_sigmask(i32 %17, i32* %3, i32* null)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 %16, i32* @errno
br label %21
21: ; preds = %12, %14, %20
ret void
}