// File: /workspace/github_c_makefile_star2_O3/FredTheDino[P]Fog/src/engine/util/printf.c
// Function: etoa(void (*)(char, void*, unsigned long, unsigned long), char*, unsigned long, unsigned long, double, unsigned int, unsigned int, unsigned int)
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
{
  // check for NaN and special values
  if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
    return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
  }

  // determine the sign
  const bool negative = value < 0;
  if (negative) {
    value = -value;
  }

  // default precision
  if (!(flags & FLAGS_PRECISION)) {
    prec = PRINTF_DEFAULT_FLOAT_PRECISION;
  }

  // determine the decimal exponent
  // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
  union {
    uint64_t U;
    double   F;
  } conv;

  conv.F = value;
  int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023;           // effectively log2
  conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U);  // drop the exponent so conv.F is now in [1,2)
  // now approximate log10 from the log2 integer part and an expansion of ln around 1.5
  int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
  // now we want to compute 10^expval but we want to be sure it won't overflow
  exp2 = (int)(expval * 3.321928094887362 + 0.5);
  const double z  = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
  const double z2 = z * z;
  conv.U = (uint64_t)(exp2 + 1023) << 52U;
  // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
  conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
  // correct for rounding errors
  if (value < conv.F) {
    expval--;
    conv.F /= 10;
  }

  // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
  unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;

  // in "%g" mode, "prec" is the number of *significant figures* not decimals
  if (flags & FLAGS_ADAPT_EXP) {
    // do we want to fall-back to "%f" mode?
    if ((value >= 1e-4) && (value < 1e6)) {
      if ((int)prec > expval) {
        prec = (unsigned)((int)prec - expval - 1);
      }
      else {
        prec = 0;
      }
      flags |= FLAGS_PRECISION;   // make sure _ftoa respects precision
      // no characters in exponent
      minwidth = 0U;
      expval   = 0;
    }
    else {
      // we use one sigfig for the whole part
      if ((prec > 0) && (flags & FLAGS_PRECISION)) {
        --prec;
      }
    }
  }

  // will everything fit?
  unsigned int fwidth = width;
  if (width > minwidth) {
    // we didn't fall-back so subtract the characters required for the exponent
    fwidth -= minwidth;
  } else {
    // not enough characters, so go back to default sizing
    fwidth = 0U;
  }
  if ((flags & FLAGS_LEFT) && minwidth) {
    // if we're padding on the right, DON'T pad the floating part
    fwidth = 0U;
  }

  // rescale the float value
  if (expval) {
    value /= conv.F;
  }

  // output the floating part
  const size_t start_idx = idx;
  idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);

  // output the exponent part
  if (minwidth) {
    // output the exponential symbol
    out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
    // output the exponent value
    idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS);
    // might need to right-pad spaces
    if (flags & FLAGS_LEFT) {
      while (idx - start_idx < width) out(' ', buffer, idx++, maxlen);
    }
  }
  return idx;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, %r12d
movl %r8d, %eax
movq %rcx, %r9
movq %rsi, %rbx
movq %rdi, %r13
movsd 0x3c5b(%rip), %xmm1     # 0x30aa0
ucomisd %xmm0, %xmm1
ja 0x2cff7
ucomisd %xmm0, %xmm0
jp 0x2cff7
ucomisd 0x3c47(%rip), %xmm0     # 0x30aa8
ja 0x2cff7
movl 0x10(%rbp), %r14d
movapd 0x34ed(%rip), %xmm1     # 0x30360
xorpd %xmm0, %xmm1
maxsd %xmm0, %xmm1
btl $0xa, %r14d
movl $0x6, %r8d
cmovbl %eax, %r8d
movq %xmm1, %rcx
movabsq $0xfffffffffffff, %rdi  # imm = 0xFFFFFFFFFFFFF
andq %rcx, %rdi
shrq $0x34, %rcx
andl $0x7ff, %ecx            # imm = 0x7FF
addl $0xfffffc01, %ecx       # imm = 0xFFFFFC01
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
cvtsi2sd %ecx, %xmm2
mulsd 0x3bfe(%rip), %xmm2     # 0x30ac0
orq %rdi, %rsi
addsd 0x3bfb(%rip), %xmm2     # 0x30ac8
movq %rsi, %xmm3
addsd 0x3bf6(%rip), %xmm3     # 0x30ad0
mulsd 0x3bf6(%rip), %xmm3     # 0x30ad8
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %edi
cvttpd2dq %xmm3, %xmm2
cvtdq2pd %xmm2, %xmm3
movsd 0x3be6(%rip), %xmm2     # 0x30ae0
mulsd %xmm3, %xmm2
addsd 0x3b52(%rip), %xmm2     # 0x30a58
cvttsd2si %xmm2, %ecx
cvttpd2dq %xmm2, %xmm2
cvtdq2pd %xmm2, %xmm2
mulsd 0x3bce(%rip), %xmm2     # 0x30ae8
mulsd 0x3bce(%rip), %xmm3     # 0x30af0
addsd %xmm2, %xmm3
movapd %xmm3, %xmm2
mulsd %xmm3, %xmm2
addl $0x3ff, %ecx            # imm = 0x3FF
movsd 0x3bbc(%rip), %xmm5     # 0x30af8
subsd %xmm3, %xmm5
movapd %xmm2, %xmm6
divsd 0x3bb4(%rip), %xmm6     # 0x30b00
addsd %xmm3, %xmm3
movsd 0x3bb0(%rip), %xmm4     # 0x30b08
addsd %xmm4, %xmm6
movapd %xmm2, %xmm7
divsd %xmm6, %xmm7
addsd 0x3ba4(%rip), %xmm7     # 0x30b10
divsd %xmm7, %xmm2
addsd %xmm5, %xmm2
divsd %xmm2, %xmm3
addsd 0x30d8(%rip), %xmm3     # 0x30058
shlq $0x34, %rcx
movq %rcx, %xmm2
mulsd %xmm3, %xmm2
xorl %ecx, %ecx
ucomisd %xmm1, %xmm2
seta %sil
movq %r9, -0x40(%rbp)
jbe 0x2cfa1
divsd %xmm4, %xmm2
movb %sil, %cl
subl %ecx, %edi
leal 0x63(%rdi), %ecx
cmpl $0xc7, %ecx
movl $0x5, %r15d
sbbl $0x0, %r15d
btl $0xb, %r14d
jae 0x2d034
ucomisd 0x3b50(%rip), %xmm1     # 0x30b18
jb 0x2d019
movsd 0x3b4e(%rip), %xmm3     # 0x30b20
ucomisd %xmm1, %xmm3
jbe 0x2d019
movl %edi, %eax
notl %eax
addl %r8d, %eax
xorl %r15d, %r15d
cmpl %edi, %r8d
cmovlel %r15d, %eax
orl $0x400, %r14d           # imm = 0x400
movl %eax, %r8d
xorl %edi, %edi
jmp 0x2d034
movq %r13, %rdi
movq %rbx, %rsi
movq %r9, %rcx
movl %eax, %r8d
movl %r12d, %r9d
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c99f
testl %r8d, %r8d
je 0x2d031
decl %eax
btl $0xa, %r14d
movl $0x6, %r8d
cmovbl %eax, %r8d
jmp 0x2d034
xorl %r8d, %r8d
xorl %r9d, %r9d
movl %r12d, %eax
subl %r15d, %eax
cmovbl %r9d, %eax
testl %r15d, %r15d
cmovel %eax, %r9d
testb $0x2, %r14b
cmovel %eax, %r9d
testl %edi, %edi
movq %rdi, -0x30(%rbp)
je 0x2d05c
divsd %xmm2, %xmm1
movapd 0x32fc(%rip), %xmm2     # 0x30360
xorpd %xmm1, %xmm2
xorpd %xmm3, %xmm3
cmpltsd %xmm3, %xmm0
movapd %xmm0, %xmm3
andnpd %xmm1, %xmm3
andpd %xmm2, %xmm0
orpd %xmm3, %xmm0
movl %r14d, %eax
andl $0xfffff7ff, %eax       # imm = 0xFFFFF7FF
subq $0x8, %rsp
movq %r13, %rdi
movq %rbx, %rsi
movq %rdx, -0x38(%rbp)
movq -0x40(%rbp), %rcx
pushq %rax
callq 0x2c99f
addq $0x10, %rsp
movl %r14d, %ecx
movq %rax, %r14
testl %r15d, %r15d
je 0x2d16c
movl %r12d, -0x44(%rbp)
xorl %edi, %edi
testb $0x20, %cl
sete %dil
movq %r14, %r12
incq %r12
shll $0x5, %edi
orl $0x45, %edi
movq %rbx, %rsi
movq %r14, %rdx
movl %ecx, -0x48(%rbp)
movq -0x40(%rbp), %r14
movq %r14, %rcx
callq *%r13
movq -0x30(%rbp), %r9
movl %r9d, %r8d
negl %r8d
cmovsl %r9d, %r8d
decl %r15d
shrl $0x1f, %r9d
movq %r13, -0x30(%rbp)
movq %r13, %rdi
movq %rbx, -0x50(%rbp)
movq %rbx, %rsi
movq %r12, %rdx
movq %r14, %rcx
pushq $0x5
pushq %r15
pushq $0x0
pushq $0xa
callq 0x2c8d4
addq $0x20, %rsp
movq %rax, %r13
testb $0x2, -0x48(%rbp)
je 0x2d169
movl -0x44(%rbp), %r15d
movq %r13, %rax
subq -0x38(%rbp), %rax
cmpq %r15, %rax
jae 0x2d169
negq -0x38(%rbp)
movq -0x50(%rbp), %rbx
movq -0x30(%rbp), %r12
leaq 0x1(%r13), %r14
movl $0x20, %edi
movq %rbx, %rsi
movq %r13, %rdx
movq -0x40(%rbp), %rcx
callq *%r12
movq -0x38(%rbp), %rax
addq %r13, %rax
incq %rax
movq %r14, %r13
cmpq %r15, %rax
jb 0x2d13f
jmp 0x2d16c
movq %r13, %r14
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq