#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(int /*<<< orphan*/ const *);
int pthread_sigmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                    int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
struct tm *htx_localtime(const time_t *p_time) {
  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(p_time);
  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>:
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   1060 <pthread_sigmask@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ae <htx_localtime+0x6e>
mov    -0x24(%rbp),%ecx
mov    0x2e33(%rip),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1205 <htx_localtime+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   1060 <pthread_sigmask@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1200 <htx_localtime+0xc0>
mov    -0x24(%rbp),%ecx
mov    0x2dda(%rip),%rax
mov    %ecx,(%rax)
jmp    1205 <htx_localtime+0xc5>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htx_localtime>:
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     11bc <htx_localtime+0x7c>
mov    %r14,%rdi
call   1030 <localtime@plt>
mov    %rax,%rbx
mov    (%r12),%ebp
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1060 <pthread_sigmask@plt>
cmp    $0xffffffff,%eax
jne    11cb <htx_localtime+0x8b>
mov    %ebp,(%r12)
jmp    11cb <htx_localtime+0x8b>
mov    %ebp,(%r12)
mov    %r14,%rdi
call   1030 <localtime@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
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(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.tm*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %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 @pthread_sigmask(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 i32*, i32** %2
%20 = call %struct.tm* @localtime(i32* %19)
store %struct.tm* %20, %struct.tm** %5
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %18
%24 = load i32, i32* @errno
store i32 %24, i32* %8
%25 = load i32, i32* @SIG_SETMASK
%26 = call i32 @pthread_sigmask(i32 %25, i32* %4, i32* null)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = load i32, i32* %8
store i32 %30, i32* @errno
br label %31
31: ; preds = %29, %23
br label %32
32: ; preds = %31, %18
%33 = load %struct.tm*, %struct.tm** %5
ret %struct.tm* %33
}

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(i32* %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 %struct.tm* @localtime(i32* %0)
br label %21
14: ; preds = %1
%15 = call %struct.tm* @localtime(i32* %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
%22 = phi %struct.tm* [ %13, %12 ], [ %15, %14 ], [ %15, %20 ]
ret %struct.tm* %22
}