// File: /workspace/github_c_makefile_star2_O2/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 $0x98, %rsp
movq %rdi, %rbx
movl (%rdi), %eax
movq 0x38(%rdi), %rdi
testl $0x60000000, %eax       # imm = 0x60000000
je 0x5c2bb
movq 0x28(%rdi), %r12
addq 0x8(%rdi), %r12
subq (%rdi), %r12
pushq $-0x1
popq %r13
testl %eax, %eax
js 0x5c2ea
cmpl $0x0, 0x4(%rbx)
je 0x5c37e
movq 0x30(%rbx), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x5c320
pushq $0x1
popq %r8
leal -0x1(%rdx), %r9d
movq %r12, %r10
shrq $0x21, %r10
movl %r12d, %ecx
shll $0xb, %ecx
xorl %ecx, %r10d
xorl %r12d, %r10d
andl %r9d, %r10d
movq 0x10(%rax), %r11
movl %r10d, %esi
movl %esi, %ecx
shrl $0x4, %ecx
movl (%r11,%rcx,4), %ebp
leal (%rsi,%rsi), %ecx
andl $0x1e, %ecx
movl %ebp, %r14d
shrl %cl, %r14d
testb $0x2, %r14b
jne 0x5c315
testb $0x1, %r14b
jne 0x5c2a8
movq 0x18(%rax), %r14
movl %esi, %r15d
cmpq %r12, (%r14,%r15,8)
je 0x5c315
addl %r8d, %esi
andl %r9d, %esi
incl %r8d
cmpl %r10d, %esi
jne 0x5c276
jmp 0x5c37e
movq 0x20(%rbx), %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x5c8c7
testl %eax, %eax
je 0x5c54d
cmpl $0x0, 0x8(%rbx)
je 0x5c2db
andl $0x0, 0xc(%rbx)
movslq %eax, %rcx
addq %rcx, 0x10(%rbx)
movl %eax, 0x8(%rbx)
jmp 0x5c551
cmpq $0x0, 0x58(%rbx)
je 0x5c239
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5c929
testl %eax, %eax
js 0x5c637
movl %eax, 0x8(%rbx)
movq %r12, 0x10(%rbx)
jmp 0x5c644
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %edx, %esi
jmp 0x5c322
xorl %esi, %esi
cmpl %edx, %esi
je 0x5c37e
movl %esi, %ecx
imulq $0x18, %rcx, %r14
addq 0x20(%rax), %r14
cmpl $0x0, 0x8(%rbx)
je 0x5c33a
andl $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 0x5f9ba
testq %rax, %rax
js 0x5c8a3
cmpl $0x0, (%r14)
jne 0x5c644
movq 0x38(%rbx), %rdi
movq %rsp, %rsi
pushq $0x12
popq %rdx
callq 0x5c8c7
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
andq %rax, %rcx
je 0x5c54d
cmpl $0x12, %ecx
jne 0x5c558
cmpb $0x1f, (%rsp)
jne 0x5c558
cmpb $-0x75, 0x1(%rsp)
jne 0x5c558
cmpb $0x8, 0x2(%rsp)
jne 0x5c558
testb $0x4, 0x3(%rsp)
je 0x5c560
cmpw $0x6, 0xa(%rsp)
jne 0x5c560
cmpb $0x42, 0xc(%rsp)
jne 0x5c560
cmpb $0x43, 0xd(%rsp)
jne 0x5c560
cmpw $0x2, 0xe(%rsp)
jne 0x5c560
movzwl 0x10(%rsp), %ebp
movq 0x28(%rbx), %rsi
movw %bp, 0x10(%rsi)
movdqa (%rsp), %xmm0
movdqu %xmm0, (%rsi)
leaq -0x11(%rbp), %r15
movq 0x38(%rbx), %rdi
addq $0x12, %rsi
movq %r15, %rdx
callq 0x5c8c7
movq %rax, %r14
cmpl %r14d, %r15d
jne 0x5c5cc
pxor %xmm0, %xmm0
leaq 0x28(%rsp), %rdi
movdqu %xmm0, 0x40(%rdi)
movq 0x28(%rbx), %rax
addq $0x12, %rax
movq %rax, (%rdi)
addl $-0xf, %ebp
movl %ebp, 0x8(%rdi)
movq 0x20(%rbx), %rax
movq %rax, 0x18(%rdi)
movl $0x10000, 0x20(%rdi)    # imm = 0x10000
leaq 0x2b326(%rip), %rdx     # 0x8778a
pushq $-0xf
popq %rsi
pushq $0x70
popq %rcx
callq 0x65d0
testl %eax, %eax
jne 0x5c637
leaq 0x28(%rsp), %r15
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0x6540
movl %eax, %ebp
xorl $0x1, %ebp
movq %r15, %rdi
callq 0x6730
orl %ebp, %eax
jne 0x5c637
movq 0x50(%rsp), %r15
movl %r13d, %ebp
testl %r15d, %r15d
js 0x5c644
cmpl $0x0, 0x8(%rbx)
je 0x5c4b7
andl $0x0, 0xc(%rbx)
movq %r12, 0x10(%rbx)
movl %r15d, 0x8(%rbx)
cmpl $0x0, 0x50(%rbx)
je 0x5c4dc
movq %rbx, %rdi
callq 0x5c9cd
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq 0x48(%rbx), %rax
addq %r15, 0x10(%rax)
movl 0x4(%rbx), %ecx
xorl %ebp, %ebp
cmpl $0x10001, %ecx          # imm = 0x10001
jl 0x5c644
movq %r14, 0x20(%rsp)
movq 0x30(%rbx), %r10
movl (%r10), %eax
movl 0x4(%r10), %edx
shll $0x10, %edx
addl $0x10000, %edx          # imm = 0x10000
pushq $0x3
popq %r11
cmpl %ecx, %edx
jbe 0x5c688
xorl %edx, %edx
xorl %r15d, %r15d
cmpl %r15d, %eax
je 0x5c688
movq 0x10(%r10), %rsi
movl %edx, %r13d
andl $0x1e, %r13d
movl %r11d, %r14d
movl %r13d, %ecx
shll %cl, %r14d
movl %r15d, %r12d
shrl $0x4, %r12d
testl %r14d, (%rsi,%r12,4)
je 0x5c775
incl %r15d
addl $0x2, %edx
jmp 0x5c517
andl $0x0, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0x5c644
orb $0x2, (%rbx)
jmp 0x5c641
movq 0x28(%rbx), %r15
movzwl 0x10(%rsp), %eax
movw %ax, 0x10(%r15)
movdqa (%rsp), %xmm0
movdqu %xmm0, (%r15)
movq 0x38(%rbx), %rdi
leaq 0x12(%r15), %rsi
movl $0xfeee, %edx           # imm = 0xFEEE
callq 0x5c8c7
movq %rax, %r14
addl $0x12, %r14d
movzbl 0x3(%rsp), %ebp
testb $0x4, %bpl
jne 0x5c5a1
pushq $0xa
popq %rax
jmp 0x5c5a9
movzwl 0xa(%r15), %eax
addl $0xc, %eax
movslq %r14d, %rcx
testb $0x8, %bpl
je 0x5c5d5
movl %eax, %edx
cmpl %r14d, %eax
cmovlel %r14d, %eax
cmpq %rcx, %rdx
jge 0x5c5d3
cmpb $0x0, (%r15,%rdx)
je 0x5c5d1
incq %rdx
jmp 0x5c5bb
orb $0x4, (%rbx)
jmp 0x5c641
movl %edx, %eax
incl %eax
testb $0x10, %bpl
je 0x5c5fc
movslq %eax, %rdx
cmpl %r14d, %edx
movl %r14d, %eax
cmovgl %edx, %eax
cmpq %rcx, %rdx
jge 0x5c5fa
cmpb $0x0, (%r15,%rdx)
je 0x5c5f8
incq %rdx
jmp 0x5c5e7
movl %edx, %eax
incl %eax
andl $0x2, %ebp
addl %eax, %ebp
movl (%rbx), %eax
subl %ebp, %r14d
jle 0x5c63c
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rbx)
pushq $0x1
popq %rdi
pushq $0x70
popq %rsi
callq 0x6470
movq %rax, 0x58(%rbx)
leaq 0x2b165(%rip), %rdx     # 0x8778a
pushq $-0xf
popq %rsi
pushq $0x70
popq %rcx
movq %rax, %rdi
callq 0x65d0
testl %eax, %eax
je 0x5c658
orb $0x1, (%rbx)
jmp 0x5c641
orl $0x2, %eax
movl %eax, (%rbx)
movl %r13d, %ebp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x58(%rbx), %rax
movl %r14d, 0x8(%rax)
movslq %ebp, %rcx
addq %rcx, %r15
movq %r15, (%rax)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x5c929
testl %eax, %eax
js 0x5c637
movl %eax, 0x8(%rbx)
movq %r12, 0x10(%rbx)
xorl %ebp, %ebp
cmpl 0x50(%rbx), %ebp
sbbl %ebp, %ebp
jmp 0x5c644
movl %eax, %r15d
movq 0x10(%rbx), %r13
movl 0x8(%r10), %eax
cmpl 0xc(%r10), %eax
jb 0x5c6d1
movl %r11d, %r12d
movl 0x4(%r10), %eax
addl %eax, %eax
cmpl %eax, %r15d
jbe 0x5c6ac
decl %r15d
jmp 0x5c6af
incl %r15d
movq %r10, %rdi
movl %r15d, %esi
movq %r10, %r14
callq 0x5dc8d
movq %r14, %r10
movl (%r14), %r15d
movl %r15d, %edx
testl %eax, %eax
movl %r12d, %r11d
js 0x5c85a
leal -0x1(%r15), %edi
movq %r13, %rsi
shrq $0x21, %rsi
movl %r13d, %eax
shll $0xb, %eax
xorl %eax, %esi
xorl %r13d, %esi
andl %edi, %esi
movq 0x10(%r10), %rax
movl %esi, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %edx
leal (%rsi,%rsi), %ecx
shrl %cl, %edx
testb $0x2, %dl
je 0x5c706
movl %esi, %edx
jmp 0x5c7ef
movq %r10, %r12
pushq $0x1
popq %r9
movl %esi, %r8d
movl %r15d, %edx
movl %r8d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %r10d
leal (%r8,%r8), %ecx
andl $0x1e, %ecx
movl %r10d, %r11d
shrl %cl, %r11d
testb $0x2, %r11b
jne 0x5c7cc
testb $0x1, %r11b
jne 0x5c74c
movq 0x18(%r12), %r11
movl %r8d, %r14d
cmpq %r13, (%r11,%r14,8)
je 0x5c7cc
btl %ecx, %r10d
cmovbl %r8d, %edx
addl %r9d, %r8d
andl %edi, %r8d
incl %r9d
cmpl %esi, %r8d
jne 0x5c713
movb $0x1, %cl
movl %r15d, %edi
cmpl %r15d, %edx
movq %r12, %r10
pushq $0x3
popq %r11
je 0x5c7e2
jmp 0x5c7ef
movq 0x20(%r10), %rax
movl %r15d, %ecx
imulq $0x18, %rcx, %rcx
movq 0x8(%rax,%rcx), %rdi
movq %r10, 0x18(%rsp)
callq 0x68a0
pushq $0x3
popq %r11
movq 0x18(%rsp), %r10
movl (%r10), %edx
cmpl %r15d, %edx
je 0x5c68b
movq 0x10(%r10), %rax
movl (%rax,%r12,4), %ecx
movl %edx, %r15d
testl %r14d, %ecx
jne 0x5c68b
btsl %r13d, %ecx
movl %ecx, (%rax,%r12,4)
decl 0x4(%r10)
movl (%r10), %r15d
jmp 0x5c68b
shrl %cl, %r10d
testb $0x2, %r10b
sete %cl
movl %edx, %edi
movl %r8d, %esi
movq %r12, %r10
pushq $0x3
popq %r11
cmpl %r15d, %edi
cmovel %esi, %edi
testb %cl, %cl
cmovnel %esi, %edi
movl %edi, %edx
movl %edx, %esi
shrl $0x4, %esi
movl (%rax,%rsi,4), %edi
leal (%rdx,%rdx), %ecx
andl $0x1e, %ecx
movl %edi, %r8d
shrl %cl, %r8d
testb $0x2, %r8b
jne 0x5c82f
testb $0x1, %r8b
je 0x5c644
movq 0x18(%r10), %r8
shll %cl, %r11d
movl %edx, %ecx
movq %r13, (%r8,%rcx,8)
notl %r11d
andl %r11d, %edi
movl %edi, (%rax,%rsi,4)
incl 0x4(%r10)
jmp 0x5c85a
movq 0x18(%r10), %r8
movl %edx, %r9d
shll %cl, %r11d
movq %r13, (%r8,%r9,8)
notl %r11d
andl %r11d, %edi
movl %edi, (%rax,%rsi,4)
movq 0x4(%r10), %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x4(%r10)
movq 0x20(%r10), %r14
movl %edx, %eax
imulq $0x18, %rax, %r15
movl 0x8(%rbx), %eax
movl %eax, (%r14,%r15)
movq 0x20(%rsp), %rax
addl $0x12, %eax
cltq
addq 0x10(%rbx), %rax
movq %rax, 0x10(%r14,%r15)
movl $0x10000, %edi          # imm = 0x10000
callq 0x69d0
movq %rax, 0x8(%r14,%r15)
movq 0x20(%rbx), %rsi
movl $0x10000, %edx          # imm = 0x10000
movq %rax, %rdi
callq 0x6570
jmp 0x5c644
movq 0x5672e(%rip), %rax     # 0xb2fd8
movq (%rax), %rdi
movq 0x10(%r14), %rdx
leaq 0x2c2ed(%rip), %rsi     # 0x88ba5
xorl %eax, %eax
callq 0x6a90
pushq $0x1
popq %rdi
callq 0x68d0