// File: /workspace/github_c_makefile_star2_O3/FredTheDino[P]Fog/src/engine/util/printf.c
// Function: ftoa(void (*)(char, void*, unsigned long, unsigned long), char*, unsigned long, unsigned long, double, unsigned int, unsigned int, unsigned int)
static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
{
  char buf[PRINTF_FTOA_BUFFER_SIZE];
  size_t len  = 0U;
  double diff = 0.0;

  // powers of 10
  static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };

  // test for special values
  if (value != value)
    return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags);
  if (value < -DBL_MAX)
    return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
  if (value > DBL_MAX)
    return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags);

  // test for very large values
  // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
  if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) {
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
    return _etoa(out, buffer, idx, maxlen, value, prec, width, flags);
#else
    return 0U;
#endif
  }

  // test for negative
  bool negative = false;
  if (value < 0) {
    negative = true;
    value = 0 - value;
  }

  // set default precision, if not set explicitly
  if (!(flags & FLAGS_PRECISION)) {
    prec = PRINTF_DEFAULT_FLOAT_PRECISION;
  }
  // limit precision to 9, cause a prec >= 10 can lead to overflow errors
  while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
    buf[len++] = '0';
    prec--;
  }

  int whole = (int)value;
  double tmp = (value - whole) * pow10[prec];
  unsigned long frac = (unsigned long)tmp;
  diff = tmp - frac;

  if (diff > 0.5) {
    ++frac;
    // handle rollover, e.g. case 0.99 with prec 1 is 1.0
    if (frac >= pow10[prec]) {
      frac = 0;
      ++whole;
    }
  }
  else if (diff < 0.5) {
  }
  else if ((frac == 0U) || (frac & 1U)) {
    // if halfway, round up if odd OR if last digit is 0
    ++frac;
  }

  if (prec == 0U) {
    diff = value - (double)whole;
    if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
      // exactly 0.5 and ODD, then round up
      // 1.5 -> 2, but 2.5 -> 2
      ++whole;
    }
  }
  else {
    unsigned int count = prec;
    // now do fractional part, as an unsigned number
    while (len < PRINTF_FTOA_BUFFER_SIZE) {
      --count;
      buf[len++] = (char)(48U + (frac % 10U));
      if (!(frac /= 10U)) {
        break;
      }
    }
    // add extra 0s
    while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
      buf[len++] = '0';
    }
    if (len < PRINTF_FTOA_BUFFER_SIZE) {
      // add decimal
      buf[len++] = '.';
    }
  }

  // do whole part, number is reversed
  while (len < PRINTF_FTOA_BUFFER_SIZE) {
    buf[len++] = (char)(48 + (whole % 10));
    if (!(whole /= 10)) {
      break;
    }
  }

  // pad leading zeros
  if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
    if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
      width--;
    }
    while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
      buf[len++] = '0';
    }
  }

  if (len < PRINTF_FTOA_BUFFER_SIZE) {
    if (negative) {
      buf[len++] = '-';
    }
    else if (flags & FLAGS_PLUS) {
      buf[len++] = '+';  // ignore the space if the '+' exists
    }
    else if (flags & FLAGS_SPACE) {
      buf[len++] = ' ';
    }
  }

  return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r9d, %r10d
movq %rsi, %rbx
movl 0x10(%rbp), %r12d
ucomisd %xmm0, %xmm0
jp 0x2ce0b
movapd %xmm0, %xmm4
movsd 0x40d0(%rip), %xmm0     # 0x30aa0
ucomisd %xmm4, %xmm0
jbe 0x2c9e5
leaq 0x5c04(%rip), %r8       # 0x325e1
movl $0x4, %r9d
jmp 0x2ca10
ucomisd 0x40bb(%rip), %xmm4     # 0x30aa8
jbe 0x2ca2e
xorl %r9d, %r9d
testb $0x4, %r12b
leaq 0x5bee(%rip), %rax      # 0x325eb
leaq 0x5be2(%rip), %r8       # 0x325e6
cmoveq %rax, %r8
setne %r9b
addq $0x3, %r9
movq %rbx, %rsi
pushq %r12
pushq %r10
callq 0x2d321
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r8d, %r15d
movapd 0x3977(%rip), %xmm0     # 0x303b0
andpd %xmm4, %xmm0
ucomisd 0x406b(%rip), %xmm0     # 0x30ab0
jbe 0x2ca67
movq %rbx, %rsi
movapd %xmm4, %xmm0
movl %r15d, %r8d
movl %r10d, %r9d
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2ce1d
movq %rdi, -0x98(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %r10, -0x48(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm4, %xmm5
cmpltsd %xmm0, %xmm5
subsd %xmm4, %xmm0
andpd %xmm5, %xmm0
andnpd %xmm4, %xmm5
orpd %xmm0, %xmm5
btl $0xa, %r12d
movl $0x6, %esi
cmovbl %r15d, %esi
cmpl $0xa, %esi
movapd %xmm4, -0x60(%rbp)
jb 0x2cb02
movapd %xmm5, -0x70(%rbp)
addl $-0xa, %esi
cmpl $0x1f, %esi
movl $0x1f, %r13d
cmovbl %esi, %r13d
incl %r13d
leaq -0x90(%rbp), %rdi
movl $0x30, %esi
movq %r13, %rdx
callq 0x21c0
xorl %ecx, %ecx
incq %rcx
cmpq %rcx, %r13
jne 0x2cadd
leaq -0x1(%rcx), %rax
cmpq $0x1f, %rax
setb %al
subl %ecx, %r15d
movl %r15d, %esi
movapd -0x60(%rbp), %xmm4
movapd -0x70(%rbp), %xmm5
jmp 0x2cb07
movb $0x1, %al
xorl %r13d, %r13d
cvttsd2si %xmm5, %r15d
cvttpd2dq %xmm5, %xmm0
cvtdq2pd %xmm0, %xmm0
movapd %xmm5, %xmm1
subsd %xmm0, %xmm1
movl %esi, %r8d
leaq 0x3eba(%rip), %rcx      # 0x309e0
movsd (%rcx,%r8,8), %xmm0
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rdx
movq %rdx, %rdi
movapd %xmm1, %xmm2
subsd 0x3f74(%rip), %xmm2     # 0x30ab8
cvttsd2si %xmm2, %rcx
sarq $0x3f, %rdi
andq %rdi, %rcx
orq %rdx, %rcx
movq %rcx, %xmm2
punpckldq 0x3760(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x3768(%rip), %xmm2     # 0x302d0
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
subsd %xmm3, %xmm1
ucomisd 0x3ed8(%rip), %xmm1     # 0x30a58
jbe 0x2cbb3
incq %rcx
movq %rcx, %xmm1
punpckldq 0x372e(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x3736(%rip), %xmm1     # 0x302d0
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jb 0x2cbd6
incl %r15d
xorl %ecx, %ecx
jmp 0x2cbd6
movsd 0x3e9d(%rip), %xmm0     # 0x30a58
ucomisd %xmm1, %xmm0
ja 0x2cbd6
testq %rcx, %rcx
sete %dl
movl %ecx, %edi
orb %dl, %dil
movzbl %dil, %edx
andl $0x1, %edx
addq %rdx, %rcx
movq %rbx, -0x70(%rbp)
testl %esi, %esi
je 0x2cccb
movl $0x20, %r14d
testb %al, %al
je 0x2cd43
movq %r8, -0x40(%rbp)
leaq -0x90(,%r13), %rdi
addq %rbp, %rdi
xorl %r8d, %r8d
movabsq $-0x3333333333333333, %r9 # imm = 0xCCCCCCCCCCCCCCCD
leaq (%r8,%r13), %r10
movq %rcx, %rax
mulq %r9
shrq $0x3, %rdx
leal (%rdx,%rdx), %eax
leal (%rax,%rax,4), %eax
movl %ecx, %r11d
subl %eax, %r11d
orb $0x30, %r11b
movb %r11b, (%rdi,%r8)
incq %r8
cmpq $0x1e, %r10
ja 0x2cc40
cmpq $0x9, %rcx
movq %rdx, %rcx
ja 0x2cc0c
leaq (%r8,%r13), %rbx
leaq (%r8,%r13), %rax
decq %rax
cmpq $0x1f, %rax
setae %al
jae 0x2ccb3
cmpl %r8d, %esi
je 0x2ccb3
subq %r8, -0x40(%rbp)
addq %r8, %r13
leaq -0x90(,%r13), %rdi
addq %rbp, %rdi
notl %r8d
addl %esi, %r8d
movl $0x1f, %edx
subq %r13, %rdx
cmpq %rdx, %r8
cmovbq %r8, %rdx
incq %rdx
movl $0x30, %esi
callq 0x21c0
movq -0x40(%rbp), %rsi
movapd -0x60(%rbp), %xmm4
movl $0x1, %ecx
movq %rbx, %rax
incq %rbx
cmpq $0x1f, %rax
setae %al
jae 0x2ccb3
leal 0x1(%rcx), %edx
cmpl %ecx, %esi
movl %edx, %ecx
jne 0x2cc9b
testb %al, %al
jne 0x2cd43
movb $0x2e, -0x90(%rbp,%rbx)
incq %rbx
movq %rbx, %r13
jmp 0x2ccf1
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
subsd %xmm0, %xmm5
movsd 0x3d79(%rip), %xmm0     # 0x30a58
ucomisd %xmm5, %xmm0
setbe %al
movl %r15d, %ecx
andb %al, %cl
movzbl %cl, %eax
addl %eax, %r15d
movl $0x20, %r14d
cmpq $0x1f, %r13
ja 0x2cd43
movq %r13, %r14
movq %r14, %rcx
movslq %r15d, %rdx
imulq $0x66666667, %rdx, %rax # imm = 0x66666667
movq %rax, %rsi
shrq $0x3f, %rsi
sarq $0x22, %rax
addl %esi, %eax
leal (%rax,%rax), %esi
leal (%rsi,%rsi,4), %esi
subl %esi, %edx
addb $0x30, %dl
movb %dl, -0x90(%rbp,%r14)
incq %r14
cmpq $0x1e, %rcx
ja 0x2cd43
addl $-0xa, %r15d
cmpl $-0x13, %r15d
movl %eax, %r15d
jb 0x2cd00
movl %r12d, %eax
andl $0x3, %eax
cmpl $0x1, %eax
jne 0x2cd73
movq -0x48(%rbp), %r15
testl %r15d, %r15d
je 0x2cd7c
xorpd %xmm0, %xmm0
ucomisd %xmm4, %xmm0
seta %al
testb $0xc, %r12b
setne %cl
orb %al, %cl
movzbl %cl, %eax
subl %eax, %r15d
jmp 0x2cd7f
movq %r14, %rbx
movq -0x48(%rbp), %r15
jmp 0x2cdb3
xorl %r15d, %r15d
cmpl $0x20, %r15d
movl $0x20, %ebx
cmovbl %r15d, %ebx
movq %rbx, %rdx
subq %r14, %rdx
jbe 0x2cdb0
leaq (%r14,%rbp), %rdi
addq $-0x90, %rdi
movl $0x30, %esi
callq 0x21c0
movapd -0x60(%rbp), %xmm4
jmp 0x2cdb3
movq %r14, %rbx
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rdx
movl $0x20, %r9d
cmpq $0x1f, %rbx
movq -0x70(%rbp), %rsi
ja 0x2cdf4
movb $0x2d, %al
xorpd %xmm0, %xmm0
ucomisd %xmm4, %xmm0
ja 0x2cde7
movb $0x2b, %al
testb $0x4, %r12b
jne 0x2cde7
movb $0x20, %al
testb $0x8, %r12b
je 0x2cdf1
movb %al, -0x90(%rbp,%rbx)
incq %rbx
movq %rbx, %r9
leaq -0x90(%rbp), %r8
movq -0x98(%rbp), %rdi
pushq %r12
pushq %r15
jmp 0x2ca17
leaq 0x57cb(%rip), %r8       # 0x325dd
movl $0x3, %r9d
jmp 0x2ca10