// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movl %r9d, %r10d
movq %rdx, %rbx
movl 0x10(%rbp), %r15d
ucomisd %xmm0, %xmm0
jp 0x28aa3
movsd 0x32ca(%rip), %xmm1     # 0x2ba78
ucomisd %xmm0, %xmm1
jbe 0x287c4
leaq 0x4dfe(%rip), %r8       # 0x2d5b9
pushq $0x4
popq %r9
jmp 0x28a86
ucomisd 0x32b4(%rip), %xmm0     # 0x2ba80
jbe 0x287f4
xorl %r9d, %r9d
testb $0x4, %r15b
leaq 0x4de7(%rip), %rax      # 0x2d5c3
leaq 0x4ddb(%rip), %r8       # 0x2d5be
cmoveq %rax, %r8
setne %r9b
addq $0x3, %r9
jmp 0x28a86
movapd 0x2ba4(%rip), %xmm1     # 0x2b3a0
andpd %xmm0, %xmm1
ucomisd 0x3280(%rip), %xmm1     # 0x2ba88
jbe 0x28823
movq %rbx, %rdx
movl %r10d, %r9d
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x28ab1
xorpd %xmm2, %xmm2
subsd %xmm0, %xmm2
btl $0xa, %r15d
pushq $0x6
popq %rax
cmovael %eax, %r8d
leaq 0x3182(%rip), %rax      # 0x2b9c0
leaq (%rax,%r8,8), %rdx
xorl %r9d, %r9d
movl %r8d, %eax
cmpq $0x1f, %r9
ja 0x28864
cmpl $0xa, %eax
jb 0x28864
movb $0x30, -0x50(%rbp,%r9)
incq %r9
decl %eax
addq $-0x8, %rdx
jmp 0x28848
xorpd %xmm3, %xmm3
movapd %xmm0, %xmm1
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
cvttsd2si %xmm1, %r11d
cvttpd2dq %xmm1, %xmm2
cvtdq2pd %xmm2, %xmm2
movapd %xmm1, %xmm3
subsd %xmm2, %xmm3
movsd (%rdx), %xmm2
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %rdx
movq %rdx, %r12
movapd %xmm3, %xmm4
subsd 0x31e2(%rip), %xmm4     # 0x2ba90
cvttsd2si %xmm4, %r14
sarq $0x3f, %r12
andq %r12, %r14
orq %rdx, %r14
movq %r14, %xmm4
punpckldq 0x29e6(%rip), %xmm4 # xmm4 = xmm4[0],mem[0],xmm4[1],mem[1]
subpd 0x29ee(%rip), %xmm4     # 0x2b2c0
movapd %xmm4, %xmm5
unpckhpd %xmm4, %xmm5            # xmm5 = xmm5[1],xmm4[1]
addsd %xmm4, %xmm5
subsd %xmm5, %xmm3
ucomisd 0x3146(%rip), %xmm3     # 0x2ba30
jbe 0x2891e
incq %r14
movq %r14, %xmm3
punpckldq 0x29b4(%rip), %xmm3 # xmm3 = xmm3[0],mem[0],xmm3[1],mem[1]
subpd 0x29bc(%rip), %xmm3     # 0x2b2c0
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
ucomisd %xmm2, %xmm4
jb 0x28942
incl %r11d
xorl %r14d, %r14d
jmp 0x28942
movsd 0x310a(%rip), %xmm2     # 0x2ba30
ucomisd %xmm3, %xmm2
ja 0x28942
testq %r14, %r14
sete %dl
movl %r14d, %r12d
orb %dl, %r12b
movzbl %r12b, %edx
andl $0x1, %edx
addq %rdx, %r14
testl %eax, %eax
je 0x2897d
cmpq $0x1f, %r9
ja 0x289a2
pushq $0xa
popq %r12
movq %r14, %rax
xorl %edx, %edx
divq %r12
movq %r9, %r13
orb $0x30, %dl
movb %dl, -0x50(%rbp,%r9)
incq %r9
cmpq $0x1e, %r13
ja 0x28975
cmpq $0x9, %r14
movq %rax, %r14
ja 0x28950
subl %r9d, %r8d
movl %r8d, %eax
jmp 0x289b5
cvtsi2sd %r11d, %xmm2
subsd %xmm2, %xmm1
movsd 0x30a2(%rip), %xmm2     # 0x2ba30
ucomisd %xmm1, %xmm2
setbe %al
movl %r11d, %edx
andb %al, %dl
movzbl %dl, %eax
addl %eax, %r11d
jmp 0x289d0
pushq $0x20
popq %r9
jmp 0x289b5
testb %dl, %dl
jne 0x289c1
movb $0x30, -0x50(%rbp,%r9)
incq %r9
subl $0x1, %eax
setb %dl
cmpq $0x1f, %r9
jbe 0x289a8
cmpq $0x1f, %r9
ja 0x28a04
movb $0x2e, -0x50(%rbp,%r9)
incq %r9
cmpq $0x1f, %r9
ja 0x28a04
pushq $0xa
popq %r8
movl %r11d, %eax
cltd
idivl %r8d
movq %r9, %r14
addb $0x30, %dl
movb %dl, -0x50(%rbp,%r9)
incq %r9
cmpq $0x1e, %r14
ja 0x28a08
addl $-0xa, %r11d
cmpl $-0x13, %r11d
movl %eax, %r11d
jb 0x289da
jmp 0x28a08
pushq $0x20
popq %r9
movl %r15d, %eax
andl $0x3, %eax
cmpl $0x1, %eax
jne 0x28a52
testl %r10d, %r10d
je 0x28a34
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
seta %al
testb $0xc, %r15b
setne %dl
orb %al, %dl
movzbl %dl, %eax
subl %eax, %r10d
jmp 0x28a37
xorl %r10d, %r10d
cmpl $0x20, %r10d
pushq $0x20
popq %rax
cmovbl %r10d, %eax
cmpq %rax, %r9
jae 0x28a52
movb $0x30, -0x50(%rbp,%r9)
incq %r9
jmp 0x28a42
cmpq $0x1f, %r9
ja 0x28a7e
movb $0x2d, %al
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x28a74
movb $0x2b, %al
testb $0x4, %r15b
jne 0x28a74
movb $0x20, %al
testb $0x8, %r15b
je 0x28a82
movb %al, -0x50(%rbp,%r9)
incq %r9
jmp 0x28a82
pushq $0x20
popq %r9
leaq -0x50(%rbp), %r8
movq %rbx, %rdx
pushq %r15
pushq %r10
callq 0x28f0d
popq %rcx
popq %rdx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4b0b(%rip), %r8       # 0x2d5b5
pushq $0x3
jmp 0x287bd