// File: karel-brinda[P]ococo/ext/htslib/faidx.c
// Function: faidx_fetch_seq
char *faidx_fetch_seq(const faidx_t *fai, const char *c_name, int p_beg_i, int p_end_i, int *len)
{
    int l, c;
    khiter_t iter;
    faidx1_t val;
    char *seq=NULL;

    // Adjust position
    iter = kh_get(s, fai->hash, c_name);
    if (iter == kh_end(fai->hash))
    {
        *len = -2;
        fprintf(stderr, "[fai_fetch_seq] The sequence \"%s\" not found\n", c_name);
        return NULL;
    }
    val = kh_value(fai->hash, iter);
    if(p_end_i < p_beg_i) p_beg_i = p_end_i;
    if(p_beg_i < 0) p_beg_i = 0;
    else if(val.len <= p_beg_i) p_beg_i = val.len - 1;
    if(p_end_i < 0) p_end_i = 0;
    else if(val.len <= p_end_i) p_end_i = val.len - 1;

    // Now retrieve the sequence
    int ret = bgzf_useek(fai->bgzf, val.offset + p_beg_i / val.line_blen * val.line_len + p_beg_i % val.line_blen, SEEK_SET);
    if ( ret<0 )
    {
        *len = -1;
        fprintf(stderr, "[fai_fetch_seq] Error: fai_fetch failed. (Seeking in a compressed, .gzi unindexed, file?)\n");
        return NULL;
    }
    l = 0;
    seq = (char*)malloc(p_end_i - p_beg_i + 2);
    while ( (c=bgzf_getc(fai->bgzf))>=0 && l < p_end_i - p_beg_i + 1)
        if (isgraph(c)) seq[l++] = c;
    seq[l] = '\0';
    *len = l;
    return seq;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x68660
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpl (%rcx), %eax
jne 0x69688
movq -0x28(%rbp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq 0x2096a(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x1509e(%rip), %rsi     # 0x7e712
movb $0x0, %al
callq 0x4830
movq $0x0, -0x8(%rbp)
jmp 0x69823
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x34(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x50(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x48(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x696c3
movl -0x20(%rbp), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x696d2
movl $0x0, -0x1c(%rbp)
jmp 0x696ec
movq -0x48(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpq %rcx, %rax
jg 0x696ea
movq -0x48(%rbp), %rax
subq $0x1, %rax
movl %eax, -0x1c(%rbp)
jmp 0x696ec
cmpl $0x0, -0x20(%rbp)
jge 0x696fb
movl $0x0, -0x20(%rbp)
jmp 0x69715
movq -0x48(%rbp), %rax
movslq -0x20(%rbp), %rcx
cmpq %rcx, %rax
jg 0x69713
movq -0x48(%rbp), %rax
subq $0x1, %rax
movl %eax, -0x20(%rbp)
jmp 0x69715
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rsi
movl -0x1c(%rbp), %eax
cltd
idivl -0x4c(%rbp)
imull -0x50(%rbp), %eax
cltq
addq %rax, %rsi
movl -0x1c(%rbp), %eax
cltd
idivl -0x4c(%rbp)
movslq %edx, %rax
addq %rax, %rsi
xorl %edx, %edx
callq 0x66c20
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jge 0x6977c
movq -0x28(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x20872(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x14fd7(%rip), %rsi     # 0x7e73f
movb $0x0, %al
callq 0x4830
movq $0x0, -0x8(%rbp)
jmp 0x69823
movl $0x0, -0x2c(%rbp)
movl -0x20(%rbp), %eax
subl -0x1c(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rdi
callq 0x47a0
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x66000
movl %eax, %ecx
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x5d(%rbp)
jl 0x697c7
movl -0x2c(%rbp), %eax
movl -0x20(%rbp), %ecx
subl -0x1c(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
setl %al
movb %al, -0x5d(%rbp)
movb -0x5d(%rbp), %al
testb $0x1, %al
jne 0x697d0
jmp 0x69806
callq 0x4610
movq (%rax), %rax
movslq -0x30(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x69804
movl -0x30(%rbp), %eax
movb %al, %dl
movq -0x58(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x2c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x69798
movq -0x58(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x2c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x58(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)