// File: /workspace/github_c_makefile_star2_O2/janet-lang[P]janet/src/core/strtod.c
// Function: janet_scan_number_base
int janet_scan_number_base(
    const uint8_t *str,
    int32_t len,
    int32_t base,
    double *out) {
    const uint8_t *end = str + len;
    int seenadigit = 0;
    int ex = 0;
    int seenpoint = 0;
    int foundexp = 0;
    int neg = 0;
    struct BigNat mant;
    bignat_zero(&mant);

    /* Prevent some kinds of overflow bugs relating to the exponent
     * overflowing.  For example, if a string was passed 2GB worth of 0s after
     * the decimal point, exponent could wrap around and become positive. It's
     * easier to reject ridiculously large inputs than to check for overflows.
     * */
    if (len > INT32_MAX / 40) goto error;

    /* Get sign */
    if (str >= end) goto error;
    if (*str == '-') {
        neg = 1;
        str++;
    } else if (*str == '+') {
        str++;
    }

    /* Check for leading 0x or digit digit r */
    if (base == 0) {
        if (str + 1 < end && str[0] == '0' && str[1] == 'x') {
            base = 16;
            str += 2;
        } else if (str + 1 < end  &&
                   str[0] >= '0' && str[0] <= '9' &&
                   str[1] == 'r') {
            base = str[0] - '0';
            str += 2;
        } else if (str + 2 < end  &&
                   str[0] >= '0' && str[0] <= '9' &&
                   str[1] >= '0' && str[1] <= '9' &&
                   str[2] == 'r') {
            base = 10 * (str[0] - '0') + (str[1] - '0');
            if (base < 2 || base > 36) goto error;
            str += 3;
        }
    }

    /* If still base is 0, set to default (10) */
    if (base == 0) {
        base = 10;
    }
    int exp_base = base;

    /* Skip leading zeros */
    while (str < end && (*str == '0' || *str == '.')) {
        if (seenpoint) ex--;
        if (*str == '.') {
            if (seenpoint) goto error;
            seenpoint = 1;
        } else {
            seenadigit = 1;
        }
        str++;
    }

    /* Parse significant digits */
    while (str < end) {
        if (*str == '.') {
            if (seenpoint) goto error;
            seenpoint = 1;
        } else if (*str == '&') {
            foundexp = 1;
            break;
        } else if (base == 16 && (*str == 'P' || *str == 'p')) { /* IEEE hex float */
            foundexp = 1;
            exp_base = 10;
            base = 2;
            ex *= 4; /* We need to correct the current exponent after we change the base */
            break;
        } else if (base == 10 && (*str == 'E' || *str == 'e')) {
            foundexp = 1;
            break;
        } else if (*str == '_') {
            if (!seenadigit) goto error;
        } else {
            int digit = digit_lookup[*str & 0x7F];
            if (*str > 127 || digit >= base) goto error;
            if (seenpoint) ex--;
            bignat_muladd(&mant, base, digit);
            seenadigit = 1;
        }
        str++;
    }

    if (!seenadigit)
        goto error;

    /* Read exponent */
    if (str < end && foundexp) {
        int eneg = 0;
        int32_t ee = 0;
        seenadigit = 0;
        str++;
        if (str >= end) goto error;
        if (*str == '-') {
            eneg = 1;
            str++;
        } else if (*str == '+') {
            str++;
        }
        /* Skip leading 0s in exponent */
        while (str < end && *str == '0') {
            str++;
            seenadigit = 1;
        }
        while (str < end) {
            int digit = digit_lookup[*str & 0x7F];
            if (*str > 127 || digit >= exp_base) goto error;
            if (ee < (INT32_MAX / 40)) {
                ee = exp_base * ee + digit;
            }
            str++;
            seenadigit = 1;
        }
        if (eneg) ex -= ee;
        else ex += ee;
    }

    if (!seenadigit)
        goto error;

    *out = convert(neg, &mant, base, ex);
    janet_free(mant.digits);
    return 0;

error:
    janet_free(mant.digits);
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
andq $0x0, -0x48(%rbp)
andl $0x0, -0x40(%rbp)
andq $0x0, -0x38(%rbp)
leal -0x1(%rsi), %eax
pushq $0x1
popq %r14
cmpl $0x3333332, %eax        # imm = 0x3333332
ja 0x42659
movl %edx, %r13d
movq %rdi, %r9
movslq %esi, %rax
movzbl (%rdi), %edx
movb $0x1, %dil
cmpl $0x2b, %edx
je 0x4230d
movq %r9, %r15
cmpl $0x2d, %edx
jne 0x42311
leaq 0x1(%r9), %r15
xorl %edi, %edi
jmp 0x42311
leaq 0x1(%r9), %r15
addq %rax, %r9
testl %r13d, %r13d
movq %rcx, -0x50(%rbp)
movl %edi, -0x2c(%rbp)
je 0x42442
xorl %eax, %eax
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpq %r9, %r15
jae 0x42374
movzbl (%r15), %ecx
cmpl $0x30, %ecx
je 0x4233e
cmpl $0x2e, %ecx
jne 0x42374
testl %r12d, %r12d
sete %dl
cmpb $0x2e, %cl
setne %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x42659
cmpb $0x2e, %cl
movl %r14d, %ecx
cmovnel %r12d, %ecx
cmovnel %r14d, %eax
cmpl %r14d, %r12d
adcl $-0x1, %ebx
incq %r15
movl %ecx, %r12d
jmp 0x4232b
incq %r15
pushq $0x1
popq %r14
leaq -0x1(%r15), %rcx
cmpq %r9, %rcx
jae 0x4246e
movzbl (%rcx), %ecx
cmpl $0x2e, %ecx
je 0x423be
cmpl $0x26, %ecx
je 0x424c2
cmpl $0xa, %r13d
je 0x423cb
cmpl $0x10, %r13d
jne 0x423e4
cmpq $0x5f, %rcx
je 0x423e9
cmpl $0x70, %ecx
je 0x425d3
cmpl $0x50, %ecx
jne 0x423f5
jmp 0x425d3
testl %r12d, %r12d
movl %r14d, %r12d
je 0x4243a
jmp 0x42659
cmpq $0x5f, %rcx
je 0x423e9
cmpl $0x45, %ecx
je 0x425ca
cmpl $0x65, %ecx
jne 0x423f5
jmp 0x425ca
cmpl $0x5f, %ecx
jne 0x423f5
testl %eax, %eax
movl %r14d, %eax
jne 0x4243a
jmp 0x42659
testb %cl, %cl
js 0x42659
andl $0x7f, %ecx
leaq 0x24099(%rip), %rax     # 0x664a0
movzbl (%rcx,%rax), %edx
cmpl %edx, %r13d
jle 0x42659
cmpl $0x1, %r12d
adcl $-0x1, %ebx
leaq -0x48(%rbp), %rdi
movl %r13d, %esi
movl %ebx, %r14d
movq %r9, %rbx
callq 0x42837
movq %rbx, %r9
movl %r14d, %ebx
pushq $0x1
popq %r14
movl %r14d, %eax
incq %r15
jmp 0x4237b
leaq 0x1(%r15), %rax
cmpq %r9, %rax
jae 0x424f3
movb (%r15), %sil
cmpb $0x30, %sil
jne 0x424ca
movb (%rax), %dl
xorl %esi, %esi
cmpb $0x78, %dl
jne 0x424db
addq $0x2, %r15
pushq $0x10
popq %r13
jmp 0x42324
testl %eax, %eax
je 0x42659
movslq -0x44(%rbp), %r14
movq %r13, %r15
cvtsi2sd %r13d, %xmm0
callq 0x120e0
cvtsi2sd %ebx, %xmm1
mulsd %xmm1, %xmm0
callq 0x129f0
movl -0x48(%rbp), %eax
orl %r14d, %eax
jne 0x42558
xorpd %xmm0, %xmm0
cmpb $0x0, -0x2c(%rbp)
jne 0x424b2
movsd 0x23fde(%rip), %xmm0    # 0x66490
movq -0x50(%rbp), %rbx
movsd %xmm0, (%rbx)
xorl %r14d, %r14d
jmp 0x42659
movl %r13d, %r10d
jmp 0x425de
leal -0x30(%rsi), %edx
cmpb $0x9, %dl
ja 0x424f3
movzbl %sil, %esi
movb (%rax), %dl
addl $-0x30, %esi
cmpb $0x72, %dl
jne 0x424f3
addq $0x2, %r15
testl %esi, %esi
pushq $0xa
popq %r13
cmovnel %esi, %r13d
jmp 0x42324
leaq 0x2(%r15), %rcx
pushq $0xa
popq %r13
cmpq %r9, %rcx
jae 0x42324
movzbl (%r15), %edx
leal -0x30(%rdx), %esi
cmpb $0x9, %sil
ja 0x42324
movzbl (%rax), %eax
leal -0x30(%rax), %esi
cmpb $0x9, %sil
ja 0x42324
cmpb $0x72, (%rcx)
jne 0x42324
imull $0xa, %edx, %r13d
leal (%rax,%r13), %ecx
addl $0xfffffdcb, %ecx       # imm = 0xFFFFFDCB
cmpl $-0x23, %ecx
jb 0x42659
addl %eax, %r13d
addl $0xfffffdf0, %r13d      # imm = 0xFFFFFDF0
addq $0x3, %r15
jmp 0x42324
shlq $0x5, %r14
cvttsd2si %xmm0, %rax
addq %r14, %rax
addq $0x10, %rax
cmpq $0x499, %rax            # imm = 0x499
jl 0x4258b
cmpb $0x0, -0x2c(%rbp)
movq -0x50(%rbp), %rbx
jne 0x42674
movsd 0x19ae2(%rip), %xmm0    # 0x5c068
jmp 0x424b6
cmpq $-0x497, %rax           # imm = 0xFB69
jl 0x424a0
movq %r15, %rsi
movl %esi, %r14d
imull %esi, %r14d
movl %r14d, %r13d
imull %r14d, %r13d
leaq -0x48(%rbp), %r12
cmpl $0x3, %ebx
jle 0x42681
movq %r12, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x42837
movq %r15, %rsi
addl $-0x4, %ebx
jmp 0x425ac
pushq $0xa
popq %r10
movl %r10d, %r13d
jmp 0x425de
shll $0x2, %ebx
pushq $0x2
popq %r13
pushq $0xa
popq %r10
testl %eax, %eax
je 0x42659
cmpq %r9, %r15
jae 0x42659
movb (%r15), %al
leaq 0x1(%r15), %rcx
leal -0x2b(%rax), %edx
testb $-0x3, %dl
cmovneq %r15, %rcx
movb $0x1, %sil
cmpq %r9, %rcx
jae 0x4260c
cmpb $0x30, (%rcx)
jne 0x4260c
incq %rcx
xorl %esi, %esi
jmp 0x425fb
xorl %edx, %edx
cmpq %r9, %rcx
jae 0x42646
movzbl (%rcx), %esi
testb %sil, %sil
js 0x42659
andl $0x7f, %esi
leaq 0x23e7b(%rip), %rdi     # 0x664a0
movzbl (%rsi,%rdi), %esi
cmpl %esi, %r10d
jle 0x42659
movl %edx, %edi
imull %r10d, %edi
addl %esi, %edi
cmpl $0x3333333, %edx        # imm = 0x3333333
cmovll %edi, %edx
incq %rcx
xorl %esi, %esi
jmp 0x4260e
movl %edx, %ecx
negl %ecx
cmpb $0x2d, %al
cmovnel %edx, %ecx
testb $0x1, %sil
je 0x42746
movq -0x38(%rbp), %rdi
callq 0x120b0
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0x12924(%rip), %xmm0    # 0x54fa0
jmp 0x424b6
leaq -0x48(%rbp), %r12
cmpl $0x1, %ebx
jle 0x4269f
movq %r12, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x42837
movq %r15, %rsi
addl $-0x2, %ebx
jmp 0x42685
leaq -0x48(%rbp), %r12
testl %ebx, %ebx
jle 0x426b8
xorl %ebx, %ebx
movq %r12, %rdi
xorl %edx, %edx
callq 0x42837
movq %r15, %rsi
jmp 0x426a3
xorl %edi, %edi
movl -0x44(%rbp), %edx
testl %ebx, %ebx
jns 0x4277f
movslq %edx, %rax
movq %rax, -0x68(%rbp)
movl %ebx, %r12d
negl %r12d
movl %r12d, %esi
shrl $0x2, %esi
andl $-0x4, %r12d
addl $0x5, %esi
leaq -0x48(%rbp), %rdi
movl %esi, -0x54(%rbp)
callq 0x42bc2
leaq -0x48(%rbp), %rax
movq 0x10(%rax), %rsi
movq %rsi, -0x60(%rbp)
leaq (%r12,%rsi), %rdi
addq $0x14, %rdi
movq -0x68(%rbp), %rdx
shlq $0x2, %rdx
callq 0x12920
leaq 0x10(%r12), %rdx
movq -0x60(%rbp), %rdi
xorl %esi, %esi
callq 0x12410
leaq -0x48(%rbp), %rcx
movl (%rcx), %eax
movq -0x60(%rbp), %rdx
movl %eax, 0x10(%rdx,%r12)
movq %rcx, %r12
andl $0x0, (%rcx)
cmpl $-0x3, %ebx
jge 0x4274d
movq %r12, %rdi
movl %r13d, %esi
callq 0x42c45
addl $0x4, %ebx
jmp 0x42731
addl %ecx, %ebx
jmp 0x42476
leaq -0x48(%rbp), %r13
leaq -0x48(%rbp), %r12
cmpl $-0x1, %ebx
jge 0x4276a
movq %r13, %rdi
movl %r14d, %esi
callq 0x42c45
addl $0x2, %ebx
jmp 0x42755
jne 0x42778
leaq -0x48(%rbp), %rdi
movl %r15d, %esi
callq 0x42c45
imull $-0x1f, -0x54(%rbp), %edi
movl -0x44(%rbp), %edx
movq -0x50(%rbp), %rbx
testl %edx, %edx
je 0x427b3
movq -0x38(%rbp), %rax
movslq %edx, %rcx
leaq (%rax,%rcx,4), %rcx
addq $-0x8, %rcx
movl 0x4(%rcx), %esi
cmpl $0x1, %edx
cmovneq %rcx, %r12
movl (%r12), %ecx
cmpl $0x3, %edx
jl 0x427b8
movl %edx, %r8d
movl -0xc(%rax,%r8,4), %r9d
jmp 0x427c4
movl -0x48(%rbp), %esi
jmp 0x42816
xorl %r9d, %r9d
cmpl $0x2, %edx
jne 0x427c4
movl -0x48(%rbp), %r9d
bsrl %esi, %r8d
xorl $0x1f, %r8d
pushq $0x20
popq %rax
subl %r8d, %eax
shlq $0x17, %rcx
shrl $0x8, %r9d
addq %rcx, %r9
movq %r9, %r10
movl %eax, %ecx
shrq %cl, %r10
leal 0x16(%r8), %ecx
shlq %cl, %rsi
orq %r10, %rsi
btq %rax, %r9
adcq $0x0, %rsi
xorl %eax, %eax
movq %rsi, %rcx
shrq $0x36, %rcx
setne %al
leal 0x1(%rax), %ecx
shrq %cl, %rsi
imull $0x1f, %edx, %ecx
addl %ecx, %edi
subl %r8d, %edi
addl %eax, %edi
addl $-0x15, %edi
cvtsi2sd %rsi, %xmm0
callq 0x12130
cmpb $0x0, -0x2c(%rbp)
jne 0x424b6
xorpd 0x23c4e(%rip), %xmm0    # 0x66480
jmp 0x424b6