// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rdi, %rbx
movl (%rdi), %eax
movq 0x38(%rdi), %r15
testl $0x60000000, %eax       # imm = 0x60000000
je 0x43ede
movq 0x28(%r15), %r13
addq 0x8(%r15), %r13
subq (%r15), %r13
testl %eax, %eax
js 0x43f59
cmpl $0x0, 0x4(%rbx)
je 0x43ff1
movq 0x30(%rbx), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x43f90
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 0x43f84
testb $0x1, %bpl
jne 0x43eca
movq 0x18(%rax), %r14
movl %esi, %r15d
cmpq %r13, (%r14,%r15,8)
je 0x43f84
addl %r10d, %esi
andl %edi, %esi
incl %r10d
cmpl %r8d, %esi
jne 0x43e95
movl %edx, %esi
jmp 0x43f92
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 0x4420
addq %r14, 0x8(%r15)
cmpq $0xffff, %r13           # imm = 0xFFFF
ja 0x43f28
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0x48187
movq %rax, %r14
testl %r14d, %r14d
js 0x442c5
je 0x440a2
cmpl $0x0, 0x8(%rbx)
je 0x43f44
movl $0x0, 0xc(%rbx)
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, 0x10(%rbx)
movl %r14d, 0x8(%rbx)
jmp 0x440a9
cmpq $0x0, 0x58(%rbx)
je 0x43e57
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x44357
testl %eax, %eax
js 0x4430f
movl %eax, 0x8(%rbx)
movq %r13, 0x10(%rbx)
jmp 0x44317
shrl %cl, %r11d
testb $0x3, %r11b
cmovnel %edx, %esi
jmp 0x43f92
xorl %esi, %esi
cmpl %edx, %esi
je 0x43ff1
movl %esi, %ecx
leaq (%rcx,%rcx,2), %r14
shlq $0x3, %r14
addq 0x20(%rax), %r14
cmpl $0x0, 0x8(%rbx)
je 0x43fb1
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 0x4420
movq 0x38(%rbx), %rdi
movq 0x10(%r14), %rsi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x48413
testq %rax, %rax
js 0x4434b
cmpl $0x0, (%r14)
jne 0x44317
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 0x4420
addq %r14, 0x8(%r15)
cmpq $0x11, %r12
ja 0x44039
movq %rsp, %rsi
movl $0x12, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x48187
movq %rax, %r14
testl %r14d, %r14d
je 0x440a2
cmpl $0x12, %r14d
jne 0x4409a
cmpb $0x1f, (%rsp)
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jne 0x4408c
cmpb $-0x75, 0x1(%rsp)
jne 0x4408c
cmpb $0x8, 0x2(%rsp)
jne 0x4408c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x4, 0x3(%rsp)
je 0x4408c
cmpw $0x6, 0xa(%rsp)
jne 0x4408c
cmpb $0x42, 0xc(%rsp)
jne 0x4408c
cmpb $0x43, 0xd(%rsp)
jne 0x4408c
xorl %eax, %eax
cmpw $0x2, 0xe(%rsp)
setne %al
negl %eax
cmpl $-0x1, %eax
je 0x440b0
cmpl $-0x2, %eax
jne 0x441f0
orb $0x2, (%rbx)
jmp 0x44312
movl $0x0, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0x44317
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 0x4420
addq %r14, 0x8(%r15)
cmpq $0xfeed, %rbp           # imm = 0xFEED
ja 0x44117
movl $0xfeee, %edx           # imm = 0xFEEE
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0x48187
movq %rax, %r14
movzbl 0x3(%rsp), %r15d
movl $0xa, %eax
testb $0x4, %r15b
movq 0x20(%rsp), %r12
je 0x44136
movzwl 0xa(%r12), %eax
addl $0xc, %eax
addl $0x12, %r14d
testb $0x8, %r15b
je 0x4415e
cmpl %r14d, %eax
jge 0x4415c
movl %eax, %eax
movl %r14d, %ecx
cmpb $0x0, (%r12,%rax)
je 0x4415c
incq %rax
cmpq %rax, %rcx
jne 0x4414a
movl %r14d, %eax
incl %eax
testb $0x10, %r15b
je 0x44183
cmpl %r14d, %eax
jge 0x44181
movslq %eax, %rcx
addq %r12, %rcx
cmpb $0x0, (%rcx)
je 0x44181
incl %eax
incq %rcx
cmpl %eax, %r14d
jne 0x4416f
movl %r14d, %eax
incl %eax
andl $0x2, %r15d
addl %eax, %r15d
movl (%rbx), %eax
subl %r15d, %r14d
jle 0x442ca
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rbx)
movl $0x1, %edi
movl $0x70, %esi
callq 0x4360
movq %rax, 0x58(%rbx)
leaq 0xee3d(%rip), %rdx      # 0x52ff3
movq %rax, %rdi
movl $0xfffffff1, %esi       # imm = 0xFFFFFFF1
movl $0x70, %ecx
callq 0x4460
testl %eax, %eax
je 0x442d1
leaq 0x20379(%rip), %rcx     # 0x64550
cmpl $0x0, (%rcx)
jle 0x4430f
leaq 0x58(%rbx), %rdi
movl %eax, %esi
callq 0x5b7f
jmp 0x4430f
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 0x4420
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rcx
addq %r12, 0x8(%rdi)
cmpq %rcx, %r15
jae 0x4426c
movq %rbp, %rsi
movq %rcx, %r15
movq %rcx, %rdx
movq %r12, %rcx
callq 0x48187
movq %r15, %rcx
movq %rax, %r12
cmpl %r12d, %ecx
jne 0x442c5
movq 0x28(%rsp), %rsi
incl %esi
movq %rbx, %rdi
callq 0x4448f
movl %eax, %ebp
testl %eax, %eax
js 0x44303
cmpl $0x0, 0x8(%rbx)
je 0x44293
movl $0x0, 0xc(%rbx)
addl %r14d, %r12d
movq %r13, 0x10(%rbx)
movl %ebp, 0x8(%rbx)
cmpl $0x0, 0x50(%rbx)
je 0x442b5
movq %rbx, %rdi
callq 0x44589
movl %ebp, %eax
movq 0x48(%rbx), %rcx
addq %rax, 0x10(%rcx)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x44614
jmp 0x440a9
orb $0x4, (%rbx)
jmp 0x44312
orl $0x2, %eax
movl %eax, (%rbx)
jmp 0x44312
movq 0x58(%rbx), %rax
movl %r14d, 0x8(%rax)
movslq %r15d, %rcx
addq %rcx, %r12
movq %r12, (%rax)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x44357
testl %eax, %eax
js 0x4430f
movl %eax, 0x8(%rbx)
movq %r13, 0x10(%rbx)
xorl %ebp, %ebp
cmpl 0x50(%rbx), %ebp
sbbl %ebp, %ebp
jmp 0x44317
leaq 0x20246(%rip), %rax     # 0x64550
cmpl $0x2, (%rax)
jge 0x44328
orb $0x1, (%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 0x1fc99(%rip), %rax     # 0x63fc8
movq (%rax), %rdi
leaq 0x11725(%rip), %rsi     # 0x55a5e
leaq 0x1170e(%rip), %rdx     # 0x55a4e
movl %ebp, %ecx
xorl %eax, %eax
callq 0x4770
jmp 0x4430f
addq $0x10, %r14
movq %r14, %rdi
callq 0x5b58