#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*/ locale_t;

/* Variables and functions */
int /*<<< orphan*/ LC_MESSAGES_MASK;
int /*<<< orphan*/ freelocale(int /*<<< orphan*/ const);
int /*<<< orphan*/ newlocale(int /*<<< orphan*/, char *, int /*<<< orphan*/);
char *strerror_l(int, int /*<<< orphan*/ const);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *error_to_str(int errnum, char *buf, size_t buflen) {
  const char *non_mem = "Unable to translate errno to text, newlocale fail";
  if (buf && buflen >= 1024) {
    locale_t const locale = newlocale(LC_MESSAGES_MASK, "", (locale_t)0);
    if (!locale) {
      strncpy(buf, non_mem, buflen - 1);
      buf[buflen - 1] = '\0';
    } else {
      char *s = strerror_l(errnum, locale);
      strncpy(buf, s, buflen - 1);
      buf[buflen - 1] = '\0';
      freelocale(locale);
    }
    return buf;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<error_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xea6(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     120e <error_to_str+0xce>
cmpq   $0x400,-0x20(%rbp)
jb     120e <error_to_str+0xce>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    0xeaa(%rip),%rsi
xor    %edx,%edx
call   1050 <newlocale@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11c5 <error_to_str+0x85>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1201 <error_to_str+0xc1>
mov    -0xc(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1060 <strerror_l@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%edi
call   1040 <freelocale@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1216 <error_to_str+0xd6>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<error_to_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
test   %rsi,%rsi
je     11c1 <error_to_str+0x81>
mov    %rdx,%r14
cmp    $0x400,%rdx
jb     11c1 <error_to_str+0x81>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
lea    0xec1(%rip),%rsi
xor    %edx,%edx
call   1050 <newlocale@plt>
test   %eax,%eax
je     11a5 <error_to_str+0x65>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %eax,%esi
call   1060 <strerror_l@plt>
lea    -0x1(%r14),%rdx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%r14,1)
mov    %ebp,%edi
call   1040 <freelocale@plt>
jmp    11be <error_to_str+0x7e>
lea    -0x1(%r14),%rdx
lea    0xe50(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%r14,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [50 x i8] c"Unable to translate errno to text, newlocale fail\00"
@LC_MESSAGES_MASK = global i32 0
@.str.1 = [1 x i8] zeroinitializer
define i8* @error_to_str(i32 %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8** %8
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %48
13: ; preds = %3
%14 = load i64, i64* %7
%15 = icmp uge i64 %14, 1024
br i1 %15, label %16, label %48
16: ; preds = %13
%17 = load i32, i32* @LC_MESSAGES_MASK
%18 = call i32 @newlocale(i32 %17, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp ne i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = load i8*, i8** %8
%24 = load i64, i64* %7
%25 = sub i64 %24, 1
%26 = call i32 @strncpy(i8* %22, i8* %23, i64 %25)
%27 = load i8*, i8** %6
%28 = load i64, i64* %7
%29 = sub i64 %28, 1
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
br label %46
31: ; preds = %16
%32 = load i32, i32* %5
%33 = load i32, i32* %9
%34 = call i8* @strerror_l(i32 %32, i32 %33)
store i8* %34, i8** %10
%35 = load i8*, i8** %6
%36 = load i8*, i8** %10
%37 = load i64, i64* %7
%38 = sub i64 %37, 1
%39 = call i32 @strncpy(i8* %35, i8* %36, i64 %38)
%40 = load i8*, i8** %6
%41 = load i64, i64* %7
%42 = sub i64 %41, 1
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i32, i32* %9
%45 = call i32 @freelocale(i32 %44)
br label %46
46: ; preds = %31, %21
%47 = load i8*, i8** %6
store i8* %47, i8** %4
br label %49
48: ; preds = %13, %3
store i8* null, i8** %4
br label %49
49: ; preds = %48, %46
%50 = load i8*, i8** %4
ret i8* %50
}

LLVM IR (-O2):
@.str = [50 x i8] c"Unable to translate errno to text, newlocale fail\00"
@LC_MESSAGES_MASK = global i32 0
@.str.1 = [1 x i8] zeroinitializer
define i8* @error_to_str(i32 %0, i8* %1, i64 %2) {
%4 = icmp ne i8* %1, null
%5 = icmp ugt i64 %2, 1023
%6 = and i1 %4, %5
br i1 %6, label %7, label %21
7: ; preds = %3
%8 = load i32, i32* @LC_MESSAGES_MASK
%9 = tail call i32 @newlocale(i32 %8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i32 0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = add i64 %2, -1
%13 = tail call i32 @strncpy(i8* %1, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i64 %12)
%14 = getelementptr inbounds i8, i8* %1, i64 %12
store i8 0, i8* %14
br label %21
15: ; preds = %7
%16 = tail call i8* @strerror_l(i32 %0, i32 %9)
%17 = add i64 %2, -1
%18 = tail call i32 @strncpy(i8* %1, i8* %16, i64 %17)
%19 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 0, i8* %19
%20 = tail call i32 @freelocale(i32 %9)
br label %21
21: ; preds = %3, %11, %15
%22 = phi i8* [ %1, %15 ], [ %1, %11 ], [ null, %3 ]
ret i8* %22
}