// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/htslib/faidx.c
// Function: fai_fetch
char *fai_fetch(const faidx_t *fai, const char *str, int *len)
{
    char *s;
    int c, i, l, k, name_end;
    khiter_t iter;
    faidx1_t val;
    khash_t(s) *h;
    int beg, end;

    beg = end = -1;
    h = fai->hash;
    name_end = l = strlen(str);
    s = (char*)malloc(l+1);
    // remove space
    for (i = k = 0; i < l; ++i)
        if (!isspace(str[i])) s[k++] = str[i];
    s[k] = 0; l = k;
    // determine the sequence name
    for (i = l - 1; i >= 0; --i) if (s[i] == ':') break; // look for colon from the end
    if (i >= 0) name_end = i;
    if (name_end < l) { // check if this is really the end
        int n_hyphen = 0;
        for (i = name_end + 1; i < l; ++i) {
            if (s[i] == '-') ++n_hyphen;
            else if (!isdigit(s[i]) && s[i] != ',') break;
        }
        if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
        s[name_end] = 0;
        iter = kh_get(s, h, s);
        if (iter == kh_end(h)) { // cannot find the sequence name
            iter = kh_get(s, h, str); // try str as the name
            if (iter == kh_end(h)) {
                *len = 0;
            free(s); return 0;
            } else s[name_end] = ':', name_end = l;
        }
    } else iter = kh_get(s, h, str);
    if(iter == kh_end(h)) {
        fprintf(stderr, "[fai_fetch] Warning - Reference %s not found in FASTA file, returning empty sequence\n", str);
        free(s);
        *len = -2;
        return 0;
    };
    val = kh_value(h, iter);
    // parse the interval
    if (name_end < l) {
        for (i = k = name_end + 1; i < l; ++i)
            if (s[i] != ',') s[k++] = s[i];
        s[k] = 0;
        beg = atoi(s + name_end + 1);
        for (i = name_end + 1; i != k; ++i) if (s[i] == '-') break;
        end = i < k? atoi(s + i + 1) : val.len;
        if (beg > 0) --beg;
    } else beg = 0, end = val.len;
    if (beg >= val.len) beg = val.len;
    if (end >= val.len) end = val.len;
    if (beg > end) beg = end;
    free(s);

    // now retrieve the sequence
    int ret = bgzf_useek(fai->bgzf, val.offset + beg / val.line_blen * val.line_len + beg % val.line_blen, SEEK_SET);
    if ( ret<0 )
    {
        *len = -1;
        fprintf(stderr, "[fai_fetch] Error: fai_fetch failed. (Seeking in a compressed, .gzi unindexed, file?)\n");
        return NULL;
    }
    l = 0;
    s = (char*)malloc(end - beg + 2);
    while ( (c=bgzf_getc(fai->bgzf))>=0 && l < end - beg )
        if (isgraph(c)) s[l++] = c;
    s[l] = '\0';
    *len = l;
    return s;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, 0x48(%rsp)
movq %rsi, %rbx
movq %rdi, %r12
movq 0x18(%rdi), %r13
movq %rsi, %rdi
callq 0x62d0
movq %rax, %rbp
incl %eax
movslq %eax, %rdi
callq 0x69d0
movq %rax, %r15
testl %ebp, %ebp
jle 0x6a0c1
callq 0x6790
movq (%rax), %rax
movl %ebp, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %r8d, %r8d
movsbq (%rbx,%rdx), %rsi
testb $0x20, 0x1(%rax,%rsi,2)
jne 0x6a0b7
movslq %r8d, %rdi
incl %r8d
movb %sil, (%r15,%rdi)
incq %rdx
cmpq %rdx, %rcx
jne 0x6a0a1
jmp 0x6a0c4
xorl %r8d, %r8d
movslq %r8d, %r14
movb $0x0, (%r15,%r14)
movl %r14d, %eax
decq %rax
movl %r8d, 0x34(%rsp)
movl %r8d, %esi
testl %esi, %esi
jle 0x6a0ee
movl %eax, %ecx
decl %esi
decq %rax
cmpb $0x3a, (%r15,%rcx)
jne 0x6a0da
jmp 0x6a0f0
movl %ebp, %esi
movl 0x34(%rsp), %ecx
cmpl %ecx, %esi
movq %r12, 0x28(%rsp)
movq %rsi, 0x38(%rsp)
jge 0x6a200
leal 0x1(%rsi), %eax
cmpl %ecx, %eax
setl %bpl
movq %r15, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
movq %r13, 0x8(%rsp)
jge 0x6a199
movslq %esi, %r12
addq $0x2, %r12
movl %esi, %r15d
notl %r15d
addl %ecx, %r15d
xorl %ebx, %ebx
movq 0x20(%rsp), %rax
movsbq -0x1(%rax,%r12), %r13
cmpq $0x2d, %r13
jne 0x6a147
incl %ebx
jmp 0x6a16d
callq 0x6790
cmpb $0x2c, %r13b
je 0x6a168
movq (%rax), %rax
movzwl (%rax,%r13,2), %eax
andl $0x800, %eax            # imm = 0x800
movq 0x38(%rsp), %rsi
jne 0x6a16d
jmp 0x6a17c
movq 0x38(%rsp), %rsi
cmpq %r14, %r12
setl %bpl
incq %r12
decl %r15d
jne 0x6a132
cmpl $0x2, %ebx
setge %al
orb %al, %bpl
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r13
testb $0x1, %bpl
cmovnel 0x34(%rsp), %esi
movslq %esi, %rax
movq %rax, 0x50(%rsp)
movb $0x0, (%r15,%rax)
movl (%r13), %eax
testl %eax, %eax
movq %rsi, 0x38(%rsp)
movq %rax, 0x40(%rsp)
je 0x6a251
leal -0x1(%rax), %edi
movsbl (%r15), %r12d
testl %r12d, %r12d
je 0x6a261
movb 0x1(%r15), %al
testb %al, %al
je 0x6a264
leaq 0x2(%r15), %rcx
movl %r12d, %edx
shll $0x5, %edx
subl %r12d, %edx
movsbl %al, %r12d
addl %edx, %r12d
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x6a1e5
jmp 0x6a264
movl (%r13), %r14d
testl %r14d, %r14d
je 0x6a259
leal -0x1(%r14), %edi
movsbl (%rbx), %r12d
testl %r12d, %r12d
movq %r15, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
je 0x6a34d
movb 0x1(%rbx), %al
testb %al, %al
je 0x6a350
leaq 0x2(%rbx), %rcx
movl %r12d, %edx
shll $0x5, %edx
subl %r12d, %edx
movsbl %al, %r12d
addl %edx, %r12d
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x6a233
jmp 0x6a350
xorl %r14d, %r14d
jmp 0x6a2f8
xorl %r14d, %r14d
jmp 0x6a4cb
xorl %r12d, %r12d
andl %edi, %r12d
movq 0x10(%r13), %rbx
movl $0x1, %r15d
movl %r12d, %r14d
movl %edi, 0x1c(%rsp)
movl %r14d, %eax
shrl $0x4, %eax
movl (%rbx,%rax,4), %r13d
leal (%r14,%r14), %ebp
andl $0x1e, %ebp
movl %r13d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x6a2d7
testb $0x1, %al
jne 0x6a2bf
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r14d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x20(%rsp), %rsi
callq 0x6a50
movl 0x1c(%rsp), %edi
movq 0x38(%rsp), %rsi
testl %eax, %eax
je 0x6a2d7
addl %r15d, %r14d
andl %edi, %r14d
incl %r15d
cmpl %r12d, %r14d
jne 0x6a278
movq 0x40(%rsp), %rax
movl %eax, %r14d
jmp 0x6a2e9
movl %ebp, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
movq 0x40(%rsp), %rax
cmovnel %eax, %r14d
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movb $0x1, %cl
cmpl %eax, %r14d
jne 0x6a4be
testl %eax, %eax
je 0x6a3ec
leal -0x1(%rax), %edi
movsbl (%rbx), %r12d
testl %r12d, %r12d
je 0x6a3f4
movb 0x1(%rbx), %al
testb %al, %al
movq 0x8(%rsp), %rsi
je 0x6a3fc
leaq 0x2(%rbx), %rcx
movl %r12d, %edx
shll $0x5, %edx
subl %r12d, %edx
movsbl %al, %r12d
addl %edx, %r12d
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x6a32f
jmp 0x6a3fc
xorl %r12d, %r12d
andl %edi, %r12d
movq %r13, 0x8(%rsp)
movq 0x10(%r13), %rdx
movl $0x1, %r15d
movl %r12d, %ebx
movl %edi, 0x1c(%rsp)
movq %rdx, 0x40(%rsp)
movl %ebx, %eax
shrl $0x4, %eax
movl (%rdx,%rax,4), %r13d
leal (%rbx,%rbx), %ebp
andl $0x1e, %ebp
movl %r13d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x6a3c6
testb $0x1, %al
jne 0x6a3b7
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %ebx, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x6a50
movq 0x40(%rsp), %rdx
movl 0x1c(%rsp), %edi
movq 0x38(%rsp), %rsi
testl %eax, %eax
je 0x6a3c6
addl %r15d, %ebx
andl %edi, %ebx
incl %r15d
cmpl %r12d, %ebx
jne 0x6a36e
jmp 0x6a3d3
movl %ebp, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
cmovel %ebx, %r14d
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r13
jmp 0x6a4cb
xorl %r14d, %r14d
jmp 0x6a48d
xorl %r12d, %r12d
movq 0x8(%rsp), %rsi
andl %edi, %r12d
movq 0x10(%rsi), %rbx
movl $0x1, %r15d
movl %r12d, %r14d
movl %edi, 0x1c(%rsp)
movl %r14d, %eax
shrl $0x4, %eax
movl (%rbx,%rax,4), %r13d
leal (%r14,%r14), %ebp
andl $0x1e, %ebp
movl %r13d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x6a46a
testb $0x1, %al
jne 0x6a452
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r14d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x6a50
movl 0x1c(%rsp), %edi
testl %eax, %eax
je 0x6a46a
addl %r15d, %r14d
andl %edi, %r14d
incl %r15d
cmpl %r12d, %r14d
jne 0x6a410
movq 0x40(%rsp), %rax
movl %eax, %r14d
jmp 0x6a47c
movl %ebp, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
movq 0x40(%rsp), %rax
cmovnel %eax, %r14d
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movb $0x1, %cl
cmpl %eax, %r14d
jne 0x6a4ae
movq 0x48(%rsp), %rax
movl $0x0, (%rax)
movq %r15, %rdi
callq 0x68a0
movq 0x38(%rsp), %rsi
xorl %ecx, %ecx
jmp 0x6a4be
movq 0x50(%rsp), %rax
movb $0x3a, (%r15,%rax)
movl 0x34(%rsp), %eax
movl %eax, %esi
movq 0x8(%rsp), %r13
testb %cl, %cl
je 0x6a6a6
cmpl (%r13), %r14d
je 0x6a669
movq 0x20(%r13), %rax
movl %r14d, %ecx
leaq (%rcx,%rcx,2), %rcx
movslq (%rax,%rcx,8), %rdx
movq %rdx, 0x8(%rsp)
movl 0x4(%rax,%rcx,8), %edx
movl %edx, 0x10(%rsp)
movq 0x8(%rax,%rcx,8), %r14
movq 0x10(%rax,%rcx,8), %rax
movq %rax, 0x38(%rsp)
movl 0x34(%rsp), %edx
movl %edx, %eax
subl %esi, %eax
jle 0x6a58e
leal 0x1(%rsi), %r12d
movslq %esi, %rcx
movl %r12d, %r13d
cmpl %edx, %r12d
jge 0x6a544
leaq (%r15,%rcx), %rdx
movl $0x1, %esi
movl %r12d, %r13d
movb (%rdx,%rsi), %dil
cmpb $0x2c, %dil
je 0x6a53d
movslq %r13d, %r8
incl %r13d
movb %dil, (%r15,%r8)
incq %rsi
cmpl %esi, %eax
jne 0x6a529
movslq %r13d, %rbx
movb $0x0, (%r15,%rbx)
leaq (%r15,%rcx), %rdi
incq %rdi
callq 0x6bd0
movl %eax, %ebp
cmpl %ebx, %r12d
je 0x6a578
movslq %r12d, %rax
addq %r15, %rax
cmpb $0x2d, (%rax)
je 0x6a578
incl %r12d
incq %rax
cmpl %r12d, %r13d
jne 0x6a565
movl %r13d, %r12d
cmpl %r13d, %r12d
jge 0x6a595
movslq %r12d, %rax
leaq (%r15,%rax), %rdi
incq %rdi
callq 0x6bd0
jmp 0x6a598
xorl %ebp, %ebp
movl %r14d, %eax
jmp 0x6a5a6
movl %r14d, %eax
movq 0x28(%rsp), %r12
xorl %ecx, %ecx
testl %ebp, %ebp
setg %cl
subl %ecx, %ebp
movslq %ebp, %r13
cmpq %r13, %r14
cmovlel %r14d, %r13d
movslq %eax, %rbx
cmpq %rbx, %r14
cmovlel %r14d, %ebx
cmpl %ebx, %r13d
cmovgel %ebx, %r13d
movq %r15, %rdi
callq 0x68a0
movq (%r12), %rdi
movl %r13d, %eax
cltd
idivl 0x10(%rsp)
cltq
imulq 0x8(%rsp), %rax
movslq %edx, %rcx
movq 0x38(%rsp), %rsi
addq %rcx, %rsi
addq %rax, %rsi
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x68da5
testl %eax, %eax
js 0x6a69c
subl %r13d, %ebx
leal 0x2(%rbx), %eax
movslq %eax, %rdi
callq 0x69d0
movq %rax, %r13
movq (%r12), %rdi
callq 0x6865b
testl %eax, %eax
js 0x6a657
testl %ebx, %ebx
jle 0x6a657
movl %eax, %ebp
xorl %r14d, %r14d
callq 0x6790
movq %rax, %r15
movq (%r15), %rax
movl %ebp, %ecx
cmpw $0x0, (%rax,%rcx,2)
jns 0x6a643
movslq %r14d, %rax
incl %r14d
movb %bpl, (%r13,%rax)
movq (%r12), %rdi
callq 0x6865b
testl %eax, %eax
js 0x6a657
movl %eax, %ebp
cmpl %ebx, %r14d
jl 0x6a62c
movslq %r14d, %rax
movb $0x0, (%r13,%rax)
movq 0x48(%rsp), %rcx
movl %eax, (%rcx)
jmp 0x6a6a9
movq 0x54968(%rip), %rax     # 0xbefd8
movq (%rax), %rdi
leaq 0x2f820(%rip), %rsi     # 0x99e9a
xorl %r13d, %r13d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6a90
movq %r15, %rdi
callq 0x68a0
movq 0x48(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x6a6a9
movq 0x48(%rsp), %rdi
callq 0x7d07
xorl %r13d, %r13d
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq