// File: /workspace/github_c_makefile_star2_O3/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, 0x38(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
movq 0x18(%rdi), %rax
movq %rax, 0x8(%rsp)
movq %rsi, %rdi
callq 0x62d0
movq %rax, %r13
incl %eax
movslq %eax, %rdi
callq 0x69d0
movq %rax, %rbp
testl %r13d, %r13d
jle 0x6a1a3
callq 0x6790
movq (%rax), %rax
movl %r13d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %r8d, %r8d
movsbq (%r15,%rdx), %rsi
testb $0x20, 0x1(%rax,%rsi,2)
jne 0x6a199
movslq %r8d, %rdi
incl %r8d
movb %sil, (%rbp,%rdi)
incq %rdx
cmpq %rdx, %rcx
jne 0x6a182
jmp 0x6a1a6
xorl %r8d, %r8d
movslq %r8d, %r12
movb $0x0, (%rbp,%r12)
movl %r12d, %eax
decq %rax
movl %r8d, %esi
testl %esi, %esi
jle 0x6a1cc
movl %eax, %ecx
decl %esi
decq %rax
cmpb $0x3a, (%rbp,%rcx)
jne 0x6a1b8
jmp 0x6a1cf
movl %r13d, %esi
movq %r15, 0x28(%rsp)
cmpl %r8d, %esi
movq %rbp, 0x10(%rsp)
movq %rsi, 0x30(%rsp)
movq %rbx, 0x50(%rsp)
movq %r8, 0x40(%rsp)
jge 0x6a2d7
leal 0x1(%rsi), %eax
cmpl %r8d, %eax
jge 0x6a273
movslq %esi, %r15
addq $0x2, %r15
leal -0x1(%r8), %r13d
xorl %r14d, %r14d
movb $0x1, %bl
movsbq -0x1(%rbp,%r15), %rbp
cmpq $0x2d, %rbp
jne 0x6a21f
incl %r14d
movq 0x10(%rsp), %rbp
jmp 0x6a24e
callq 0x6790
cmpb $0x2c, %bpl
je 0x6a244
movq (%rax), %rax
movzwl (%rax,%rbp,2), %eax
andl $0x800, %eax            # imm = 0x800
movq 0x10(%rsp), %rbp
movq 0x30(%rsp), %rsi
jne 0x6a24e
jmp 0x6a25f
movq 0x10(%rsp), %rbp
movq 0x30(%rsp), %rsi
cmpq %r12, %r15
setl %bl
incq %r15
decl %r13d
cmpl %r13d, %esi
jne 0x6a209
cmpl $0x2, %r14d
movq 0x40(%rsp), %r8
cmovgel %r8d, %esi
testb $0x1, %bl
cmovnel %r8d, %esi
movq %rsi, 0x30(%rsp)
movslq %esi, %rax
movq %rax, 0x48(%rsp)
movb $0x0, (%rbp,%rax)
movq 0x8(%rsp), %rax
movl (%rax), %eax
testl %eax, %eax
movq %rax, 0x18(%rsp)
je 0x6a327
leal -0x1(%rax), %esi
movsbl (%rbp), %r12d
testl %r12d, %r12d
je 0x6a3b8
movb 0x1(%rbp), %al
testb %al, %al
je 0x6a3bb
leaq 0x2(%rbp), %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 0x6a2b9
jmp 0x6a3bb
movq 0x8(%rsp), %rax
movl (%rax), %eax
movq %rax, 0x18(%rsp)
testl %eax, %eax
je 0x6a70e
movq 0x18(%rsp), %rax
leal -0x1(%rax), %esi
movq 0x28(%rsp), %rcx
movsbl (%rcx), %r15d
testl %r15d, %r15d
je 0x6a32f
movb 0x1(%rcx), %al
testb %al, %al
je 0x6a332
addq $0x2, %rcx
movl %r15d, %edx
shll $0x5, %edx
subl %r15d, %edx
movsbl %al, %r15d
addl %edx, %r15d
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x6a30c
jmp 0x6a332
xorl %r13d, %r13d
jmp 0x6a43a
xorl %r15d, %r15d
andl %esi, %r15d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r14
movl $0x1, %r12d
movl %r15d, %r13d
movl %esi, 0x20(%rsp)
movl %r13d, %eax
shrl $0x4, %eax
movl (%r14,%rax,4), %ebx
leal (,%r13,2), %ebp
andl $0x1e, %ebp
movl %ebx, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x6a3a3
testb $0x1, %al
jne 0x6a390
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x28(%rsp), %rsi
callq 0x6a50
movl 0x20(%rsp), %esi
testl %eax, %eax
je 0x6a3a3
addl %r12d, %r13d
andl %esi, %r13d
incl %r12d
cmpl %r15d, %r13d
jne 0x6a34b
jmp 0x6a70e
movl %ebp, %ecx
shrl %cl, %ebx
testb $0x3, %bl
movq 0x18(%rsp), %rax
cmovnel %eax, %r13d
jmp 0x6a54c
xorl %r12d, %r12d
andl %esi, %r12d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r14
movl $0x1, %ebx
movl %r12d, %r13d
movl %esi, 0x20(%rsp)
movl %r13d, %eax
shrl $0x4, %eax
movl (%r14,%rax,4), %r15d
leal (,%r13,2), %ebp
andl $0x1e, %ebp
movl %r15d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x6a428
testb $0x1, %al
jne 0x6a419
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x6a50
movl 0x20(%rsp), %esi
testl %eax, %eax
je 0x6a428
addl %ebx, %r13d
andl %esi, %r13d
incl %ebx
cmpl %r12d, %r13d
jne 0x6a3d3
jmp 0x6a443
movl %ebp, %ecx
shrl %cl, %r15d
testb $0x3, %r15b
movq 0x18(%rsp), %rax
cmovnel %eax, %r13d
cmpl %eax, %r13d
jne 0x6a547
movq 0x18(%rsp), %rax
testl %eax, %eax
je 0x6a483
leal -0x1(%rax), %esi
movq 0x28(%rsp), %rcx
movsbl (%rcx), %r12d
testl %r12d, %r12d
je 0x6a48b
movb 0x1(%rcx), %al
testb %al, %al
je 0x6a48e
addq $0x2, %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 0x6a468
jmp 0x6a48e
xorl %r13d, %r13d
jmp 0x6a50d
xorl %r12d, %r12d
andl %esi, %r12d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r15
movl $0x1, %ebx
movl %r12d, %r13d
movl %esi, 0x20(%rsp)
movl %r13d, %eax
shrl $0x4, %eax
movl (%r15,%rax,4), %r14d
leal (,%r13,2), %ebp
andl $0x1e, %ebp
movl %r14d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x6a4fb
testb $0x1, %al
jne 0x6a4ec
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x28(%rsp), %rsi
callq 0x6a50
movl 0x20(%rsp), %esi
testl %eax, %eax
je 0x6a4fb
addl %ebx, %r13d
andl %esi, %r13d
incl %ebx
cmpl %r12d, %r13d
jne 0x6a4a6
jmp 0x6a512
movl %ebp, %ecx
shrl %cl, %r14d
testb $0x3, %r14b
movq 0x18(%rsp), %rax
cmovnel %eax, %r13d
cmpl %eax, %r13d
jne 0x6a52f
movq 0x38(%rsp), %rax
movl $0x0, (%rax)
movq 0x10(%rsp), %rdi
callq 0x68a0
xorl %r13d, %r13d
jmp 0x6a6fc
movq 0x10(%rsp), %rax
movq 0x48(%rsp), %rcx
movb $0x3a, (%rax,%rcx)
movq 0x40(%rsp), %r14
movq %r14, 0x30(%rsp)
movq 0x18(%rsp), %rax
cmpl %eax, %r13d
je 0x6a70e
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movl %r13d, %ecx
leaq (%rcx,%rcx,2), %rcx
movslq (%rax,%rcx,8), %rsi
movl 0x4(%rax,%rcx,8), %edi
movq 0x8(%rax,%rcx,8), %r12
movq 0x10(%rax,%rcx,8), %r15
movq 0x40(%rsp), %rdx
movl %edx, %eax
movq 0x30(%rsp), %rcx
subl %ecx, %eax
movq %rsi, 0x8(%rsp)
movl %edi, 0x28(%rsp)
jle 0x6a608
movq %r15, 0x20(%rsp)
leal 0x1(%rcx), %r15d
movslq %ecx, %rcx
movl %r15d, %r14d
cmpl %edx, %r15d
movq 0x10(%rsp), %r13
jge 0x6a5d1
leaq (%rcx,%r13), %rdx
movl $0x1, %esi
movl %r15d, %r14d
movb (%rdx,%rsi), %dil
cmpb $0x2c, %dil
je 0x6a5ca
movslq %r14d, %r8
incl %r14d
movb %dil, (%r13,%r8)
incq %rsi
cmpl %esi, %eax
jne 0x6a5b5
movslq %r14d, %rbx
movb $0x0, (%r13,%rbx)
leaq (%rcx,%r13), %rdi
incq %rdi
callq 0x6bd0
movl %eax, %ebp
cmpl %ebx, %r15d
jne 0x6a60f
cmpl %r14d, %r15d
jge 0x6a625
movslq %r15d, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %rdi
incq %rdi
callq 0x6bd0
jmp 0x6a628
xorl %ebp, %ebp
movl %r12d, %eax
jmp 0x6a636
movslq %r15d, %rax
addq %r13, %rax
cmpb $0x2d, (%rax)
je 0x6a5ed
incl %r15d
incq %rax
cmpl %r15d, %r14d
jne 0x6a615
movl %r12d, %eax
movq 0x20(%rsp), %r15
xorl %ecx, %ecx
testl %ebp, %ebp
setg %cl
subl %ecx, %ebp
movslq %ebp, %r13
cmpq %r13, %r12
cmovlel %r12d, %r13d
movslq %eax, %rbx
cmpq %rbx, %r12
cmovlel %r12d, %ebx
cmpl %ebx, %r13d
cmovgel %ebx, %r13d
movq 0x10(%rsp), %rdi
callq 0x68a0
movq 0x50(%rsp), %r12
movq (%r12), %rdi
movl %r13d, %eax
cltd
idivl 0x28(%rsp)
cltq
imulq 0x8(%rsp), %rax
movslq %edx, %rcx
addq %rcx, %r15
addq %rax, %r15
xorl %r14d, %r14d
movq %r15, %rsi
xorl %edx, %edx
callq 0x68ea3
testl %eax, %eax
js 0x6a745
subl %r13d, %ebx
leal 0x2(%rbx), %eax
movslq %eax, %rdi
callq 0x69d0
movq %rax, %r13
movq (%r12), %rdi
callq 0x68744
testl %eax, %eax
js 0x6a6ec
testl %ebx, %ebx
jle 0x6a6ec
movl %eax, %ebp
xorl %r14d, %r14d
callq 0x6790
movq %rax, %r15
movq (%r15), %rax
movl %ebp, %ecx
cmpw $0x0, (%rax,%rcx,2)
jns 0x6a6d8
movslq %r14d, %rax
incl %r14d
movb %bpl, (%r13,%rax)
movq (%r12), %rdi
callq 0x68744
testl %eax, %eax
js 0x6a6ec
movl %eax, %ebp
cmpl %ebx, %r14d
jl 0x6a6c1
movslq %r14d, %rax
movb $0x0, (%r13,%rax)
movq 0x38(%rsp), %rcx
movl %eax, (%rcx)
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x548c3(%rip), %rax     # 0xbefd8
movq (%rax), %rdi
leaq 0x2f9ab(%rip), %rsi     # 0x9a0ca
xorl %r13d, %r13d
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x6a90
movq 0x10(%rsp), %rdi
callq 0x68a0
movq 0x38(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x6a6fc
movq 0x38(%rsp), %rdi
callq 0x7cb3
jmp 0x6a527