// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/bgzf.c
// Function: bgzf_read_block
int bgzf_read_block(BGZF *fp)
{
    hts_tpool_result *r;

    if (fp->errcode) return -1;

    if (fp->mt) {
    again:
        if (fp->mt->hit_eof) {
            // Further reading at EOF will always return 0
            fp->block_length = 0;
            return 0;
        }
        r = hts_tpool_next_result_wait(fp->mt->out_queue);
        bgzf_job *j = r ? (bgzf_job *)hts_tpool_result_data(r) : NULL;

        if (!j || j->errcode == BGZF_ERR_MT) {
            if (!fp->mt->free_block) {
                fp->uncompressed_block = malloc(2 * BGZF_MAX_BLOCK_SIZE);
                if (fp->uncompressed_block == NULL) return -1;
                fp->compressed_block = (char *)fp->uncompressed_block + BGZF_MAX_BLOCK_SIZE;
            } // else it's already allocated with malloc, maybe even in-use.
            if (mt_destroy(fp->mt) < 0) {
                fp->errcode = BGZF_ERR_IO;
            }
            fp->mt = NULL;
            hts_tpool_delete_result(r, 0);
            if (fp->errcode) {
                return -1;
            }
            goto single_threaded;
        }

        if (j->errcode) {
            fp->errcode = j->errcode;
            hts_log_error("BGZF decode jobs returned error %d "
                          "for block offset %"PRId64,
                          j->errcode, j->block_address);
            hts_tpool_delete_result(r, 0);
            return -1;
        }

        if (j->hit_eof) {
            if (!fp->last_block_eof && !fp->no_eof_block) {
                fp->no_eof_block = 1;
                hts_log_warning("EOF marker is absent. The input may be truncated");
            }
            fp->mt->hit_eof = 1;
        }

        // Zero length blocks in the middle of a file are (wrongly)
        // considered as EOF by many callers.  We work around this by
        // trying again to see if we hit a genuine EOF.
        if (!j->hit_eof && j->uncomp_len == 0) {
            fp->last_block_eof = 1;
            hts_tpool_delete_result(r, 0);
            goto again;
        }

        // block_length=0 and block_offset set by bgzf_seek.
        if (fp->block_length != 0) fp->block_offset = 0;
        if (!j->hit_eof) fp->block_address = j->block_address;
        fp->block_clength = j->comp_len;
        fp->block_length = j->uncomp_len;
        // bgzf_read() can change fp->block_length
        fp->last_block_eof = (fp->block_length == 0);

        if ( j->uncomp_len && j->fp->idx_build_otf )
        {
            bgzf_index_add_block(j->fp);
            j->fp->idx->ublock_addr += j->uncomp_len;
        }

        // Steal the data block as it's quicker than a memcpy.
        // We just need to make sure we delay the pool free.
        if (fp->mt->curr_job) {
            pthread_mutex_lock(&fp->mt->job_pool_m);
            pool_free(fp->mt->job_pool, fp->mt->curr_job);
            pthread_mutex_unlock(&fp->mt->job_pool_m);
        }
        fp->uncompressed_block = j->uncomp_data;
        fp->mt->curr_job = j;
        if (fp->mt->free_block) {
            free(fp->mt->free_block); // clear up last non-mt block
            fp->mt->free_block = NULL;
        }

        hts_tpool_delete_result(r, 0);
        return 0;
    }

    uint8_t header[BLOCK_HEADER_LENGTH], *compressed_block;
    int count, size, block_length, remaining;

 single_threaded:
    size = 0;

    int64_t block_address;
    block_address = bgzf_htell(fp);

    // Reading an uncompressed file
    if ( !fp->is_compressed )
    {
        count = hread(fp->fp, fp->uncompressed_block, BGZF_MAX_BLOCK_SIZE);
        if (count < 0)  // Error
        {
            hts_log_error("Failed to read uncompressed data "
                          "at offset %"PRId64"%s%s",
                          block_address, errno ? ": " : "", strerror(errno));
            fp->errcode |= BGZF_ERR_IO;
            return -1;
        }
        else if (count == 0)  // EOF
        {
            fp->block_length = 0;
            return 0;
        }
        if (fp->block_length != 0) fp->block_offset = 0;
        fp->block_address = block_address;
        fp->block_length = count;
        return 0;
    }

    // Reading compressed file
    if ( fp->is_gzip && fp->gz_stream ) // is this is an initialized gzip stream?
    {
        count = inflate_gzip_block(fp);
        if ( count<0 )
        {
            hts_log_error("Reading GZIP stream failed at offset %"PRId64,
                          block_address);
            fp->errcode |= BGZF_ERR_ZLIB;
            return -1;
        }
        fp->block_length = count;
        fp->block_address = block_address;
        return 0;
    }
    if (fp->cache_size && load_block_from_cache(fp, block_address)) return 0;

    // loop to skip empty bgzf blocks
    while (1)
    {
        count = hread(fp->fp, header, sizeof(header));
        if (count == 0) { // no data read
            if (!fp->last_block_eof && !fp->no_eof_block && !fp->is_gzip) {
                fp->no_eof_block = 1;
                hts_log_warning("EOF marker is absent. The input may be truncated");
            }
            fp->block_length = 0;
            return 0;
        }
        int ret = 0;
        if ( count != sizeof(header) || (ret=check_header(header))==-2 )
        {
            fp->errcode |= BGZF_ERR_HEADER;
            hts_log_error("%s BGZF header at offset %"PRId64,
                          ret ? "Invalid" : "Failed to read",
                          block_address);
            return -1;
        }
        if ( ret==-1 )
        {
            // GZIP, not BGZF
            uint8_t *cblock = (uint8_t*)fp->compressed_block;
            memcpy(cblock, header, sizeof(header));
            count = hread(fp->fp, cblock+sizeof(header), BGZF_BLOCK_SIZE - sizeof(header)) + sizeof(header);

            fp->is_gzip = 1;
            fp->gz_stream = (z_stream*) calloc(1,sizeof(z_stream));
            // Set up zlib, using a window size of 15, and its built-in GZIP header processing (+16).
            int ret = inflateInit2(fp->gz_stream, 15 + 16);
            if (ret != Z_OK)
            {
                hts_log_error("Call to inflateInit2 failed: %s", bgzf_zerr(ret, fp->gz_stream));
                fp->errcode |= BGZF_ERR_ZLIB;
                return -1;
            }
            fp->gz_stream->avail_in = count;
            fp->gz_stream->next_in  = cblock;
            count = inflate_gzip_block(fp);
            if ( count<0 )
            {
                hts_log_error("Reading GZIP stream failed at offset %"PRId64,
                              block_address);
                fp->errcode |= BGZF_ERR_ZLIB;
                return -1;
            }
            fp->block_length = count;
            fp->block_address = block_address;
            if ( fp->idx_build_otf ) return -1; // cannot build index for gzip
            return 0;
        }
        size = count;
        block_length = unpackInt16((uint8_t*)&header[16]) + 1; // +1 because when writing this number, we used "-1"
        if (block_length < BLOCK_HEADER_LENGTH)
        {
            hts_log_error("Invalid BGZF block length at offset %"PRId64,
                          block_address);
            fp->errcode |= BGZF_ERR_HEADER;
            return -1;
        }
        compressed_block = (uint8_t*)fp->compressed_block;
        memcpy(compressed_block, header, BLOCK_HEADER_LENGTH);
        remaining = block_length - BLOCK_HEADER_LENGTH;
        count = hread(fp->fp, &compressed_block[BLOCK_HEADER_LENGTH], remaining);
        if (count != remaining) {
            hts_log_error("Failed to read BGZF block data at offset %"PRId64
                          " expected %d bytes; hread returned %d",
                          block_address, remaining, count);
            fp->errcode |= BGZF_ERR_IO;
            return -1;
        }
        size += count;
        if ((count = inflate_block(fp, block_length)) < 0) {
            hts_log_debug("Inflate block operation failed for "
                          "block at offset %"PRId64": %s",
                          block_address, bgzf_zerr(count, NULL));
            fp->errcode |= BGZF_ERR_ZLIB;
            return -1;
        }
        fp->last_block_eof = (count == 0);
        if ( count ) break;     // otherwise an empty bgzf block
        block_address = bgzf_htell(fp); // update for new block start
    }
    if (fp->block_length != 0) fp->block_offset = 0; // Do not reset offset if this read follows a seek.
    fp->block_address = block_address;
    fp->block_length = count;
    if ( fp->idx_build_otf )
    {
        bgzf_index_add_block(fp);
        fp->idx->ublock_addr += count;
    }
    cache_block(fp, size);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpw $0x0, (%rdi)
jne 0xb565
movq %rdi, %rbx
movq 0x48(%rdi), %rax
testq %rax, %rax
je 0xafb5
cmpl $0x0, 0x68(%rax)
je 0xae9a
movl $0x0, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0xb565
leaq 0x89bd7(%rip), %r13     # 0x94a78
xorl %r12d, %r12d
movq 0x20(%rax), %rdi
callq 0x35e75
movq %rax, %r14
testq %rax, %rax
je 0xaf5c
movq %r14, %rdi
callq 0x361a4
testq %rax, %rax
je 0xaf5c
movq %rax, %r15
movl 0x20018(%rax), %eax
testl %eax, %eax
jne 0xaf53
cmpl $0x0, 0x20028(%r15)
je 0xaf24
movl (%rbx), %eax
testl $0x20040000, %eax       # imm = 0x20040000
jne 0xb29a
orl $0x40000, %eax          # imm = 0x40000
movl %eax, (%rbx)
movl $0x3, %edi
leaq 0x89b2f(%rip), %rsi     # 0x94a30
movq %r13, %rdx
xorl %eax, %eax
callq 0x13de5
cmpl $0x0, 0x20028(%r15)
movq 0x48(%rbx), %rax
movl $0x1, 0x68(%rax)
jne 0xb2a8
cmpq $0x0, 0x20010(%r15)
jne 0xb295
orb $0x20, 0x3(%rbx)
movq %r14, %rdi
xorl %esi, %esi
callq 0x3617f
movq 0x48(%rbx), %rax
cmpl $0x0, 0x68(%rax)
je 0xaea4
jmp 0xae8c
cmpl $0x10, %eax
jne 0xb25b
movq 0x48(%rbx), %r15
cmpq $0x0, 0x60(%r15)
jne 0xaf88
movl $0x20000, %edi          # imm = 0x20000
callq 0x4680
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0xb565
addq $0x10000, %rax          # imm = 0x10000
movq %rax, 0x30(%rbx)
movq %r15, %rdi
callq 0xb605
testl %eax, %eax
jns 0xaf99
movw $0x4, (%rbx)
movq $0x0, 0x48(%rbx)
movq %r14, %rdi
xorl %esi, %esi
callq 0x3617f
cmpw $0x0, (%rbx)
jne 0xb565
movq %rbx, %rdi
callq 0xb7a5
movq %rax, %rbp
movl (%rbx), %eax
btl $0x1e, %eax
jb 0xb04e
movq 0x28(%rbx), %r14
movq 0x40(%rbx), %r12
movq 0x8(%r12), %rsi
movq 0x10(%r12), %r13
subq %rsi, %r13
movl $0x10000, %r15d         # imm = 0x10000
cmpq %r15, %r13
cmovbq %r13, %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0x43c0
addq %r15, 0x8(%r12)
cmpq $0xffff, %r13           # imm = 0xFFFF
ja 0xb025
testb $0x2, 0x30(%r12)
je 0xb025
movl $0x10000, %edx          # imm = 0x10000
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0xf129
movq %rax, %r15
testl %r15d, %r15d
js 0xb1e9
je 0xae8c
cmpl $0x0, 0x8(%rbx)
je 0xb041
movl $0x0, 0x10(%rbx)
movq %rbp, 0x18(%rbx)
movl %r15d, 0x8(%rbx)
jmp 0xae93
testl %eax, %eax
js 0xb234
movq %rbp, %r12
cmpl $0x0, 0x4(%rbx)
je 0xb074
movq %rbx, %rdi
movq %r12, %rsi
callq 0xbab0
xorl %ebp, %ebp
testl %eax, %eax
jne 0xb565
leaq 0x10(%rsp), %rbp
leaq 0x89aaa(%rip), %r13     # 0x94b2a
movq %r12, 0x8(%rsp)
movq %rbx, 0x28(%rsp)
movq 0x40(%rbx), %r14
movq 0x8(%r14), %rsi
movq 0x10(%r14), %r15
subq %rsi, %r15
cmpq $0x12, %r15
movl $0x12, %r12d
cmovbq %r15, %r12
movq %rbp, %rdi
movq %r12, %rdx
callq 0x43c0
addq %r12, 0x8(%r14)
cmpq $0x11, %r15
ja 0xb0d9
testb $0x2, 0x30(%r14)
je 0xb0d9
movl $0x12, %edx
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rcx
callq 0xf129
movq %rax, %r12
testl %r12d, %r12d
je 0xb3a4
cmpl $0x12, %r12d
jne 0xb4b5
movq %r12, 0x30(%rsp)
movq %rbp, %rdi
callq 0xbbc1
cmpl $-0x1, %eax
je 0xb3d7
cmpl $-0x2, %eax
je 0xb4ae
movzwl 0x20(%rsp), %r15d
cmpl $0x10, %r15d
jbe 0xb4df
movq 0x30(%rbx), %r12
movzwl 0x20(%rsp), %eax
movw %ax, 0x10(%r12)
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%r12)
leal -0x11(%r15), %ebp
movq 0x40(%rbx), %r14
addq $0x12, %r12
movq 0x8(%r14), %rsi
movq 0x10(%r14), %r13
subq %rsi, %r13
cmpq %rbp, %r13
movq %rbp, %rbx
cmovbq %r13, %rbx
movq %r12, %rdi
movq %rbx, %rdx
callq 0x43c0
addq %rbx, 0x8(%r14)
cmpq %rbp, %r13
movq %rbp, %r13
jae 0xb187
testb $0x2, 0x30(%r14)
je 0xb187
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0xf129
movq %rax, %rbx
cmpl %ebx, %r13d
jne 0xb503
incl %r15d
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movl %r15d, %esi
callq 0xbc02
movl %eax, %ebp
testl %eax, %eax
js 0xb532
sete %al
movzbl %al, %eax
shll $0x1d, %eax
movl (%rbx), %ecx
movl $0xdfffffff, %edx       # imm = 0xDFFFFFFF
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, (%rbx)
testl %ebp, %ebp
jne 0xb576
movq %rbx, %rdi
callq 0xb7a5
movq %rax, 0x8(%rsp)
leaq 0x8994b(%rip), %r13     # 0x94b2a
leaq 0x10(%rsp), %rbp
jmp 0xb08a
callq 0x4050
movl (%rax), %edi
testl %edi, %edi
leaq 0x8ab74(%rip), %rax     # 0x95d6d
leaq 0x8ad16(%rip), %r14     # 0x95f16
cmoveq %rax, %r14
callq 0x4150
leaq 0x89820(%rip), %rsi     # 0x94a30
leaq 0x89892(%rip), %rdx     # 0x94aa9
movl $0x1, %edi
movq %rbp, %rcx
movq %r14, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x13de5
orb $0x4, (%rbx)
jmp 0xb560
cmpq $0x0, 0x60(%rbx)
je 0xb056
movq %rbx, %rdi
callq 0xb858
testl %eax, %eax
js 0xb389
movl %eax, 0x8(%rbx)
movq %rbp, 0x18(%rbx)
jmp 0xae93
movw %ax, (%rbx)
movl 0x20018(%r15), %ecx
movq 0x20020(%r15), %r8
leaq 0x897bd(%rip), %rsi     # 0x94a30
leaq 0x897c6(%rip), %rdx     # 0x94a40
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movq %r14, %rdi
xorl %esi, %esi
callq 0x3617f
jmp 0xb560
movb $0x1, %r12b
jmp 0xb2a8
movq 0x48(%rbx), %rax
movl $0x1, 0x68(%rax)
xorl %r12d, %r12d
cmpl $0x0, 0x8(%rbx)
je 0xb2b5
movl $0x0, 0x10(%rbx)
testb %r12b, %r12b
je 0xb2c5
movq 0x20020(%r15), %rax
movq %rax, 0x18(%rbx)
movl 0x10008(%r15), %eax
movl %eax, 0xc(%rbx)
movl 0x20010(%r15), %eax
movl %eax, 0x8(%rbx)
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
shll $0x1d, %ecx
movl $0xdfffffff, %eax       # imm = 0xDFFFFFFF
andl (%rbx), %eax
orl %ecx, %eax
movl %eax, (%rbx)
cmpq $0x0, 0x20010(%r15)
je 0xb318
movq (%r15), %rdi
cmpl $0x0, 0x58(%rdi)
je 0xb318
callq 0xb715
movq 0x20010(%r15), %rax
movq (%r15), %rcx
movq 0x50(%rcx), %rcx
addq %rax, 0x10(%rcx)
movq 0x48(%rbx), %rdi
cmpq $0x0, 0x8(%rdi)
je 0xb34d
addq $0x30, %rdi
callq 0x4750
movq 0x48(%rbx), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x57e84
movq 0x48(%rbx), %rdi
addq $0x30, %rdi
callq 0x4340
movq 0x48(%rbx), %rdi
movq %r15, %rax
addq $0x10010, %rax          # imm = 0x10010
movq %rax, 0x28(%rbx)
movq %r15, 0x8(%rdi)
movq 0x60(%rdi), %rdi
testq %rdi, %rdi
je 0xb378
callq 0x45c0
movq 0x48(%rbx), %rax
movq $0x0, 0x60(%rax)
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
callq 0x3617f
jmp 0xb565
leaq 0x896a0(%rip), %rsi     # 0x94a30
leaq 0x89745(%rip), %rdx     # 0x94adc
movl $0x1, %edi
movq %rbp, %rcx
jmp 0xb4a2
movl (%rbx), %eax
testl $0xa0040000, %eax       # imm = 0xA0040000
jne 0xae8c
orl $0x40000, %eax          # imm = 0x40000
movl %eax, (%rbx)
leaq 0x89671(%rip), %rsi     # 0x94a30
leaq 0x896b2(%rip), %rdx     # 0x94a78
movl $0x3, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0xae8c
movq 0x30(%rbx), %r13
movzwl 0x20(%rsp), %eax
movw %ax, 0x10(%r13)
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%r13)
movq 0x40(%rbx), %r14
leaq 0x12(%r13), %r12
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rbp
subq %rsi, %rbp
movl $0xfeee, %r15d          # imm = 0xFEEE
cmpq %r15, %rbp
cmovbq %rbp, %r15
movq %r12, %rdi
movq %r15, %rdx
callq 0x43c0
addq %r15, 0x8(%r14)
cmpq $0xfeed, %rbp           # imm = 0xFEED
ja 0xb444
testb $0x2, 0x30(%r14)
je 0xb444
movl $0xfeee, %edx           # imm = 0xFEEE
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0xf129
movq %rax, %r15
movq 0x8(%rsp), %r14
orb $-0x80, 0x3(%rbx)
movl $0x1, %edi
movl $0x70, %esi
callq 0x4300
movq %rax, 0x60(%rbx)
leaq 0x89559(%rip), %rdx     # 0x949c0
movq %rax, %rdi
movl $0x1f, %esi
movl $0x70, %ecx
callq 0x43e0
testl %eax, %eax
je 0xb5bc
movq 0x60(%rbx), %rsi
movl %eax, %edi
callq 0xadc9
leaq 0x8959d(%rip), %rsi     # 0x94a30
leaq 0x8969f(%rip), %rdx     # 0x94b39
movl $0x1, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x13de5
jmp 0xb55d
leaq 0x8966d(%rip), %r13     # 0x94b22
orb $0x2, (%rbx)
leaq 0x89571(%rip), %rsi     # 0x94a30
leaq 0x8963f(%rip), %rdx     # 0x94b05
movl $0x1, %edi
movq %r13, %rcx
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x13de5
jmp 0xb560
leaq 0x8954a(%rip), %rsi     # 0x94a30
leaq 0x8966c(%rip), %rdx     # 0x94b59
movl $0x1, %edi
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x13de5
orb $0x2, (%rbx)
jmp 0xb560
leaq 0x89526(%rip), %rsi     # 0x94a30
leaq 0x89670(%rip), %rdx     # 0x94b81
movl $0x1, %edi
movq 0x8(%rsp), %rcx
movl %r13d, %r8d
movl %ebx, %r9d
xorl %eax, %eax
callq 0x13de5
movq 0x28(%rsp), %rax
orb $0x4, (%rax)
jmp 0xb560
movl %ebp, %edi
xorl %esi, %esi
callq 0xadc9
leaq 0x894ee(%rip), %rsi     # 0x94a30
leaq 0x8968a(%rip), %rdx     # 0x94bd3
movl $0x5, %edi
movq 0x8(%rsp), %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x13de5
orb $0x1, (%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x8(%rbx)
je 0xb583
movl $0x0, 0x10(%rbx)
addl 0x30(%rsp), %r13d
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rbx)
movl %ebp, 0x8(%rbx)
cmpl $0x0, 0x58(%rbx)
je 0xb5ac
movq %rbx, %rdi
callq 0xb715
movl %ebp, %eax
movq 0x50(%rbx), %rcx
addq %rax, 0x10(%rcx)
movq %rbx, %rdi
movl %r13d, %esi
callq 0xbc5b
jmp 0xae93
addl $0x12, %r15d
movq 0x60(%rbx), %rax
movl %r15d, 0x8(%rax)
movq %r13, (%rax)
movq %rbx, %rdi
callq 0xb858
testl %eax, %eax
js 0xb5ea
movl %eax, 0x8(%rbx)
movq %r14, 0x18(%rbx)
xorl %ebp, %ebp
cmpl 0x58(%rbx), %ebp
sbbl %ebp, %ebp
jmp 0xb565
leaq 0x8943f(%rip), %rsi     # 0x94a30
leaq 0x894e4(%rip), %rdx     # 0x94adc
movl $0x1, %edi
movq %r14, %rcx
jmp 0xb4a2