// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/faidx.c
// Function: fai_retrieve
static char *fai_retrieve(const faidx_t *fai, const faidx1_t *val,
                          uint64_t offset, hts_pos_t beg, hts_pos_t end, hts_pos_t *len) {
    char *buffer, *s;
    ssize_t nread, remaining, firstline_len, firstline_blen;
    int ret;

    if ((uint64_t) end - (uint64_t) beg >= SIZE_MAX - 2) {
        hts_log_error("Range %"PRId64"..%"PRId64" too big", beg, end);
        *len = -1;
        return NULL;
    }

    if (val->line_blen <= 0) {
        hts_log_error("Invalid line length in index: %d", val->line_blen);
        *len = -1;
        return NULL;
    }

    ret = bgzf_useek(fai->bgzf,
                     offset
                     + beg / val->line_blen * val->line_len
                     + beg % val->line_blen, SEEK_SET);

    if (ret < 0) {
        *len = -1;
        hts_log_error("Failed to retrieve block. (Seeking in a compressed, .gzi unindexed, file?)");
        return NULL;
    }

    // Over-allocate so there is extra space for one end-of-line sequence
    buffer = (char*)malloc((size_t) end - beg + val->line_len - val->line_blen + 1);
    if (!buffer) {
        *len = -1;
        return NULL;
    }

    remaining = *len = end - beg;
    firstline_blen = val->line_blen - beg % val->line_blen;

    // Special case when the entire interval requested is within a single FASTA/Q line
    if (remaining <= firstline_blen) {
        nread = bgzf_read_small(fai->bgzf, buffer, remaining);
        if (nread < remaining) goto error;
        buffer[nread] = '\0';
        return buffer;
    }

    s = buffer;
    firstline_len = val->line_len - beg % val->line_blen;

    // Read the (partial) first line and its line terminator, but increment  s  past the
    // line contents only, so the terminator characters will be overwritten by the next line.
    nread = bgzf_read_small(fai->bgzf, s, firstline_len);
    if (nread < firstline_len) goto error;
    s += firstline_blen;
    remaining -= firstline_blen;

    // Similarly read complete lines and their line terminator characters, but overwrite the latter.
    while (remaining > val->line_blen) {
        nread = bgzf_read_small(fai->bgzf, s, val->line_len);
        if (nread < (ssize_t) val->line_len) goto error;
        s += val->line_blen;
        remaining -= val->line_blen;
    }

    if (remaining > 0) {
        nread = bgzf_read_small(fai->bgzf, s, remaining);
        if (nread < remaining) goto error;
        s += remaining;
    }

    *s = '\0';
    return buffer;

error:
    hts_log_error("Failed to retrieve block: %s",
                  (nread == 0)? "unexpected end of file" : "error reading file");
    free(buffer);
    *len = -1;
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r15
movq %rcx, %rbx
movq %r8, %rbp
subq %rcx, %rbp
cmpq $-0x3, %rbp
jb 0x82bfc
leaq 0x1f03c(%rip), %rsi     # 0xa1c1a
leaq 0x1f042(%rip), %rdx     # 0xa1c27
xorl %r14d, %r14d
movl $0x1, %edi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x13de5
jmp 0x82d01
movq %rsi, %r12
movl 0x8(%rsi), %esi
testq %rsi, %rsi
je 0x82ce2
movq %rdx, %rcx
movq %rdi, %r14
movq (%rdi), %rdi
movl 0x4(%r12), %r8d
movq %rbx, %rax
cqto
idivq %rsi
imulq %rax, %r8
addq %rdx, %rcx
addq %r8, %rcx
movq %rcx, %rsi
xorl %edx, %edx
callq 0xe4bd
testl %eax, %eax
js 0x82d1a
movq %r14, 0x10(%rsp)
movl 0x4(%r12), %eax
movl 0x8(%r12), %r14d
movq %rax, 0x18(%rsp)
leaq (%rax,%rbp), %rdi
subq %r14, %rdi
incq %rdi
callq 0x4680
testq %rax, %rax
je 0x82d40
movq %rax, %r13
movq %rax, 0x8(%rsp)
movq %r15, 0x20(%rsp)
movq %rbp, (%r15)
movq %rbx, %rax
cqto
idivq %r14
movq %r14, %r15
subq %rdx, %r15
movq %rbp, %rbx
subq %r15, %rbx
jle 0x82d4c
movq %r14, %rcx
movq 0x18(%rsp), %r14
subq %rdx, %r14
movq 0x10(%rsp), %rax
movq (%rax), %rbp
movslq 0x8(%rbp), %rax
movslq 0x10(%rbp), %rsi
subq %rsi, %rax
cmpq %rax, %r14
jge 0x82d88
addq 0x28(%rbp), %rsi
movq 0x8(%rsp), %rdi
movq %r14, %rdx
movq %rcx, 0x18(%rsp)
callq 0x43c0
movq 0x18(%rsp), %rax
addl %r14d, 0x10(%rbp)
addq %r14, 0x20(%rbp)
jmp 0x82da6
leaq 0x1ef31(%rip), %rsi     # 0xa1c1a
leaq 0x1ef4e(%rip), %rdx     # 0xa1c3e
xorl %r14d, %r14d
movl $0x1, %edi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x13de5
movq $-0x1, (%r15)
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $-0x1, (%r15)
leaq 0x1eef2(%rip), %rsi     # 0xa1c1a
leaq 0x1ef30(%rip), %rdx     # 0xa1c5f
xorl %r14d, %r14d
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x82d08
movq $-0x1, (%r15)
jmp 0x82e95
movq 0x10(%rsp), %rax
movq (%rax), %rbx
movslq 0x8(%rbx), %rax
movslq 0x10(%rbx), %rsi
subq %rsi, %rax
cmpq %rax, %rbp
jge 0x82e37
addq 0x28(%rbx), %rsi
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rdx
callq 0x43c0
addl %ebp, 0x10(%rbx)
addq %rbp, 0x20(%rbx)
jmp 0x82e9d
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0xbefc
cmpq %r14, %rax
jl 0x82e30
movl 0x8(%r12), %eax
addq %r15, %r13
cmpq %rax, %rbx
jle 0x82e0f
movl 0x4(%r12), %r15d
movq 0x10(%rsp), %rax
movq (%rax), %rbp
movl %r15d, %r14d
movslq 0x8(%rbp), %rax
movslq 0x10(%rbp), %rsi
subq %rsi, %rax
cmpq %r14, %rax
jle 0x82de7
addq 0x28(%rbp), %rsi
movq %r13, %rdi
movq %r14, %rdx
callq 0x43c0
addl %r15d, 0x10(%rbp)
addq %r14, 0x20(%rbp)
jmp 0x82dff
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xbefc
movl 0x4(%r12), %r15d
cmpq %r15, %rax
jl 0x82e30
movl 0x8(%r12), %eax
addq %rax, %r13
subq %rax, %rbx
cmpq %rax, %rbx
jg 0x82db3
testq %rbx, %rbx
jle 0x82eaa
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x83e59
cmpq %rbx, %rax
jge 0x82ea7
movq 0x8(%rsp), %r14
jmp 0x82e52
movq %rbx, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
movq %rbp, %rdx
callq 0xbefc
cmpq %rbp, %rax
movq %rax, %rbp
jge 0x82e9d
testq %rax, %rax
leaq 0x1ee6b(%rip), %rax     # 0xa1cc7
leaq 0x1ee7b(%rip), %rcx     # 0xa1cde
cmoveq %rax, %rcx
leaq 0x1edac(%rip), %rsi     # 0xa1c1a
leaq 0x1ee35(%rip), %rdx     # 0xa1caa
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movq %r14, %rdi
callq 0x45c0
movq 0x20(%rsp), %rax
movq $-0x1, (%rax)
xorl %r14d, %r14d
jmp 0x82d08
movb $0x0, (%r14,%rbp)
jmp 0x82d08
addq %rbx, %r13
movb $0x0, (%r13)
movq 0x8(%rsp), %r14
jmp 0x82d08