// File: /workspace/github_c_makefile_star2_O2/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, %r9
movl %edx, %ecx
decl %ecx
movabsq $0x1, %r8
shlq %cl, %r8
imull $0x3e8, %ecx, %eax      # imm = 0x3E8
movl $0xcfa, %ecx            # imm = 0xCFA
xorl %edx, %edx
divl %ecx
movzbl (%rdi), %ecx
cmpl $0x2b, %ecx
je 0x222cf
cmpl $0x2d, %ecx
jne 0x222d2
incq %rdi
xorl %ecx, %ecx
subl $0x1, %eax
jb 0x22306
movzbl (%rdi), %edx
leal -0x30(%rdx), %r10d
cmpb $0xa, %r10b
jae 0x22388
imulq $0xa, %rcx, %rcx
addq %rdx, %rcx
addq $-0x30, %rcx
incq %rdi
jmp 0x222a9
incq %rdi
decq %r8
xorl %ecx, %ecx
subl $0x1, %eax
jb 0x222f9
movzbl (%rdi), %edx
leal -0x30(%rdx), %r10d
cmpb $0xa, %r10b
jae 0x222fe
imulq $0xa, %rcx, %rcx
incq %rdi
addq %rdx, %rcx
addq $-0x30, %rcx
jmp 0x222d7
movb $0x1, %r10b
jmp 0x22309
movq %rdi, (%rsi)
jmp 0x2238e
xorl %r10d, %r10d
movb (%rdi), %r11b
cmpb $0x30, %r11b
jae 0x2231c
movq %rdi, (%rsi)
testb %r10b, %r10b
je 0x2238b
jmp 0x2238e
movabsq $0xa, %rbx
movq %r8, %rax
xorl %edx, %edx
divq %rbx
imulq $-0xa, %rax, %rdx
addq %r8, %rdx
movzbl %r11b, %r11d
addl $-0x30, %r11d
cmpl $0x9, %r11d
ja 0x22312
movl %r11d, %r11d
cmpq %rax, %rcx
jb 0x22352
jne 0x22362
cmpq %r11, %rdx
jb 0x22362
imulq $0xa, %rcx, %rcx
addq %r11, %rcx
movb 0x1(%rdi), %r11b
incq %rdi
jmp 0x22335
movzbl 0x1(%rdi), %eax
incq %rdi
addl $-0x30, %eax
cmpl $0xa, %eax
jb 0x22362
movl $0x1, (%r9)
movq %rdi, (%rsi)
testb %r10b, %r10b
jne 0x22383
negq %r8
movq %r8, %rcx
jmp 0x2238e
movq %rdi, (%rsi)
negq %rcx
movq %rcx, %rax
popq %rbx
retq