// File: /workspace/github_c_makefile_star2_O3/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, %rbp
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 0x1a0b8
movq %rdx, %rbx
movq %rsi, %r14
movq %r9, 0x38(%rsp)
movq %r8, 0x40(%rsp)
movq %rdi, %r13
movl 0x90(%rsp), %r15d
callq 0x41d0
movq %rax, %r12
leaq (%rax,%r13), %rcx
cmpb $0x7b, (%r13)
jne 0x19d69
movq %rcx, %r15
movq %rbx, 0x10(%rsp)
movq %r13, %rdi
movl $0x7d, %esi
movq %r12, %rdx
callq 0x4550
testq %rax, %rax
je 0x19de1
movq %rax, %rbx
movq %r14, 0x18(%rsp)
incq %r13
decq %r12
movb 0x1(%rax), %r14b
movl 0x90(%rsp), %eax
testb $0x4, %al
movq %rbp, 0x28(%rsp)
je 0x19cf8
movq %rbx, %rdi
movl $0x2c, %esi
callq 0x4190
testq %rax, %rax
je 0x19cf8
movq %rax, %r12
subq %r13, %r12
incq %rax
movq %rax, %r15
movq %r15, 0x20(%rsp)
movq $-0x1, %rbp
cmpb $0x3a, %r14b
jne 0x19dd5
incq %rbx
xorl %ecx, %ecx
xorl %r15d, %r15d
movq 0x18(%rsp), %r14
subq %r13, %rbp
addq %rbx, %rbp
movq %rbp, %rax
addq $0x2, %rax
je 0x19f64
cmpq %rax, %rcx
jae 0x19f9c
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdx
shrq %rdx
xorl %esi, %esi
testq %rcx, %rcx
cmoveq %rdx, %rsi
addq %rax, %rsi
movq %r15, %rdi
callq 0x4840
movq %rax, %r12
testq %rax, %rax
jne 0x19f9f
jmp 0x19f6f
testb $0x4, %r15b
je 0x19d93
movq %r13, %rdi
movl $0x2c, %esi
movq %rcx, %r15
callq 0x4190
movq %r15, %rcx
testq %rax, %rax
je 0x19d93
movq %rax, %r12
subq %r13, %r12
incq %rax
movq %rax, %rcx
movq %rbp, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
testq %r12, %r12
je 0x19e0c
movq %rbx, 0x10(%rsp)
movq %r14, 0x18(%rsp)
leaq -0x1(%r13), %rbx
movq $-0x1, %r15
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpb $0x3a, (%rbx,%r12)
je 0x19eb0
incq %r14
decq %rbx
decq %r15
cmpq %r14, %r12
jne 0x19dbc
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rbx
jmp 0x19e11
leaq 0x7c911(%rip), %rsi     # 0x966f9
leaq 0x7c91b(%rip), %rdx     # 0x9670a
xorl %ebp, %ebp
movl $0x1, %edi
movq %r13, %rcx
xorl %eax, %eax
callq 0x13de5
movl $0xffffffff, (%r14)     # imm = 0xFFFFFFFF
jmp 0x1a0ba
xorl %ebp, %ebp
xorl %r12d, %r12d
movq $0x0, (%rbx)
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
addq %r12, %rbp
movq %rbp, %rax
addq $0x2, %rax
je 0x19e99
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdx
shrq %rdx
xorl %edi, %edi
testq %rcx, %rcx
cmoveq %rdx, %rdi
addq %rax, %rdi
callq 0x4680
testq %rax, %rax
je 0x19ea4
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x43c0
movb $0x0, (%rbx,%rbp)
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq *0x40(%rsp)
movl %eax, %ebp
movl %eax, (%r14)
movq %rbx, %rdi
callq 0x45c0
xorl %eax, %eax
testl %ebp, %ebp
movq 0x20(%rsp), %rbp
cmovsq %rax, %rbp
jmp 0x1a0ba
callq 0x4050
movl $0x4b, (%rax)
movl $0xfffffffe, (%r14)     # imm = 0xFFFFFFFE
jmp 0x1a0b8
movq 0x10(%rsp), %rax
movq $0x0, (%rax)
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
movq %r12, %rax
addq $0x2, %rax
je 0x19f81
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdx
shrq %rdx
xorl %edi, %edi
testq %rcx, %rcx
cmoveq %rdx, %rdi
addq %rax, %rdi
movq %rdi, 0x48(%rsp)
callq 0x4680
testq %rax, %rax
je 0x19f8c
movq %rax, %rbp
addq %r12, %rbx
movq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x43c0
movb $0x0, (%rbp,%r12)
movq 0x38(%rsp), %rdi
movq %rbp, 0x30(%rsp)
movq %rbp, %rsi
callq *0x40(%rsp)
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
testl %eax, %eax
js 0x1a0ad
leaq 0x1(%r12), %rax
cmpq %r14, %rax
jne 0x1a117
callq 0x4050
movl $0x4b, (%rax)
movq 0x30(%rsp), %r14
jmp 0x1a1ec
callq 0x4050
movl $0x4b, (%rax)
testq %r15, %r15
movl 0x90(%rsp), %r13d
jne 0x19fbd
jmp 0x19ea4
callq 0x4050
movl $0x4b, (%rax)
movq 0x18(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x1a0b8
movq %r15, %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x43c0
movb $0x0, (%r12,%rbp)
movq %r12, %r15
movl 0x90(%rsp), %r13d
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq *0x40(%rsp)
movl %eax, (%r14)
movq %r15, %rdi
callq 0x45c0
cmpl $0x0, (%r14)
js 0x1a0b8
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), %r12
movq %rbx, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x19927
movq %rax, %r14
decq %rax
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movq (%r12), %r12
movzbl (%r12), %r15d
testq %r14, %r14
jle 0x1a082
testq %r15, %r15
je 0x1a03c
testb $0x4, %r13b
je 0x1a165
cmpb $0x2c, %r15b
jne 0x1a165
testb $0x2, %r13b
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
cmoveq %rax, %r14
movq 0x28(%rsp), %rax
movq %r14, (%rax)
movq 0x20(%rsp), %rbp
movq 0x10(%rsp), %rdx
testq %r14, %r14
jne 0x1a077
movabsq $0x7fffffff7fffffff, %r14 # imm = 0x7FFFFFFF7FFFFFFF
movq 0x28(%rsp), %rax
movq %r14, (%rax)
xorl %eax, %eax
cmpq %r14, (%rdx)
cmovgeq %rax, %rbp
jmp 0x1a0ba
je 0x1a0cc
cmpb $0x2d, %r15b
jne 0x1a0cc
cmpb $0x0, (%rbx)
je 0x1a0cc
leaq 0x7c663(%rip), %rsi     # 0x966f9
leaq 0x7c6b9(%rip), %rdx     # 0x96756
xorl %ebp, %ebp
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x1a0ba
xorl %ebp, %ebp
cmpl $-0x1, %eax
je 0x1a24b
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4510
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r15,2)
jne 0x1a0eb
cmpq $0x2c, %r15
je 0x1a0eb
testl %r15d, %r15d
jne 0x1a25f
negq %r14
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
cmovbq %r14, %rax
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
movq 0x10(%rsp), %rax
movq $0x0, (%rax)
movq 0x20(%rsp), %rbp
jmp 0x1a0ba
movq %r12, %rbp
subq %r14, %rbp
leaq 0x1(%rbp), %rax
cmpq %rax, 0x48(%rsp)
jae 0x1a1ca
movabsq $0x3fffffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFFFF
movq %rax, %rdx
shrq %rdx
xorl %esi, %esi
cmpq %rcx, %rax
cmovbeq %rdx, %rsi
addq %r12, %rsi
subq %r14, %rsi
incq %rsi
movq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x4840
testq %rax, %rax
jne 0x1a1cf
jmp 0x1a1ec
cmpl $0x2d, %r15d
jne 0x1a268
incq %r12
leaq 0x50(%rsp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x19927
movq %rax, %r14
movq 0x28(%rsp), %rax
movq %r14, (%rax)
movq (%rbx), %rcx
movzbl (%rcx), %eax
testl %eax, %eax
je 0x1a056
cmpl $0x2c, %eax
movq 0x20(%rsp), %rbp
movq 0x10(%rsp), %rdx
je 0x1a060
leaq 0x7c542(%rip), %rsi     # 0x966f9
leaq 0x7c5b0(%rip), %rdx     # 0x9676e
xorl %ebp, %ebp
movl $0x1, %edi
jmp 0x1a280
movq 0x30(%rsp), %rax
decq %rbp
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x43c0
leaq (%r14,%r12), %rax
movb $0x0, (%r15,%rax)
movq 0x38(%rsp), %rdi
movq %r14, %rsi
callq *0x40(%rsp)
movl %eax, %ebp
movq %r14, %rdi
callq 0x45c0
testl %ebp, %ebp
movq 0x20(%rsp), %rbp
movq 0x18(%rsp), %rax
js 0x1a0ba
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
addl %r15d, %r12d
movq %rbx, (%rsp)
leaq 0x7c4d1(%rip), %rsi     # 0x966f9
leaq 0x7c4f6(%rip), %rdx     # 0x96725
xorl %ebp, %ebp
movl $0x1, %edi
movq %r13, %rcx
movl %r12d, %r8d
movq %r13, %r9
xorl %eax, %eax
callq 0x13de5
jmp 0x1a0ba
movq 0x18(%rsp), %r14
movq 0x30(%rsp), %r15
movq 0x48(%rsp), %rcx
jmp 0x19d1b
testq %r14, %r14
je 0x1a023
leaq 0x7c48a(%rip), %rsi     # 0x966f9
leaq 0x7c4f8(%rip), %rdx     # 0x9676e
xorl %ebp, %ebp
movl $0x1, %edi
movq %r12, %rcx
xorl %eax, %eax
callq 0x13de5
jmp 0x1a0ba