// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %r9, %rbx
movq %rcx, %rbp
movq %r8, %r13
subq %rcx, %r13
cmpq $-0x3, %r13
jb 0x681ed
leaq 0x1b339(%rip), %rsi     # 0x8350a
leaq 0x1b33f(%rip), %rdx     # 0x83517
xorl %r14d, %r14d
pushq $0x1
popq %rdi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x110c6
jmp 0x682fe
movq %rsi, %r12
movl 0x8(%rsi), %esi
testq %rsi, %rsi
je 0x682e1
movq %rdx, %rcx
movq %rdi, %r15
movq (%rdi), %rdi
movl 0x4(%r12), %r8d
movq %rbp, %rax
cqto
idivq %rsi
imulq %rax, %r8
addq %rdx, %rcx
addq %r8, %rcx
movq %rcx, %rsi
xorl %edx, %edx
callq 0xc960
testl %eax, %eax
js 0x68314
movq %rbx, 0x8(%rsp)
movl 0x4(%r12), %eax
movl 0x8(%r12), %ebx
movq %rax, 0x10(%rsp)
leaq (%rax,%r13), %rdi
subq %rbx, %rdi
incq %rdi
callq 0x4680
testq %rax, %rax
je 0x68335
movq %rax, %r14
movq 0x8(%rsp), %rax
movq %r13, (%rax)
movq %rbp, %rax
cqto
idivq %rbx
subq %rdx, %rbx
movq %r13, %rbp
subq %rbx, %rbp
jle 0x68340
movq 0x10(%rsp), %r13
subq %rdx, %r13
movq (%r15), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x690df
cmpq %r13, %rax
jl 0x68353
movq %r14, %r13
addq %rbx, %r13
movl 0x8(%r12), %eax
movq 0x8(%rsp), %rbx
movl %eax, %eax
cmpq %rax, %rbp
jle 0x683a3
movq (%r15), %rdi
movl 0x4(%r12), %edx
movq %r13, %rsi
callq 0x690df
movl 0x4(%r12), %ecx
cmpq %rcx, %rax
jl 0x68358
movl 0x8(%r12), %eax
addq %rax, %r13
subq %rax, %rbp
jmp 0x682ab
leaq 0x1b222(%rip), %rsi     # 0x8350a
leaq 0x1b23f(%rip), %rdx     # 0x8352e
xorl %r14d, %r14d
pushq $0x1
popq %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x110c6
orq $-0x1, (%rbx)
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
orq $-0x1, (%rbx)
leaq 0x1b1eb(%rip), %rsi     # 0x8350a
leaq 0x1b229(%rip), %rdx     # 0x8354f
xorl %r14d, %r14d
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
jmp 0x68302
movq 0x8(%rsp), %rax
orq $-0x1, (%rax)
jmp 0x68391
movq (%r15), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x690df
cmpq %r13, %rax
jge 0x68399
movq 0x8(%rsp), %rbx
testq %rax, %rax
leaq 0x1b255(%rip), %rax     # 0x835b7
leaq 0x1b265(%rip), %rcx     # 0x835ce
cmoveq %rax, %rcx
leaq 0x1b196(%rip), %rsi     # 0x8350a
leaq 0x1b21f(%rip), %rdx     # 0x8359a
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
movq %r14, %rdi
callq 0x45c0
orq $-0x1, (%rbx)
xorl %r14d, %r14d
jmp 0x68302
movb $0x0, (%r14,%rax)
jmp 0x68302
testq %rbp, %rbp
jle 0x683be
movq (%r15), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x690df
cmpq %rbp, %rax
jl 0x68358
addq %rbp, %r13
movb $0x0, (%r13)
jmp 0x68302