// File: /workspace/github_cpp_makefile_star2_O3/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), %r14
testl $0x60000000, %eax       # imm = 0x60000000
je 0x42ed7
movq 0x8(%r14), %rsi
movq 0x28(%r14), %r12
addq %rsi, %r12
subq (%r14), %r12
testl %eax, %eax
js 0x42f52
cmpl $0x0, 0x4(%rbx)
je 0x42ff1
movq 0x30(%rbx), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x42f88
leal -0x1(%rdx), %r8d
movq %r12, %r9
shrq $0x21, %r9
movl %r12d, %ecx
shll $0xb, %ecx
xorl %ecx, %r9d
xorl %r12d, %r9d
andl %r8d, %r9d
movq 0x10(%rax), %r10
movl $0x1, %r11d
movl %r9d, %edi
movl %edi, %ecx
shrl $0x4, %ecx
movl (%r10,%rcx,4), %ebp
leal (%rdi,%rdi), %ecx
andl $0x1e, %ecx
movl %ebp, %r15d
shrl %cl, %r15d
testb $0x2, %r15b
jne 0x42f7d
testb $0x1, %r15b
jne 0x42ec4
movq 0x18(%rax), %r15
movl %edi, %r13d
cmpq %r12, (%r15,%r13,8)
je 0x42f7d
addl %r11d, %edi
andl %r8d, %edi
incl %r11d
cmpl %r9d, %edi
jne 0x42e8e
jmp 0x42ff1
movq 0x20(%rbx), %r12
movq 0x8(%r14), %rsi
movq 0x10(%r14), %r13
subq %rsi, %r13
movl $0x10000, %r15d         # imm = 0x10000
cmpq %r15, %r13
cmovbq %r13, %r15
movq %r12, %rdi
movq %r15, %rdx
callq 0x4420
addq %r15, 0x8(%r14)
cmpq $0xffff, %r13           # imm = 0xFFFF
ja 0x42f21
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x4716b
movq %rax, %r15
testl %r15d, %r15d
js 0x43180
je 0x43172
cmpl $0x0, 0x8(%rbx)
je 0x42f3d
movl $0x0, 0xc(%rbx)
movl %r15d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, 0x10(%rbx)
movl %r15d, 0x8(%rbx)
jmp 0x43179
cmpq $0x0, 0x58(%rbx)
je 0x42e4f
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4334e
testl %eax, %eax
js 0x43306
movl %eax, 0x8(%rbx)
movq %r12, 0x10(%rbx)
jmp 0x4330e
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %edx, %edi
jmp 0x42f8a
xorl %edi, %edi
cmpl %edx, %edi
je 0x42ff1
movl %edi, %ecx
leaq (%rcx,%rcx,2), %r14
shlq $0x3, %r14
addq 0x20(%rax), %r14
cmpl $0x0, 0x8(%rbx)
je 0x42fa9
movl $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 0x473bc
testq %rax, %rax
js 0x43342
cmpl $0x0, (%r14)
jne 0x4330e
movq 0x38(%rbx), %r14
movq 0x8(%r14), %rsi
movq 0x10(%r14), %r13
subq %rsi, %r13
cmpq $0x12, %r13
movl $0x12, %r15d
cmovbq %r13, %r15
movq %rsp, %rdi
movq %r15, %rdx
callq 0x4420
addq %r15, 0x8(%r14)
cmpq $0x11, %r13
ja 0x43031
movq %rsp, %rsi
movl $0x12, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x4716b
movq %rax, %r15
testl %r15d, %r15d
je 0x43172
cmpl $0x12, %r15d
jne 0x43188
cmpb $0x1f, (%rsp)
jne 0x43188
cmpb $-0x75, 0x1(%rsp)
jne 0x43188
cmpb $0x8, 0x2(%rsp)
jne 0x43188
testb $0x4, 0x3(%rsp)
je 0x43190
cmpw $0x6, 0xa(%rsp)
jne 0x43190
cmpb $0x42, 0xc(%rsp)
jne 0x43190
cmpb $0x43, 0xd(%rsp)
jne 0x43190
cmpw $0x2, 0xe(%rsp)
jne 0x43190
movzwl 0x10(%rsp), %ecx
movq 0x28(%rbx), %r13
movzwl 0x10(%rsp), %eax
movw %ax, 0x10(%r13)
movaps (%rsp), %xmm0
movups %xmm0, (%r13)
movq %rcx, 0x28(%rsp)
leaq -0x11(%rcx), %rbp
movq %rbp, 0x30(%rsp)
movq 0x38(%rbx), %rcx
movq %rcx, 0x20(%rsp)
addq $0x12, %r13
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %r14
subq %rsi, %r14
cmpq %rbp, %r14
cmovbq %r14, %rbp
movq %r13, %rdi
movq %rbp, %rdx
callq 0x4420
movq 0x20(%rsp), %rdi
addq %rbp, 0x8(%rdi)
movq 0x30(%rsp), %rax
cmpq %rax, %r14
movq %rax, %r14
jae 0x43118
movq %r13, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x4716b
movq %rax, %rbp
cmpl %ebp, %r14d
jne 0x43180
movq 0x28(%rsp), %rsi
incl %esi
movq %rbx, %rdi
callq 0x43475
movl %eax, %ebp
testl %eax, %eax
js 0x432fa
cmpl $0x0, 0x8(%rbx)
je 0x43143
movl $0x0, 0xc(%rbx)
addl %r15d, %r14d
movq %r12, 0x10(%rbx)
movl %ebp, 0x8(%rbx)
cmpl $0x0, 0x50(%rbx)
je 0x43165
movq %rbx, %rdi
callq 0x4356f
movl %ebp, %eax
movq 0x48(%rbx), %rcx
addq %rax, 0x10(%rcx)
movq %rbx, %rdi
movl %r14d, %esi
callq 0x435fa
jmp 0x43179
movl $0x0, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0x4330e
orb $0x4, (%rbx)
jmp 0x43309
orb $0x2, (%rbx)
jmp 0x43309
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), %r13
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rbp
subq %rsi, %rbp
movl $0xfeee, %r14d          # imm = 0xFEEE
cmpq %r14, %rbp
cmovbq %rbp, %r14
movq %r13, %rdi
movq %r14, %rdx
callq 0x4420
addq %r14, 0x8(%r15)
cmpq $0xfeed, %rbp           # imm = 0xFEED
ja 0x431f7
movl $0xfeee, %edx           # imm = 0xFEEE
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0x4716b
movq %rax, %r14
movzbl 0x3(%rsp), %r15d
movl $0xa, %eax
testb $0x4, %r15b
movq 0x20(%rsp), %r13
je 0x43215
movzwl 0xa(%r13), %eax
addl $0xc, %eax
addl $0x12, %r14d
testb $0x8, %r15b
je 0x4323e
cmpl %r14d, %eax
jge 0x4323c
movl %eax, %eax
movl %r14d, %ecx
cmpb $0x0, (%r13,%rax)
je 0x4323c
incq %rax
cmpq %rax, %rcx
jne 0x43229
movl %r14d, %eax
incl %eax
testb $0x10, %r15b
je 0x43263
cmpl %r14d, %eax
jge 0x43261
movslq %eax, %rcx
addq %r13, %rcx
cmpb $0x0, (%rcx)
je 0x43261
incl %eax
incq %rcx
cmpl %eax, %r14d
jne 0x4324f
movl %r14d, %eax
incl %eax
andl $0x2, %r15d
addl %eax, %r15d
movl (%rbx), %eax
subl %r15d, %r14d
jle 0x432c1
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rbx)
movl $0x1, %edi
movl $0x70, %esi
callq 0x4360
movq %rax, 0x58(%rbx)
leaq 0xdd93(%rip), %rdx      # 0x51025
movq %rax, %rdi
movl $0xfffffff1, %esi       # imm = 0xFFFFFFF1
movl $0x70, %ecx
callq 0x4460
testl %eax, %eax
je 0x432c8
leaq 0x1f2a1(%rip), %rcx     # 0x62550
cmpl $0x0, (%rcx)
jle 0x43306
leaq 0x58(%rbx), %rdi
movl %eax, %esi
callq 0x5ac8
jmp 0x43306
orl $0x2, %eax
movl %eax, (%rbx)
jmp 0x43309
movq 0x58(%rbx), %rax
movl %r14d, 0x8(%rax)
movslq %r15d, %rcx
addq %rcx, %r13
movq %r13, (%rax)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4334e
testl %eax, %eax
js 0x43306
movl %eax, 0x8(%rbx)
movq %r12, 0x10(%rbx)
xorl %ebp, %ebp
cmpl 0x50(%rbx), %ebp
sbbl %ebp, %ebp
jmp 0x4330e
leaq 0x1f24f(%rip), %rax     # 0x62550
cmpl $0x2, (%rax)
jge 0x4331f
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 0x1eca2(%rip), %rax     # 0x61fc8
movq (%rax), %rdi
leaq 0x109ee(%rip), %rsi     # 0x53d1e
leaq 0x109d7(%rip), %rdx     # 0x53d0e
movl %ebp, %ecx
xorl %eax, %eax
callq 0x4770
jmp 0x43306
addq $0x10, %r14
movq %r14, %rdi
callq 0x5aa1