// File: cphinton42[P]hinton-language/src/stb/stb_sprintf.h
// Function: stbsp__real_to_str(char const**, unsigned int*, char*, int*, double, unsigned int)
static stbsp__int32 stbsp__real_to_str( char const * * start, stbsp__uint32 * len, char *out, stbsp__int32 * decimal_pos, double value, stbsp__uint32 frac_digits )
{
  double d;
  stbsp__int64 bits = 0;
  stbsp__int32 expo, e, ng, tens;

  d = value;
  STBSP__COPYFP(bits,d);
  expo = (stbsp__int32) ((bits >> 52) & 2047);
  ng = (stbsp__int32)(bits >> 63);
  if (ng) d=-d;

  if ( expo == 2047 ) // is nan or inf?
  {
    *start = (bits&((((stbsp__uint64)1)<<52)-1)) ? "NaN" : "Inf";
    *decimal_pos =  STBSP__SPECIAL;
    *len = 3;
    return ng;
  } 

  if ( expo == 0 ) // is zero or denormal
  {
    if ((bits<<1)==0) // do zero
    {
      *decimal_pos = 1; 
      *start = out;
      out[0] = '0'; *len = 1;
      return ng;
    }
    // find the right expo for denormals
    {
      stbsp__int64 v = ((stbsp__uint64)1)<<51;
      while ((bits&v)==0) { --expo; v >>= 1; }
    }
  }

  // find the decimal exponent as well as the decimal bits of the value
  {
    double ph,pl;

    // log10 estimate - very specifically tweaked to hit or undershoot by no more than 1 of log10 of all expos 1..2046
    tens=expo-1023; tens = (tens<0)?((tens*617)/2048):(((tens*1233)/4096)+1);

    // move the significant bits into position and stick them into an int 
    stbsp__raise_to_power10( &ph, &pl, d, 18-tens );

    // get full as much precision from double-double as possible
    stbsp__ddtoS64( bits, ph,pl );

    // check if we undershot
    if ( ((stbsp__uint64)bits) >= stbsp__tento19th ) ++tens; 
  }

  // now do the rounding in integer land
  frac_digits = ( frac_digits & 0x80000000 ) ? ( (frac_digits&0x7ffffff) + 1 ) : ( tens + frac_digits );
  if ( ( frac_digits < 24 ) )
  {
    stbsp__uint32 dg = 1; if ((stbsp__uint64)bits >= stbsp__powten[9] ) dg=10; while( (stbsp__uint64)bits >= stbsp__powten[dg] ) { ++dg; if (dg==20) goto noround; }
    if ( frac_digits < dg )
    {
      stbsp__uint64 r;
      // add 0.5 at the right position and round
      e = dg - frac_digits;
      if ( (stbsp__uint32)e >= 24 ) goto noround;
      r = stbsp__powten[e];
      bits = bits + (r/2);
      if ( (stbsp__uint64)bits >= stbsp__powten[dg] ) ++tens;
      bits /= r;
    } 
    noround:;
  }

  // kill long trailing runs of zeros
  if ( bits )
  {
    stbsp__uint32 n;
    for(;;) { if ( bits<=0xffffffff ) break; if (bits%1000) goto donez; bits/=1000; }
    n = (stbsp__uint32)bits;
    while ((n%1000)==0) n/=1000;
    bits=n;
    donez:;
  }

  // convert to string
  out += 64;
  e = 0; 
  for(;;)
  {
    stbsp__uint32 n;
    char * o = out-8;
    // do the conversion in chunks of U32s (avoid most 64-bit divides, worth it, constant denomiators be damned)
    if (bits>=100000000) { n = (stbsp__uint32)( bits % 100000000);  bits /= 100000000; } else {n = (stbsp__uint32)bits; bits = 0; }
    while(n) { out-=2; *(stbsp__uint16*)out=*(stbsp__uint16*)&stbsp__digitpair[(n%100)*2]; n/=100; e+=2; }
    if (bits==0) { if ((e) && (out[0]=='0')) { ++out; --e; } break; }
    while( out!=o ) { *--out ='0'; ++e; }
  }
  
  *decimal_pos = tens;
  *start = out;
  *len = e;
  return ng;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movsd %xmm0, -0x30(%rbp)
movl %r8d, -0x34(%rbp)
movq $0x0, -0x48(%rbp)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x5c(%rbp)
cmpl $0x8, -0x5c(%rbp)
jge 0x1173e
movslq -0x5c(%rbp), %rax
movb -0x40(%rbp,%rax), %cl
movslq -0x5c(%rbp), %rax
movb %cl, -0x48(%rbp,%rax)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1171d
movq -0x48(%rbp), %rax
sarq $0x34, %rax
andq $0x7ff, %rax            # imm = 0x7FF
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
sarq $0x3f, %rax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x11781
movsd -0x40(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x40(%rbp)
cmpl $0x7ff, -0x4c(%rbp)     # imm = 0x7FF
jne 0x117eb
movabsq $0xfffffffffffff, %rax  # imm = 0xFFFFFFFFFFFFF
andq -0x48(%rbp), %rax
cmpq $0x0, %rax
je 0x117ae
leaq 0x9bc1(%rip), %rax      # 0x1b366
movq %rax, -0xc0(%rbp)
jmp 0x117be
leaq 0x9bb5(%rip), %rax      # 0x1b36a
movq %rax, -0xc0(%rbp)
jmp 0x117be
movq -0xc0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movl $0x7000, (%rax)         # imm = 0x7000
movq -0x18(%rbp), %rax
movl $0x3, (%rax)
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x11c5e
cmpl $0x0, -0x4c(%rbp)
jne 0x11863
movq -0x48(%rbp), %rax
shlq %rax
cmpq $0x0, %rax
jne 0x1182f
movq -0x28(%rbp), %rax
movl $0x1, (%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movb $0x30, (%rax)
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x11c5e
movabsq $0x8000000000000, %rax  # imm = 0x8000000000000
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
andq -0x68(%rbp), %rax
cmpq $0x0, %rax
jne 0x11861
movl -0x4c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x68(%rbp), %rax
sarq %rax
movq %rax, -0x68(%rbp)
jmp 0x1183d
jmp 0x11863
movl -0x4c(%rbp), %eax
subl $0x3ff, %eax            # imm = 0x3FF
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jge 0x1188b
imull $0x269, -0x58(%rbp), %eax # imm = 0x269
movl $0x800, %ecx            # imm = 0x800
cltd
idivl %ecx
movl %eax, -0xc4(%rbp)
jmp 0x118a3
imull $0x4d1, -0x58(%rbp), %eax # imm = 0x4D1
movl $0x1000, %ecx           # imm = 0x1000
cltd
idivl %ecx
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
movl %eax, -0x58(%rbp)
movsd -0x40(%rbp), %xmm0
movl $0x12, %edx
subl -0x58(%rbp), %edx
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x15fd0
xorps %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
cvttsd2si -0x70(%rbp), %rax
movq %rax, -0x48(%rbp)
cvtsi2sdq -0x48(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x70(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
subsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x70(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
subsd -0x98(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd -0x90(%rbp), %xmm1
addsd -0x98(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x80(%rbp), %xmm0
addsd -0x88(%rbp), %xmm0
addsd -0x78(%rbp), %xmm0
cvttsd2si %xmm0, %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movabsq $0xde0b6b3a7640000, %rax # imm = 0xDE0B6B3A7640000
cmpq %rax, -0x48(%rbp)
jb 0x11974
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movl -0x34(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x11994
movl -0x34(%rbp), %eax
andl $0x7ffffff, %eax        # imm = 0x7FFFFFF
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x119a0
movl -0x58(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0x34(%rbp)
cmpl $0x18, -0x34(%rbp)
jae 0x11a92
movl $0x1, -0x9c(%rbp)
movq -0x48(%rbp), %rax
cmpq 0x7f00(%rip), %rax      # 0x198c8
jb 0x119d4
movl $0xa, -0x9c(%rbp)
jmp 0x119d6
movq -0x48(%rbp), %rax
movl -0x9c(%rbp), %ecx
movl %ecx, %edx
leaq 0x7e97(%rip), %rcx      # 0x19880
cmpq (%rcx,%rdx,8), %rax
jb 0x11a0e
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
cmpl $0x14, -0x9c(%rbp)
jne 0x11a0c
jmp 0x11a90
jmp 0x119d6
movl -0x34(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jae 0x11a8e
movl -0x9c(%rbp), %eax
subl -0x34(%rbp), %eax
movl %eax, -0x50(%rbp)
cmpl $0x18, -0x50(%rbp)
jb 0x11a2d
jmp 0x11a90
movslq -0x50(%rbp), %rcx
leaq 0x7e48(%rip), %rax      # 0x19880
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
movq -0x48(%rbp), %rax
movq -0xa8(%rbp), %rcx
shrq %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl -0x9c(%rbp), %ecx
movl %ecx, %edx
leaq 0x7e15(%rip), %rcx      # 0x19880
cmpq (%rcx,%rdx,8), %rax
jb 0x11a7a
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movq -0xa8(%rbp), %rcx
movq -0x48(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rax, -0x48(%rbp)
jmp 0x11a90
jmp 0x11a92
cmpq $0x0, -0x48(%rbp)
je 0x11b13
jmp 0x11a9b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x48(%rbp)
jg 0x11aa8
jmp 0x11ad2
movq -0x48(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x11abe
jmp 0x11b11
movq -0x48(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, -0x48(%rbp)
jmp 0x11a9b
movq -0x48(%rbp), %rax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
jne 0x11b07
movl -0xac(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divl %ecx
movl %eax, -0xac(%rbp)
jmp 0x11adc
movl -0xac(%rbp), %eax
movq %rax, -0x48(%rbp)
jmp 0x11b13
movq -0x20(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rax
addq $-0x8, %rax
movq %rax, -0xb8(%rbp)
cmpq $0x5f5e100, -0x48(%rbp) # imm = 0x5F5E100
jl 0x11b69
movq -0x48(%rbp), %rax
movl $0x5f5e100, %ecx        # imm = 0x5F5E100
cqto
idivq %rcx
movl %edx, %eax
movl %eax, -0xb0(%rbp)
movq -0x48(%rbp), %rax
movl $0x5f5e100, %ecx        # imm = 0x5F5E100
cqto
idivq %rcx
movq %rax, -0x48(%rbp)
jmp 0x11b7b
movq -0x48(%rbp), %rax
movl %eax, -0xb0(%rbp)
movq $0x0, -0x48(%rbp)
jmp 0x11b7d
cmpl $0x0, -0xb0(%rbp)
je 0x11bd9
movq -0x20(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x20(%rbp)
movl -0xb0(%rbp), %eax
movl $0x64, %ecx
xorl %edx, %edx
divl %ecx
shll %edx
movl %edx, %eax
movl %eax, %ecx
leaq 0xdff2(%rip), %rax      # 0x1fba0
movw (%rax,%rcx), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movl -0xb0(%rbp), %eax
movl $0x64, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0xb0(%rbp)
movl -0x50(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x50(%rbp)
jmp 0x11b7d
cmpq $0x0, -0x48(%rbp)
jne 0x11c09
cmpl $0x0, -0x50(%rbp)
je 0x11c07
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x11c07
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movl -0x50(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x11c3b
jmp 0x11c0b
movq -0x20(%rbp), %rax
cmpq -0xb8(%rbp), %rax
je 0x11c36
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x30, -0x1(%rax)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x11c0b
jmp 0x11b26
movl -0x58(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0x50(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)