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

/* Variables and functions */
int /*<<< orphan*/ aeron_format_min(int, size_t);
int /*<<< orphan*/ llabs(long long);
size_t snprintf(char *, int /*<<< orphan*/, char *, ...);
size_t aeron_format_number_next(long long value, const char *sep, char *buffer,
                                size_t buffer_len) {
  if (0 <= value && value < 1000) {
    return snprintf(buffer, aeron_format_min(4, buffer_len), "%lld", value);
  } else if (-1000 < value && value < 0) {
    return snprintf(buffer, aeron_format_min(5, buffer_len), "%lld", value);
  } else {
    size_t printed_len =
        aeron_format_number_next(value / 1000, sep, buffer, buffer_len);
    return printed_len + snprintf(buffer + printed_len,
                                  aeron_format_min(5, buffer_len - printed_len),
                                  "%s%03lld", sep, llabs(value % 1000));
  }
}


x86 assembly (clang, -O0):
<aeron_format_number_next>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jg     11a9 <aeron_format_number_next+0x69>
cmpq   $0x3e8,-0x10(%rbp)
jge    11a9 <aeron_format_number_next+0x69>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rsi
mov    $0x4,%edi
call   1040 <aeron_format_min@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe67(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1293 <aeron_format_number_next+0x153>
mov    $0xfffffffffffffc18,%rax
cmp    -0x10(%rbp),%rax
jge    11fc <aeron_format_number_next+0xbc>
cmpq   $0x0,-0x10(%rbp)
jge    11fc <aeron_format_number_next+0xbc>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rsi
mov    $0x5,%edi
call   1040 <aeron_format_min@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe14(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1293 <aeron_format_number_next+0x153>
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1060 <aeron_format_number_next@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    $0x5,%edi
call   1040 <aeron_format_min@plt>
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rdx,%rdi
call   1050 <llabs@plt>
mov    -0x60(%rbp),%rdi
mov    -0x54(%rbp),%esi
mov    -0x50(%rbp),%rcx
mov    %eax,%r8d
lea    0xd87(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeron_format_number_next>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rdi,%rbx
cmp    $0x3e7,%rdi
ja     1164 <aeron_format_number_next+0x24>
mov    $0x4,%edi
jmp    1172 <aeron_format_number_next+0x32>
cmp    $0xfffffffffffffc19,%rbx
jb     119e <aeron_format_number_next+0x5e>
mov    $0x5,%edi
mov    %r12,%rsi
call   1040 <aeron_format_min@plt>
lea    0xe7f(%rip),%rdx
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rcx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <snprintf@plt>
mov    %rsi,%r15
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mov    %rbx,%rax
imul   %rcx
mov    %rdx,%rbp
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rbp
add    %rax,%rbp
mov    %rbp,%rdi
mov    %r14,%rdx
mov    %r12,%rcx
call   1060 <aeron_format_number_next@plt>
mov    %rax,%r13
add    %rax,%r14
sub    %rax,%r12
mov    $0x5,%edi
mov    %r12,%rsi
call   1040 <aeron_format_min@plt>
mov    %eax,%r12d
imul   $0x3e8,%rbp,%rax
sub    %rax,%rbx
mov    %rbx,%rdi
call   1050 <llabs@plt>
lea    0xe03(%rip),%rdx
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r15,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
add    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [9 x i8] c"%s%03lld\00"
define i64 @aeron_format_number_next(i64 %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i64 %0, i64* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%11 = load i64, i64* %6
%12 = icmp sle i64 0, %11
br i1 %12, label %13, label %22
13: ; preds = %4
%14 = load i64, i64* %6
%15 = icmp slt i64 %14, 1000
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i8*, i8** %8
%18 = load i64, i64* %9
%19 = call i32 @aeron_format_min(i32 4, i64 %18)
%20 = load i64, i64* %6
%21 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %17, i32 %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %20)
store i64 %21, i64* %5
br label %55
22: ; preds = %13, %4
%23 = load i64, i64* %6
%24 = icmp slt i64 -1000, %23
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = load i64, i64* %6
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i8*, i8** %8
%30 = load i64, i64* %9
%31 = call i32 @aeron_format_min(i32 5, i64 %30)
%32 = load i64, i64* %6
%33 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %29, i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %32)
store i64 %33, i64* %5
br label %55
34: ; preds = %25, %22
%35 = load i64, i64* %6
%36 = sdiv i64 %35, 1000
%37 = load i8*, i8** %7
%38 = load i8*, i8** %8
%39 = load i64, i64* %9
%40 = call i64 @aeron_format_number_next(i64 %36, i8* %37, i8* %38, i64 %39)
store i64 %40, i64* %10
%41 = load i64, i64* %10
%42 = load i8*, i8** %8
%43 = load i64, i64* %10
%44 = getelementptr inbounds i8, i8* %42, i64 %43
%45 = load i64, i64* %9
%46 = load i64, i64* %10
%47 = sub i64 %45, %46
%48 = call i32 @aeron_format_min(i32 5, i64 %47)
%49 = load i8*, i8** %7
%50 = load i64, i64* %6
%51 = srem i64 %50, 1000
%52 = call i32 @llabs(i64 %51)
%53 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %44, i32 %48, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %49, i32 %52)
%54 = add i64 %41, %53
store i64 %54, i64* %5
br label %55
55: ; preds = %34, %28, %16
%56 = load i64, i64* %5
ret i64 %56
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [9 x i8] c"%s%03lld\00"
define i64 @aeron_format_number_next(i64 %0, i8* %1, i8* %2, i64 %3) {
%5 = icmp ult i64 %0, 1000
br i1 %5, label %6, label %9
6: ; preds = %4
%7 = tail call i32 @aeron_format_min(i32 4, i64 %3)
%8 = tail call i64 (i8*, i32, i8*, ...) @snprintf(i8* %2, i32 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %0)
br label %14
9: ; preds = %4
%10 = icmp ugt i64 %0, -1000
br i1 %10, label %11, label %16
11: ; preds = %9
%12 = tail call i32 @aeron_format_min(i32 5, i64 %3)
%13 = tail call i64 (i8*, i32, i8*, ...) @snprintf(i8* %2, i32 %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %0)
br label %14
14: ; preds = %6, %11, %16
%15 = phi i64 [ %25, %16 ], [ %8, %6 ], [ %13, %11 ]
ret i64 %15
16: ; preds = %9
%17 = sdiv i64 %0, 1000
%18 = tail call i64 @aeron_format_number_next(i64 %17, i8* %1, i8* %2, i64 %3)
%19 = getelementptr inbounds i8, i8* %2, i64 %18
%20 = sub i64 %3, %18
%21 = tail call i32 @aeron_format_min(i32 5, i64 %20)
%22 = srem i64 %0, 1000
%23 = tail call i32 @llabs(i64 %22)
%24 = tail call i64 (i8*, i32, i8*, ...) @snprintf(i8* %19, i32 %21, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %23)
%25 = add i64 %24, %18
br label %14
}