#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*/ time_t;
struct tm {
  int dummy;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_SETMASK;
int errno;
struct tm *localtime_r(int /*<<< orphan*/ const *, struct tm *);
int pthread_sigmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                    int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
struct tm *htx_localtime_r(const time_t *timep, struct tm *result) {
  sigset_t newMask, oldMask;
  struct tm *ptr;
  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;
  }
  ptr = localtime_r(timep, result);
  if (skip_reset == 0) {
    old_errno = errno;
    ret = pthread_sigmask(SIG_SETMASK, &oldMask, NULL);
    if (ret == -1) {
      errno = old_errno;
    }
  }
  return (ptr);
}


x86 assembly (clang, -O0):
<htx_localtime_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <sigemptyset@plt>
lea    -0x14(%rbp),%rdi
call   1050 <sigfillset@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1060 <pthread_sigmask@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b2 <htx_localtime_r+0x72>
mov    -0x2c(%rbp),%ecx
mov    0x2e2f(%rip),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <localtime_r@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    120d <htx_localtime_r+0xcd>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <pthread_sigmask@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1208 <htx_localtime_r+0xc8>
mov    -0x2c(%rbp),%ecx
mov    0x2dd2(%rip),%rax
mov    %ecx,(%rax)
jmp    120d <htx_localtime_r+0xcd>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htx_localtime_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%r12
mov    %r12,%rdi
call   1040 <sigemptyset@plt>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sigfillset@plt>
mov    0x2e68(%rip),%r13
mov    0x0(%r13),%ebp
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1060 <pthread_sigmask@plt>
cmp    $0xffffffff,%eax
je     11bd <htx_localtime_r+0x7d>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <localtime_r@plt>
mov    %rax,%rbx
mov    0x0(%r13),%ebp
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1060 <pthread_sigmask@plt>
cmp    $0xffffffff,%eax
jne    11cf <htx_localtime_r+0x8f>
mov    %ebp,0x0(%r13)
jmp    11cf <htx_localtime_r+0x8f>
mov    %ebp,0x0(%r13)
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <localtime_r@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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