// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
xorl %eax, %eax
movq %rax, -0x48(%rbp)
movl $0x0, -0x40(%rbp)
movq %rax, -0x38(%rbp)
leal -0x1(%rsi), %eax
cmpl $0x3333332, %eax        # imm = 0x3333332
ja 0x45e82
movl %edx, %r11d
movq %rdi, %r12
movslq %esi, %rax
movzbl (%rdi), %edx
movb $0x1, %r10b
cmpl $0x2b, %edx
je 0x45ab6
movq %r12, %r15
cmpl $0x2d, %edx
jne 0x45abb
leaq 0x1(%r12), %r15
xorl %r10d, %r10d
jmp 0x45abb
leaq 0x1(%r12), %r15
addq %rax, %r12
testl %r11d, %r11d
je 0x45c6b
xorl %eax, %eax
cmpq %r12, %r15
jae 0x45b2c
xorl %edx, %edx
movl $0x1, %r9d
xorl %ebx, %ebx
xorl %eax, %eax
movzbl (%r15), %esi
cmpl $0x30, %esi
je 0x45aec
cmpl $0x2e, %esi
jne 0x45cd0
testl %edx, %edx
sete %dil
cmpb $0x2e, %sil
setne %r8b
orb %dil, %r8b
cmpb $0x1, %r8b
jne 0x45e82
cmpb $0x2e, %sil
movl $0x1, %r14d
cmovnel %edx, %r14d
cmovnel %r9d, %eax
cmpl %r9d, %edx
adcl $-0x1, %ebx
incq %r15
movl %r14d, %edx
cmpq %r12, %r15
jb 0x45ada
jmp 0x45b31
xorl %ebx, %ebx
xorl %r14d, %r14d
movl %r10d, -0x30(%rbp)
movq %rcx, -0x50(%rbp)
cmpq %r12, %r15
jae 0x45c17
incq %r15
movzbl -0x1(%r15), %edx
cmpl $0x2e, %edx
je 0x45b7d
cmpl $0x26, %edx
je 0x45d84
cmpl $0xa, %r11d
je 0x45b93
cmpl $0x10, %r11d
jne 0x45bb1
cmpq $0x5f, %rdx
je 0x45bb6
cmpl $0x70, %edx
je 0x45dd5
cmpl $0x50, %edx
jne 0x45bca
jmp 0x45dd5
movl $0x1, %r13d
testl %r14d, %r14d
movl $0x1, %r14d
je 0x45c07
jmp 0x45ffd
cmpq $0x5f, %rdx
je 0x45bb6
movl $0xa, %ecx
cmpl $0x45, %edx
je 0x45dcd
cmpl $0x65, %edx
jne 0x45bca
jmp 0x45dcd
cmpl $0x5f, %edx
jne 0x45bca
movl $0x1, %r13d
testl %eax, %eax
movl $0x1, %eax
jne 0x45c07
jmp 0x45ffd
testb %dl, %dl
js 0x45e82
andl $0x7f, %edx
leaq 0x248a4(%rip), %rax     # 0x6a480
movzbl (%rdx,%rax), %edx
cmpl %edx, %r11d
jle 0x45e82
cmpl $0x1, %r14d
adcl $-0x1, %ebx
leaq -0x48(%rbp), %rdi
movl %r11d, %esi
movl %r11d, %r13d
callq 0x46018
movl %r13d, %r11d
movl $0x1, %eax
leaq 0x1(%r15), %rcx
cmpq %r12, %r15
movq %rcx, %r15
jb 0x45b45
movl $0x1, %r13d
testl %eax, %eax
je 0x45ffd
movslq -0x44(%rbp), %r14
movl %r11d, -0x2c(%rbp)
cvtsi2sd %r11d, %xmm0
callq 0x120e0
cvtsi2sd %ebx, %xmm1
mulsd %xmm1, %xmm0
callq 0x129f0
movl -0x48(%rbp), %eax
orl %r14d, %eax
jne 0x45c99
xorpd %xmm0, %xmm0
cmpb $0x0, -0x30(%rbp)
movq -0x50(%rbp), %r13
jne 0x45ff4
movsd 0x2480a(%rip), %xmm0    # 0x6a470
jmp 0x45ff4
leaq 0x1(%r15), %rax
cmpq %r12, %rax
jae 0x45d03
movb (%r15), %sil
cmpb $0x30, %sil
jne 0x45cd8
movb (%rax), %dl
xorl %esi, %esi
cmpb $0x78, %dl
jne 0x45ce9
addq $0x2, %r15
movl $0x10, %r11d
jmp 0x45ac7
shlq $0x5, %r14
cvttsd2si %xmm0, %rax
addq %r14, %rax
addq $0x10, %rax
cmpq $0x499, %rax            # imm = 0x499
movq -0x50(%rbp), %r13
jl 0x45d69
cmpb $0x0, -0x30(%rbp)
jne 0x45d89
movsd 0x1a38d(%rip), %xmm0    # 0x60058
jmp 0x45ff4
movl %edx, %r14d
jmp 0x45b31
leal -0x30(%rsi), %edx
cmpb $0x9, %dl
ja 0x45d03
movzbl %sil, %esi
movb (%rax), %dl
addl $-0x30, %esi
cmpb $0x72, %dl
jne 0x45d03
addq $0x2, %r15
testl %esi, %esi
movl $0xa, %r11d
cmovnel %esi, %r11d
jmp 0x45ac7
leaq 0x2(%r15), %rdi
movl $0xa, %r11d
cmpq %r12, %rdi
jae 0x45ac7
movzbl (%r15), %edx
leal -0x30(%rdx), %esi
cmpb $0x9, %sil
ja 0x45ac7
movzbl (%rax), %eax
leal -0x30(%rax), %esi
cmpb $0x9, %sil
ja 0x45ac7
cmpb $0x72, (%rdi)
jne 0x45ac7
leal (%rdx,%rdx,4), %esi
leal (%rax,%rsi,2), %edx
addl $0xfffffdcb, %edx       # imm = 0xFFFFFDCB
cmpl $-0x23, %edx
jb 0x45e82
leal (%rax,%rsi,2), %r11d
addl $0xfffffdf0, %r11d      # imm = 0xFFFFFDF0
addq $0x3, %r15
jmp 0x45ac7
cmpq $-0x497, %rax           # imm = 0xFB69
jge 0x45d96
xorpd %xmm0, %xmm0
cmpb $0x0, -0x30(%rbp)
je 0x45c5e
jmp 0x45ff4
movl %r11d, %ecx
jmp 0x45de3
movsd 0x1320f(%rip), %xmm0    # 0x58fa0
jmp 0x45ff4
cmpl $0x4, %ebx
jl 0x45e8d
movl -0x2c(%rbp), %r15d
imull %r15d, %r15d
imull %r15d, %r15d
leaq -0x48(%rbp), %r12
movq %r12, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x46018
leal -0x4(%rbx), %r14d
cmpl $0x7, %ebx
movl %r14d, %ebx
ja 0x45daf
jmp 0x45e90
movl $0xa, %r11d
jmp 0x45de3
shll $0x2, %ebx
movl $0xa, %ecx
movl $0x2, %r11d
testl %eax, %eax
je 0x45e82
cmpq %r12, %r15
jae 0x45e82
movb (%r15), %al
leaq 0x1(%r15), %rdx
leal -0x2b(%rax), %esi
testb $-0x3, %sil
cmovneq %r15, %rdx
movb $0x1, %sil
cmpq %r12, %rdx
jae 0x45e2e
cmpb $0x30, (%rdx)
setne %sil
jne 0x45e2e
incq %rdx
movq %rdx, %rdi
movq %rdi, %rdx
cmpq %r12, %rdi
jae 0x45e2e
leaq 0x1(%rdx), %rdi
cmpb $0x30, (%rdx)
je 0x45e1d
cmpq %r12, %rdx
jae 0x45edb
xorl %esi, %esi
movzbl (%rdx), %edi
testb %dil, %dil
js 0x45e82
andl $0x7f, %edi
leaq 0x24635(%rip), %r8      # 0x6a480
movzbl (%rdi,%r8), %edi
cmpl %edi, %ecx
jle 0x45e82
movl %esi, %r8d
imull %ecx, %r8d
addl %edi, %r8d
cmpl $0x3333333, %esi        # imm = 0x3333333
cmovll %r8d, %esi
incq %rdx
cmpq %r12, %rdx
jb 0x45e39
movl %esi, %ecx
negl %ecx
cmpb $0x2d, %al
cmovnel %esi, %ecx
addl %ebx, %ecx
movl %ecx, %ebx
jmp 0x45c25
movl $0x1, %r13d
jmp 0x45ffd
movl %ebx, %r14d
cmpl $0x2, %r14d
jl 0x45ebe
movl -0x2c(%rbp), %r15d
imull %r15d, %r15d
leaq -0x48(%rbp), %r12
movq %r12, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x46018
leal -0x2(%r14), %ebx
cmpl $0x3, %r14d
movl %ebx, %r14d
ja 0x45ea2
jmp 0x45ec1
movl %r14d, %ebx
testl %ebx, %ebx
jle 0x45eef
leaq -0x48(%rbp), %rdi
xorl %r15d, %r15d
movl -0x2c(%rbp), %esi
xorl %edx, %edx
callq 0x46018
jmp 0x45fda
movl $0x1, %r13d
testb %sil, %sil
je 0x45c25
jmp 0x45ffd
xorl %r15d, %r15d
testl %ebx, %ebx
jns 0x45fda
movl %ebx, %r14d
negl %r14d
movl %r14d, %r15d
shrl $0x2, %r15d
andl $-0x4, %r14d
addl $0x5, %r15d
leaq -0x48(%rbp), %rdi
movslq 0x4(%rdi), %r13
movl %r15d, %esi
callq 0x463f6
leaq -0x48(%rbp), %rax
movq 0x10(%rax), %r12
leaq (%r14,%r12), %rdi
addq $0x14, %rdi
shlq $0x2, %r13
movq %r12, %rsi
movq %r13, %rdx
callq 0x12920
leaq 0x10(%r14), %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x12410
leaq -0x48(%rbp), %rcx
movl (%rcx), %eax
movl %eax, 0x10(%r12,%r14)
movl $0x0, (%rcx)
cmpl $-0x4, %ebx
ja 0x45f8b
movl -0x2c(%rbp), %r12d
imull %r12d, %r12d
imull %r12d, %r12d
leaq -0x48(%rbp), %r13
movq %r13, %rdi
movl %r12d, %esi
callq 0x46454
leal 0x4(%rbx), %r14d
cmpl $-0x7, %ebx
movl %r14d, %ebx
jb 0x45f72
jmp 0x45f8e
movl %ebx, %r14d
movl %r15d, %ebx
shll $0x5, %ebx
cmpl $-0x2, %r14d
jg 0x45fc0
movl -0x2c(%rbp), %r12d
imull %r12d, %r12d
leaq -0x48(%rbp), %r13
movq %r13, %rdi
movl %r12d, %esi
callq 0x46454
leal 0x2(%r14), %eax
cmpl $-0x3, %r14d
movl %eax, %r14d
jb 0x45fa6
jmp 0x45fc3
movl %r14d, %eax
subl %ebx, %r15d
testl %eax, %eax
jns 0x45fd6
leaq -0x48(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x46454
movq -0x50(%rbp), %r13
leaq -0x48(%rbp), %rdi
movl %r15d, %esi
callq 0x464cf
cmpb $0x0, -0x30(%rbp)
jne 0x45ff4
xorpd 0x2446c(%rip), %xmm0    # 0x6a460
movsd %xmm0, (%r13)
xorl %r13d, %r13d
movq -0x38(%rbp), %rdi
callq 0x120b0
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq