// File: /workspace/github_c_makefile_star2_O2/OnionIoT[P]lv_example/lvgl/src/lv_misc/lv_printf.c
// Function: etoa
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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %r14d
movl %r8d, %eax
movq %rcx, %rbx
movq %rdx, 0x20(%rsp)
movq %rsi, %r15
movq %rdi, %r12
movsd 0x1517d(%rip), %xmm1    # 0x3f140
ucomisd %xmm0, %xmm1
ja 0x2a16c
ucomisd %xmm0, %xmm0
jp 0x2a16c
ucomisd 0x15169(%rip), %xmm0    # 0x3f148
ja 0x2a16c
movl 0x70(%rsp), %ebp
movapd 0x1521f(%rip), %xmm1    # 0x3f210
xorpd %xmm0, %xmm1
maxsd %xmm0, %xmm1
btl $0xa, %ebp
pushq $0x6
popq %rcx
movl %eax, %r8d
cmovael %ecx, %r8d
movq %xmm1, %rdx
movabsq $0xfffffffffffff, %rsi  # imm = 0xFFFFFFFFFFFFF
andq %rdx, %rsi
shrq $0x34, %rdx
andl $0x7ff, %edx            # imm = 0x7FF
addl $0xfffffc01, %edx       # imm = 0xFFFFFC01
movabsq $0x3ff0000000000000, %rdi # imm = 0x3FF0000000000000
cvtsi2sd %edx, %xmm2
mulsd 0x15129(%rip), %xmm2    # 0x3f168
orq %rsi, %rdi
addsd 0x15126(%rip), %xmm2    # 0x3f170
movq %rdi, %xmm3
addsd 0x15121(%rip), %xmm3    # 0x3f178
mulsd 0x15121(%rip), %xmm3    # 0x3f180
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %edi
cvttpd2dq %xmm3, %xmm2
cvtdq2pd %xmm2, %xmm3
movsd 0x15111(%rip), %xmm2    # 0x3f188
mulsd %xmm3, %xmm2
addsd 0x150dd(%rip), %xmm2    # 0x3f160
cvttsd2si %xmm2, %edx
cvttpd2dq %xmm2, %xmm2
cvtdq2pd %xmm2, %xmm2
mulsd 0x150f9(%rip), %xmm2    # 0x3f190
mulsd 0x150f9(%rip), %xmm3    # 0x3f198
addsd %xmm2, %xmm3
movapd %xmm3, %xmm2
mulsd %xmm3, %xmm2
addl $0x3ff, %edx            # imm = 0x3FF
movsd 0x150e7(%rip), %xmm5    # 0x3f1a0
subsd %xmm3, %xmm5
movapd %xmm2, %xmm6
divsd 0x150df(%rip), %xmm6    # 0x3f1a8
addsd %xmm3, %xmm3
movsd 0x150db(%rip), %xmm4    # 0x3f1b0
addsd %xmm4, %xmm6
movapd %xmm2, %xmm7
divsd %xmm6, %xmm7
addsd 0x150cf(%rip), %xmm7    # 0x3f1b8
divsd %xmm7, %xmm2
addsd %xmm5, %xmm2
divsd %xmm2, %xmm3
addsd 0x150c3(%rip), %xmm3    # 0x3f1c0
shlq $0x34, %rdx
movq %rdx, %xmm2
mulsd %xmm3, %xmm2
xorl %edx, %edx
ucomisd %xmm1, %xmm2
seta %sil
jbe 0x2a11a
divsd %xmm4, %xmm2
movb %sil, %dl
subl %edx, %edi
leal 0x63(%rdi), %edx
cmpl $0xc7, %edx
pushq $0x5
popq %r13
sbbl $0x0, %r13d
btl $0xb, %ebp
jae 0x2a1a9
ucomisd 0x1508a(%rip), %xmm1    # 0x3f1c8
jb 0x2a193
movsd 0x15088(%rip), %xmm3    # 0x3f1d0
ucomisd %xmm1, %xmm3
jbe 0x2a193
movl %edi, %eax
notl %eax
addl %r8d, %eax
xorl %r13d, %r13d
cmpl %edi, %r8d
cmovlel %r13d, %eax
orl $0x400, %ebp            # imm = 0x400
movl %eax, %r8d
xorl %edi, %edi
jmp 0x2a1a9
movq %r12, %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
movq %rbx, %rcx
movl %eax, %r8d
movl %r14d, %r9d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x29c75
testl %r8d, %r8d
je 0x2a1a6
decl %eax
btl $0xa, %ebp
cmovael %ecx, %eax
movl %eax, %r8d
jmp 0x2a1a9
xorl %r8d, %r8d
xorl %r9d, %r9d
movl %r14d, %eax
subl %r13d, %eax
cmovbl %r9d, %eax
testl %r13d, %r13d
cmovel %eax, %r9d
testb $0x2, %bpl
cmovel %eax, %r9d
testl %edi, %edi
movq %rdi, 0x30(%rsp)
je 0x2a1d2
divsd %xmm2, %xmm1
movapd 0x15036(%rip), %xmm2    # 0x3f210
xorpd %xmm1, %xmm2
xorpd %xmm3, %xmm3
cmpltsd %xmm3, %xmm0
movapd %xmm0, %xmm3
andnpd %xmm1, %xmm3
andpd %xmm2, %xmm0
orpd %xmm3, %xmm0
movl %ebp, %eax
andl $0xfffff7ff, %eax       # imm = 0xFFFFF7FF
movl %eax, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
movq %rbx, %rcx
callq 0x29c75
movq %rax, %rdx
testl %r13d, %r13d
je 0x2a2b8
movl %r14d, 0x2c(%rsp)
movl %ebp, %eax
andb $0x20, %al
xorb $0x65, %al
movl %ebp, %r14d
movq %rdx, %rbp
incq %rbp
movzbl %al, %edi
movq %r15, %rsi
movq %rbx, %rcx
callq *%r12
movq 0x30(%rsp), %r9
movl %r9d, %r8d
negl %r8d
cmovsl %r9d, %r8d
decl %r13d
movl %r13d, 0x10(%rsp)
movl $0x5, 0x18(%rsp)
andl $0x0, 0x8(%rsp)
movq $0xa, (%rsp)
shrl $0x1f, %r9d
movq %r12, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x29ba8
movq %rax, %rdx
testb $0x2, %r14b
je 0x2a2b8
movl 0x2c(%rsp), %r13d
negq 0x20(%rsp)
pushq $0x20
popq %rbp
movq 0x20(%rsp), %rax
addq %rdx, %rax
cmpq %r13, %rax
jae 0x2a2b8
leaq 0x1(%rdx), %r14
movl %ebp, %edi
movq %r15, %rsi
movq %rbx, %rcx
callq *%r12
movq %r14, %rdx
jmp 0x2a297
movq %rdx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq