// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/hts.c
// Function: hts_parse_region
const char *hts_parse_region(const char *s, int *tid, hts_pos_t *beg,
                             hts_pos_t *end, hts_name2id_f getid, void *hdr,
                             int flags)
{
    if (!s || !tid || !beg || !end || !getid)
        return NULL;

    size_t s_len = strlen(s);
    kstring_t ks = { 0, 0, NULL };

    const char *colon = NULL, *comma = NULL;
    int quoted = 0;

    if (flags & HTS_PARSE_LIST)
        flags &= ~HTS_PARSE_THOUSANDS_SEP;
    else
        flags |= HTS_PARSE_THOUSANDS_SEP;

    const char *s_end = s + s_len;

    // Braced quoting of references is permitted to resolve ambiguities.
    if (*s == '{') {
        const char *close = memchr(s, '}', s_len);
        if (!close) {
            hts_log_error("Mismatching braces in \"%s\"", s);
            *tid = -1;
            return NULL;
        }
        s++;
        s_len--;
        if (close[1] == ':')
            colon = close+1;
        quoted = 1; // number of trailing characters to trim

        // Truncate to this item only, if appropriate.
        if (flags & HTS_PARSE_LIST) {
            comma = strchr(close, ',');
            if (comma) {
                s_len = comma-s;
                s_end = comma+1;
            }
        }
    } else {
        // Truncate to this item only, if appropriate.
        if (flags & HTS_PARSE_LIST) {
            comma = strchr(s, ',');
            if (comma) {
                s_len = comma-s;
                s_end = comma+1;
            }
        }

        colon = hts_memrchr(s, ':', s_len);
    }

    // No colon is simplest case; just check and return.
    if (colon == NULL) {
        *beg = 0; *end = HTS_POS_MAX;
        kputsn(s, s_len-quoted, &ks); // convert to nul terminated string
        if (!ks.s) {
            *tid = -2;
            return NULL;
        }

        *tid = getid(hdr, ks.s);
        free(ks.s);

        return *tid >= 0 ? s_end : NULL;
    }

    // Has a colon, but check whole name first.
    if (!quoted) {
        *beg = 0; *end = HTS_POS_MAX;
        kputsn(s, s_len, &ks); // convert to nul terminated string
        if (!ks.s) {
            *tid = -2;
            return NULL;
        }
        if ((*tid = getid(hdr, ks.s)) >= 0) {
            // Entire name matches, but also check this isn't
            // ambiguous.  eg we have ref chr1 and ref chr1:100-200
            // both present.
            ks.l = 0;
            kputsn(s, colon-s, &ks); // convert to nul terminated string
            if (!ks.s) {
                *tid = -2;
                return NULL;
            }
            if (getid(hdr, ks.s) >= 0) {
                free(ks.s);
                *tid = -1;
                hts_log_error("Range is ambiguous. "
                              "Use {%s} or {%.*s}%s instead",
                              s, (int)(colon-s), s, colon);
                return NULL;
            }
            free(ks.s);

            return s_end;
        }
        if (*tid < -1) // Failed to parse header
            return NULL;
    }

    // Quoted, or unquoted and whole string isn't a name.
    // Check the pre-colon part is valid.
    ks.l = 0;
    kputsn(s, colon-s-quoted, &ks); // convert to nul terminated string
    if (!ks.s) {
        *tid = -2;
        return NULL;
    }
    *tid = getid(hdr, ks.s);
    free(ks.s);
    if (*tid < 0)
        return NULL;

    // Finally parse the post-colon coordinates
    char *hyphen;
    *beg = hts_parse_decimal(colon+1, &hyphen, flags) - 1;
    if (*beg < 0) {
        if (*beg != -1 && *hyphen == '-' && colon[1] != '\0') {
            // User specified zero, but we're 1-based.
            hts_log_error("Coordinates must be > 0");
            return NULL;
        }
        if (isdigit_c(*hyphen) || *hyphen == '\0' || *hyphen == ',') {
            // interpret chr:-100 as chr:1-100
            *end = *beg==-1 ? HTS_POS_MAX : -(*beg+1);
            *beg = 0;
            return s_end;
        } else if (*beg < -1) {
            hts_log_error("Unexpected string \"%s\" after region", hyphen);
            return NULL;
        }
    }

    if (*hyphen == '\0' || ((flags & HTS_PARSE_LIST) && *hyphen == ',')) {
        *end = flags & HTS_PARSE_ONE_COORD ? *beg+1 : HTS_POS_MAX;
    } else if (*hyphen == '-') {
        *end = hts_parse_decimal(hyphen+1, &hyphen, flags);
        if (*hyphen != '\0' && *hyphen != ',') {
            hts_log_error("Unexpected string \"%s\" after region", hyphen);
            return NULL;
        }
    } else {
        hts_log_error("Unexpected string \"%s\" after region", hyphen);
        return NULL;
    }

    if (*end == 0)
        *end = HTS_POS_MAX; // interpret chr:100- as chr:100-<end>

    if (*beg >= *end) return NULL;

    return s_end;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %rbx
movq %rsi, %xmm0
movq %rdi, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movq %rcx, %xmm0
movq %rdx, %xmm2
punpcklqdq %xmm0, %xmm2    # xmm2 = xmm2[0],xmm0[0]
testq %r8, %r8
sete %al
pxor %xmm0, %xmm0
pcmpeqd %xmm0, %xmm2
pcmpeqd %xmm0, %xmm1
movdqa %xmm1, %xmm0
shufps $0xdd, %xmm2, %xmm0     # xmm0 = xmm0[1,3],xmm2[1,3]
shufps $0x88, %xmm2, %xmm1     # xmm1 = xmm1[0,2],xmm2[0,2]
andps %xmm0, %xmm1
movmskps %xmm1, %ecx
testl %ecx, %ecx
setne %cl
orb %al, %cl
jne 0x16281
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r13
movq %r9, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movl 0x90(%rsp), %ebp
callq 0x41d0
movq %rax, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
addq %r13, %rax
movq %rax, 0x18(%rsp)
cmpb $0x7b, (%r13)
movq %rbx, 0x8(%rsp)
jne 0x160fc
pushq $0x7d
popq %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x4550
testq %rax, %rax
je 0x16295
movq %rax, %rbx
movq %r14, 0x10(%rsp)
incq %r13
decq %r15
movb 0x1(%rax), %r14b
movl 0x90(%rsp), %eax
testb $0x4, %al
je 0x15ff5
pushq $0x2c
popq %rsi
movq %rbx, %rdi
callq 0x4190
testq %rax, %rax
je 0x15ff5
movq %rax, %r15
subq %r13, %r15
incq %rax
movq %rax, 0x18(%rsp)
pushq $-0x1
popq %rsi
cmpb $0x3a, %r14b
jne 0x16218
incq %rbx
leaq 0x30(%rsp), %r14
andq $0x0, (%r14)
subq %r13, %rsi
addq %rbx, %rsi
movq %r13, %rdi
movq %r14, %rdx
callq 0x163d5
movq 0x10(%r14), %r15
testq %r15, %r15
je 0x16279
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq *0x28(%rsp)
movl %eax, (%r12)
movq %r15, %rdi
callq 0x45c0
cmpl $0x0, (%r12)
js 0x16281
movl 0x90(%rsp), %r13d
movl %r13d, %eax
andl $-0x2, %eax
movl %r13d, %ebp
shrl $0x2, %ebp
andl $0x1, %ebp
orl %eax, %ebp
xorl $0x1, %ebp
incq %rbx
leaq 0x50(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x15c0a
movq %rax, %r15
decq %rax
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movq (%r14), %r12
movzbl (%r12), %r14d
testq %r15, %r15
jle 0x162c5
testl %r14d, %r14d
je 0x160b8
testb $0x4, %r13b
je 0x1632b
cmpb $0x2c, %r14b
jne 0x1632b
testb $0x2, %r13b
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
cmoveq %rax, %r15
movq 0x8(%rsp), %rax
movq %r15, (%rax)
movq 0x18(%rsp), %rbp
movq 0x10(%rsp), %rdx
testq %r15, %r15
jne 0x160ee
movabsq $0x7fffffff7fffffff, %r15 # imm = 0x7FFFFFFF7FFFFFFF
movq %r15, (%rax)
xorl %eax, %eax
cmpq %r15, (%rdx)
cmovgeq %rax, %rbp
jmp 0x16283
testb $0x4, %bpl
movq %r14, 0x10(%rsp)
je 0x16125
pushq $0x2c
popq %rsi
movq %r13, %rdi
callq 0x4190
testq %rax, %rax
je 0x16125
movq %rax, %r15
subq %r13, %r15
incq %rax
movq %rax, 0x18(%rsp)
leaq (%r15,%r13), %rax
xorl %esi, %esi
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x16218
leaq -0x1(%rax), %rbx
incq %r14
cmpb $0x3a, -0x1(%rax)
movq %rbx, %rax
jne 0x1612e
movq 0x10(%rsp), %rax
andq $0x0, (%rax)
movq 0x8(%rsp), %rax
movabsq $0x7fffffff7fffffff, %rcx # imm = 0x7FFFFFFF7FFFFFFF
movq %rcx, (%rax)
leaq 0x30(%rsp), %rbp
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x163d5
movq 0x10(%rbp), %rsi
testq %rsi, %rsi
je 0x16279
movq 0x20(%rsp), %rdi
callq *0x28(%rsp)
movl %eax, (%r12)
testl %eax, %eax
js 0x162b9
leaq 0x30(%rsp), %rbp
andq $0x0, (%rbp)
movq %r15, %rsi
subq %r14, %rsi
movq %r13, %rdi
movq %rbp, %rdx
callq 0x163d5
movq 0x10(%rbp), %rbp
testq %rbp, %rbp
je 0x16279
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq *0x28(%rsp)
movl %eax, 0x8(%rsp)
movq %rbp, %rdi
callq 0x45c0
cmpl $0x0, 0x8(%rsp)
movq 0x18(%rsp), %rbp
js 0x16283
orl $-0x1, (%r12)
subl %r14d, %r15d
movq %rbx, (%rsp)
leaq 0x624f0(%rip), %rsi     # 0x786ea
leaq 0x62515(%rip), %rdx     # 0x78716
xorl %ebp, %ebp
pushq $0x1
popq %rdi
movq %r13, %rcx
movl %r15d, %r8d
movq %r13, %r9
xorl %eax, %eax
callq 0x110c6
jmp 0x16283
movq 0x10(%rsp), %rax
addq %r15, %rsi
andq $0x0, (%rax)
movq 0x8(%rsp), %rax
movabsq $0x7fffffff7fffffff, %rcx # imm = 0x7FFFFFFF7FFFFFFF
movq %rcx, (%rax)
leaq 0x30(%rsp), %rbx
movq %r13, %rdi
movq %rbx, %rdx
callq 0x163d5
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
je 0x16279
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq *0x28(%rsp)
movl %eax, (%r12)
movq %rbx, %rdi
callq 0x45c0
xorl %eax, %eax
cmpl $0x0, (%r12)
movq 0x18(%rsp), %rbp
cmovsq %rax, %rbp
jmp 0x16283
movl $0xfffffffe, (%r12)     # imm = 0xFFFFFFFE
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6244e(%rip), %rsi     # 0x786ea
leaq 0x62458(%rip), %rdx     # 0x786fb
xorl %ebp, %ebp
pushq $0x1
popq %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0x110c6
orl $-0x1, (%r12)
jmp 0x16283
xorl %esi, %esi
cmpl $-0x1, %eax
jne 0x16281
jmp 0x16005
je 0x162ee
cmpb $0x2d, %r14b
jne 0x162ee
cmpb $0x0, (%rbx)
je 0x162ee
leaq 0x62411(%rip), %rsi     # 0x786ea
leaq 0x62467(%rip), %rdx     # 0x78747
xorl %ebp, %ebp
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
jmp 0x16283
movsbl %r14b, %edi
callq 0x15ee4
testl %eax, %eax
je 0x16388
negq %r15
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
cmovbq %r15, %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x10(%rsp), %rax
andq $0x0, (%rax)
movq 0x18(%rsp), %rbp
jmp 0x16283
cmpl $0x2d, %r14d
jne 0x163a4
incq %r12
leaq 0x50(%rsp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x15c0a
movq %rax, %r15
movq 0x8(%rsp), %rax
movq %r15, (%rax)
movq (%rbx), %rcx
movzbl (%rcx), %eax
testl %eax, %eax
movq 0x10(%rsp), %rdx
je 0x163c6
cmpl $0x2c, %eax
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %rax
je 0x160dc
leaq 0x62370(%rip), %rsi     # 0x786ea
leaq 0x623de(%rip), %rdx     # 0x7875f
xorl %ebp, %ebp
pushq $0x1
popq %rdi
jmp 0x163ba
cmpl $0x2c, %r14d
je 0x162ff
testl %r14d, %r14d
je 0x162ff
testq %r15, %r15
je 0x1609f
leaq 0x6233f(%rip), %rsi     # 0x786ea
leaq 0x623ad(%rip), %rdx     # 0x7875f
xorl %ebp, %ebp
pushq $0x1
popq %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x110c6
jmp 0x16283
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %rax
jmp 0x160dc