// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/bamtools/src/api/internal/io/BgzfStream_p.cpp
// Function: BamTools::Internal::BgzfStream::DeflateBlock(int)
size_t BgzfStream::DeflateBlock(int32_t blockLength) {

    // initialize the gzip header
    char* buffer = m_compressedBlock.Buffer;
    memset(buffer, 0, 18);
    buffer[0]  = Constants::GZIP_ID1;
    buffer[1]  = Constants::GZIP_ID2;
    buffer[2]  = Constants::CM_DEFLATE;
    buffer[3]  = Constants::FLG_FEXTRA;
    buffer[9]  = Constants::OS_UNKNOWN;
    buffer[10] = Constants::BGZF_XLEN;
    buffer[12] = Constants::BGZF_ID1;
    buffer[13] = Constants::BGZF_ID2;
    buffer[14] = Constants::BGZF_LEN;

    // set compression level
    const int compressionLevel = ( m_isWriteCompressed ? Z_DEFAULT_COMPRESSION : 0 );

    // loop to retry for blocks that do not compress enough
    int inputLength = blockLength;
    size_t compressedLength = 0;
    const unsigned int bufferSize = Constants::BGZF_MAX_BLOCK_SIZE;

    while ( true ) {

        // initialize zstream values
        z_stream zs;
        zs.zalloc    = NULL;
        zs.zfree     = NULL;
        zs.next_in   = (Bytef*)m_uncompressedBlock.Buffer;
        zs.avail_in  = inputLength;
        zs.next_out  = (Bytef*)&buffer[Constants::BGZF_BLOCK_HEADER_LENGTH];
        zs.avail_out = bufferSize -
                       Constants::BGZF_BLOCK_HEADER_LENGTH -
                       Constants::BGZF_BLOCK_FOOTER_LENGTH;

        // initialize the zlib compression algorithm
        int status = deflateInit2(&zs,
                                  compressionLevel,
                                  Z_DEFLATED,
                                  Constants::GZIP_WINDOW_BITS,
                                  Constants::Z_DEFAULT_MEM_LEVEL,
                                  Z_DEFAULT_STRATEGY);
        if ( status != Z_OK )
            throw BamException("BgzfStream::DeflateBlock", "zlib deflateInit2 failed");

        // compress the data
        status = deflate(&zs, Z_FINISH);

        // if not at stream end
        if ( status != Z_STREAM_END ) {

            deflateEnd(&zs);

            // there was not enough space available in buffer
            // try to reduce the input length & re-start loop
            if ( status == Z_OK ) {
                inputLength -= 1024;
                if ( inputLength < 0 )
                    throw BamException("BgzfStream::DeflateBlock", "input reduction failed");
                continue;
            }

            throw BamException("BgzfStream::DeflateBlock", "zlib deflate failed");
        }

        // finalize the compression routine
        status = deflateEnd(&zs);
        if ( status != Z_OK )
            throw BamException("BgzfStream::DeflateBlock", "zlib deflateEnd failed");

        // update compressedLength
        compressedLength = zs.total_out +
                           Constants::BGZF_BLOCK_HEADER_LENGTH +
                           Constants::BGZF_BLOCK_FOOTER_LENGTH;
        if ( compressedLength > Constants::BGZF_MAX_BLOCK_SIZE )
            throw BamException("BgzfStream::DeflateBlock", "deflate overflow");

        // quit while loop
        break;
    }

    // store the compressed length
    BamTools::PackUnsignedShort(&buffer[16], static_cast<uint16_t>(compressedLength - 1));

    // store the CRC32 checksum
    uint32_t crc = crc32(0, NULL, 0);
    crc = crc32(crc, (Bytef*)m_uncompressedBlock.Buffer, inputLength);
    BamTools::PackUnsignedInt(&buffer[compressedLength - 8], crc);
    BamTools::PackUnsignedInt(&buffer[compressedLength - 4], inputLength);

    // ensure that we have less than a block of data left
    int remaining = blockLength - inputLength;
    if ( remaining > 0 ) {
        if ( remaining > inputLength )
            throw BamException("BgzfStream::DeflateBlock", "after deflate, remainder too large");
        memcpy(m_uncompressedBlock.Buffer, m_uncompressedBlock.Buffer + inputLength, remaining);
    }

    // update block data
    m_blockOffset = remaining;

    // return result
    return compressedLength;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq 0x30(%rdi), %rax
andq $0x0, 0x4(%rax)
andq $0x0, 0xa(%rax)
movl $0x4088b1f, (%rax)      # imm = 0x4088B1F
movb $-0x1, 0x9(%rax)
movb $0x6, 0xa(%rax)
movw $0x4342, 0xc(%rax)      # imm = 0x4342
movb $0x2, 0xe(%rax)
movzbl 0x10(%rdi), %ecx
negl %ecx
movl %ecx, 0x10(%rsp)
movq %rax, 0x58(%rsp)
addq $0x12, %rax
movq %rax, 0x60(%rsp)
movl %esi, %ebx
xorl %r14d, %r14d
leaq 0x68(%rsp), %r12
pushq $0x8
popq %rbp
leaq 0xa8(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rdi, %r15
movq 0x20(%rdi), %rax
movq %rax, 0x68(%rsp)
movl %ebx, 0x70(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x80(%rsp)
movl $0xffe6, 0x88(%rsp) # imm = 0xFFE6
movq %r12, %rdi
movl 0x10(%rsp), %esi
movl %ebp, %edx
pushq $-0xf
popq %rcx
movl %ebp, %r8d
xorl %r9d, %r9d
pushq $0x70
leaq 0xc616(%rip), %rax      # 0x5cd2b
pushq %rax
callq 0x31200
addq $0x10, %rsp
testl %eax, %eax
jne 0x50850
movq %r12, %rdi
pushq $0x4
popq %rsi
callq 0x31d80
movl %eax, %r13d
movq %r12, %rdi
callq 0x32360
testl %r13d, %r13d
jne 0x50764
cmpl $0x3ff, %ebx            # imm = 0x3FF
jle 0x50922
addq $-0x400, %rbx           # imm = 0xFC00
addq $0x400, %r14            # imm = 0x400
movq %r15, %rdi
jmp 0x506c4
cmpl $0x1, %r13d
jne 0x508b9
testl %eax, %eax
jne 0x5098b
movq 0x90(%rsp), %rcx
addq $0x1a, %rcx
cmpq $0x10001, %rcx          # imm = 0x10001
jae 0x509f4
leal -0x1(%rcx), %eax
movq %rcx, 0x10(%rsp)
movq 0x58(%rsp), %rbp
movw %ax, 0x10(%rbp)
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32780
movl %eax, %edi
movq 0x20(%r15), %rsi
movl %ebx, %edx
callq 0x32780
movq 0x10(%rsp), %rcx
movb %al, -0x8(%rbp,%rcx)
movq 0x10(%rsp), %rcx
movb %ah, -0x7(%rbp,%rcx)
movl %eax, %ecx
shrl $0x10, %ecx
movq 0x10(%rsp), %rdx
movb %cl, -0x6(%rbp,%rdx)
shrl $0x18, %eax
movq 0x10(%rsp), %rcx
movb %al, -0x5(%rbp,%rcx)
movq 0x10(%rsp), %rax
movb %bl, -0x4(%rbp,%rax)
movq 0x10(%rsp), %rax
movb %bh, -0x3(%rbp,%rax)
movl %ebx, %eax
shrl $0x10, %eax
movq 0x10(%rsp), %rcx
movb %al, -0x2(%rbp,%rcx)
movl %ebx, %eax
shrl $0x18, %eax
movq 0x10(%rsp), %rcx
movb %al, -0x1(%rbp,%rcx)
movq 0x10(%rsp), %r12
testl %r14d, %r14d
jle 0x50837
cmpl %ebx, %r14d
jg 0x50a5a
movq 0x20(%r15), %rdi
addq %rdi, %rbx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x32b40
movl %r14d, 0x4(%r15)
movq %r12, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x28
popq %rdi
callq 0x31e70
movq %rax, %rbx
leaq 0xc4d0(%rip), %rsi      # 0x5cd32
leaq 0x18(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x31b30
leaq 0xc4d3(%rip), %rsi      # 0x5cd4b
leaq 0x38(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x31b30
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %rbx, %rdi
callq 0x344c0
xorl %ebp, %ebp
movq 0x24693(%rip), %rsi     # 0x74f38
movq 0x246cc(%rip), %rdx     # 0x74f78
movq %rbx, %rdi
callq 0x34c90
jmp 0x50abe
pushq $0x28
popq %rdi
callq 0x31e70
movq %rax, %rbx
leaq 0xc467(%rip), %rsi      # 0x5cd32
leaq 0x18(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x31b30
leaq 0xc49a(%rip), %rsi      # 0x5cd7b
leaq 0x38(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x31b30
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %rbx, %rdi
callq 0x344c0
xorl %ebp, %ebp
movq 0x2462a(%rip), %rsi     # 0x74f38
movq 0x24663(%rip), %rdx     # 0x74f78
movq %rbx, %rdi
callq 0x34c90
jmp 0x50abe
pushq $0x28
popq %rdi
callq 0x31e70
movq %rax, %rbx
leaq 0xc3fe(%rip), %rsi      # 0x5cd32
leaq 0x18(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x31b30
leaq 0xc41a(%rip), %rsi      # 0x5cd64
leaq 0x38(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x31b30
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %rbx, %rdi
callq 0x344c0
xorl %ebp, %ebp
movq 0x245c1(%rip), %rsi     # 0x74f38
movq 0x245fa(%rip), %rdx     # 0x74f78
movq %rbx, %rdi
callq 0x34c90
jmp 0x50abe
pushq $0x28
popq %rdi
callq 0x31e70
movq %rax, %rbx
leaq 0xc395(%rip), %rsi      # 0x5cd32
leaq 0x18(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x31b30
leaq 0xc3dc(%rip), %rsi      # 0x5cd8f
leaq 0x38(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x31b30
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %rbx, %rdi
callq 0x344c0
xorl %ebp, %ebp
movq 0x24558(%rip), %rsi     # 0x74f38
movq 0x24591(%rip), %rdx     # 0x74f78
movq %rbx, %rdi
callq 0x34c90
jmp 0x50abe
pushq $0x28
popq %rdi
callq 0x31e70
movq %rax, %rbx
leaq 0xc32c(%rip), %rsi      # 0x5cd32
leaq 0x18(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x31b30
leaq 0xc38a(%rip), %rsi      # 0x5cda6
leaq 0x38(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x31b30
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %rbx, %rdi
callq 0x344c0
xorl %ebp, %ebp
movq 0x244ef(%rip), %rsi     # 0x74f38
movq 0x24528(%rip), %rdx     # 0x74f78
movq %rbx, %rdi
callq 0x34c90
jmp 0x50abe
pushq $0x28
popq %rdi
callq 0x31e70
movq %rax, %rbx
leaq 0xc2c6(%rip), %rsi      # 0x5cd32
leaq 0x68(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0x31b30
leaq 0xc335(%rip), %rsi      # 0x5cdb7
leaq 0x18(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x31b30
movb $0x1, %bpl
leaq 0x68(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %rbx, %rdi
callq 0x344c0
xorl %ebp, %ebp
movq 0x24489(%rip), %rsi     # 0x74f38
movq 0x244c2(%rip), %rdx     # 0x74f78
movq %rbx, %rdi
callq 0x34c90
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x33a00
jmp 0x50ad3
movq %rax, %r14
movb $0x1, %bpl
leaq 0x68(%rsp), %rdi
callq 0x33a00
testb %bpl, %bpl
jne 0x50b2f
jmp 0x50bac
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x33a00
jmp 0x50afc
movq %rax, %r14
movb $0x1, %bpl
leaq 0x18(%rsp), %rdi
callq 0x33a00
jmp 0x50b96
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x33a00
jmp 0x50b20
movq %rax, %r14
movb $0x1, %bpl
leaq 0x18(%rsp), %rdi
callq 0x33a00
jmp 0x50b96
movq %rax, %r14
jmp 0x50ba4
jmp 0x50ba1
jmp 0x50ba1
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x33a00
jmp 0x50b4a
movq %rax, %r14
movb $0x1, %bpl
leaq 0x18(%rsp), %rdi
callq 0x33a00
jmp 0x50b96
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x33a00
jmp 0x50b6b
movq %rax, %r14
movb $0x1, %bpl
leaq 0x18(%rsp), %rdi
callq 0x33a00
jmp 0x50b96
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x33a00
jmp 0x50b8c
movq %rax, %r14
movb $0x1, %bpl
leaq 0x18(%rsp), %rdi
callq 0x33a00
testb %bpl, %bpl
jne 0x50ba4
jmp 0x50bac
jmp 0x50ba1
jmp 0x50ba1
movq %rax, %r14
movq %rbx, %rdi
callq 0x327c0
movq %r14, %rdi
callq 0x34ed0
movq %rax, %rdi
callq 0x39b2f