#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;
scalar_t__ malloc(size_t);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
int sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
void *htx_malloc(size_t size) {
  sigset_t newMask, oldMask;
  void *ptr;
  int ret = 0, skip_reset = 0, old_errno;
  sigemptyset(&oldMask);
  sigfillset(&newMask);
  old_errno = errno;
  ret = sigprocmask(SIG_BLOCK, &newMask, &oldMask);
  if (ret == -1) {
    errno = old_errno;
    skip_reset = 1;
  }
  ptr = (void *)malloc(size);
  if (skip_reset == 0) {
    old_errno = errno;
    ret = sigprocmask(SIG_SETMASK, &oldMask, NULL);
    if (ret == -1) {
      errno = old_errno;
    }
  }
  return (ptr);
}


x86 assembly (clang, -O0):
<htx_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%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,-0x24(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1030 <sigprocmask@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ae <htx_malloc+0x6e>
mov    -0x24(%rbp),%ecx
mov    0x2e33(%rip),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1205 <htx_malloc+0xc5>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1200 <htx_malloc+0xc0>
mov    -0x24(%rbp),%ecx
mov    0x2dda(%rip),%rax
mov    %ecx,(%rax)
jmp    1205 <htx_malloc+0xc5>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htx_malloc>:
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   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
je     11bc <htx_malloc+0x7c>
mov    %r14,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    (%r12),%ebp
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    11cb <htx_malloc+0x8b>
mov    %ebp,(%r12)
jmp    11cb <htx_malloc+0x8b>
mov    %ebp,(%r12)
mov    %r14,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rbx,%rax
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 i8* @htx_malloc(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i64 %0, i64* %2
store i32 0, i32* %6
store i32 0, i32* %7
%9 = call i32 @sigemptyset(i32* %4)
%10 = call i32 @sigfillset(i32* %3)
%11 = load i32, i32* @errno
store i32 %11, i32* %8
%12 = load i32, i32* @SIG_BLOCK
%13 = call i32 @sigprocmask(i32 %12, i32* %3, i32* %4)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = load i32, i32* %8
store i32 %17, i32* @errno
store i32 1, i32* %7
br label %18
18: ; preds = %16, %1
%19 = load i64, i64* %2
%20 = call i64 @malloc(i64 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %5
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %18
%25 = load i32, i32* @errno
store i32 %25, i32* %8
%26 = load i32, i32* @SIG_SETMASK
%27 = call i32 @sigprocmask(i32 %26, i32* %4, i32* null)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = load i32, i32* %8
store i32 %31, i32* @errno
br label %32
32: ; preds = %30, %24
br label %33
33: ; preds = %32, %18
%34 = load i8*, i8** %5
ret i8* %34
}

LLVM IR (-O2):
@errno = global i32 0
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
define i8* @htx_malloc(i64 %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 @sigprocmask(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 i64 @malloc(i64 %0)
br label %21
14: ; preds = %1
%15 = call i64 @malloc(i64 %0)
%16 = load i32, i32* @errno
%17 = load i32, i32* @SIG_SETMASK
%18 = call i32 @sigprocmask(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
%22 = phi i64 [ %13, %12 ], [ %15, %14 ], [ %15, %20 ]
%23 = inttoptr i64 %22 to i8*
ret i8* %23
}