// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movl %r9d, %r15d
movl 0x10(%rbp), %r12d
ucomisd %xmm0, %xmm0
jp 0x2d5a1
movapd %xmm0, %xmm4
movsd 0x38db(%rip), %xmm0     # 0x30ab0
ucomisd %xmm4, %xmm0
jbe 0x2d1ed
leaq 0x540f(%rip), %r8       # 0x325f1
movl $0x4, %r9d
jmp 0x2d589
ucomisd 0x38c3(%rip), %xmm4     # 0x30ab8
jbe 0x2d21d
xorl %r9d, %r9d
testb $0x4, %r12b
leaq 0x53f6(%rip), %rax      # 0x325fb
leaq 0x53ea(%rip), %r8       # 0x325f6
cmoveq %rax, %r8
setne %r9b
addq $0x3, %r9
jmp 0x2d589
movl %r8d, %ebx
movapd 0x3198(%rip), %xmm0     # 0x303c0
andpd %xmm4, %xmm0
ucomisd 0x388c(%rip), %xmm0     # 0x30ac0
jbe 0x2d253
movapd %xmm4, %xmm0
movl %ebx, %r8d
movl %r15d, %r9d
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d5b0
movq %rdi, -0x88(%rbp)
movq %rsi, -0x90(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%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, %eax
cmovael %eax, %ebx
cmpl $0xa, %ebx
movapd %xmm4, -0x60(%rbp)
jb 0x2d2e9
movapd %xmm5, -0x50(%rbp)
leal -0xa(%rbx), %eax
cmpl $0x1f, %eax
movl $0x1f, %r14d
cmovbl %eax, %r14d
incl %r14d
leaq -0x80(%rbp), %rdi
movl $0x30, %esi
movq %r14, %rdx
callq 0x21c0
xorl %ecx, %ecx
incq %rcx
cmpq %rcx, %r14
jne 0x2d2c8
leaq -0x1(%rcx), %rax
cmpq $0x1f, %rax
setb %al
subl %ecx, %ebx
movapd -0x60(%rbp), %xmm4
movapd -0x50(%rbp), %xmm5
jmp 0x2d2ee
movb $0x1, %al
xorl %r14d, %r14d
cvttsd2si %xmm5, %r13d
cvttpd2dq %xmm5, %xmm0
cvtdq2pd %xmm0, %xmm0
movapd %xmm5, %xmm1
subsd %xmm0, %xmm1
movl %ebx, %ecx
leaq 0x36e4(%rip), %rdx      # 0x309f0
movsd (%rdx,%rcx,8), %xmm0
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
movq %rcx, %rsi
movapd %xmm1, %xmm2
subsd 0x379f(%rip), %xmm2     # 0x30ac8
cvttsd2si %xmm2, %rdx
sarq $0x3f, %rsi
andq %rsi, %rdx
orq %rcx, %rdx
movq %rdx, %xmm2
punpckldq 0x2f7b(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x2f83(%rip), %xmm2     # 0x302d0
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
subsd %xmm3, %xmm1
ucomisd 0x3703(%rip), %xmm1     # 0x30a68
jbe 0x2d398
incq %rdx
movq %rdx, %xmm1
punpckldq 0x2f49(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x2f51(%rip), %xmm1     # 0x302d0
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jb 0x2d3bb
incl %r13d
xorl %edx, %edx
jmp 0x2d3bb
movsd 0x36c8(%rip), %xmm0     # 0x30a68
ucomisd %xmm1, %xmm0
ja 0x2d3bb
testq %rdx, %rdx
sete %cl
movl %edx, %esi
orb %cl, %sil
movzbl %sil, %ecx
andl $0x1, %ecx
addq %rcx, %rdx
testl %ebx, %ebx
je 0x2d408
movq %r15, -0x50(%rbp)
testb %al, %al
je 0x2d430
movabsq $-0x3333333333333333, %rsi # imm = 0xCCCCCCCCCCCCCCCD
movq %rdx, %rcx
decl %ebx
movq %rdx, %rax
mulq %rsi
shrq $0x3, %rdx
leal (%rdx,%rdx), %eax
leal (%rax,%rax,4), %eax
movl %ecx, %edi
subl %eax, %edi
orb $0x30, %dil
leaq 0x1(%r14), %r15
movb %dil, -0x80(%rbp,%r14)
cmpq $0xa, %rcx
jb 0x2d433
cmpq $0x1f, %r14
movq %r15, %r14
jb 0x2d3d1
jmp 0x2d433
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
subsd %xmm0, %xmm5
movsd 0x364c(%rip), %xmm0     # 0x30a68
ucomisd %xmm5, %xmm0
setbe %al
movl %r13d, %ecx
andb %al, %cl
movzbl %cl, %eax
addl %eax, %r13d
jmp 0x2d49d
movq %r14, %r15
cmpq $0x20, %r15
setae %al
jae 0x2d489
testl %ebx, %ebx
je 0x2d489
leaq (%r15,%rbp), %rdi
addq $-0x80, %rdi
decl %ebx
movl $0x1f, %edx
subq %r15, %rdx
cmpq %rdx, %rbx
cmovbq %rbx, %rdx
incq %rdx
movl $0x30, %esi
callq 0x21c0
movapd -0x60(%rbp), %xmm4
movq %r15, %rdx
cmpq $0x1f, %r15
leaq 0x1(%r15), %r15
setae %al
subl $0x1, %ebx
setb %cl
cmpq $0x1e, %rdx
ja 0x2d489
testb %cl, %cl
je 0x2d46b
testb %al, %al
jne 0x2d496
movb $0x2e, -0x80(%rbp,%r15)
incq %r15
movq %r15, %r14
movq -0x50(%rbp), %r15
cmpq $0x1f, %r14
ja 0x2d4e3
movq %r14, %rcx
movslq %r13d, %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, -0x80(%rbp,%r14)
incq %r14
cmpq $0x1e, %rcx
ja 0x2d4e3
addl $-0xa, %r13d
cmpl $-0x13, %r13d
movl %eax, %r13d
jb 0x2d4a3
movl %r12d, %eax
andl $0x3, %eax
cmpl $0x1, %eax
jne 0x2d540
testl %r15d, %r15d
je 0x2d50f
xorpd %xmm0, %xmm0
ucomisd %xmm4, %xmm0
seta %al
testb $0xc, %r12b
setne %cl
orb %al, %cl
movzbl %cl, %eax
subl %eax, %r15d
jmp 0x2d512
xorl %r15d, %r15d
cmpl $0x20, %r15d
movl $0x20, %ebx
cmovbl %r15d, %ebx
movq %rbx, %rdx
subq %r14, %rdx
jbe 0x2d540
leaq (%r14,%rbp), %rdi
addq $-0x80, %rdi
movl $0x30, %esi
callq 0x21c0
movapd -0x60(%rbp), %xmm4
jmp 0x2d543
movq %r14, %rbx
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rdx
cmpq $0x1f, %rbx
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdi
ja 0x2d582
movb $0x2d, %al
xorpd %xmm0, %xmm0
ucomisd %xmm4, %xmm0
ja 0x2d57b
movb $0x2b, %al
testb $0x4, %r12b
jne 0x2d57b
movb $0x20, %al
testb $0x8, %r12b
je 0x2d582
movb %al, -0x80(%rbp,%rbx)
incq %rbx
leaq -0x80(%rbp), %r8
movq %rbx, %r9
pushq %r12
pushq %r15
callq 0x2dab9
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5045(%rip), %r8       # 0x325ed
movl $0x3, %r9d
jmp 0x2d589