// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x30(%rbp), %rax
subq -0x28(%rbp), %rax
cmpq $-0x3, %rax
jb 0xd46fb
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movl $0x1, %edi
leaq 0x23821(%rip), %rsi     # 0xf7ef6
leaq 0x23827(%rip), %rdx     # 0xf7f03
movb $0x0, %al
callq 0x1b030
movq -0x38(%rbp), %rax
movq $-0x1, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0xd49f8
movq -0x18(%rbp), %rax
cmpl $0x0, 0x8(%rax)
ja 0xd473e
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movl $0x1, %edi
leaq 0x237de(%rip), %rsi     # 0xf7ef6
leaq 0x237fb(%rip), %rdx     # 0xf7f1a
movb $0x0, %al
callq 0x1b030
movq -0x38(%rbp), %rax
movq $-0x1, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0xd49f8
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cqto
idivq %rcx
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
imulq %rcx, %rax
addq %rax, %rsi
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cqto
idivq %rcx
addq %rdx, %rsi
xorl %edx, %edx
callq 0x11a20
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jge 0xd47bc
movq -0x38(%rbp), %rax
movq $-0x1, (%rax)
movl $0x1, %edi
leaq 0x23755(%rip), %rsi     # 0xf7ef6
leaq 0x23793(%rip), %rdx     # 0xf7f3b
movb $0x0, %al
callq 0x1b030
movq $0x0, -0x8(%rbp)
jmp 0xd49f8
movq -0x30(%rbp), %rdi
subq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
addq %rax, %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
subq %rax, %rdi
addq $0x1, %rdi
callq 0x4650
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0xd4804
movq -0x38(%rbp), %rax
movq $-0x1, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0xd49f8
movq -0x30(%rbp), %rax
subq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rax, (%rcx)
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cqto
idivq %rcx
movq -0x78(%rbp), %rax
subq %rdx, %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x68(%rbp), %rax
jg 0xd4887
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0xd7090
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0xd486e
jmp 0xd49a8
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xd49f8
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cqto
idivq %rcx
movq -0x80(%rbp), %rax
subq %rdx, %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0xd7090
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0xd48dc
jmp 0xd49a8
movq -0x68(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x68(%rbp), %rcx
movq -0x58(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jle 0xd4960
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
callq 0xd7090
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jge 0xd4936
jmp 0xd49a8
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x48(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %ecx
movq -0x58(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x58(%rbp)
jmp 0xd48f7
cmpq $0x0, -0x58(%rbp)
jle 0xd4997
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0xd7090
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0xd498b
jmp 0xd49a8
movq -0x58(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movb $0x0, (%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xd49f8
movq -0x50(%rbp), %rdx
leaq 0x23607(%rip), %rcx     # 0xf7fba
leaq 0x235e9(%rip), %rax     # 0xf7fa3
cmpq $0x0, %rdx
cmoveq %rax, %rcx
movl $0x1, %edi
leaq 0x23528(%rip), %rsi     # 0xf7ef6
leaq 0x235b1(%rip), %rdx     # 0xf7f86
movb $0x0, %al
callq 0x1b030
movq -0x40(%rbp), %rdi
callq 0x45a0
movq -0x38(%rbp), %rax
movq $-0x1, (%rax)
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)