// File: /workspace/github_c_makefile_star2_O1/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 $0x1e8, %rsp            # imm = 0x1E8
movl %r9d, %ebp
movq %r8, %r12
movq %rsi, %r14
movq %rdi, %r15
movq 0x220(%rsp), %r13
cmpl $0xa, %edx
jne 0xe5cf
movsbl %bpl, %r8d
movq %r15, %rdi
movq %r14, %rsi
movq %rcx, %rdx
movq %r12, %rcx
movq %r13, %r9
callq 0x95820
movq %rax, %rbx
movq (%r13), %r14
leaq 0x1(%r14), %rsi
movq %r15, %rdi
callq 0xa305
movq %rax, %rbp
movq (%rax), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x93e0
movb $0x0, (%r15,%r14)
jmp 0xeb27
movl %edx, %ebx
movzbl (%r14), %eax
cmpl $0xd, %eax
movq %rcx, 0x10(%rsp)
je 0xe6ab
cmpl $0xc, %eax
je 0xe650
cmpl $0xb, %eax
jne 0xe719
leaq 0x80(%rcx), %rsi
movq %r15, %rdi
callq 0xa305
movq %rax, %rbp
movq (%rax), %r15
movq 0x8(%r14), %rsi
movq %r15, %rdi
movl %ebx, %edx
callq 0x96536
movq 0x10(%rsp), %rbx
subq %rax, %rbx
jbe 0xe647
leaq (%r15,%rbx), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x9550
movq %r15, %rdi
movl $0x20, %esi
movq %rbx, %rdx
callq 0x9270
movq 0x10(%rsp), %rax
movb $0x0, (%r15,%rax)
movq %rax, (%r13)
jmp 0xeb27
leaq 0x100(%rcx), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0xa305
movq %rax, 0x20(%rsp)
movq (%rax), %r13
movq 0x8(%r14), %rsi
movq %r13, %rdi
movl %ebx, %edx
callq 0x96536
movq %rax, %rbp
leaq (%rax,%r13), %rdi
incq %rdi
movb $0x2f, -0x1(%rdi)
movq 0x10(%r14), %rsi
movl %ebx, %edx
callq 0x96536
leaq (%rax,%rbp), %rcx
addq %rbp, %rax
incq %rax
movq %rax, 0x18(%rsp)
movb $0x0, 0x1(%r13,%rcx)
jmp 0xeaa7
movsd 0x8(%r14), %xmm1
ucomisd %xmm1, %xmm1
jp 0xed2a
movq %xmm1, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
movaps %xmm1, 0x20(%rsp)
jne 0xe7b6
movq $0x6, (%r13)
movl $0x7, %esi
movq %r15, %rdi
callq 0xa305
movq %rax, %rbp
xorpd %xmm0, %xmm0
ucomisd 0x20(%rsp), %xmm0
movq (%rax), %rax
jbe 0xe957
movw $0x302e, 0x4(%rax)      # imm = 0x302E
movl $0x666e692d, (%rax)     # imm = 0x666E692D
jmp 0xe963
movsd 0x8(%r14), %xmm0
movq 0x1f28(%r15), %rax
movsd %xmm0, 0x8(%rax)
movq 0x1f28(%r15), %rsi
leaq 0x18(%rsp), %rax
movq %rax, (%rsp)
movsbl %bpl, %r13d
movq %r15, %rdi
movl %ebx, %edx
xorl %ecx, %ecx
movq %r12, %r8
movl %r13d, %r9d
callq 0xe55c
movq %rax, %rbp
movsd 0x10(%r14), %xmm0
movq 0x1f30(%r15), %rax
movsd %xmm0, 0x8(%rax)
movq 0x1f30(%r15), %rsi
leaq 0x18(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
movl %ebx, %edx
xorl %ecx, %ecx
movq %r12, %r8
movl %r13d, %r9d
callq 0xe55c
movq %rax, %rbx
movq (%rax), %r8
leaq 0x838(%r15), %r14
movq 0x880(%r15), %r12
testq %r12, %r12
je 0xe8ce
movq 0x18(%r12), %rax
movq %rax, 0x880(%r15)
jmp 0xe9d3
movapd 0x104602(%rip), %xmm0   # 0x112dc0
xorpd %xmm1, %xmm0
maxsd %xmm1, %xmm0
ucomisd 0x10086a(%rip), %xmm0   # 0x10f038
movapd %xmm0, 0x50(%rsp)
jbe 0xe96f
leaq 0x18(%rsp), %r14
movq $0x0, (%r14)
callq 0x92b0
movsd %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x48(%rsp)
callq 0x92b0
movsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x95f0
movapd %xmm0, 0x30(%rsp)
cvttpd2dq %xmm0, %xmm1
cvtdq2pd %xmm1, %xmm1
movsd 0x48(%rsp), %xmm0
callq 0x9160
movapd 0x50(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x1f28(%r15), %rax
movsd %xmm1, 0x8(%rax)
movq 0x1f28(%r15), %rsi
movq %r14, (%rsp)
movsbl %bpl, %r9d
movq %r15, %rdi
movl %ebx, %edx
movq 0x10(%rsp), %rcx
movq %r12, %r8
callq 0xe55c
movq %rax, %r12
movq (%r14), %rbx
addq $0x8, %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0xa305
xorpd %xmm0, %xmm0
ucomisd 0x20(%rsp), %xmm0
movq %rax, %rbp
cvttsd2si 0x30(%rsp), %edi
leaq 0x114bc6(%rip), %rax    # 0x12345b
leaq 0x116b22(%rip), %r10    # 0x1253be
cmovaq %rax, %r10
movq (%rbp), %r13
movb $0x0, (%r13)
movq (%r12), %rcx
testl %edi, %edi
jne 0xeb3c
testb $0x1, 0x14416c(%rip)    # 0x152a28
jne 0xeb3c
leaq 0x110c9f(%rip), %r9     # 0x11f568
jmp 0xecd4
cmpq $0x0, (%r14)
jne 0xe912
movq %r8, %r12
movl $0x2800, %edi           # imm = 0x2800
callq 0x9480
movq %rax, (%r14)
addq $0x28, %rax
movl $0xff, %ecx
movq %rax, -0x10(%rax)
movl $0x0, -0x20(%rax)
addq $0x28, %rax
decl %ecx
jne 0xe8ed
movq $0x0, -0x10(%rax)
movl $0x0, -0x20(%rax)
movq %r12, %r8
movq 0x838(%r15), %r12
movq 0x18(%r12), %rax
movq %rax, 0x838(%r15)
movl $0x0, 0x8(%r12)
movq 0x8c8(%r15), %rax
leaq 0x200(%rax), %rcx
cmpq $0x10001, %rcx          # imm = 0x10001
jb 0xe9b8
movl $0x200, %edi            # imm = 0x200
movq %r8, %r13
callq 0x9480
movq %r13, %r8
jmp 0xe9c6
movw $0x302e, 0x4(%rax)      # imm = 0x302E
movl $0x666e692b, (%rax)     # imm = 0x666E692B
movb $0x0, 0x6(%rax)
xorl %r14d, %r14d
jmp 0xed11
callq 0x95f0
cvttsd2si %xmm0, %r14
leaq 0x160(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x96536
leal -0x41(%r12), %eax
cmpl $0xffffff7f, %eax       # imm = 0xFFFFFF7F
ja 0xeb9e
movl %r12d, %eax
negl %eax
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
callq 0x9160
jmp 0xebbc
addq 0x8d0(%r15), %rax
movq %rcx, 0x8c8(%r15)
movq %rax, (%r12)
movl $0x9, 0x8(%r12)
movq $0x200, 0x10(%r12)      # imm = 0x200
movq (%r12), %r13
movb $0x0, (%r13)
movq (%rbp), %rdx
movb (%r8), %al
addb $-0x2b, %al
leaq 0x1169c9(%rip), %rsi    # 0x1253be
leaq 0x10ed78(%rip), %rcx    # 0x11d774
testb $-0x3, %al
cmoveq %rsi, %rcx
movq $0x0, (%rsp)
leaq 0x114a5b(%rip), %r9     # 0x12346c
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdi
xorl %eax, %eax
callq 0xa4d7
movq %rax, 0x18(%rsp)
movslq 0x8(%rbp), %rax
cmpq $0x11, %rax
jne 0xea51
movq (%rbp), %rdi
testq %rdi, %rdi
je 0xea45
callq 0x9050
movq $0x0, (%rbp)
movl $0x0, 0x8(%rbp)
movq %r14, %rax
jmp 0xea55
leaq (%r14,%rax,8), %rax
movq (%rax), %rcx
movq %rcx, 0x18(%rbp)
movq %rbp, (%rax)
movslq 0x8(%rbx), %rax
cmpq $0x11, %rax
movq %r12, 0x20(%rsp)
jne 0xea95
movq (%rbx), %rdi
testq %rdi, %rdi
je 0xea82
callq 0x9050
movq $0x0, (%rbx)
movl $0x0, 0x8(%rbx)
movq (%r14), %rax
movq %rax, 0x18(%rbx)
movq %rbx, (%r14)
jmp 0xeaa1
movq (%r14,%rax,8), %rcx
movq %rcx, 0x18(%rbx)
movq %rbx, (%r14,%rax,8)
movl $0x200, %r12d           # imm = 0x200
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jge 0xeac2
cmpq %rcx, %r12
jle 0xeacc
movq 0x20(%rsp), %rbp
jmp 0xeae8
movq %rax, %rcx
movq 0x20(%rsp), %rbp
jmp 0xeb1c
leaq 0x1(%rcx), %rdx
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0xae0f
movq 0x10(%rsp), %rcx
movq %rax, %rbp
movq (%rax), %r13
movq %rcx, %rbx
subq 0x18(%rsp), %rbx
movb $0x0, (%r13,%rcx)
leaq (%rbx,%r13), %rdi
movq 0x18(%rsp), %rdx
movq %r13, %rsi
callq 0x9550
movq %r13, %rdi
movl $0x20, %esi
movq %rbx, %rdx
callq 0x9270
movq 0x10(%rsp), %rcx
movq 0x220(%rsp), %rax
movq %rcx, (%rax)
movq %rbp, %rax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb2e(%r15), %r9
movb $0x0, 0xb2f(%r15)
movl %edi, %esi
negl %esi
cmovsl %edi, %esi
leaq 0x1108d7(%rip), %r8     # 0x11f430
movabsq $-0x3333333333333333, %r11 # imm = 0xCCCCCCCCCCCCCCCD
movq %rsi, %rax
mulq %r11
shrq $0x3, %rdx
leaq (%rdx,%rdx), %rax
leaq (%rax,%rax,4), %rax
movq %rsi, %r14
subq %rax, %r14
movb (%r8,%r14), %al
movb %al, (%r9)
decq %r9
cmpq $0x9, %rsi
movq %rdx, %rsi
ja 0xeb63
testl %edi, %edi
js 0xecd0
incq %r9
jmp 0xecd4
movslq %ebx, %rax
movl $0x40, %ecx
subl %r12d, %ecx
shlq $0xa, %rax
leaq 0x14730c(%rip), %rdx    # 0x155ec0
addq %rax, %rdx
movsd (%rdx,%rcx,8), %xmm0
xorl %ecx, %ecx
testq %r12, %r12
movapd 0x50(%rsp), %xmm4
jle 0xec4e
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
subsd %xmm1, %xmm4
ucomisd %xmm0, %xmm4
movl $0x0, %eax
jbe 0xec50
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
movslq %ebx, %rcx
decl %ebx
movslq %ebx, %rdx
xorl %eax, %eax
movl $0x30, %esi
movapd %xmm1, %xmm2
movapd %xmm4, %xmm3
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %rdi
cmpq %rcx, %rdi
cmovgeq %rdx, %rdi
cmpq $0xa, %rdi
movl $0x57, %r8d
cmovll %esi, %r8d
addb %dil, %r8b
movb %r8b, 0x60(%rsp,%rax)
incq %rax
cmpq %r12, %rax
jge 0xec4a
xorps %xmm3, %xmm3
cvtsi2sd %rdi, %xmm3
divsd %xmm2, %xmm3
subsd %xmm3, %xmm4
mulsd %xmm1, %xmm2
ucomisd %xmm0, %xmm4
ja 0xebfe
movl %eax, %ecx
jmp 0xec50
xorl %eax, %eax
testl %ecx, %ecx
jne 0xec5e
movb $0x30, 0x60(%rsp,%rax)
movl $0x1, %ecx
movl %ecx, %eax
xorl %ebx, %ebx
movb %bl, 0x60(%rsp,%rax)
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
callq 0xa305
xorpd %xmm0, %xmm0
ucomisd 0x20(%rsp), %xmm0
movq %rax, 0x20(%rsp)
movq (%rax), %r13
movb %bl, (%r13)
leaq 0x1147cb(%rip), %rax    # 0x12345b
leaq 0x116727(%rip), %rdx    # 0x1253be
cmovaq %rax, %rdx
movq $0x0, (%rsp)
leaq 0x10bfb7(%rip), %r8     # 0x11ac61
leaq 0x160(%rsp), %rcx
leaq 0x60(%rsp), %r9
movl $0x100, %esi            # imm = 0x100
movq %r13, %rdi
xorl %eax, %eax
callq 0xa4d7
movq %rax, 0x18(%rsp)
movb $0x1, %r14b
jmp 0xed11
movb $0x2d, (%r9)
movq $0x0, (%rsp)
leaq 0x110871(%rip), %r8     # 0x11f554
xorl %r14d, %r14d
movq %r13, %rdi
movq %rbx, %rsi
movq %r10, %rdx
xorl %eax, %eax
callq 0xa4d7
movq 0x220(%rsp), %rcx
movq %rax, (%rcx)
movq %r15, %rdi
movq %r12, 0x20(%rsp)
movq %r12, %rsi
callq 0xa9a3
movq 0x10(%rsp), %rcx
movl $0x100, %r12d           # imm = 0x100
testb %r14b, %r14b
jne 0xeaac
jmp 0xeb27
movq $0x6, (%r13)
movq 0x850(%r15), %rbp
testq %rbp, %rbp
je 0xed4e
movq 0x18(%rbp), %rax
movq %rax, 0x850(%r15)
jmp 0xee01
cmpq $0x0, 0x838(%r15)
jne 0xed94
movl $0x2800, %edi           # imm = 0x2800
callq 0x9480
movq %rax, 0x838(%r15)
addq $0x28, %rax
movl $0xff, %ecx
movq %rax, -0x10(%rax)
movl $0x0, -0x20(%rax)
addq $0x28, %rax
decl %ecx
jne 0xed72
movq $0x0, -0x10(%rax)
movl $0x0, -0x20(%rax)
movq 0x838(%r15), %rbp
movq 0x18(%rbp), %rax
movq %rax, 0x838(%r15)
movl $0x0, 0x8(%rbp)
movq 0x8c8(%r15), %rax
addq $0x8, %rax
cmpq $0x10001, %rax          # imm = 0x10001
jb 0xede1
movl $0x10000, %edi          # imm = 0x10000
callq 0x9480
movq %rax, 0x8d0(%r15)
movq $0x0, 0x8c8(%r15)
movl $0x8, %eax
movq 0x8d0(%r15), %rcx
addq 0x8c8(%r15), %rcx
movq %rax, 0x8c8(%r15)
movq %rcx, (%rbp)
movl $0x3, 0x8(%rbp)
movq $0x7, 0x10(%rbp)
movq (%rbp), %rax
movw $0x302e, 0x4(%rax)      # imm = 0x302E
movl $0x6e616e2b, (%rax)     # imm = 0x6E616E2B
jmp 0xe963