// File: /workspace/github_cpp_makefile_star2_O1/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, 0x48(%rsp)
movq %rsi, %rbx
movq %rdi, %r12
movq 0x18(%rdi), %r13
movq %rsi, %rdi
callq 0x4230
movq %rax, %rbp
incl %eax
movslq %eax, %rdi
callq 0x4700
movq %rax, %r15
testl %ebp, %ebp
jle 0x471f5
callq 0x4580
movq (%rax), %rax
movl %ebp, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %r8d, %r8d
movzbl (%rbx,%rdx), %esi
testb $0x20, 0x1(%rax,%rsi,2)
jne 0x471eb
movslq %r8d, %rdi
incl %r8d
movb %sil, (%r15,%rdi)
incq %rdx
cmpq %rdx, %rcx
jne 0x471d6
jmp 0x471f8
xorl %r8d, %r8d
movslq %r8d, %r14
movb $0x0, (%r15,%r14)
movl %r14d, %eax
decq %rax
movl %r8d, 0x34(%rsp)
movl %r8d, %esi
testl %esi, %esi
jle 0x47222
movl %eax, %ecx
decl %esi
decq %rax
cmpb $0x3a, (%r15,%rcx)
jne 0x4720e
jmp 0x47224
movl %ebp, %esi
movl 0x34(%rsp), %ecx
cmpl %ecx, %esi
movq %r12, 0x28(%rsp)
movq %rsi, 0x38(%rsp)
jge 0x47334
leal 0x1(%rsi), %eax
cmpl %ecx, %eax
setl %bpl
movq %r15, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
movq %r13, 0x8(%rsp)
jge 0x472cd
movslq %esi, %r12
addq $0x2, %r12
movl %esi, %r15d
notl %r15d
addl %ecx, %r15d
xorl %ebx, %ebx
movq 0x20(%rsp), %rax
movzbl -0x1(%rax,%r12), %r13d
cmpq $0x2d, %r13
jne 0x4727b
incl %ebx
jmp 0x472a1
callq 0x4580
cmpq $0x2c, %r13
je 0x4729c
movq (%rax), %rax
movzwl (%rax,%r13,2), %eax
andl $0x800, %eax            # imm = 0x800
movq 0x38(%rsp), %rsi
jne 0x472a1
jmp 0x472b0
movq 0x38(%rsp), %rsi
cmpq %r14, %r12
setl %bpl
incq %r12
decl %r15d
jne 0x47266
cmpl $0x2, %ebx
setge %al
orb %al, %bpl
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r13
testb $0x1, %bpl
cmovnel 0x34(%rsp), %esi
movslq %esi, %rax
movq %rax, 0x50(%rsp)
movb $0x0, (%r15,%rax)
movl (%r13), %eax
testl %eax, %eax
movq %rsi, 0x38(%rsp)
movq %rax, 0x40(%rsp)
je 0x47385
leal -0x1(%rax), %edi
movsbl (%r15), %r12d
testl %r12d, %r12d
je 0x47395
movb 0x1(%r15), %al
testb %al, %al
je 0x47398
leaq 0x2(%r15), %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 0x47319
jmp 0x47398
movl (%r13), %r14d
testl %r14d, %r14d
je 0x4738d
leal -0x1(%r14), %edi
movsbl (%rbx), %r12d
testl %r12d, %r12d
movq %r15, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
je 0x47481
movb 0x1(%rbx), %al
testb %al, %al
je 0x47484
leaq 0x2(%rbx), %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 0x47367
jmp 0x47484
xorl %r14d, %r14d
jmp 0x4742c
xorl %r14d, %r14d
jmp 0x475ff
xorl %r12d, %r12d
andl %edi, %r12d
movq 0x10(%r13), %rbx
movl $0x1, %r15d
movl %r12d, %r14d
movl %edi, 0x1c(%rsp)
movl %r14d, %eax
shrl $0x4, %eax
movl (%rbx,%rax,4), %r13d
leal (%r14,%r14), %ebp
andl $0x1e, %ebp
movl %r13d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x4740b
testb $0x1, %al
jne 0x473f3
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r14d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x20(%rsp), %rsi
callq 0x4750
movl 0x1c(%rsp), %edi
movq 0x38(%rsp), %rsi
testl %eax, %eax
je 0x4740b
addl %r15d, %r14d
andl %edi, %r14d
incl %r15d
cmpl %r12d, %r14d
jne 0x473ac
movq 0x40(%rsp), %rax
movl %eax, %r14d
jmp 0x4741d
movl %ebp, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
movq 0x40(%rsp), %rax
cmovnel %eax, %r14d
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movb $0x1, %cl
cmpl %eax, %r14d
jne 0x475f2
testl %eax, %eax
je 0x47520
leal -0x1(%rax), %edi
movsbl (%rbx), %r12d
testl %r12d, %r12d
je 0x47528
movb 0x1(%rbx), %al
testb %al, %al
movq 0x8(%rsp), %rsi
je 0x47530
leaq 0x2(%rbx), %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 0x47463
jmp 0x47530
xorl %r12d, %r12d
andl %edi, %r12d
movq %r13, 0x8(%rsp)
movq 0x10(%r13), %rdx
movl $0x1, %r15d
movl %r12d, %ebx
movl %edi, 0x1c(%rsp)
movq %rdx, 0x40(%rsp)
movl %ebx, %eax
shrl $0x4, %eax
movl (%rdx,%rax,4), %r13d
leal (%rbx,%rbx), %ebp
andl $0x1e, %ebp
movl %r13d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x474fa
testb $0x1, %al
jne 0x474eb
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %ebx, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x4750
movq 0x40(%rsp), %rdx
movl 0x1c(%rsp), %edi
movq 0x38(%rsp), %rsi
testl %eax, %eax
je 0x474fa
addl %r15d, %ebx
andl %edi, %ebx
incl %r15d
cmpl %r12d, %ebx
jne 0x474a2
jmp 0x47507
movl %ebp, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
cmovel %ebx, %r14d
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r13
jmp 0x475ff
xorl %r14d, %r14d
jmp 0x475c1
xorl %r12d, %r12d
movq 0x8(%rsp), %rsi
andl %edi, %r12d
movq 0x10(%rsi), %rbx
movl $0x1, %r15d
movl %r12d, %r14d
movl %edi, 0x1c(%rsp)
movl %r14d, %eax
shrl $0x4, %eax
movl (%rbx,%rax,4), %r13d
leal (%r14,%r14), %ebp
andl $0x1e, %ebp
movl %r13d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x4759e
testb $0x1, %al
jne 0x47586
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl %r14d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x4750
movl 0x1c(%rsp), %edi
testl %eax, %eax
je 0x4759e
addl %r15d, %r14d
andl %edi, %r14d
incl %r15d
cmpl %r12d, %r14d
jne 0x47544
movq 0x40(%rsp), %rax
movl %eax, %r14d
jmp 0x475b0
movl %ebp, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
movq 0x40(%rsp), %rax
cmovnel %eax, %r14d
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movb $0x1, %cl
cmpl %eax, %r14d
jne 0x475e2
movq 0x48(%rsp), %rax
movl $0x0, (%rax)
movq %r15, %rdi
callq 0x4610
movq 0x38(%rsp), %rsi
xorl %ecx, %ecx
jmp 0x475f2
movq 0x50(%rsp), %rax
movb $0x3a, (%r15,%rax)
movl 0x34(%rsp), %eax
movl %eax, %esi
movq 0x8(%rsp), %r13
testb %cl, %cl
je 0x477da
cmpl (%r13), %r14d
je 0x4779d
movq 0x20(%r13), %rax
movl %r14d, %ecx
leaq (%rcx,%rcx,2), %rcx
movslq (%rax,%rcx,8), %rdx
movq %rdx, 0x8(%rsp)
movl 0x4(%rax,%rcx,8), %edx
movl %edx, 0x10(%rsp)
movq 0x8(%rax,%rcx,8), %r14
movq 0x10(%rax,%rcx,8), %rax
movq %rax, 0x38(%rsp)
movl 0x34(%rsp), %edx
movl %edx, %eax
subl %esi, %eax
jle 0x476c2
leal 0x1(%rsi), %r12d
movslq %esi, %rcx
movl %r12d, %r13d
cmpl %edx, %r12d
jge 0x47678
leaq (%r15,%rcx), %rdx
movl $0x1, %esi
movl %r12d, %r13d
movb (%rdx,%rsi), %dil
cmpb $0x2c, %dil
je 0x47671
movslq %r13d, %r8
incl %r13d
movb %dil, (%r15,%r8)
incq %rsi
cmpl %esi, %eax
jne 0x4765d
movslq %r13d, %rbx
movb $0x0, (%r15,%rbx)
leaq (%r15,%rcx), %rdi
incq %rdi
callq 0x4840
movl %eax, %ebp
cmpl %ebx, %r12d
je 0x476ac
movslq %r12d, %rax
addq %r15, %rax
cmpb $0x2d, (%rax)
je 0x476ac
incl %r12d
incq %rax
cmpl %r12d, %r13d
jne 0x47699
movl %r13d, %r12d
cmpl %r13d, %r12d
jge 0x476c9
movslq %r12d, %rax
leaq (%r15,%rax), %rdi
incq %rdi
callq 0x4840
jmp 0x476cc
xorl %ebp, %ebp
movl %r14d, %eax
jmp 0x476da
movl %r14d, %eax
movq 0x28(%rsp), %r12
xorl %ecx, %ecx
testl %ebp, %ebp
setg %cl
subl %ecx, %ebp
movslq %ebp, %r13
cmpq %r13, %r14
cmovlel %r14d, %r13d
movslq %eax, %rbx
cmpq %rbx, %r14
cmovlel %r14d, %ebx
cmpl %ebx, %r13d
cmovgel %ebx, %r13d
movq %r15, %rdi
callq 0x4610
movq (%r12), %rdi
movl %r13d, %eax
cltd
idivl 0x10(%rsp)
cltq
imulq 0x8(%rsp), %rax
movslq %edx, %rcx
movq 0x38(%rsp), %rsi
addq %rcx, %rsi
addq %rax, %rsi
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x45d91
testl %eax, %eax
js 0x477d0
subl %r13d, %ebx
leal 0x2(%rbx), %eax
movslq %eax, %rdi
callq 0x4700
movq %rax, %r13
movq (%r12), %rdi
callq 0x456b0
testl %eax, %eax
js 0x4778b
testl %ebx, %ebx
jle 0x4778b
movl %eax, %ebp
xorl %r14d, %r14d
callq 0x4580
movq %rax, %r15
movq (%r15), %rax
movl %ebp, %ecx
cmpw $0x0, (%rax,%rcx,2)
jns 0x47777
movslq %r14d, %rax
incl %r14d
movb %bpl, (%r13,%rax)
movq (%r12), %rdi
callq 0x456b0
testl %eax, %eax
js 0x4778b
movl %eax, %ebp
cmpl %ebx, %r14d
jl 0x47760
movslq %r14d, %rax
movb $0x0, (%r13,%rax)
movq 0x48(%rsp), %rcx
movl %eax, (%rcx)
jmp 0x477dd
movq 0x1c824(%rip), %rax     # 0x63fc8
movq (%rax), %rdi
leaq 0xe9e7(%rip), %rsi      # 0x56195
xorl %r13d, %r13d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4770
movq %r15, %rdi
callq 0x4610
movq 0x48(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x477dd
movq 0x48(%rsp), %rdi
callq 0x60f2
xorl %r13d, %r13d
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq