#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  char *thousands_sep;
};

/* Variables and functions */
int /*<<< orphan*/ LC_NUMERIC;
int /*<<< orphan*/ aeron_format_number_next(long long, char const *, char *,
                                            size_t);
TYPE_1__ *localeconv();
int /*<<< orphan*/ setlocale(int /*<<< orphan*/, char *);
int strlen(char *);
char *aeron_format_number_to_locale(long long value, char *buffer,
                                    size_t buffer_len) {
  setlocale(LC_NUMERIC, "");
  const char *sep = 1 == strlen(localeconv()->thousands_sep)
                        ? localeconv()->thousands_sep
                        : ",";
  aeron_format_number_next(value, sep, buffer, buffer_len);
  buffer[buffer_len - 1] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<aeron_format_number_to_locale>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
lea    0xe9d(%rip),%rsi
call   1050 <setlocale@plt>
mov    $0x0,%al
call   1030 <localeconv@plt>
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    119a <aeron_format_number_to_locale+0x5a>
mov    $0x0,%al
call   1030 <localeconv@plt>
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11aa <aeron_format_number_to_locale+0x6a>
lea    0xe5f(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11aa <aeron_format_number_to_locale+0x6a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1060 <aeron_format_number_next@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeron_format_number_to_locale>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
lea    0xea3(%rip),%rsi
call   1050 <setlocale@plt>
xor    %eax,%eax
call   1030 <localeconv@plt>
mov    (%rax),%rdi
call   1040 <strlen@plt>
cmp    $0x1,%eax
jne    1183 <aeron_format_number_to_locale+0x43>
xor    %eax,%eax
call   1030 <localeconv@plt>
mov    (%rax),%rsi
jmp    118a <aeron_format_number_to_locale+0x4a>
lea    0xe76(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1060 <aeron_format_number_next@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_2__ = type { i8* }
@LC_NUMERIC = global i32 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c",\00"
define i8* @aeron_format_number_to_locale(i64 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i64 %0, i64* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i32, i32* @LC_NUMERIC
%9 = call i32 @setlocale(i32 %8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%10 = call %struct.TYPE_2__* (...) @localeconv()
%11 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %10, i32 0, i32 0
%12 = load i8*, i8** %11
%13 = call i32 @strlen(i8* %12)
%14 = icmp eq i32 1, %13
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = call %struct.TYPE_2__* (...) @localeconv()
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
%18 = load i8*, i8** %17
br label %20
19: ; preds = %3
br label %20
20: ; preds = %19, %15
%21 = phi i8* [ %18, %15 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %19 ]
store i8* %21, i8** %7
%22 = load i64, i64* %4
%23 = load i8*, i8** %7
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = call i32 @aeron_format_number_next(i64 %22, i8* %23, i8* %24, i64 %25)
%27 = load i8*, i8** %5
%28 = load i64, i64* %6
%29 = sub i64 %28, 1
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
%31 = load i8*, i8** %5
ret i8* %31
}

LLVM IR (-O2):
%struct.TYPE_2__ = type { i8* }
@LC_NUMERIC = global i32 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c",\00"
define i8* @aeron_format_number_to_locale(i64 %0, i8* returned %1, i64 %2) {
%4 = load i32, i32* @LC_NUMERIC
%5 = tail call i32 @setlocale(i32 %4, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%6 = tail call %struct.TYPE_2__* (...) @localeconv()
%7 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %6, i64 0, i32 0
%8 = load i8*, i8** %7
%9 = tail call i32 @strlen(i8* %8)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %3
%12 = tail call %struct.TYPE_2__* (...) @localeconv()
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i64 0, i32 0
%14 = load i8*, i8** %13
br label %15
15: ; preds = %3, %11
%16 = phi i8* [ %14, %11 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %3 ]
%17 = tail call i32 @aeron_format_number_next(i64 %0, i8* %16, i8* %1, i64 %2)
%18 = add i64 %2, -1
%19 = getelementptr inbounds i8, i8* %1, i64 %18
store i8 0, i8* %19
ret i8* %1
}