// File: /workspace/github_c_makefile_star2_O3/icebert[P]pblat/htslib/bgzf.c
// Function: bgzf_read_block
int bgzf_read_block(BGZF *fp)
{
    uint8_t header[BLOCK_HEADER_LENGTH], *compressed_block;
    int count, size = 0, block_length, remaining;

    // Reading an uncompressed file
    if ( !fp->is_compressed )
    {
        count = hread(fp->fp, fp->uncompressed_block, BGZF_MAX_BLOCK_SIZE);
        if ( count==0 )
        {
            fp->block_length = 0;
            return 0;
        }
        if (fp->block_length != 0) fp->block_offset = 0;
        fp->block_address += count;
        fp->block_length = count;
        return 0;
    }

    // Reading compressed file
    int64_t block_address;
    block_address = htell(fp->fp);
    if ( fp->is_gzip && fp->gz_stream ) // is this is a initialized gzip stream?
    {
        count = inflate_gzip_block(fp, 0);
        if ( count<0 )
        {
            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;
    count = hread(fp->fp, header, sizeof(header));
    if (count == 0) { // no data read
        fp->block_length = 0;
        return 0;
    }
    int ret;
    if ( count != sizeof(header) || (ret=check_header(header))==-2 )
    {
        fp->errcode |= BGZF_ERR_HEADER;
        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);
        int nskip = 10;

        // Check optional fields to skip: FLG.FNAME,FLG.FCOMMENT,FLG.FHCRC,FLG.FEXTRA
        // Note: Some of these fields are untested, I did not have appropriate data available
        if ( header[3] & 0x4 ) // FLG.FEXTRA
        {
            nskip += unpackInt16(&cblock[nskip]) + 2;
        }
        if ( header[3] & 0x8 ) // FLG.FNAME
        {
            while ( nskip<count && cblock[nskip] ) nskip++;
            nskip++;
        }
        if ( header[3] & 0x10 ) // FLG.FCOMMENT
        {
            while ( nskip<count && cblock[nskip] ) nskip++;
            nskip++;
        }
        if ( header[3] & 0x2 ) nskip += 2;  //  FLG.FHCRC

        /* FIXME: Should handle this better.  There's no reason why
           someone shouldn't include a massively long comment in their
           gzip stream. */
        if ( nskip >= count )
        {
            fp->errcode |= BGZF_ERR_HEADER;
            return -1;
        }

        fp->is_gzip = 1;
        fp->gz_stream = (z_stream*) calloc(1,sizeof(z_stream));
        int ret = inflateInit2(fp->gz_stream, -15);
        if (ret != Z_OK)
        {
            fp->errcode |= BGZF_ERR_ZLIB;
            return -1;
        }
        fp->gz_stream->avail_in = count - nskip;
        fp->gz_stream->next_in  = cblock + nskip;
        count = inflate_gzip_block(fp, 1);
        if ( count<0 )
        {
            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"
    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) {
        fp->errcode |= BGZF_ERR_IO;
        return -1;
    }
    size += count;
    if ((count = inflate_block(fp, block_length)) < 0) return -1;
    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
movq %rdi, %rbx
movl (%rdi), %eax
movq 0x38(%rdi), %r14
testl $0x60000000, %eax       # imm = 0x60000000
je 0x670b6
movq 0x8(%r14), %rsi
movq 0x28(%r14), %r12
addq %rsi, %r12
subq (%r14), %r12
testl %eax, %eax
js 0x67126
cmpl $0x0, 0x4(%rbx)
je 0x671c5
movq 0x30(%rbx), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x6715c
leal -0x1(%rdx), %r8d
movq %r12, %r9
shrq $0x21, %r9
movl %r12d, %ecx
shll $0xb, %ecx
xorl %ecx, %r9d
xorl %r12d, %r9d
andl %r8d, %r9d
movq 0x10(%rax), %r10
movl $0x1, %r11d
movl %r9d, %edi
movl %edi, %ecx
shrl $0x4, %ecx
movl (%r10,%rcx,4), %ebp
leal (%rdi,%rdi), %ecx
andl $0x1e, %ecx
movl %ebp, %r15d
shrl %cl, %r15d
testb $0x2, %r15b
jne 0x67151
testb $0x1, %r15b
jne 0x670a3
movq 0x18(%rax), %r15
movl %edi, %r13d
cmpq %r12, (%r15,%r13,8)
je 0x67151
addl %r11d, %edi
andl %r8d, %edi
incl %r11d
cmpl %r9d, %edi
jne 0x6706d
jmp 0x671c5
movq 0x20(%rbx), %r12
movq 0x8(%r14), %rsi
movq 0x10(%r14), %r13
subq %rsi, %r13
movl $0x10000, %r15d         # imm = 0x10000
cmpq %r15, %r13
cmovbq %r13, %r15
movq %r12, %rdi
movq %r15, %rdx
callq 0x6570
addq %r15, 0x8(%r14)
cmpq $0xffff, %r13           # imm = 0xFFFF
ja 0x67100
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x6b0a3
movq %rax, %r15
testl %r15d, %r15d
je 0x6734a
cmpl $0x0, 0x8(%rbx)
je 0x67116
movl $0x0, 0xc(%rbx)
movslq %r15d, %rax
addq %rax, 0x10(%rbx)
movl %r15d, 0x8(%rbx)
jmp 0x67351
cmpq $0x0, 0x58(%rbx)
je 0x6702e
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x674db
testl %eax, %eax
js 0x67478
movl %eax, 0x8(%rbx)
movq %r12, 0x10(%rbx)
jmp 0x6748c
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %edx, %edi
jmp 0x6715e
xorl %edi, %edi
cmpl %edx, %edi
je 0x671c5
movl %edi, %ecx
leaq (%rcx,%rcx,2), %r14
shlq $0x3, %r14
addq 0x20(%rax), %r14
cmpl $0x0, 0x8(%rbx)
je 0x6717d
movl $0x0, 0xc(%rbx)
movq %r12, 0x10(%rbx)
movl (%r14), %eax
movl %eax, 0x8(%rbx)
movq 0x20(%rbx), %rdi
movq 0x8(%r14), %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x6570
movq 0x38(%rbx), %rdi
movq 0x10(%r14), %rsi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x6b2f4
testq %rax, %rax
js 0x674cf
cmpl $0x0, (%r14)
jne 0x6748c
movq 0x38(%rbx), %r14
movq 0x8(%r14), %rsi
movq 0x10(%r14), %r13
subq %rsi, %r13
cmpq $0x12, %r13
movl $0x12, %r15d
cmovbq %r13, %r15
movq %rsp, %rdi
movq %r15, %rdx
callq 0x6570
addq %r15, 0x8(%r14)
cmpq $0x11, %r13
ja 0x67205
movq %rsp, %rsi
movl $0x12, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x6b0a3
movq %rax, %r15
testl %r15d, %r15d
je 0x6734a
cmpl $0x12, %r15d
jne 0x67358
cmpb $0x1f, (%rsp)
jne 0x67358
cmpb $-0x75, 0x1(%rsp)
jne 0x67358
cmpb $0x8, 0x2(%rsp)
jne 0x67358
testb $0x4, 0x3(%rsp)
je 0x67360
cmpw $0x6, 0xa(%rsp)
jne 0x67360
cmpb $0x42, 0xc(%rsp)
jne 0x67360
cmpb $0x43, 0xd(%rsp)
jne 0x67360
cmpw $0x2, 0xe(%rsp)
jne 0x67360
movzwl 0x10(%rsp), %ecx
movq 0x28(%rbx), %r13
movzwl 0x10(%rsp), %eax
movw %ax, 0x10(%r13)
movaps (%rsp), %xmm0
movups %xmm0, (%r13)
movq %rcx, 0x28(%rsp)
leaq -0x11(%rcx), %rbp
movq %rbp, 0x30(%rsp)
movq 0x38(%rbx), %rcx
movq %rcx, 0x20(%rsp)
addq $0x12, %r13
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %r14
subq %rsi, %r14
cmpq %rbp, %r14
cmovbq %r14, %rbp
movq %r13, %rdi
movq %rbp, %rdx
callq 0x6570
movq 0x20(%rsp), %rdi
addq %rbp, 0x8(%rdi)
movq 0x30(%rsp), %rax
cmpq %rax, %r14
movq %rax, %r14
jae 0x672ec
movq %r13, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x6b0a3
movq %rax, %rbp
cmpl %ebp, %r14d
jne 0x67484
movq 0x28(%rsp), %rsi
incl %esi
movq %rbx, %rdi
callq 0x675d6
testl %eax, %eax
js 0x67487
movl %eax, %ebp
cmpl $0x0, 0x8(%rbx)
je 0x6731b
movl $0x0, 0xc(%rbx)
addl %r15d, %r14d
movq %r12, 0x10(%rbx)
movl %ebp, 0x8(%rbx)
cmpl $0x0, 0x50(%rbx)
je 0x6733d
movq %rbx, %rdi
callq 0x6765f
movl %ebp, %eax
movq 0x48(%rbx), %rcx
addq %rax, 0x10(%rcx)
movq %rbx, %rdi
movl %r14d, %esi
callq 0x676ea
jmp 0x67351
movl $0x0, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0x6748c
orb $0x2, (%rbx)
jmp 0x67487
movq 0x28(%rbx), %rcx
movzwl 0x10(%rsp), %eax
movw %ax, 0x10(%rcx)
movaps (%rsp), %xmm0
movups %xmm0, (%rcx)
movq 0x38(%rbx), %r15
movq %rcx, 0x20(%rsp)
leaq 0x12(%rcx), %r13
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rbp
subq %rsi, %rbp
movl $0xfeee, %r14d          # imm = 0xFEEE
cmpq %r14, %rbp
cmovbq %rbp, %r14
movq %r13, %rdi
movq %r14, %rdx
callq 0x6570
addq %r14, 0x8(%r15)
cmpq $0xfeed, %rbp           # imm = 0xFEED
ja 0x673c7
movl $0xfeee, %edx           # imm = 0xFEEE
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0x6b0a3
movq %rax, %r14
movzbl 0x3(%rsp), %r15d
movl $0xa, %eax
testb $0x4, %r15b
movq 0x20(%rsp), %r13
je 0x673e5
movzwl 0xa(%r13), %eax
addl $0xc, %eax
addl $0x12, %r14d
testb $0x8, %r15b
je 0x6740e
cmpl %r14d, %eax
jge 0x6740c
movl %eax, %eax
movl %r14d, %ecx
cmpb $0x0, (%r13,%rax)
je 0x6740c
incq %rax
cmpq %rax, %rcx
jne 0x673f9
movl %r14d, %eax
incl %eax
testb $0x10, %r15b
je 0x67433
cmpl %r14d, %eax
jge 0x67431
movslq %eax, %rcx
addq %r13, %rcx
cmpb $0x0, (%rcx)
je 0x67431
incl %eax
incq %rcx
cmpl %eax, %r14d
jne 0x6741f
movl %r14d, %eax
incl %eax
andl $0x2, %r15d
addl %eax, %r15d
movl (%rbx), %eax
subl %r15d, %r14d
jle 0x6747d
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rbx)
movl $0x1, %edi
movl $0x70, %esi
callq 0x6470
movq %rax, 0x58(%rbx)
leaq 0x313fa(%rip), %rdx     # 0x9885c
movq %rax, %rdi
movl $0xfffffff1, %esi       # imm = 0xFFFFFFF1
movl $0x70, %ecx
callq 0x65d0
testl %eax, %eax
je 0x6749d
orb $0x1, (%rbx)
jmp 0x67487
orl $0x2, %eax
movl %eax, (%rbx)
jmp 0x67487
orb $0x4, (%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
movq 0x58(%rbx), %rax
movl %r14d, 0x8(%rax)
movslq %r15d, %rcx
addq %rcx, %r13
movq %r13, (%rax)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x674db
testl %eax, %eax
js 0x67478
movl %eax, 0x8(%rbx)
movq %r12, 0x10(%rbx)
xorl %ebp, %ebp
cmpl 0x50(%rbx), %ebp
sbbl %ebp, %ebp
jmp 0x6748c
addq $0x10, %r14
movq %r14, %rdi
callq 0x7b57