// File: /workspace/github_c_makefile_star2_O1/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), %r15
testl $0x60000000, %eax       # imm = 0x60000000
je 0x670a0
movq 0x28(%r15), %r13
addq 0x8(%r15), %r13
subq (%r15), %r13
testl %eax, %eax
js 0x67110
cmpl $0x0, 0x4(%rbx)
je 0x671a8
movq 0x30(%rbx), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x67147
leal -0x1(%rdx), %edi
movq %r13, %r8
shrq $0x21, %r8
movl %r13d, %ecx
shll $0xb, %ecx
xorl %ecx, %r8d
xorl %r13d, %r8d
andl %edi, %r8d
movq 0x10(%rax), %r9
movl $0x1, %r10d
movl %r8d, %esi
movl %esi, %ecx
shrl $0x4, %ecx
movl (%r9,%rcx,4), %r11d
leal (%rsi,%rsi), %ecx
andl $0x1e, %ecx
movl %r11d, %ebp
shrl %cl, %ebp
testb $0x2, %bpl
jne 0x6713b
testb $0x1, %bpl
jne 0x6708c
movq 0x18(%rax), %r14
movl %esi, %r15d
cmpq %r13, (%r14,%r15,8)
je 0x6713b
addl %r10d, %esi
andl %edi, %esi
incl %r10d
cmpl %r8d, %esi
jne 0x67057
movl %edx, %esi
jmp 0x67149
movq 0x20(%rbx), %r12
movq 0x8(%r15), %rsi
movq 0x10(%r15), %r13
subq %rsi, %r13
movl $0x10000, %r14d         # imm = 0x10000
cmpq %r14, %r13
cmovbq %r13, %r14
movq %r12, %rdi
movq %r14, %rdx
callq 0x6570
addq %r14, 0x8(%r15)
cmpq $0xffff, %r13           # imm = 0xFFFF
ja 0x670ea
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0x6b053
movq %rax, %r14
testl %r14d, %r14d
je 0x67259
cmpl $0x0, 0x8(%rbx)
je 0x67100
movl $0x0, 0xc(%rbx)
movslq %r14d, %rax
addq %rax, 0x10(%rbx)
movl %r14d, 0x8(%rbx)
jmp 0x67260
cmpq $0x0, 0x58(%rbx)
je 0x67019
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x674c6
testl %eax, %eax
js 0x67387
movl %eax, 0x8(%rbx)
movq %r13, 0x10(%rbx)
jmp 0x67473
shrl %cl, %r11d
testb $0x3, %r11b
cmovnel %edx, %esi
jmp 0x67149
xorl %esi, %esi
cmpl %edx, %esi
je 0x671a8
movl %esi, %ecx
leaq (%rcx,%rcx,2), %r14
shlq $0x3, %r14
addq 0x20(%rax), %r14
cmpl $0x0, 0x8(%rbx)
je 0x67168
movl $0x0, 0xc(%rbx)
movq %r13, 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 0x6b2df
testq %rax, %rax
js 0x674ba
cmpl $0x0, (%r14)
jne 0x67473
movq 0x38(%rbx), %r15
movq 0x8(%r15), %rsi
movq 0x10(%r15), %r12
subq %rsi, %r12
cmpq $0x12, %r12
movl $0x12, %r14d
cmovbq %r12, %r14
movq %rsp, %rdi
movq %r14, %rdx
callq 0x6570
addq %r14, 0x8(%r15)
cmpq $0x11, %r12
ja 0x671f0
movq %rsp, %rsi
movl $0x12, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x6b053
movq %rax, %r14
testl %r14d, %r14d
je 0x67259
cmpl $0x12, %r14d
jne 0x67251
cmpb $0x1f, (%rsp)
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jne 0x67243
cmpb $-0x75, 0x1(%rsp)
jne 0x67243
cmpb $0x8, 0x2(%rsp)
jne 0x67243
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x4, 0x3(%rsp)
je 0x67243
cmpw $0x6, 0xa(%rsp)
jne 0x67243
cmpb $0x42, 0xc(%rsp)
jne 0x67243
cmpb $0x43, 0xd(%rsp)
jne 0x67243
xorl %eax, %eax
cmpw $0x2, 0xe(%rsp)
setne %al
negl %eax
cmpl $-0x1, %eax
je 0x67267
cmpl $-0x2, %eax
jne 0x6738f
orb $0x2, (%rbx)
jmp 0x6746e
movl $0x0, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0x67473
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), %r12
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rbp
subq %rsi, %rbp
movl $0xfeee, %r14d          # imm = 0xFEEE
cmpq %r14, %rbp
cmovbq %rbp, %r14
movq %r12, %rdi
movq %r14, %rdx
callq 0x6570
addq %r14, 0x8(%r15)
cmpq $0xfeed, %rbp           # imm = 0xFEED
ja 0x672ce
movl $0xfeee, %edx           # imm = 0xFEEE
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0x6b053
movq %rax, %r14
movzbl 0x3(%rsp), %r15d
movl $0xa, %eax
testb $0x4, %r15b
movq 0x20(%rsp), %r12
je 0x672ed
movzwl 0xa(%r12), %eax
addl $0xc, %eax
addl $0x12, %r14d
testb $0x8, %r15b
je 0x67315
cmpl %r14d, %eax
jge 0x67313
movl %eax, %eax
movl %r14d, %ecx
cmpb $0x0, (%r12,%rax)
je 0x67313
incq %rax
cmpq %rax, %rcx
jne 0x67301
movl %r14d, %eax
incl %eax
testb $0x10, %r15b
je 0x6733a
cmpl %r14d, %eax
jge 0x67338
movslq %eax, %rcx
addq %r12, %rcx
cmpb $0x0, (%rcx)
je 0x67338
incl %eax
incq %rcx
cmpl %eax, %r14d
jne 0x67326
movl %r14d, %eax
incl %eax
andl $0x2, %r15d
addl %eax, %r15d
movl (%rbx), %eax
subl %r15d, %r14d
jle 0x67464
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rbx)
movl $0x1, %edi
movl $0x70, %esi
callq 0x6470
movq %rax, 0x58(%rbx)
leaq 0x313fb(%rip), %rdx     # 0x98768
movq %rax, %rdi
movl $0xfffffff1, %esi       # imm = 0xFFFFFFF1
movl $0x70, %ecx
callq 0x65d0
testl %eax, %eax
je 0x67484
orb $0x1, (%rbx)
jmp 0x6746e
movzwl 0x10(%rsp), %ecx
movq 0x28(%rbx), %rbp
movzwl 0x10(%rsp), %eax
movw %ax, 0x10(%rbp)
movaps (%rsp), %xmm0
movups %xmm0, (%rbp)
movq %rcx, 0x28(%rsp)
leaq -0x11(%rcx), %r12
movq %r12, 0x30(%rsp)
movq 0x38(%rbx), %rcx
movq %rcx, 0x20(%rsp)
addq $0x12, %rbp
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %r15
subq %rsi, %r15
cmpq %r12, %r15
cmovbq %r15, %r12
movq %rbp, %rdi
movq %r12, %rdx
callq 0x6570
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rcx
addq %r12, 0x8(%rdi)
cmpq %rcx, %r15
jae 0x6740b
movq %rbp, %rsi
movq %rcx, %r15
movq %rcx, %rdx
movq %r12, %rcx
callq 0x6b053
movq %r15, %rcx
movq %rax, %r12
cmpl %r12d, %ecx
jne 0x6746b
movq 0x28(%rsp), %rsi
incl %esi
movq %rbx, %rdi
callq 0x675da
testl %eax, %eax
js 0x6746e
movl %eax, %ebp
cmpl $0x0, 0x8(%rbx)
je 0x67432
movl $0x0, 0xc(%rbx)
addl %r14d, %r12d
movq %r13, 0x10(%rbx)
movl %ebp, 0x8(%rbx)
cmpl $0x0, 0x50(%rbx)
je 0x67454
movq %rbx, %rdi
callq 0x67663
movl %ebp, %eax
movq 0x48(%rbx), %rcx
addq %rax, 0x10(%rcx)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x676ee
jmp 0x67260
orl $0x2, %eax
movl %eax, (%rbx)
jmp 0x6746e
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, %r12
movq %r12, (%rax)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x674c6
testl %eax, %eax
js 0x67387
movl %eax, 0x8(%rbx)
movq %r13, 0x10(%rbx)
xorl %ebp, %ebp
cmpl 0x50(%rbx), %ebp
sbbl %ebp, %ebp
jmp 0x67473
addq $0x10, %r14
movq %r14, %rdi
callq 0x7bab