// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movl (%rdi), %eax
movq 0x38(%rdi), %rdi
testl $0x60000000, %eax       # imm = 0x60000000
je 0x366b0
movq 0x28(%rdi), %r12
addq 0x8(%rdi), %r12
subq (%rdi), %r12
testl %eax, %eax
js 0x366ea
cmpl $0x0, 0x4(%rbx)
je 0x3677e
movq 0x30(%rbx), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x36720
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 0x36715
testb $0x1, %r14b
jne 0x3669d
movq 0x18(%rax), %r14
movl %esi, %r15d
cmpq %r12, (%r14,%r15,8)
je 0x36715
addl %r8d, %esi
andl %r9d, %esi
incl %r8d
cmpl %r10d, %esi
jne 0x3666b
jmp 0x3677e
movq 0x20(%rbx), %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x36e62
testl %eax, %eax
js 0x368dd
je 0x368d2
cmpl $0x0, 0x8(%rbx)
je 0x366d6
andl $0x0, 0xc(%rbx)
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, 0x10(%rbx)
movl %eax, 0x8(%rbx)
jmp 0x368d6
cmpq $0x0, 0x58(%rbx)
je 0x3662e
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x36ec4
testl %eax, %eax
js 0x369d0
movl %eax, 0x8(%rbx)
movq %r12, 0x10(%rbx)
jmp 0x369dd
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %edx, %esi
jmp 0x36722
xorl %esi, %esi
cmpl %edx, %esi
je 0x3677e
movl %esi, %ecx
imulq $0x18, %rcx, %r14
addq 0x20(%rax), %r14
cmpl $0x0, 0x8(%rbx)
je 0x3673a
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 0x4420
movq 0x38(%rbx), %rdi
movq 0x10(%r14), %rsi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x3a30a
testq %rax, %rax
js 0x36e3e
cmpl $0x0, (%r14)
jne 0x369dd
movq 0x38(%rbx), %rdi
leaq 0x10(%rsp), %rsi
pushq $0x12
popq %rdx
callq 0x36e62
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
andq %rax, %rcx
je 0x368d2
cmpl $0x12, %ecx
jne 0x368e5
cmpb $0x1f, 0x10(%rsp)
jne 0x368e5
cmpb $-0x75, 0x11(%rsp)
jne 0x368e5
cmpb $0x8, 0x12(%rsp)
jne 0x368e5
testb $0x4, 0x13(%rsp)
je 0x368ed
cmpw $0x6, 0x1a(%rsp)
jne 0x368ed
cmpb $0x42, 0x1c(%rsp)
jne 0x368ed
cmpb $0x43, 0x1d(%rsp)
jne 0x368ed
cmpw $0x2, 0x1e(%rsp)
jne 0x368ed
movzwl 0x20(%rsp), %r13d
movq 0x28(%rbx), %rsi
movw %r13w, 0x10(%rsi)
movdqa 0x10(%rsp), %xmm0
movdqu %xmm0, (%rsi)
leaq -0x11(%r13), %r15
movq 0x38(%rbx), %rdi
addq $0x12, %rsi
movq %r15, %rdx
callq 0x36e62
movq %rax, %r14
cmpl %r14d, %r15d
jne 0x368dd
leaq 0x38(%rsp), %rdi
andq $0x0, 0x30(%rdi)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x40(%rdi)
movq 0x28(%rbx), %rax
addq $0x12, %rax
movq %rax, (%rdi)
addl $-0xf, %r13d
movl %r13d, 0x8(%rdi)
movq 0x20(%rbx), %rax
movq %rax, 0x18(%rdi)
movl $0x10000, 0x20(%rdi)    # imm = 0x10000
leaq 0xd7f2(%rip), %rdx      # 0x44063
pushq $-0xf
popq %rsi
pushq $0x70
popq %rcx
callq 0x4460
testl %eax, %eax
je 0x36a21
leaq 0x20cbd(%rip), %rcx     # 0x57548
cmpl $0x0, (%rcx)
jg 0x36b82
orb $0x1, (%rbx)
pushq $-0x1
popq %r15
leaq 0x20ca6(%rip), %rax     # 0x57548
cmpl $0x2, (%rax)
jl 0x369d0
movq 0x2071e(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0x101b2(%rip), %rsi     # 0x46a6e
leaq 0x1019b(%rip), %rdx     # 0x46a5e
movl %r15d, %ecx
xorl %eax, %eax
callq 0x4770
jmp 0x369d0
andl $0x0, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0x369dd
orb $0x4, (%rbx)
jmp 0x369da
orb $0x2, (%rbx)
jmp 0x369da
movq 0x28(%rbx), %r15
movzwl 0x20(%rsp), %eax
movw %ax, 0x10(%r15)
movdqa 0x10(%rsp), %xmm0
movdqu %xmm0, (%r15)
movq 0x38(%rbx), %rdi
leaq 0x12(%r15), %rsi
movl $0xfeee, %edx           # imm = 0xFEEE
callq 0x36e62
movq %rax, %r14
addl $0x12, %r14d
movzbl 0x13(%rsp), %ebp
testb $0x4, %bpl
jne 0x3692f
pushq $0xa
popq %rax
jmp 0x36937
movzwl 0xa(%r15), %eax
addl $0xc, %eax
movslq %r14d, %rcx
testb $0x8, %bpl
je 0x3695e
movl %eax, %edx
cmpl %r14d, %eax
cmovlel %r14d, %eax
cmpq %rcx, %rdx
jge 0x3695c
cmpb $0x0, (%r15,%rdx)
je 0x3695a
incq %rdx
jmp 0x36949
movl %edx, %eax
incl %eax
testb $0x10, %bpl
je 0x36985
movslq %eax, %rdx
cmpl %r14d, %edx
movl %r14d, %eax
cmovgl %edx, %eax
cmpq %rcx, %rdx
jge 0x36983
cmpb $0x0, (%r15,%rdx)
je 0x36981
incq %rdx
jmp 0x36970
movl %edx, %eax
incl %eax
andl $0x2, %ebp
addl %eax, %ebp
movl (%rbx), %eax
subl %ebp, %r14d
jle 0x369d5
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rbx)
pushq $0x1
popq %rdi
pushq $0x70
popq %rsi
callq 0x4370
movq %rax, 0x58(%rbx)
leaq 0xd6b5(%rip), %rdx      # 0x44063
pushq $-0xf
popq %rsi
pushq $0x70
popq %rcx
movq %rax, %rdi
callq 0x4460
testl %eax, %eax
je 0x369f1
leaq 0x20b81(%rip), %rcx     # 0x57548
cmpl $0x0, (%rcx)
jg 0x36b4d
orb $0x1, (%rbx)
jmp 0x369da
orl $0x2, %eax
movl %eax, (%rbx)
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0xa8, %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 0x36ec4
testl %eax, %eax
js 0x369d0
movl %eax, 0x8(%rbx)
movq %r12, 0x10(%rbx)
xorl %ebp, %ebp
cmpl 0x50(%rbx), %ebp
sbbl %ebp, %ebp
jmp 0x369dd
leaq 0x38(%rsp), %r15
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0x4410
cmpl $0x1, %eax
jne 0x36a56
leaq 0x38(%rsp), %rdi
callq 0x4560
testl %eax, %eax
je 0x36aa4
leaq 0x20afd(%rip), %rcx     # 0x57548
cmpl $0x0, (%rcx)
jle 0x36894
jmp 0x36a85
leaq 0x20aeb(%rip), %r12     # 0x57548
cmpl $0x0, (%r12)
jg 0x36bde
leaq 0x38(%rsp), %rdi
callq 0x4560
testl %eax, %eax
je 0x36894
cmpl $0x2, (%r12)
jl 0x36894
movq 0x20544(%rip), %rcx     # 0x56fd0
movq (%rcx), %r14
movl %eax, %edi
xorl %esi, %esi
callq 0x36571
leaq 0x103e0(%rip), %rsi     # 0x46e7f
jmp 0x36b9f
movq 0x60(%rsp), %r15
testl %r15d, %r15d
js 0x3689b
cmpl $0x0, 0x8(%rbx)
je 0x36abc
andl $0x0, 0xc(%rbx)
movq %r12, 0x10(%rbx)
movl %r15d, 0x8(%rbx)
cmpl $0x0, 0x50(%rbx)
je 0x36ae1
movq %rbx, %rdi
callq 0x36fc0
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 0x369dd
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 0x36bb8
xorl %edx, %edx
xorl %r8d, %r8d
cmpl %r8d, %eax
je 0x36bb8
movq 0x10(%r10), %rsi
movl %edx, %r13d
andl $0x1e, %r13d
movl %r11d, %r15d
movl %r13d, %ecx
shll %cl, %r15d
movl %r8d, %r12d
shrl $0x4, %r12d
testl %r15d, (%rsi,%r12,4)
je 0x36cfb
incl %r8d
addl $0x2, %edx
jmp 0x36b17
movq 0x2047c(%rip), %rcx     # 0x56fd0
movq (%rcx), %r14
movq 0x58(%rbx), %rsi
movl %eax, %edi
callq 0x36571
leaq 0xfed5(%rip), %rsi      # 0x46a3e
leaq 0xfeee(%rip), %rdx      # 0x46a5e
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x4770
jmp 0x369d0
movq 0x20447(%rip), %rcx     # 0x56fd0
movq (%rcx), %r14
leaq 0x38(%rsp), %rsi
movl %eax, %edi
callq 0x36571
leaq 0x102b1(%rip), %rsi     # 0x46e50
leaq 0x102cb(%rip), %rdx     # 0x46e71
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x4770
jmp 0x36894
movl %eax, %esi
movq 0x10(%rbx), %rax
movq %rax, 0x8(%rsp)
movl 0x8(%r10), %eax
cmpl 0xc(%r10), %eax
jb 0x36c3b
movl %r11d, %r12d
movl 0x4(%r10), %eax
addl %eax, %eax
cmpl %eax, %esi
jbe 0x36c1b
decl %esi
jmp 0x36c1d
movq 0x203eb(%rip), %rcx     # 0x56fd0
movq (%rcx), %r14
xorl %ecx, %ecx
cmpl $-0x3, %eax
cmovneq %rcx, %r15
movl %eax, %edi
movq %r15, %rsi
callq 0x36571
leaq 0x10207(%rip), %rsi     # 0x46e09
leaq 0x10268(%rip), %rdx     # 0x46e71
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x4770
jmp 0x36a68
incl %esi
movq %r10, %rdi
movq %r10, %r15
callq 0x384b0
movq %r15, %r10
movl (%r15), %esi
movl %esi, %edx
testl %eax, %eax
movl %r12d, %r11d
js 0x36df8
leal -0x1(%rsi), %edi
movq 0x8(%rsp), %rcx
movq %rcx, %r13
shrq $0x21, %r13
movl %ecx, %eax
shll $0xb, %eax
xorl %eax, %r13d
xorl %ecx, %r13d
andl %edi, %r13d
movq 0x10(%r10), %rax
movl %r13d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %edx
leal (,%r13,2), %ecx
shrl %cl, %edx
testb $0x2, %dl
je 0x36c7c
movl %r13d, %edx
jmp 0x36d83
movq %r10, %r12
pushq $0x1
popq %r9
movq %rsi, %rdx
movl %r13d, %r8d
movq %rsi, 0x30(%rsp)
movq 0x8(%rsp), %rsi
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 0x36d5a
testb $0x1, %r11b
jne 0x36ccc
movq 0x18(%r12), %r11
movl %r8d, %r15d
cmpq %rsi, (%r11,%r15,8)
je 0x36d5a
btl %ecx, %r10d
cmovbl %r8d, %edx
addl %r9d, %r8d
andl %edi, %r8d
incl %r9d
cmpl %r13d, %r8d
jne 0x36c93
movb $0x1, %cl
movq 0x30(%rsp), %rsi
movl %esi, %edi
cmpl %esi, %edx
movq %r12, %r10
pushq $0x3
popq %r11
je 0x36d75
jmp 0x36d83
movq 0x20(%r10), %rax
movl %r8d, %ecx
imulq $0x18, %rcx, %rcx
movq 0x8(%rax,%rcx), %rdi
movq %r10, 0x8(%rsp)
movq %r8, 0x30(%rsp)
callq 0x4600
movq 0x30(%rsp), %rsi
pushq $0x3
popq %r11
movq 0x8(%rsp), %r10
movl (%r10), %edx
cmpl %esi, %edx
je 0x36bba
movq 0x10(%r10), %rax
movl (%rax,%r12,4), %ecx
movl %edx, %esi
testl %r15d, %ecx
jne 0x36bba
btsl %r13d, %ecx
movl %ecx, (%rax,%r12,4)
decl 0x4(%r10)
movl (%r10), %esi
jmp 0x36bba
shrl %cl, %r10d
testb $0x2, %r10b
sete %cl
movl %edx, %edi
movl %r8d, %r13d
movq %r12, %r10
pushq $0x3
popq %r11
movq 0x30(%rsp), %rsi
cmpl %esi, %edi
cmovel %r13d, %edi
testb %cl, %cl
cmovnel %r13d, %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 0x36dc8
testb $0x1, %r8b
je 0x369dd
movq 0x18(%r10), %r8
shll %cl, %r11d
movl %edx, %ecx
movq 0x8(%rsp), %r9
movq %r9, (%r8,%rcx,8)
notl %r11d
andl %r11d, %edi
movl %edi, (%rax,%rsi,4)
incl 0x4(%r10)
jmp 0x36df8
movq 0x18(%r10), %r8
movl %edx, %r9d
shll %cl, %r11d
movq 0x8(%rsp), %rcx
movq %rcx, (%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), %r15
movl %edx, %eax
imulq $0x18, %rax, %r12
movl 0x8(%rbx), %eax
movl %eax, (%r15,%r12)
addl $0x12, %r14d
movslq %r14d, %rax
addq 0x10(%rbx), %rax
movq %rax, 0x10(%r15,%r12)
movl $0x10000, %edi          # imm = 0x10000
callq 0x46f0
movq %rax, 0x8(%r15,%r12)
movq 0x20(%rbx), %rsi
movl $0x10000, %edx          # imm = 0x10000
movq %rax, %rdi
callq 0x4420
jmp 0x369dd
movq 0x2018b(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
movq 0x10(%r14), %rdx
leaq 0xffe5(%rip), %rsi      # 0x46e38
xorl %eax, %eax
callq 0x4770
pushq $0x1
popq %rdi
callq 0x4620