// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x64(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
callq 0x4230
movl %eax, -0x34(%rbp)
movl %eax, -0x3c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x47a0
movq %rax, -0x28(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x690ab
movq -0x18(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x694e0
cmpl $0x0, %eax
jne 0x6909e
movq -0x18(%rbp), %rax
movslq -0x30(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x38(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x690a0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x69060
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x38(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jl 0x690ec
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x3a, %eax
jne 0x690df
jmp 0x690ec
jmp 0x690e1
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x690c6
cmpl $0x0, -0x30(%rbp)
jl 0x690f8
movl -0x30(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x691fc
movl $0x0, -0x6c(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x69170
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2d, %eax
jne 0x69138
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x69163
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x69510
cmpl $0x0, %eax
jne 0x69161
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2c, %eax
je 0x69161
jmp 0x69170
jmp 0x69163
jmp 0x69165
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x69114
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jl 0x6917e
cmpl $0x1, -0x6c(%rbp)
jle 0x69184
movl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x60(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x68660
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x691fa
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x68660
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x691e6
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movq -0x28(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x694c4
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb $0x3a, (%rax,%rcx)
movl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x691fa
jmp 0x6920c
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x68660
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x69253
movq 0x20db2(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x15439(%rip), %rsi     # 0x7e665
movb $0x0, %al
callq 0x4830
movq -0x28(%rbp), %rdi
callq 0x46a0
movq -0x20(%rbp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq $0x0, -0x8(%rbp)
jmp 0x694c4
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x58(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x50(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x69372
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x692da
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2c, %eax
je 0x692cd
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x38(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x692cf
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x69294
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x28(%rbp), %rdi
movslq -0x3c(%rbp), %rax
addq %rax, %rdi
addq $0x1, %rdi
callq 0x4910
movl %eax, -0x64(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x38(%rbp), %eax
je 0x6932e
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2d, %eax
jne 0x69321
jmp 0x6932e
jmp 0x69323
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x69306
movl -0x30(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x69352
movq -0x28(%rbp), %rdi
movslq -0x30(%rbp), %rax
addq %rax, %rdi
addq $0x1, %rdi
callq 0x4910
cltq
movq %rax, -0x78(%rbp)
jmp 0x6935a
movq -0x50(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x64(%rbp)
jle 0x69370
movl -0x64(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x69380
movl $0x0, -0x64(%rbp)
movq -0x50(%rbp), %rax
movl %eax, -0x68(%rbp)
movslq -0x64(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x69391
movq -0x50(%rbp), %rax
movl %eax, -0x64(%rbp)
movslq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x693a2
movq -0x50(%rbp), %rax
movl %eax, -0x68(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x68(%rbp), %eax
jle 0x693b0
movl -0x68(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x28(%rbp), %rdi
callq 0x46a0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rsi
movl -0x64(%rbp), %eax
cltd
idivl -0x54(%rbp)
imull -0x58(%rbp), %eax
cltq
addq %rax, %rsi
movl -0x64(%rbp), %eax
cltd
idivl -0x54(%rbp)
movslq %edx, %rax
addq %rax, %rsi
xorl %edx, %edx
callq 0x66c20
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jge 0x69420
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x20bce(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x152af(%rip), %rsi     # 0x7e6bb
movb $0x0, %al
callq 0x4830
movq $0x0, -0x8(%rbp)
jmp 0x694c4
movl $0x0, -0x34(%rbp)
movl -0x68(%rbp), %eax
subl -0x64(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rdi
callq 0x47a0
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x66000
movl %eax, %ecx
movl %ecx, -0x2c(%rbp)
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x79(%rbp)
jl 0x69468
movl -0x34(%rbp), %eax
movl -0x68(%rbp), %ecx
subl -0x64(%rbp), %ecx
cmpl %ecx, %eax
setl %al
movb %al, -0x79(%rbp)
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x69471
jmp 0x694a7
callq 0x4610
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x694a5
movl -0x2c(%rbp), %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x34(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x6943c
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x34(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)