// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/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_c(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_c(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, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq %rsi, %rdi
callq 0x4210
movq %rax, %rbx
incl %eax
movslq %eax, %rdi
callq 0x46f0
movq %rax, %r15
xorl %r14d, %r14d
testl %ebx, %ebx
movl $0x0, %ebp
movq %rbx, (%rsp)
cmovgl %ebx, %ebp
xorl %r13d, %r13d
movslq %r13d, %rbx
cmpq %r14, %rbp
je 0x396f4
movq 0x10(%rsp), %rax
movzbl (%rax,%r14), %r12d
callq 0x4580
movq (%rax), %rax
testb $0x20, 0x1(%rax,%r12,2)
jne 0x396ef
leal 0x1(%rbx), %r13d
movb %r12b, (%r15,%rbx)
incq %r14
jmp 0x396c5
movb $0x0, (%r15,%rbx)
movl %ebx, %eax
decq %rax
movl %r13d, %ebp
testl %ebp, %ebp
jle 0x39715
movl %eax, %ecx
decl %ebp
decq %rax
cmpb $0x3a, (%r15,%rcx)
jne 0x39701
jmp 0x39719
movq (%rsp), %rbp
cmpl %r13d, %ebp
jge 0x39766
movslq %ebp, %r14
movl $0x0, (%rsp)
incq %r14
movq %r14, %rcx
movq %rcx, %r14
cmpq %rbx, %rcx
jge 0x3977f
movzbl (%r15,%r14), %r12d
cmpq $0x2d, %r12
je 0x39761
callq 0x4580
leaq 0x1(%r14), %rcx
cmpq $0x2c, %r12
je 0x3972e
movq (%rax), %rax
movzwl (%rax,%r12,2), %eax
andl $0x800, %eax            # imm = 0x800
jne 0x3972e
jmp 0x3977f
incl (%rsp)
jmp 0x39728
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x38f95
movl %eax, %edx
movl (%r14), %ecx
jmp 0x397e4
cmpl $0x2, (%rsp)
cmovael %r13d, %ebp
cmpq %rbx, %r14
cmovll %r13d, %ebp
movslq %ebp, %r14
movb $0x0, (%r15,%r14)
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x38f95
movl %eax, %edx
movl (%r12), %ecx
cmpl %ecx, %eax
jne 0x397e4
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movl %edx, %ebp
callq 0x38f95
cmpl %ebp, %eax
jne 0x397d8
movq 0x18(%rsp), %rax
andl $0x0, (%rax)
movq %r15, %rdi
callq 0x4600
jmp 0x399f4
movl %ebp, %ecx
movb $0x3a, (%r15,%r14)
movl %eax, %edx
movl %r13d, %ebp
cmpl %ecx, %edx
je 0x3999b
movq 0x8(%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), %r12
cmpl %r13d, %ebp
jge 0x3984b
movq %r14, 0x30(%rsp)
movq %r12, (%rsp)
pushq $0x1
popq %r12
leal 0x1(%rbp), %r14d
movslq %ebp, %rsi
leaq 0x1(%rsi), %rax
movslq %r14d, %r13
cmpq %rbx, %rax
jge 0x39854
movb (%r15,%rax), %cl
cmpb $0x2c, %cl
je 0x39846
incl %r14d
movb %cl, (%r15,%r13)
incq %rax
jmp 0x3982e
xorl %esi, %esi
movl %edx, %eax
jmp 0x398de
movb $0x0, (%r15,%r13)
leaq (%r15,%rsi), %rbx
leaq (%r15,%rsi), %rdi
incq %rdi
movq %rdx, 0x8(%rsp)
movq %rsi, 0x28(%rsp)
callq 0x4830
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 0x398ca
cmpb $0x2d, (%rbx,%r12)
je 0x398a7
addq %rax, %rcx
incq %r12
jmp 0x39893
addl %r12d, %ebp
cmpl %ebp, %r14d
jle 0x398ca
sarq $0x20, %rcx
leaq (%r15,%rcx), %rdi
incq %rdi
movl %esi, %ebx
callq 0x4830
movl %ebx, %esi
movq 0x8(%rsp), %rdx
jmp 0x398cc
movl %edx, %eax
xorl %ecx, %ecx
testl %esi, %esi
setg %cl
subl %ecx, %esi
movq (%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 0x4600
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movl %r13d, %eax
cltd
idivl 0x10(%rsp)
cltq
imulq %r14, %rax
movslq %edx, %rcx
addq %rcx, %r12
addq %rax, %r12
xorl %r14d, %r14d
movq %r12, %rsi
xorl %edx, %edx
callq 0x38372
testl %eax, %eax
js 0x399d0
subl %r13d, %ebx
leal 0x2(%rbx), %eax
movslq %eax, %rdi
callq 0x46f0
movq %rax, %r13
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x37c4a
movl %r14d, %r15d
testl %eax, %eax
js 0x3997b
cmpl %ebx, %r14d
jge 0x3997b
movl %eax, %ebp
callq 0x4580
movq (%rax), %rax
movl %ebp, %ecx
cmpw $0x0, (%rax,%rcx,2)
jns 0x39945
incl %r14d
movb %bpl, (%r13,%r15)
jmp 0x39945
movb $0x0, (%r13,%r15)
movq 0x18(%rsp), %rax
movl %r14d, (%rax)
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1d62e(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0xd7f9(%rip), %rsi      # 0x471a5
xorl %r13d, %r13d
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x4770
movq %r15, %rdi
callq 0x4600
movq 0x18(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x39989
movq 0x18(%rsp), %rax
orl $-0x1, (%rax)
movq 0x1d5f1(%rip), %rax     # 0x56fd0
movq (%rax), %rcx
leaq 0xd812(%rip), %rdi      # 0x471fb
pushq $0x56
popq %rsi
pushq $0x1
popq %rdx
callq 0x4a20
xorl %r13d, %r13d
jmp 0x39989