// File: /workspace/github_c_makefile_star2_O0/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
subq $0x100, %rsp            # imm = 0x100
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movsd %xmm0, -0x30(%rbp)
movl %r8d, -0x34(%rbp)
movl %r9d, -0x38(%rbp)
movsd -0x30(%rbp), %xmm0
ucomisd -0x30(%rbp), %xmm0
jne 0x3fbfb
jp 0x3fbfb
movsd -0x30(%rbp), %xmm0
movsd 0x5e92(%rip), %xmm1     # 0x45a78
ucomisd %xmm1, %xmm0
ja 0x3fbfb
movsd 0x5e7c(%rip), %xmm0     # 0x45a70
ucomisd -0x30(%rbp), %xmm0
jbe 0x3fc2c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movsd -0x30(%rbp), %xmm0
movl -0x34(%rbp), %r8d
movl -0x38(%rbp), %r9d
movl 0x10(%rbp), %eax
movl %eax, (%rsp)
callq 0x3f590
movq %rax, -0x8(%rbp)
jmp 0x4012f
xorps %xmm0, %xmm0
ucomisd -0x30(%rbp), %xmm0
seta %al
andb $0x1, %al
movb %al, -0x39(%rbp)
testb $0x1, -0x39(%rbp)
je 0x3fc63
movsd -0x30(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x30(%rbp)
movl 0x10(%rbp), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
jne 0x3fc77
movl $0x6, -0x34(%rbp)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x48(%rbp), %rax
shrq $0x34, %rax
andq $0x7ff, %rax            # imm = 0x7FF
subl $0x3ff, %eax            # imm = 0x3FF
movl %eax, -0x4c(%rbp)
movabsq $0xfffffffffffff, %rax  # imm = 0xFFFFFFFFFFFFF
andq -0x48(%rbp), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
orq %rcx, %rax
movq %rax, -0x48(%rbp)
cvtsi2sdl -0x4c(%rbp), %xmm1
movsd 0x5e0d(%rip), %xmm0     # 0x45ad0
mulsd %xmm0, %xmm1
movsd 0x5e09(%rip), %xmm0     # 0x45ad8
addsd %xmm0, %xmm1
movsd -0x48(%rbp), %xmm0
movsd 0x5d70(%rip), %xmm2     # 0x45a50
subsd %xmm2, %xmm0
movsd 0x5ddc(%rip), %xmm2     # 0x45ac8
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x50(%rbp)
cvtsi2sdl -0x50(%rbp), %xmm0
movsd 0x5db8(%rip), %xmm1     # 0x45ac0
mulsd %xmm1, %xmm0
movsd 0x533c(%rip), %xmm1     # 0x45050
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x4c(%rbp)
cvtsi2sdl -0x50(%rbp), %xmm0
cvtsi2sdl -0x4c(%rbp), %xmm1
movsd 0x5d87(%rip), %xmm2     # 0x45ab8
mulsd %xmm2, %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
movsd 0x5d5c(%rip), %xmm2     # 0x45ab0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x4c(%rbp), %eax
addl $0x3ff, %eax            # imm = 0x3FF
cltq
shlq $0x34, %rax
movq %rax, -0x48(%rbp)
movsd 0x5cae(%rip), %xmm1     # 0x45a38
mulsd -0x58(%rbp), %xmm1
movsd 0x5ca1(%rip), %xmm0     # 0x45a38
subsd -0x58(%rbp), %xmm0
movsd -0x60(%rbp), %xmm2
movsd -0x60(%rbp), %xmm4
movsd -0x60(%rbp), %xmm5
movsd 0x5cf5(%rip), %xmm3     # 0x45aa8
divsd %xmm3, %xmm5
movsd 0x5ce1(%rip), %xmm3     # 0x45aa0
addsd %xmm5, %xmm3
divsd %xmm3, %xmm4
movsd 0x5cc9(%rip), %xmm3     # 0x45a98
addsd %xmm4, %xmm3
divsd %xmm3, %xmm2
addsd %xmm2, %xmm0
divsd %xmm0, %xmm1
movsd 0x5279(%rip), %xmm0     # 0x45060
addsd %xmm1, %xmm0
mulsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x30(%rbp), %xmm1
movsd -0x48(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3fe24
movl -0x50(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x50(%rbp)
movsd -0x48(%rbp), %xmm0
movsd 0x5c85(%rip), %xmm1     # 0x45aa0
divsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
xorl %eax, %eax
cmpl $0x64, -0x50(%rbp)
movb %al, -0x71(%rbp)
jge 0x3fe39
cmpl $-0x64, -0x50(%rbp)
setg %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %dl
movl $0x5, %eax
movl $0x4, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x64(%rbp)
movl 0x10(%rbp), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x3fed6
movsd -0x30(%rbp), %xmm0
movsd 0x5c77(%rip), %xmm1     # 0x45ae0
ucomisd %xmm1, %xmm0
jb 0x3feb6
movsd 0x5c71(%rip), %xmm0     # 0x45ae8
ucomisd -0x30(%rbp), %xmm0
jbe 0x3feb6
movl -0x34(%rbp), %eax
cmpl -0x50(%rbp), %eax
jle 0x3fe94
movl -0x34(%rbp), %eax
subl -0x50(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3fe9b
movl $0x0, -0x34(%rbp)
movl 0x10(%rbp), %eax
orl $0x400, %eax            # imm = 0x400
movl %eax, 0x10(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x50(%rbp)
jmp 0x3fed4
cmpl $0x0, -0x34(%rbp)
jbe 0x3fed2
movl 0x10(%rbp), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
je 0x3fed2
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3fed4
jmp 0x3fed6
movl -0x38(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x64(%rbp), %eax
jbe 0x3fef1
movl -0x64(%rbp), %ecx
movl -0x68(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x68(%rbp)
jmp 0x3fef8
movl $0x0, -0x68(%rbp)
movl 0x10(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x3ff10
cmpl $0x0, -0x64(%rbp)
je 0x3ff10
movl $0x0, -0x68(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x3ff29
movsd -0x48(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
testb $0x1, -0x39(%rbp)
je 0x3ff86
movsd -0x30(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0xa0(%rbp)
jmp 0x3ff93
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movq -0x80(%rbp), %rcx
movq -0x88(%rbp), %rdx
movq -0x90(%rbp), %rsi
movq -0x98(%rbp), %rdi
movsd -0xa0(%rbp), %xmm0
movl -0x34(%rbp), %r8d
movl -0x68(%rbp), %r9d
movl 0x10(%rbp), %eax
andl $0xfffff7ff, %eax       # imm = 0xFFFFF7FF
movl %eax, (%rsp)
callq 0x3f590
movq %rax, -0x20(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0x40127
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl 0x10(%rbp), %ecx
andl $0x20, %ecx
movb $0x45, %al
movb $0x65, %dl
movb %dl, -0xa2(%rbp)
cmpl $0x0, %ecx
movb %al, -0xa1(%rbp)
jne 0x4000c
movb -0xa2(%rbp), %al
movb %al, -0xa1(%rbp)
movq -0xb0(%rbp), %rax
movb -0xa1(%rbp), %dil
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq %rdx, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movq -0x28(%rbp), %rcx
movsbl %dil, %edi
callq *%rax
movq -0x10(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xb8(%rbp)
cmpl $0x0, -0x50(%rbp)
jge 0x40076
xorl %eax, %eax
subl -0x50(%rbp), %eax
movl %eax, -0xd4(%rbp)
jmp 0x4007f
movl -0x50(%rbp), %eax
movl %eax, -0xd4(%rbp)
movq -0xb8(%rbp), %rcx
movq -0xc0(%rbp), %rdx
movq -0xc8(%rbp), %rsi
movq -0xd0(%rbp), %rdi
movl -0xd4(%rbp), %eax
movslq %eax, %r8
cmpl $0x0, -0x50(%rbp)
setl %al
movl -0x64(%rbp), %r10d
subl $0x1, %r10d
movq %rsp, %r9
movl %r10d, 0x10(%r9)
movl $0x5, 0x18(%r9)
movl $0x0, 0x8(%r9)
movq $0xa, (%r9)
movzbl %al, %r9d
andl $0x1, %r9d
callq 0x3f420
movq %rax, -0x20(%rbp)
movl 0x10(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x40125
jmp 0x400ef
movq -0x20(%rbp), %rax
subq -0x70(%rbp), %rax
movl -0x38(%rbp), %ecx
cmpq %rcx, %rax
jae 0x40123
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq %rdx, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movq -0x28(%rbp), %rcx
movl $0x20, %edi
callq *%rax
jmp 0x400ef
jmp 0x40125
jmp 0x40127
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)