// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdx, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x10(%rsp)
movq %rsi, (%rsp)
movq %rsi, %rdi
callq 0x62d0
movq %rax, %rbx
incl %eax
movslq %eax, %rdi
callq 0x69d0
movq %rax, %r15
xorl %r14d, %r14d
testl %ebx, %ebx
movl $0x0, %ebp
movq %rbx, %r12
cmovgl %ebx, %ebp
xorl %r13d, %r13d
movslq %r13d, %rbx
cmpq %r14, %rbp
je 0x5ee15
callq 0x6790
movq (%rax), %rcx
movq (%rsp), %rax
movsbq (%rax,%r14), %rax
testb $0x20, 0x1(%rcx,%rax,2)
jne 0x5ee10
leal 0x1(%rbx), %r13d
movb %al, (%r15,%rbx)
incq %r14
jmp 0x5ede8
movb $0x0, (%r15,%rbx)
movl %ebx, %eax
decq %rax
movl %r13d, %ebp
testl %ebp, %ebp
jle 0x5ee36
movl %eax, %ecx
decl %ebp
decq %rax
cmpb $0x3a, (%r15,%rcx)
jne 0x5ee22
jmp 0x5ee39
movl %r12d, %ebp
cmpl %r13d, %ebp
jge 0x5ee88
movslq %ebp, %r14
movl $0x0, 0x8(%rsp)
incq %r14
movq %r14, %rcx
movq %rcx, %r14
cmpq %rbx, %rcx
jge 0x5eea0
movsbq (%r15,%r14), %r12
cmpq $0x2d, %r12
je 0x5ee82
callq 0x6790
leaq 0x1(%r14), %rcx
cmpb $0x2c, %r12b
je 0x5ee4f
movq (%rax), %rax
movzwl (%rax,%r12,2), %eax
andl $0x800, %eax            # imm = 0x800
jne 0x5ee4f
jmp 0x5eea0
incl 0x8(%rsp)
jmp 0x5ee49
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x5e74a
movl %eax, %edx
movl (%r14), %ecx
jmp 0x5eeff
cmpl $0x2, 0x8(%rsp)
cmovael %r13d, %ebp
cmpq %rbx, %r14
cmovll %r13d, %ebp
movslq %ebp, %r14
movb $0x0, (%r15,%r14)
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x5e74a
movl %eax, %edx
movl (%r12), %ecx
cmpl %ecx, %eax
jne 0x5eeff
movq %r12, %rdi
movq (%rsp), %rsi
movl %edx, %ebp
callq 0x5e74a
cmpl %ebp, %eax
movq 0x18(%rsp), %r12
jne 0x5f0b9
andl $0x0, (%r12)
movq %r15, %rdi
callq 0x68a0
jmp 0x5f11c
movq 0x18(%rsp), %r12
cmpl %ecx, %edx
je 0x5f0ca
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movl %edx, %ecx
imulq $0x18, %rcx, %rcx
movslq (%rax,%rcx), %r14
movl 0x4(%rax,%rcx), %edx
movl %edx, 0x10(%rsp)
movq 0x8(%rax,%rcx), %rdx
movq 0x10(%rax,%rcx), %rax
movq %rax, (%rsp)
cmpl %r13d, %ebp
jge 0x5ef6b
movq %r14, 0x30(%rsp)
pushq $0x1
popq %r12
leal 0x1(%rbp), %r14d
movslq %ebp, %rsi
leaq 0x1(%rsi), %rax
movslq %r14d, %r13
cmpq %rbx, %rax
jge 0x5ef74
movb (%r15,%rax), %cl
cmpb $0x2c, %cl
je 0x5ef66
incl %r14d
movb %cl, (%r15,%r13)
incq %rax
jmp 0x5ef4e
xorl %esi, %esi
movl %edx, %eax
jmp 0x5efff
movb $0x0, (%r15,%r13)
leaq (%r15,%rsi), %rbx
leaq (%r15,%rsi), %rdi
incq %rdi
movq %rdx, 0x8(%rsp)
movq %rsi, 0x28(%rsp)
callq 0x6bd0
movq 0x28(%rsp), %rcx
movq 0x8(%rsp), %rdx
movl %eax, %esi
subq %rcx, %r13
shlq $0x20, %rcx
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %rcx
cmpq %r12, %r13
je 0x5efea
cmpb $0x2d, (%rbx,%r12)
je 0x5efc7
addq %rax, %rcx
incq %r12
jmp 0x5efb3
addl %r12d, %ebp
cmpl %ebp, %r14d
jle 0x5efea
sarq $0x20, %rcx
leaq (%r15,%rcx), %rdi
incq %rdi
movl %esi, %ebx
callq 0x6bd0
movl %ebx, %esi
movq 0x8(%rsp), %rdx
jmp 0x5efec
movl %edx, %eax
xorl %ecx, %ecx
testl %esi, %esi
setg %cl
subl %ecx, %esi
movq 0x18(%rsp), %r12
movq 0x30(%rsp), %r14
movslq %esi, %r13
cmpq %r13, %rdx
cmovlel %edx, %r13d
movslq %eax, %rbx
cmpq %rbx, %rdx
cmovlel %edx, %ebx
cmpl %ebx, %r13d
cmovgel %ebx, %r13d
movq %r15, %rdi
callq 0x68a0
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movl %r13d, %eax
cltd
idivl 0x10(%rsp)
cltq
imulq %r14, %rax
movslq %edx, %rcx
movq (%rsp), %rsi
addq %rcx, %rsi
addq %rax, %rsi
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x5db46
testl %eax, %eax
js 0x5f0fb
subl %r13d, %ebx
leal 0x2(%rbx), %eax
movslq %eax, %rdi
callq 0x69d0
movq %rax, %r13
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x5d480
movl %r14d, %r15d
testl %eax, %eax
js 0x5f09d
cmpl %ebx, %r14d
jge 0x5f09d
movl %eax, %ebp
callq 0x6790
movq (%rax), %rax
movl %ebp, %ecx
cmpw $0x0, (%rax,%rcx,2)
jns 0x5f067
incl %r14d
movb %bpl, (%r13,%r15)
jmp 0x5f067
movb $0x0, (%r13,%r15)
movl %r14d, (%r12)
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %ecx
movb $0x3a, (%r15,%r14)
movl %eax, %edx
movl %r13d, %ebp
jmp 0x5ef04
movq 0x53f07(%rip), %rax     # 0xb2fd8
movq (%rax), %rdi
leaq 0x29d6f(%rip), %rsi     # 0x88e4a
xorl %r13d, %r13d
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x6a90
movq %r15, %rdi
callq 0x68a0
movl $0xfffffffe, (%r12)     # imm = 0xFFFFFFFE
jmp 0x5f0a7
orl $-0x1, (%r12)
movq 0x53ed1(%rip), %rax     # 0xb2fd8
movq (%rax), %rcx
leaq 0x29d8f(%rip), %rdi     # 0x88ea0
pushq $0x56
popq %rsi
pushq $0x1
popq %rdx
callq 0x6e70
xorl %r13d, %r13d
jmp 0x5f0a7