// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rdx, 0x38(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
movq 0x18(%rdi), %rax
movq %rax, 0x8(%rsp)
movq %rsi, %rdi
callq 0x4230
movq %rax, %r13
incl %eax
movslq %eax, %rdi
callq 0x4700
movq %rax, %rbp
testl %r13d, %r13d
jle 0x46269
callq 0x4580
movq (%rax), %rax
movl %r13d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %r8d, %r8d
movzbl (%r15,%rdx), %esi
testb $0x20, 0x1(%rax,%rsi,2)
jne 0x4625f
movslq %r8d, %rdi
incl %r8d
movb %sil, (%rbp,%rdi)
incq %rdx
cmpq %rdx, %rcx
jne 0x46248
jmp 0x4626c
xorl %r8d, %r8d
movslq %r8d, %r12
movb $0x0, (%rbp,%r12)
movl %r12d, %eax
decq %rax
movl %r8d, %esi
testl %esi, %esi
jle 0x46292
movl %eax, %ecx
decl %esi
decq %rax
cmpb $0x3a, (%rbp,%rcx)
jne 0x4627e
jmp 0x46295
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 0x4639d
leal 0x1(%rsi), %eax
cmpl %r8d, %eax
jge 0x46339
movslq %esi, %r15
addq $0x2, %r15
leal -0x1(%r8), %r13d
xorl %r14d, %r14d
movb $0x1, %bl
movzbl -0x1(%rbp,%r15), %ebp
cmpq $0x2d, %rbp
jne 0x462e5
incl %r14d
movq 0x10(%rsp), %rbp
jmp 0x46314
callq 0x4580
cmpq $0x2c, %rbp
je 0x4630a
movq (%rax), %rax
movzwl (%rax,%rbp,2), %eax
andl $0x800, %eax            # imm = 0x800
movq 0x10(%rsp), %rbp
movq 0x30(%rsp), %rsi
jne 0x46314
jmp 0x46325
movq 0x10(%rsp), %rbp
movq 0x30(%rsp), %rsi
cmpq %r12, %r15
setl %bl
incq %r15
decl %r13d
cmpl %r13d, %esi
jne 0x462cf
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 0x463ed
leal -0x1(%rax), %esi
movsbl (%rbp), %r12d
testl %r12d, %r12d
je 0x4647e
movb 0x1(%rbp), %al
testb %al, %al
je 0x46481
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 0x4637f
jmp 0x46481
movq 0x8(%rsp), %rax
movl (%rax), %eax
movq %rax, 0x18(%rsp)
testl %eax, %eax
je 0x467d4
movq 0x18(%rsp), %rax
leal -0x1(%rax), %esi
movq 0x28(%rsp), %rcx
movsbl (%rcx), %r15d
testl %r15d, %r15d
je 0x463f5
movb 0x1(%rcx), %al
testb %al, %al
je 0x463f8
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 0x463d2
jmp 0x463f8
xorl %r13d, %r13d
jmp 0x46500
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 0x46469
testb $0x1, %al
jne 0x46456
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x28(%rsp), %rsi
callq 0x4750
movl 0x20(%rsp), %esi
testl %eax, %eax
je 0x46469
addl %r12d, %r13d
andl %esi, %r13d
incl %r12d
cmpl %r15d, %r13d
jne 0x46411
jmp 0x467d4
movl %ebp, %ecx
shrl %cl, %ebx
testb $0x3, %bl
movq 0x18(%rsp), %rax
cmovnel %eax, %r13d
jmp 0x46612
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 0x464ee
testb $0x1, %al
jne 0x464df
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x4750
movl 0x20(%rsp), %esi
testl %eax, %eax
je 0x464ee
addl %ebx, %r13d
andl %esi, %r13d
incl %ebx
cmpl %r12d, %r13d
jne 0x46499
jmp 0x46509
movl %ebp, %ecx
shrl %cl, %r15d
testb $0x3, %r15b
movq 0x18(%rsp), %rax
cmovnel %eax, %r13d
cmpl %eax, %r13d
jne 0x4660d
movq 0x18(%rsp), %rax
testl %eax, %eax
je 0x46549
leal -0x1(%rax), %esi
movq 0x28(%rsp), %rcx
movsbl (%rcx), %r12d
testl %r12d, %r12d
je 0x46551
movb 0x1(%rcx), %al
testb %al, %al
je 0x46554
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 0x4652e
jmp 0x46554
xorl %r13d, %r13d
jmp 0x465d3
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 0x465c1
testb $0x1, %al
jne 0x465b2
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x28(%rsp), %rsi
callq 0x4750
movl 0x20(%rsp), %esi
testl %eax, %eax
je 0x465c1
addl %ebx, %r13d
andl %esi, %r13d
incl %ebx
cmpl %r12d, %r13d
jne 0x4656c
jmp 0x465d8
movl %ebp, %ecx
shrl %cl, %r14d
testb $0x3, %r14b
movq 0x18(%rsp), %rax
cmovnel %eax, %r13d
cmpl %eax, %r13d
jne 0x465f5
movq 0x38(%rsp), %rax
movl $0x0, (%rax)
movq 0x10(%rsp), %rdi
callq 0x4610
xorl %r13d, %r13d
jmp 0x467c2
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 0x467d4
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 0x466ce
movq %r15, 0x20(%rsp)
leal 0x1(%rcx), %r15d
movslq %ecx, %rcx
movl %r15d, %r14d
cmpl %edx, %r15d
movq 0x10(%rsp), %r13
jge 0x46697
leaq (%rcx,%r13), %rdx
movl $0x1, %esi
movl %r15d, %r14d
movb (%rdx,%rsi), %dil
cmpb $0x2c, %dil
je 0x46690
movslq %r14d, %r8
incl %r14d
movb %dil, (%r13,%r8)
incq %rsi
cmpl %esi, %eax
jne 0x4667b
movslq %r14d, %rbx
movb $0x0, (%r13,%rbx)
leaq (%rcx,%r13), %rdi
incq %rdi
callq 0x4840
movl %eax, %ebp
cmpl %ebx, %r15d
jne 0x466d5
cmpl %r14d, %r15d
jge 0x466eb
movslq %r15d, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %rdi
incq %rdi
callq 0x4840
jmp 0x466ee
xorl %ebp, %ebp
movl %r12d, %eax
jmp 0x466fc
movslq %r15d, %rax
addq %r13, %rax
cmpb $0x2d, (%rax)
je 0x466b3
incl %r15d
incq %rax
cmpl %r15d, %r14d
jne 0x466db
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 0x4610
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 0x44e67
testl %eax, %eax
js 0x4680b
subl %r13d, %ebx
leal 0x2(%rbx), %eax
movslq %eax, %rdi
callq 0x4700
movq %rax, %r13
movq (%r12), %rdi
callq 0x4476a
testl %eax, %eax
js 0x467b2
testl %ebx, %ebx
jle 0x467b2
movl %eax, %ebp
xorl %r14d, %r14d
callq 0x4580
movq %rax, %r15
movq (%r15), %rax
movl %ebp, %ecx
cmpw $0x0, (%rax,%rcx,2)
jns 0x4679e
movslq %r14d, %rax
incl %r14d
movb %bpl, (%r13,%rax)
movq (%r12), %rdi
callq 0x4476a
testl %eax, %eax
js 0x467b2
movl %eax, %ebp
cmpl %ebx, %r14d
jl 0x46787
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 0x1b7ed(%rip), %rax     # 0x61fc8
movq (%rax), %rdi
leaq 0xdc70(%rip), %rsi      # 0x54455
xorl %r13d, %r13d
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x4770
movq 0x10(%rsp), %rdi
callq 0x4610
movq 0x38(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x467c2
movq 0x38(%rsp), %rdi
callq 0x6050
jmp 0x465ed