// File: /workspace/github_c_makefile_star2_O3/BernardTatin[P]s7sh/s7.c
// Function: number_to_string_with_radix
static block_t *number_to_string_with_radix(s7_scheme *sc, s7_pointer obj, int32_t radix, s7_int width, s7_int precision, char float_choice, s7_int *nlen)
    {
        /* called by s7_number_to_string (char*), g_number_to_string (strp), number_to_string_p_pp (strp), format_number (strp basically) */
        /* the rest of s7 assumes nlen is set to the correct length */
        block_t *b;
        char *p;
        s7_int len, str_len;

#if WITH_GMP
        if (s7_is_bignum(obj))
        {
            p = big_number_to_string_with_radix(obj, radix, width, nlen, P_WRITE);
            b = string_to_block(sc, p, *nlen);
            free(p);
            return(b);
        }
        /* this ignores precision because it's way too hard to get the mpfr string to look like
         *   C's output -- we either have to call mpfr_get_str twice (the first time just to
         *   find out what the exponent is and how long the string actually is), or we have
         *   to do messy string manipulations.  So (format #f "",3F" pi) ignores the "3" and
         *   prints the full string.
         */
#endif

        if (radix == 10)
        {
            p = number_to_string_base_10(sc, obj, width, precision, float_choice, nlen, P_WRITE);
            return(string_to_block(sc, p, *nlen));
        }

        switch (type(obj))
        {
            case T_INTEGER:
                {
                    size_t len1;
                    b = mallocate(sc, (128 + width));
                    p = (char *)block_data(b);
                    len1 = integer_to_string_any_base(p, integer(obj), radix);
                    if ((size_t)width > len1)
                    {
                        size_t start;
                        start = width - len1;
                        memmove((void *)(p + start), (void *)p, len1);
                        memset((void *)p, (int)' ', start);
                        p[width] = '\0';
                        *nlen = width;
                    }
                    else *nlen = len1;
                    return(b);
                }

            case T_RATIO:
                {
                    size_t len1, len2;
                    str_len = 256 + width;
                    b = mallocate(sc, str_len);
                    p = (char *)block_data(b);
                    len1 = integer_to_string_any_base(p, numerator(obj), radix);
                    p[len1] = '/';
                    len2 = integer_to_string_any_base((char *)(p + len1 + 1), denominator(obj), radix);
                    len = len1 + 1 + len2;
                    p[len] = '\0';
                }
                break;

            case T_REAL:
                {
                    int32_t i;
                    s7_int int_part;
                    s7_double x, frac_part, min_frac, base;
                    bool sign = false;
                    char n[128], d[256];

                    x = real(obj);

                    if (is_NaN(x))
                        return(string_to_block(sc, "+nan.0", *nlen = 6));
                    if (is_inf(x))
                    {
                        if (x < 0.0)
                            return(string_to_block(sc, "-inf.0", *nlen = 6));
                        return(string_to_block(sc, "+inf.0", *nlen = 6));
                    }

                    if (x < 0.0)
                    {
                        sign = true;
                        x = -x;
                    }

                    if (x > 1.0e18) /* i.e. close to or greater than most-positive-fixnum (9.22e18), so the code below is unlikely to work, (format #f "~X" 1e19) */
                    {
                        int32_t ep;
                        block_t *b1;
                        len = 0;
                        ep = (int32_t)floor(log(x) / log((double)radix));
                        real(sc->real_wrapper3) = x / pow((double)radix, (double)ep); /* divide it down to one digit, then the fractional part */
                        b = number_to_string_with_radix(sc, sc->real_wrapper3, radix, width, precision, float_choice, &len);
                        b1 = mallocate(sc, len + 8);
                        p = (char *)block_data(b1);
                        p[0] = '\0';
                        (*nlen) = catstrs(p, len + 8, (sign) ? "-" : "", (char *)block_data(b), "e", integer_to_string_no_length(sc, ep), NULL);
                        liberate(sc, b);
                        return(b1);
                    }

                    int_part = (s7_int)floor(x);
                    frac_part = x - int_part;
                    integer_to_string_any_base(n, int_part, radix);
                    min_frac = dpow(radix, -precision);

                    /* doesn't this assume precision < 128/256 and that we can fit in 256 digits (1e308)? */
                    for (i = 0, base = radix; (i < precision) && (frac_part > min_frac); i++, base *= radix)
                    {
                        s7_int ipart;
                        ipart = (s7_int)(frac_part * base);
                        if (ipart >= radix)         /* rounding confusion */
                            ipart = radix - 1;
                        frac_part -= (ipart / base);
                        if (ipart < 10)
                            d[i] = (char)('0' + ipart);
                        else d[i] = (char)('a' + ipart -  10);
                    }
                    if (i == 0)
                        d[i++] = '0';
                    d[i] = '\0';
                    b = mallocate(sc, 256);
                    p = (char *)block_data(b);
                    p[0] = '\0';
                    len = catstrs(p, 256, (sign) ? "-" : "", n, ".", d, NULL);
                    str_len = 256;
                }
                break;

            default:
                {
                    block_t *n, *d;
                    char *dp;
                    real(sc->real_wrapper3) = real_part(obj);
                    n = number_to_string_with_radix(sc, sc->real_wrapper3, radix, 0, precision, float_choice, &len); /* include floatify */
                    real(sc->real_wrapper4) = imag_part(obj);
                    d = number_to_string_with_radix(sc, sc->real_wrapper4, radix, 0, precision, float_choice, &len);
                    dp = (char *)block_data(d);
                    b = mallocate(sc, 512);
                    p = (char *)block_data(b);
                    p[0] = '\0';
                    len = catstrs(p, 512, (char *)block_data(n), ((dp[0] == '+') || (dp[0] == '-')) ? "" : "+", dp, "i", NULL);
                    str_len = 512;
                    liberate(sc, n);
                    liberate(sc, d);
                }
                break;
        }

        if (width > len)
        {
            s7_int spaces;
            if (width >= str_len)
            {
                str_len = width + 1;
                b = reallocate(sc, b, str_len);
                p = (char *)block_data(b);
            }
            spaces = width - len;
            p[width] = '\0';
            memmove((void *)(p + spaces), (void *)p, len);
            memset((void *)p, (int)' ', spaces);
            (*nlen) = width;
        }
        else (*nlen) = len;
        return(b);
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movl %r9d, %r15d
movq %r8, %r12
movq %rcx, %r14
movq %rsi, %rbx
movq %rdi, %r13
movq 0x210(%rsp), %r9
cmpl $0xa, %edx
jne 0xd843
movsbl %r15b, %r8d
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r12, %rcx
movq %r9, %r14
callq 0x8f49e
movq %rax, %rbx
movq (%r14), %r14
leaq 0x1(%r14), %rsi
movq %r13, %rdi
callq 0x9357
movq %rax, %r12
movq (%rax), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x83e0
movb $0x0, (%r15,%r14)
jmp 0xddbe
movl %edx, %ebp
movzbl (%rbx), %eax
cmpl $0xd, %eax
je 0xd91f
cmpl $0xc, %eax
je 0xd8b7
cmpl $0xb, %eax
jne 0xd98b
leaq 0x80(%r14), %rsi
movq %r13, %rdi
callq 0x9357
movq %rax, %r12
movq (%rax), %r15
movq 0x8(%rbx), %rsi
movq %r15, %rdi
movl %ebp, %edx
callq 0x90361
movq %r14, %rbx
subq %rax, %rbx
jbe 0xda3d
leaq (%r15,%rbx), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x8550
movq %r15, %rdi
movl $0x20, %esi
movq %rbx, %rdx
callq 0x8270
movb $0x0, (%r15,%r14)
jmp 0xda40
leaq 0x100(%r14), %r12
movq %r13, 0x10(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x9357
movq %rax, 0x30(%rsp)
movq (%rax), %r13
movq 0x8(%rbx), %rsi
movq %r13, %rdi
movl %ebp, %edx
callq 0x90361
movq %rax, %r15
leaq (%rax,%r13), %rdi
incq %rdi
movb $0x2f, -0x1(%rdi)
movq 0x10(%rbx), %rsi
movl %ebp, %edx
callq 0x90361
movq 0x210(%rsp), %rdx
leaq (%r15,%rax), %rcx
addq %rax, %r15
incq %r15
movq %r15, 0x8(%rsp)
movb $0x0, 0x1(%r13,%rcx)
jmp 0xdd4d
movsd 0x8(%rbx), %xmm1
ucomisd %xmm1, %xmm1
jp 0xdfab
movq %xmm1, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
movaps %xmm1, 0x30(%rsp)
jne 0xda50
movq $0x6, (%r9)
movl $0x7, %esi
movq %r13, %rdi
callq 0x9357
movq %rax, %r12
xorpd %xmm0, %xmm0
ucomisd 0x30(%rsp), %xmm0
movq (%rax), %rax
jbe 0xdbdc
movw $0x302e, 0x4(%rax)      # imm = 0x302E
movl $0x666e692d, (%rax)     # imm = 0x666E692D
jmp 0xdbe8
movsd 0x8(%rbx), %xmm0
movq 0x1f28(%r13), %rax
movsd %xmm0, 0x8(%rax)
movq 0x1f28(%r13), %rsi
movq %r13, 0x10(%rsp)
leaq 0x8(%rsp), %r13
movq %r13, (%rsp)
movsbl %r15b, %r15d
movq 0x10(%rsp), %rdi
movl %ebp, %edx
xorl %ecx, %ecx
movq %r12, %r8
movl %r15d, %r9d
callq 0xd7ce
movq %rax, 0x20(%rsp)
movsd 0x10(%rbx), %xmm0
movq 0x10(%rsp), %rax
movq 0x1f30(%rax), %rax
movsd %xmm0, 0x8(%rax)
movq 0x10(%rsp), %rax
movq 0x1f30(%rax), %rsi
movq %r13, (%rsp)
movq 0x10(%rsp), %r13
movq %r13, %rdi
movl %ebp, %edx
xorl %ecx, %ecx
movq %r12, %r8
movl %r15d, %r9d
callq 0xd7ce
movq %rax, %rbp
movq (%rax), %r15
leaq 0x838(%r13), %rbx
movq 0x880(%r13), %r12
testq %r12, %r12
je 0xdb65
movq 0x18(%r12), %rax
movq %rax, 0x880(%r13)
movq (%r12), %r13
jmp 0xdc61
movq %rax, %r14
movq 0x210(%rsp), %rax
movq %r14, (%rax)
jmp 0xddbe
movapd 0xfc458(%rip), %xmm0    # 0x109eb0
xorpd %xmm1, %xmm0
maxsd %xmm1, %xmm0
ucomisd 0xf85d0(%rip), %xmm0    # 0x106038
movapd %xmm0, 0x10(%rsp)
jbe 0xdbf1
leaq 0x8(%rsp), %rbx
movq $0x0, (%rbx)
callq 0x82b0
movsd %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x48(%rsp)
callq 0x82b0
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x85f0
movapd %xmm0, 0x20(%rsp)
cvttpd2dq %xmm0, %xmm1
cvtdq2pd %xmm1, %xmm1
movsd 0x48(%rsp), %xmm0
callq 0x8160
movapd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x1f28(%r13), %rax
movsd %xmm1, 0x8(%rax)
movq 0x1f28(%r13), %rsi
movq %rbx, (%rsp)
movsbl %r15b, %r9d
movq %r13, %rdi
movl %ebp, %edx
movq %r14, %rcx
movq %r12, %r8
callq 0xd7ce
movq %rax, %r14
movq (%rbx), %r15
addq $0x8, %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x9357
xorpd %xmm0, %xmm0
ucomisd 0x30(%rsp), %xmm0
movq %rax, %r12
cvttsd2si 0x20(%rsp), %r8d
leaq 0x10ca3d(%rip), %rax    # 0x11a56b
leaq 0x10e999(%rip), %r10    # 0x11c4ce
cmovaq %rax, %r10
movq (%r12), %rdi
movb $0x0, (%rdi)
movq (%r14), %rcx
testl %r8d, %r8d
jne 0xddd3
testb $0x1, 0x13ced5(%rip)    # 0x14aa28
jne 0xddd3
leaq 0x108b18(%rip), %r9     # 0x116678
jmp 0xdf74
movq (%rbx), %r12
testq %r12, %r12
jne 0xdba8
movl $0x2800, %edi           # imm = 0x2800
callq 0x8480
movq %rax, %r12
movq %rax, (%rbx)
addq $0x28, %rax
movl $0xff, %ecx
movq %rax, -0x10(%rax)
movl $0x0, -0x20(%rax)
addq $0x28, %rax
decl %ecx
jne 0xdb86
movq $0x0, -0x10(%rax)
movl $0x0, -0x20(%rax)
movq 0x18(%r12), %rax
movq %rax, 0x838(%r13)
movq %r13, %rcx
movq 0x8c8(%r13), %r13
leaq 0x200(%r13), %rax
cmpq $0x10000, %rax          # imm = 0x10000
jbe 0xdc46
movl $0x200, %edi            # imm = 0x200
callq 0x8480
movq %rax, %r13
jmp 0xdc54
movw $0x302e, 0x4(%rax)      # imm = 0x302E
movl $0x666e692b, (%rax)     # imm = 0x666E692B
movb $0x0, 0x6(%rax)
jmp 0xddbe
callq 0x85f0
cvttsd2si %xmm0, %rbx
leaq 0x150(%rsp), %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x90361
leal -0x41(%r12), %eax
cmpl $0xffffff7f, %eax       # imm = 0xFFFFFF7F
ja 0xde3b
movl %r12d, %eax
negl %eax
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x20(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
callq 0x8160
movsd 0x20(%rsp), %xmm4
jmp 0xde5d
addq 0x8d0(%rcx), %r13
movq %rax, 0x8c8(%rcx)
movq %r13, (%r12)
movl $0x9, 0x8(%r12)
movq $0x200, 0x10(%r12)      # imm = 0x200
movb $0x0, (%r13)
movq 0x20(%rsp), %rax
movq (%rax), %rdx
movb (%r15), %al
addb $-0x2b, %al
leaq 0x10e84b(%rip), %rsi    # 0x11c4ce
leaq 0x106bfa(%rip), %rcx    # 0x114884
testb $-0x3, %al
cmoveq %rsi, %rcx
movq $0x0, (%rsp)
leaq 0x10c8dd(%rip), %r9     # 0x11a57c
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdi
movq %r15, %r8
xorl %eax, %eax
callq 0x9517
movq 0x20(%rsp), %rdx
movq %rax, %r15
movq %rax, 0x8(%rsp)
movslq 0x8(%rdx), %rax
cmpq $0x11, %rax
jne 0xdced
movq (%rdx), %rdi
testq %rdi, %rdi
je 0xdce1
callq 0x8050
movq 0x20(%rsp), %rdx
movq $0x0, (%rdx)
movl $0x0, 0x8(%rdx)
movq %rbx, %rax
jmp 0xdcf1
leaq (%rbx,%rax,8), %rax
movq (%rax), %rcx
movq %rcx, 0x18(%rdx)
movq %rdx, (%rax)
movslq 0x8(%rbp), %rax
cmpq $0x11, %rax
movq 0x210(%rsp), %rdx
movq %r12, 0x30(%rsp)
jne 0xdd39
movq (%rbp), %rdi
testq %rdi, %rdi
je 0xdd30
callq 0x8050
movq 0x210(%rsp), %rdx
movq $0x0, (%rbp)
movl $0x0, 0x8(%rbp)
jmp 0xdd3d
leaq (%rbx,%rax,8), %rbx
movq (%rbx), %rax
movq %rax, 0x18(%rbp)
movq %rbp, (%rbx)
movl $0x200, %r12d           # imm = 0x200
cmpq %r14, %r15
jge 0xdd5e
cmpq %r14, %r12
jle 0xdd68
movq 0x30(%rsp), %r12
jmp 0xdd86
movq %r15, %r14
movq 0x30(%rsp), %r12
jmp 0xddbb
leaq 0x1(%r14), %rdx
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x9fd6
movq %rax, %r12
movq (%rax), %r13
movq 0x8(%rsp), %r15
movq %r14, %rbx
subq %r15, %rbx
movb $0x0, (%r13,%r14)
leaq (%rbx,%r13), %rdi
movq 0x8(%rsp), %rdx
movq %r13, %rsi
callq 0x8550
movq %r13, %rdi
movl $0x20, %esi
movq %rbx, %rdx
callq 0x8270
movq 0x210(%rsp), %rdx
movq %r14, (%rdx)
movq %r12, %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb2e(%r13), %r9
movq %r13, %rbp
movb $0x0, 0xb2f(%r13)
movl %r8d, %esi
negl %esi
cmovsl %r8d, %esi
leaq 0x10874b(%rip), %r11    # 0x116540
movabsq $-0x3333333333333333, %rbx # imm = 0xCCCCCCCCCCCCCCCD
movq %rsi, %rax
mulq %rbx
shrq $0x3, %rdx
leaq (%rdx,%rdx), %rax
leaq (%rax,%rax,4), %rax
movq %rsi, %r13
subq %rax, %r13
movb (%r11,%r13), %al
movb %al, (%r9)
decq %r9
cmpq $0x9, %rsi
movq %rdx, %rsi
ja 0xddff
testl %r8d, %r8d
js 0xdf6d
incq %r9
jmp 0xdf71
movslq %ebp, %rax
movl $0x40, %ecx
subl %r12d, %ecx
shlq $0xa, %rax
leaq 0x14006f(%rip), %rdx    # 0x14dec0
addq %rax, %rdx
movsd (%rdx,%rcx,8), %xmm0
cvtsi2sd %ebp, %xmm4
testq %r12, %r12
movapd 0x10(%rsp), %xmm3
jle 0xdedd
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
subsd %xmm1, %xmm3
ucomisd %xmm0, %xmm3
jbe 0xdedd
movslq %ebp, %rax
decl %ebp
movslq %ebp, %rdx
xorl %ecx, %ecx
movl $0x30, %esi
movapd %xmm4, %xmm1
movapd %xmm3, %xmm2
mulsd %xmm1, %xmm2
cvttsd2si %xmm2, %rdi
cmpq %rax, %rdi
cmovgeq %rdx, %rdi
cmpq $0xa, %rdi
movl $0x57, %r8d
cmovll %esi, %r8d
addb %dil, %r8b
movb %r8b, 0x50(%rsp,%rcx)
incq %rcx
cmpq %r12, %rcx
jge 0xded9
xorps %xmm2, %xmm2
cvtsi2sd %rdi, %xmm2
divsd %xmm1, %xmm2
subsd %xmm2, %xmm3
mulsd %xmm4, %xmm1
ucomisd %xmm0, %xmm3
ja 0xde8d
movl %ecx, %eax
jmp 0xdee7
movb $0x30, 0x50(%rsp)
movl $0x1, %eax
xorl %ebx, %ebx
movb %bl, 0x50(%rsp,%rax)
movl $0x100, %r12d           # imm = 0x100
movl $0x100, %esi            # imm = 0x100
movq %r13, 0x10(%rsp)
movq %r13, %rdi
callq 0x9357
xorpd %xmm0, %xmm0
ucomisd 0x30(%rsp), %xmm0
movq %rax, 0x30(%rsp)
movq (%rax), %r13
movb %bl, (%r13)
leaq 0x10c649(%rip), %rax    # 0x11a56b
leaq 0x10e5a5(%rip), %rdx    # 0x11c4ce
cmovaq %rax, %rdx
movq $0x0, (%rsp)
leaq 0x103e35(%rip), %r8     # 0x111d71
leaq 0x150(%rsp), %rcx
leaq 0x50(%rsp), %r9
movl $0x100, %esi            # imm = 0x100
movq %r13, %rdi
xorl %eax, %eax
callq 0x9517
movq %rax, %r15
movq %rax, 0x8(%rsp)
movq 0x210(%rsp), %rdx
jmp 0xdd4d
movb $0x2d, (%r9)
movq %rbp, %r13
movq $0x0, (%rsp)
leaq 0x1086e1(%rip), %r8     # 0x116664
movq %r15, %rsi
movq %r10, %rdx
xorl %eax, %eax
callq 0x9517
movq 0x210(%rsp), %rcx
movq %rax, (%rcx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x9a12
jmp 0xddbe
movq $0x6, (%r9)
movq %r13, %rbx
movq 0x850(%r13), %rax
testq %rax, %rax
je 0xdfd9
movq %rax, %r12
movq 0x18(%r12), %rax
movq %rax, 0x850(%rbx)
movq (%r12), %rcx
jmp 0xe07f
movq 0x838(%rbx), %rax
movq %rax, %r12
testq %rax, %rax
jne 0xe027
movl $0x2800, %edi           # imm = 0x2800
callq 0x8480
movq %rax, 0x838(%rbx)
movq %rax, %r12
addq $0x28, %rax
movl $0xff, %ecx
movq %rax, -0x10(%rax)
movl $0x0, -0x20(%rax)
addq $0x28, %rax
decl %ecx
jne 0xe005
movq $0x0, -0x10(%rax)
movl $0x0, -0x20(%rax)
movq 0x18(%r12), %rax
movq %rax, 0x838(%rbx)
movq 0x8c8(%rbx), %rcx
leaq 0x8(%rcx), %rdx
cmpq $0x10000, %rdx          # imm = 0x10000
jbe 0xe061
movl $0x10000, %edi          # imm = 0x10000
callq 0x8480
movq %rax, 0x8d0(%rbx)
movl $0x8, %edx
xorl %ecx, %ecx
jmp 0xe068
movq 0x8d0(%rbx), %rax
addq %rax, %rcx
movq %rdx, 0x8c8(%rbx)
movq %rcx, (%r12)
movl $0x3, 0x8(%r12)
movq $0x7, 0x10(%r12)
movw $0x302e, 0x4(%rcx)      # imm = 0x302E
movl $0x6e616e2b, (%rcx)     # imm = 0x6E616E2B
movb $0x0, 0x6(%rcx)
jmp 0xddbe