// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rcx, %r14
movq %rsi, %rbx
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
testq %rdx, %rdx
sete %al
testq %r14, %r14
sete %sil
orb %al, %sil
orb %cl, %sil
testq %r8, %r8
sete %al
orb %sil, %al
jne 0x1a657
movq %rdi, %rbp
movq %r9, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movq %rdx, 0x38(%rsp)
movl 0x80(%rsp), %r15d
callq 0x41d0
movq %rax, %r13
leaq (%rax,%rbp), %r12
cmpb $0x7b, (%rbp)
movq %r14, 0x18(%rsp)
jne 0x1a252
movq %rbp, %rdi
movl $0x7d, %esi
movq %r13, %rdx
callq 0x4550
movq %rax, %r15
testq %rax, %rax
je 0x1a2a4
incq %rbp
decq %r13
leaq 0x1(%r15), %rax
xorl %ecx, %ecx
cmpb $0x3a, 0x1(%r15)
cmoveq %rax, %rcx
movl $0x1, %r14d
movl 0x80(%rsp), %eax
testb $0x4, %al
je 0x1a2cc
movq %rcx, 0x8(%rsp)
movq %r15, %rdi
movl $0x2c, %esi
callq 0x4190
testq %rax, %rax
je 0x1a24b
movq %rax, %r13
subq %rbp, %r13
incq %rax
movq %rax, %r12
movq 0x8(%rsp), %rcx
jmp 0x1a2cc
testb $0x4, %r15b
je 0x1a276
movq %rbp, %rdi
movl $0x2c, %esi
callq 0x4190
testq %rax, %rax
je 0x1a276
movq %rax, %r13
subq %rbp, %r13
incq %rax
movq %rax, %r12
xorl %r14d, %r14d
testq %r13, %r13
je 0x1a2d6
movl $0x1, %eax
subq %r13, %rax
leaq -0x1(%r13), %rcx
cmpb $0x3a, (%rbp,%rcx)
je 0x1a2da
incq %rax
decq %rcx
cmpq $0x1, %rax
jne 0x1a28a
xorl %ecx, %ecx
xorl %r14d, %r14d
jmp 0x1a2e0
leaq 0x7f3ae(%rip), %rsi     # 0x99659
leaq 0x7f3b8(%rip), %rdx     # 0x9966a
movl $0x1, %edi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x14378
xorl %ecx, %ecx
movl $0xffffffff, (%rbx)     # imm = 0xFFFFFFFF
xorl %r14d, %r14d
testq %r15, %r15
jne 0x1a2e0
jmp 0x1a657
xorl %ecx, %ecx
jmp 0x1a2e0
movq %rbp, %rcx
subq %rax, %rcx
testq %rcx, %rcx
je 0x1a353
movq %r12, 0x40(%rsp)
xorl %r12d, %r12d
movl $0x0, %r15d
testl %r14d, %r14d
je 0x1a3ce
movl %r14d, %eax
addq %rbp, %rax
movq %rcx, %r13
subq %rax, %r13
movq %r13, %rax
movq %rcx, %r14
addq $0x2, %rax
je 0x1a44c
cmpq %rax, %r12
jae 0x1a4a5
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 0x1a4a8
jmp 0x1a4be
movq %rbx, 0x10(%rsp)
movq 0x38(%rsp), %rax
movq $0x0, (%rax)
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movl %r14d, %eax
subq %rax, %r13
movq %r13, %rax
addq $0x2, %rax
je 0x1a459
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdi
shrq %rdi
xorl %ebx, %ebx
testq %rcx, %rcx
cmovneq %rbx, %rdi
addq %rax, %rdi
callq 0x4680
testq %rax, %rax
je 0x1a466
movq %rax, %r15
movq %rax, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x43c0
movb $0x0, (%r15,%r13)
movq %r15, %rbx
jmp 0x1a466
movq %rcx, 0x8(%rsp)
movq 0x38(%rsp), %rax
movq $0x0, (%rax)
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq %r13, %rax
addq $0x2, %rax
je 0x1a590
movq %rbx, 0x10(%rsp)
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %r12
shrq %r12
xorl %r15d, %r15d
testq %rcx, %rcx
cmovneq %r15, %r12
addq %rax, %r12
movq %r12, %rdi
callq 0x4680
testq %rax, %rax
je 0x1a5df
movq %rax, %rbx
movq %rax, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x43c0
movb $0x0, (%rbx,%r13)
movq %rbx, %r15
jmp 0x1a5e2
callq 0x4050
movl $0x4b, (%rax)
jmp 0x1a4be
callq 0x4050
movl $0x4b, (%rax)
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x1a495
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq *0x28(%rsp)
movl %eax, %ebp
movq 0x10(%rsp), %rax
movl %ebp, (%rax)
movq %rbx, %rdi
callq 0x45c0
xorl %eax, %eax
testl %ebp, %ebp
cmovsq %rax, %r12
jmp 0x1a65a
movq 0x10(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x1a657
movq %r15, %r12
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x43c0
movb $0x0, (%r12,%r13)
movq %r12, %r15
testq %r15, %r15
je 0x1a651
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq *0x28(%rsp)
movl %eax, (%rbx)
movq %r15, %rdi
callq 0x45c0
cmpl $0x0, (%rbx)
js 0x1a657
movl 0x80(%rsp), %r15d
movl %r15d, %ebp
shrl $0x2, %ebp
movl %r15d, %eax
andl $-0x2, %eax
andl $0x1, %ebp
orl %eax, %ebp
xorl $0x1, %ebp
movq %r14, %rbx
incq %rbx
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x19e86
movq %rax, %r14
decq %rax
movq 0x38(%rsp), %r13
movq %rax, (%r13)
testq %r14, %r14
movq 0x40(%rsp), %r12
jle 0x1a5a3
movq 0x30(%rsp), %rcx
movb (%rcx), %al
testb %al, %al
je 0x1a551
testb $0x4, %r15b
sete %dl
cmpb $0x2c, %al
setne %sil
orb %dl, %sil
jne 0x1a6d3
testb $0x2, %r15b
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
cmoveq %rax, %r14
movq 0x18(%rsp), %rdx
movq %r14, (%rdx)
cmpq $0x0, (%rdx)
jne 0x1a57e
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
movq %rax, (%rdx)
movq (%r13), %rax
xorl %ecx, %ecx
cmpq (%rdx), %rax
cmovgeq %rcx, %r12
jmp 0x1a65a
callq 0x4050
movl $0x4b, (%rax)
xorl %r12d, %r12d
xorl %r15d, %r15d
jmp 0x1a5e7
je 0x1a67b
movq 0x30(%rsp), %rax
cmpb $0x2d, (%rax)
jne 0x1a67b
cmpb $0x0, (%rbx)
je 0x1a67b
leaq 0x7f092(%rip), %rsi     # 0x99659
leaq 0x7f0e8(%rip), %rdx     # 0x996b6
xorl %r12d, %r12d
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x1a65a
xorl %r12d, %r12d
movq 0x10(%rsp), %rbx
testq %r15, %r15
je 0x1a651
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq *0x28(%rsp)
movl %eax, (%rbx)
testl %eax, %eax
js 0x1a66c
movq 0x8(%rsp), %r13
subq %rbp, %r13
movq %r13, %rax
movq %rbx, %r14
addq $0x2, %rax
je 0x1a6c6
cmpq %rax, %r12
jae 0x1a72d
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 0x1a730
jmp 0x1a746
movl $0xfffffffe, (%rbx)     # imm = 0xFFFFFFFE
xorl %r12d, %r12d
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $-0x1, %eax
movq 0x8(%rsp), %rcx
jne 0x1a657
jmp 0x1a2fc
movq 0x30(%rsp), %rbx
movzbl (%rbx), %r15d
callq 0x4510
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r15,2)
jne 0x1a6a3
cmpl $0x2c, %r15d
je 0x1a6a3
testl %r15d, %r15d
jne 0x1a7a4
negq %r14
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
cmovbq %r14, %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq $0x0, (%r13)
jmp 0x1a65a
callq 0x4050
movl $0x4b, (%rax)
jmp 0x1a746
cmpb $0x2d, %al
jne 0x1a70b
incq %rcx
leaq 0x30(%rsp), %rbx
movq %rcx, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x19e86
movq 0x18(%rsp), %rdx
movq %rax, (%rdx)
movq (%rbx), %rcx
movzbl (%rcx), %eax
testl %eax, %eax
je 0x1a56b
cmpl $0x2c, %eax
je 0x1a56b
leaq 0x7ef47(%rip), %rsi     # 0x99659
leaq 0x7efb5(%rip), %rdx     # 0x996ce
xorl %r12d, %r12d
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x1a65a
movq %r15, %r12
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x43c0
movb $0x0, (%r12,%r13)
movq %r12, %r15
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq *0x28(%rsp)
movl %eax, %ebx
movq %r15, %rdi
callq 0x45c0
testl %ebx, %ebx
movq 0x40(%rsp), %r12
js 0x1a65a
movl $0xffffffff, (%r14)     # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rax
movq %rax, (%rsp)
leaq 0x7eed9(%rip), %rsi     # 0x99659
leaq 0x7eefe(%rip), %rdx     # 0x99685
xorl %r12d, %r12d
movl $0x1, %edi
movq %rbp, %rcx
movl %r13d, %r8d
movq %rbp, %r9
xorl %eax, %eax
callq 0x14378
jmp 0x1a65a
testq %r14, %r14
movl 0x80(%rsp), %r15d
jns 0x1a530
leaq 0x7ee9d(%rip), %rsi     # 0x99659
leaq 0x7ef0b(%rip), %rdx     # 0x996ce
xorl %r12d, %r12d
movl $0x1, %edi
movq %rbx, %rcx
jmp 0x1a721