// File: karel-brinda[P]ococo/ext/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)  // Error
        {
            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 += 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)
        {
            if (hts_verbose >= 1) {
                fprintf(stderr, "[E::%s] inflateInit2 failed: %s",
                        __func__, bgzf_zerr(ret, fp->gz_stream));
            }
            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) {
        if (hts_verbose >= 2) fprintf(stderr, "[E::%s] inflate_block error %d\n", __func__, count);
        fp->errcode |= BGZF_ERR_ZLIB;
        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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x1d, %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x63b75
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x64100
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x63b1b
movq -0x10(%rbp), %rax
movl (%rax), %edx
andl $0xffff, %edx           # imm = 0xFFFF
orl $0x4, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x640e9
cmpl $0x0, -0x3c(%rbp)
jne 0x63b38
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x640e9
jmp 0x63b3a
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x63b4f
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movslq -0x3c(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movl -0x3c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x640e9
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x641a0
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x63c02
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x63c02
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x641d0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x63be0
movq -0x10(%rbp), %rax
movl (%rax), %edx
andl $0xffff, %edx           # imm = 0xFFFF
orl $0x1, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x640e9
movl -0x3c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x640e9
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x63c2a
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x643d0
cmpl $0x0, %eax
je 0x63c2a
movl $0x0, -0x4(%rbp)
jmp 0x640e9
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
leaq -0x30(%rbp), %rsi
movl $0x12, %edx
callq 0x64100
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x63c60
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x640e9
movslq -0x3c(%rbp), %rax
cmpq $0x12, %rax
jne 0x63c7b
leaq -0x30(%rbp), %rdi
callq 0x644d0
movl %eax, -0x54(%rbp)
cmpl $-0x2, %eax
jne 0x63ca8
movq -0x10(%rbp), %rax
movl (%rax), %edx
andl $0xffff, %edx           # imm = 0xFFFF
orl $0x2, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x640e9
cmpl $-0x1, -0x54(%rbp)
jne 0x63f75
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x8(%rax)
movw -0x20(%rbp), %cx
movw %cx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x60(%rbp), %rsi
addq $0x12, %rsi
movl $0xfeee, %edx           # imm = 0xFEEE
callq 0x64100
addq $0x12, %rax
movl %eax, -0x3c(%rbp)
movl $0xa, -0x64(%rbp)
movzbl -0x2d(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x63d26
movq -0x60(%rbp), %rdi
movslq -0x64(%rbp), %rax
addq %rax, %rdi
callq 0x645a0
addl $0x2, %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
movzbl -0x2d(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x63d73
jmp 0x63d34
movl -0x64(%rbp), %ecx
xorl %eax, %eax
cmpl -0x3c(%rbp), %ecx
movb %al, -0x69(%rbp)
jge 0x63d56
movq -0x60(%rbp), %rax
movslq -0x64(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x63d5f
jmp 0x63d6a
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x63d34
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movzbl -0x2d(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x63dc0
jmp 0x63d81
movl -0x64(%rbp), %ecx
xorl %eax, %eax
cmpl -0x3c(%rbp), %ecx
movb %al, -0x6a(%rbp)
jge 0x63da3
movq -0x60(%rbp), %rax
movslq -0x64(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x6a(%rbp)
movb -0x6a(%rbp), %al
testb $0x1, %al
jne 0x63dac
jmp 0x63db7
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x63d81
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movzbl -0x2d(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x63dd5
movl -0x64(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jl 0x63e0a
movq -0x10(%rbp), %rax
movl (%rax), %edx
andl $0xffff, %edx           # imm = 0xFFFF
orl $0x2, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x640e9
movq -0x10(%rbp), %rax
movl (%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, (%rax)
movl $0x1, %edi
movl $0x70, %esi
callq 0x4370
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movl $0xfffffff1, %esi       # imm = 0xFFFFFFF1
leaq 0x1735e(%rip), %rdx     # 0x7b1aa
movl $0x70, %ecx
callq 0x44b0
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0x63ed2
leaq 0x2677a(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x63ea5
movq 0x2615e(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movl -0x68(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rsi
callq 0x63940
movq -0x78(%rbp), %rdi
movq %rax, %rcx
leaq 0x1a067(%rip), %rsi     # 0x7defe
leaq 0x1a080(%rip), %rdx     # 0x7df1e
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rax
movl (%rax), %edx
andl $0xffff, %edx           # imm = 0xFFFF
orl $0x1, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x640e9
movl -0x3c(%rbp), %ecx
subl -0x64(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl %ecx, 0x8(%rax)
movq -0x60(%rbp), %rcx
movslq -0x64(%rbp), %rax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x641d0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x63f3d
movq -0x10(%rbp), %rax
movl (%rax), %edx
andl $0xffff, %edx           # imm = 0xFFFF
orl $0x1, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x640e9
movl -0x3c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x50(%rax)
je 0x63f69
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x640e9
movl $0x0, -0x4(%rbp)
jmp 0x640e9
movl -0x3c(%rbp), %eax
movl %eax, -0x40(%rbp)
leaq -0x30(%rbp), %rdi
addq $0x10, %rdi
callq 0x645a0
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x8(%rax)
movw -0x20(%rbp), %cx
movw %cx, 0x10(%rax)
movl -0x44(%rbp), %eax
subl $0x12, %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x12, %rsi
movslq -0x48(%rbp), %rdx
callq 0x64100
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x48(%rbp), %eax
je 0x6400f
movq -0x10(%rbp), %rax
movl (%rax), %edx
andl $0xffff, %edx           # imm = 0xFFFF
orl $0x4, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x640e9
movl -0x3c(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x645c0
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
jge 0x64084
leaq 0x265ad(%rip), %rax     # 0x8a5e0
cmpl $0x2, (%rax)
jl 0x6405a
movq 0x25f91(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x3c(%rbp), %ecx
leaq 0x19ee2(%rip), %rsi     # 0x7df2e
leaq 0x19ecb(%rip), %rdx     # 0x7df1e
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rax
movl (%rax), %edx
andl $0xffff, %edx           # imm = 0xFFFF
orl $0x1, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x640e9
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x64099
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x3c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x50(%rax)
je 0x640d6
movq -0x10(%rbp), %rdi
callq 0x64890
movslq -0x3c(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x64a50
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)