// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/./textutils_internal.h
// Function: hts_str2int
static inline int64_t hts_str2int(const char *in, char **end, int bits,
                                    int *failed) {
    uint64_t n = 0, limit = (1ULL << (bits - 1)) - 1;
    uint32_t fast = (bits - 1) * 1000 / 3322 + 1; // log(10)/log(2) ~= 3.322
    const unsigned char *v = (const unsigned char *) in;
    const unsigned int ascii_zero = '0'; // Prevents conversion to signed
    unsigned int d;

    int neg;
    switch(*v) {
    case '-':
        limit++;
        neg=1;
        v++;
        // See "dup" comment below
        while (--fast && *v>='0' && *v<='9')
            n = n*10 + *v++ - ascii_zero;
        break;

    case '+':
        v++;
        // fall through

    default:
        neg = 0;
        // dup of above.  This is somewhat unstable and mainly for code
        // size cheats to prevent instruction cache lines spanning 32-byte
        // blocks in the sam_parse_B_vals calling code.  It's been tested
        // on gcc7, gcc13, clang10 and clang16 with -O2 and -O3.  While
        // not exhaustive, this code duplication gives stable fast results
        // while a single copy does not.
        // (NB: system was "seq4d", so quite old)
        while (--fast && *v>='0' && *v<='9')
            n = n*10 + *v++ - ascii_zero;
        break;
    }

    // NB gcc7 is slow with (unsigned)(*v - ascii_zero) < 10,
    // while gcc13 prefers it.
    if (*v>='0' && !fast) { // rejects ',' and tab
        uint64_t limit_d_10 = limit / 10;
        uint64_t limit_m_10 = limit - 10 * limit_d_10;
        while ((d = *v - ascii_zero) < 10) {
            if (n < limit_d_10 || (n == limit_d_10 && d <= limit_m_10)) {
                n = n*10 + d;
                v++;
            } else {
                do { v++; } while (*v - ascii_zero < 10);
                n = limit;
                *failed = 1;
                break;
            }
        }
    }

    *end = (char *)v;

    return neg ? (int64_t)-n : (int64_t)n;
}

// Assembly:
pushq %rbx
movq %rcx, %r8
movl %edx, %ecx
decl %ecx
movl $0x1, %r9d
shlq %cl, %r9
imull $0x3e8, %ecx, %edx      # imm = 0x3E8
movl $0x9dd2b003, %eax       # imm = 0x9DD2B003
imulq %rdx, %rax
shrq $0x2b, %rax
movzbl (%rdi), %edx
cmpl $0x2b, %edx
je 0x28dc8
cmpl $0x2d, %edx
jne 0x28dcb
leaq 0x1(%rdi), %r10
cmpl $0x4, %ecx
jae 0x28e0f
xorl %edx, %edx
xorl %edi, %edi
jmp 0x28ddb
incq %rdi
movq %rdi, %r10
decq %r9
movb $0x1, %dil
cmpl $0x4, %ecx
jae 0x28ddf
xorl %edx, %edx
xorl %ecx, %ecx
jmp 0x28e47
leal -0x1(%rax), %ecx
leaq (%rcx,%r10), %r11
incq %r11
xorl %ecx, %ecx
movzbl (%r10), %ebx
leal -0x3a(%rbx), %edx
cmpb $-0xa, %dl
setb %dl
jb 0x28e47
leaq (%rcx,%rcx,4), %rcx
incq %r10
leaq (%rbx,%rcx,2), %rcx
addq $-0x30, %rcx
decl %eax
jne 0x28deb
jmp 0x28e40
leal -0x1(%rax), %ecx
leaq (%rdi,%rcx), %r11
addq $0x2, %r11
xorl %ecx, %ecx
movzbl (%r10), %edi
leal -0x3a(%rdi), %edx
cmpb $-0xa, %dl
setb %dl
jb 0x28e45
leaq (%rcx,%rcx,4), %rcx
leaq (%rdi,%rcx,2), %rcx
addq $-0x30, %rcx
incq %r10
decl %eax
jne 0x28e1c
xorl %edi, %edi
movq %r11, %r10
jmp 0x28e47
xorl %edi, %edi
testb %dl, %dl
jne 0x28ec6
cmpb $0x30, (%r10)
jb 0x28ec6
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
movq %r9, %rax
mulq %rdx
movzbl (%r10), %r11d
addl $-0x30, %r11d
cmpl $0x9, %r11d
ja 0x28ec6
shrq $0x3, %rdx
leaq (%rdx,%rdx), %rax
leaq (%rax,%rax,4), %rbx
movq %r9, %rax
subq %rbx, %rax
movl %r11d, %r11d
cmpq %rdx, %rcx
jb 0x28e90
jne 0x28eac
cmpq %r11, %rax
jb 0x28eac
leaq (%rcx,%rcx,4), %rcx
leaq (%r11,%rcx,2), %rcx
movzbl 0x1(%r10), %r11d
incq %r10
addl $-0x30, %r11d
cmpl $0xa, %r11d
jb 0x28e81
jmp 0x28ec6
movzbl 0x1(%r10), %eax
incq %r10
addl $-0x30, %eax
cmpl $0xa, %eax
jb 0x28eac
movl $0x1, (%r8)
movq %r9, %rcx
movq %r10, (%rsi)
movq %rcx, %rax
negq %rax
testb %dil, %dil
cmovneq %rcx, %rax
popq %rbx
retq