// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq %r8, %rbp
subq %rcx, %rbp
cmpq $-0x3, %rbp
jb 0x85172
leaq 0x1f336(%rip), %rsi     # 0xa448a
leaq 0x1f33c(%rip), %rdx     # 0xa4497
xorl %r14d, %r14d
movl $0x1, %edi
movq %r13, %rcx
xorl %eax, %eax
callq 0x14378
jmp 0x85260
movq %rsi, %r12
movl 0x8(%rsi), %esi
testq %rsi, %rsi
je 0x85241
movq %rdx, %rcx
movq %rdi, %r14
movq (%rdi), %rdi
movl 0x4(%r12), %r8d
movq %r13, %rax
cqto
idivq %rsi
imulq %rax, %r8
addq %rdx, %rcx
addq %r8, %rcx
movq %rcx, %rsi
xorl %edx, %edx
callq 0xe607
testl %eax, %eax
js 0x85279
movq %r14, (%rsp)
movl 0x4(%r12), %eax
movl 0x8(%r12), %r15d
movq %rax, 0x10(%rsp)
leaq (%rax,%rbp), %rdi
subq %r15, %rdi
incq %rdi
callq 0x4680
testq %rax, %rax
je 0x8529f
movq %rax, %r14
movq %rbx, 0x8(%rsp)
movq %rbp, (%rbx)
movq %r13, %rax
cqto
idivq %r15
subq %rdx, %r15
movq %rbp, %r13
subq %r15, %r13
jle 0x852ab
movq 0x10(%rsp), %rbp
subq %rdx, %rbp
movq (%rsp), %rax
movq (%rax), %rbx
movslq 0x8(%rbx), %rax
movslq 0x10(%rbx), %rsi
subq %rsi, %rax
cmpq %rax, %rbp
jge 0x852e4
addq 0x28(%rbx), %rsi
movq %r14, %rdi
movq %rbp, %rdx
callq 0x43c0
addl %ebp, 0x10(%rbx)
addq %rbp, 0x20(%rbx)
movq %rbp, %rbx
jmp 0x852f5
leaq 0x1f242(%rip), %rsi     # 0xa448a
leaq 0x1f25f(%rip), %rdx     # 0xa44ae
xorl %r14d, %r14d
movl $0x1, %edi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x14378
movq $-0x1, (%rbx)
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $-0x1, (%rbx)
leaq 0x1f203(%rip), %rsi     # 0xa448a
leaq 0x1f241(%rip), %rdx     # 0xa44cf
xorl %r14d, %r14d
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x85267
movq $-0x1, (%rbx)
jmp 0x853c8
movq (%rsp), %rax
movq (%rax), %rbx
movslq 0x8(%rbx), %rax
movslq 0x10(%rbx), %rsi
subq %rsi, %rax
cmpq %rax, %rbp
jge 0x8536f
addq 0x28(%rbx), %rsi
movq %r14, %rdi
movq %rbp, %rdx
callq 0x43c0
addl %ebp, 0x10(%rbx)
addq %rbp, 0x20(%rbx)
movq %rbp, %rbx
jmp 0x85380
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0xbf5d
movq %rax, %rbx
cmpq %rbp, %rbx
jl 0x85385
movq %r14, %rbp
addq %r15, %rbp
movl 0x8(%r12), %eax
cmpq %rax, %r13
jle 0x853da
movq (%rsp), %rax
movq (%rax), %r15
movl 0x4(%r12), %ebx
movslq 0x8(%r15), %rax
movslq 0x10(%r15), %rsi
subq %rsi, %rax
cmpq %rbx, %rax
jle 0x85347
addq 0x28(%r15), %rsi
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x43c0
addl %ebx, 0x10(%r15)
addq %rbx, 0x20(%r15)
jmp 0x85358
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0xbf5d
movq %rax, %rbx
movl 0x4(%r12), %eax
cmpq %rax, %rbx
jl 0x85385
movl 0x8(%r12), %eax
addq %rax, %rbp
subq %rax, %r13
jmp 0x85309
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0xbf5d
movq %rax, %rbx
cmpq %rbp, %rbx
jge 0x853d0
testq %rbx, %rbx
leaq 0x1f1a8(%rip), %rax     # 0xa4537
leaq 0x1f1b8(%rip), %rcx     # 0xa454e
cmoveq %rax, %rcx
leaq 0x1f0e9(%rip), %rsi     # 0xa448a
leaq 0x1f172(%rip), %rdx     # 0xa451a
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
movq %r14, %rdi
callq 0x45c0
movq 0x8(%rsp), %rax
movq $-0x1, (%rax)
xorl %r14d, %r14d
jmp 0x85267
movb $0x0, (%r14,%rbx)
jmp 0x85267
testq %r13, %r13
jle 0x853fc
movq (%rsp), %rax
movq (%rax), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x863bf
movq %rax, %rbx
cmpq %r13, %rax
jl 0x85385
addq %r13, %rbp
movb $0x0, (%rbp)
jmp 0x85267