// File: /workspace/github_cpp_makefile_star2_O3/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 $0xc8, %rsp
movq 0x30(%rdi), %rcx
xorl %eax, %eax
movq %rax, 0x4(%rcx)
xorl %r14d, %r14d
movq %rax, 0xa(%rcx)
movl $0x4088b1f, (%rcx)      # imm = 0x4088B1F
movb $-0x1, 0x9(%rcx)
movb $0x6, 0xa(%rcx)
movw $0x4342, 0xc(%rcx)      # imm = 0x4342
movb $0x2, 0xe(%rcx)
movzbl 0x10(%rdi), %ebp
negl %ebp
movq %rcx, 0x50(%rsp)
leaq 0x12(%rcx), %rax
movq %rax, 0x8(%rsp)
movl %esi, %ebx
leaq 0x58(%rsp), %r12
leaq 0x98(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rdi, %r15
movq 0x20(%rdi), %rax
movq %rax, 0x58(%rsp)
movl %ebx, 0x60(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x70(%rsp)
movl $0xffe6, 0x78(%rsp)     # imm = 0xFFE6
movq %r12, %rdi
movl %ebp, %esi
movl $0x8, %edx
movl $0xfffffff1, %ecx       # imm = 0xFFFFFFF1
movl $0x8, %r8d
xorl %r9d, %r9d
pushq $0x70
leaq 0xdb1c(%rip), %rax      # 0x56cfc
pushq %rax
callq 0x261a0
addq $0x10, %rsp
testl %eax, %eax
jne 0x4931d
movq %r12, %rdi
movl $0x4, %esi
callq 0x26900
movl %eax, %r13d
movq %r12, %rdi
callq 0x26d50
testl %r13d, %r13d
jne 0x49231
cmpl $0x3ff, %ebx            # imm = 0x3FF
jle 0x49421
addq $-0x400, %rbx           # imm = 0xFC00
addq $0x400, %r14            # imm = 0x400
movq %r15, %rdi
jmp 0x4918f
cmpl $0x1, %r13d
jne 0x4939f
testl %eax, %eax
jne 0x494a3
movq 0x80(%rsp), %rcx
addq $0x1a, %rcx
cmpq $0x10001, %rcx          # imm = 0x10001
jae 0x49525
leal -0x1(%rcx), %eax
movq %rcx, 0x8(%rsp)
movq 0x50(%rsp), %rbp
movw %ax, 0x10(%rbp)
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x27060
movl %eax, %edi
movq 0x20(%r15), %rsi
movl %ebx, %edx
callq 0x27060
movq 0x8(%rsp), %rcx
movb %al, -0x8(%rbp,%rcx)
movq 0x8(%rsp), %rcx
movb %ah, -0x7(%rbp,%rcx)
movl %eax, %ecx
shrl $0x10, %ecx
movq 0x8(%rsp), %rdx
movb %cl, -0x6(%rbp,%rdx)
shrl $0x18, %eax
movq 0x8(%rsp), %rcx
movb %al, -0x5(%rbp,%rcx)
movq 0x8(%rsp), %rax
movb %bl, -0x4(%rbp,%rax)
movq 0x8(%rsp), %rax
movb %bh, -0x3(%rbp,%rax)
movl %ebx, %eax
shrl $0x10, %eax
movq 0x8(%rsp), %rcx
movb %al, -0x2(%rbp,%rcx)
movl %ebx, %eax
shrl $0x18, %eax
movq 0x8(%rsp), %rcx
movb %al, -0x1(%rbp,%rcx)
movq 0x8(%rsp), %r12
testl %r14d, %r14d
jle 0x49304
cmpl %ebx, %r14d
jg 0x495a4
movq 0x20(%r15), %rdi
addq %rdi, %rbx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x27260
movl %r14d, 0x4(%r15)
movq %r12, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd9c9(%rip), %rsi      # 0x56d03
leaq 0xd9da(%rip), %rdx      # 0x56d1b
leaq 0x10(%rsp), %rdi
callq 0x26750
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd9c0(%rip), %rsi      # 0x56d1c
leaq 0xd9d1(%rip), %rdx      # 0x56d34
leaq 0x30(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x28480
xorl %ebp, %ebp
movq 0x21bad(%rip), %rsi     # 0x6af38
movq 0x21be6(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a20
jmp 0x49621
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd947(%rip), %rsi      # 0x56d03
leaq 0xd958(%rip), %rdx      # 0x56d1b
leaq 0x10(%rsp), %rdi
callq 0x26750
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd96e(%rip), %rsi      # 0x56d4c
leaq 0xd97a(%rip), %rdx      # 0x56d5f
leaq 0x30(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x28480
xorl %ebp, %ebp
movq 0x21b2b(%rip), %rsi     # 0x6af38
movq 0x21b64(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a20
jmp 0x49621
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd8c5(%rip), %rsi      # 0x56d03
leaq 0xd8d6(%rip), %rdx      # 0x56d1b
leaq 0x10(%rsp), %rdi
callq 0x26750
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd8d5(%rip), %rsi      # 0x56d35
leaq 0xd8e4(%rip), %rdx      # 0x56d4b
leaq 0x30(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x28480
xorl %ebp, %ebp
movq 0x21aa9(%rip), %rsi     # 0x6af38
movq 0x21ae2(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a20
jmp 0x49621
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd843(%rip), %rsi      # 0x56d03
leaq 0xd854(%rip), %rdx      # 0x56d1b
leaq 0x10(%rsp), %rdi
callq 0x26750
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd87e(%rip), %rsi      # 0x56d60
leaq 0xd88d(%rip), %rdx      # 0x56d76
leaq 0x30(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x28480
xorl %ebp, %ebp
movq 0x21a27(%rip), %rsi     # 0x6af38
movq 0x21a60(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a20
jmp 0x49621
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd7c1(%rip), %rsi      # 0x56d03
leaq 0xd7d2(%rip), %rdx      # 0x56d1b
leaq 0x10(%rsp), %rdi
callq 0x26750
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd813(%rip), %rsi      # 0x56d77
leaq 0xd81c(%rip), %rdx      # 0x56d87
leaq 0x30(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x28480
xorl %ebp, %ebp
movq 0x219a5(%rip), %rsi     # 0x6af38
movq 0x219de(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a20
jmp 0x49621
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x68(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd742(%rip), %rsi      # 0x56d03
leaq 0xd753(%rip), %rdx      # 0x56d1b
leaq 0x58(%rsp), %rdi
callq 0x26750
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd7a5(%rip), %rsi      # 0x56d88
leaq 0xd7c0(%rip), %rdx      # 0x56daa
leaq 0x10(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0x58(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x28480
xorl %ebp, %ebp
movq 0x21926(%rip), %rsi     # 0x6af38
movq 0x2195f(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a20
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x49633
callq 0x27560
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x49642
callq 0x27560
testb %bpl, %bpl
jne 0x4965e
jmp 0x49790
movq %rax, %r14
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x4965e
callq 0x27560
jmp 0x49788
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x49675
callq 0x27560
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x49766
jmp 0x49761
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x49788
jmp 0x4977a
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x496b0
callq 0x27560
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x49766
jmp 0x49761
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x49788
jmp 0x4977a
movq %rax, %r14
jmp 0x49788
jmp 0x49785
jmp 0x49785
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x496fd
callq 0x27560
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x49766
jmp 0x49761
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x49788
jmp 0x4977a
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x4972a
callq 0x27560
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x49766
jmp 0x49761
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x49788
jmp 0x4977a
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x49757
callq 0x27560
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x49766
callq 0x27560
testb %bpl, %bpl
jne 0x49788
jmp 0x49790
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x49788
callq 0x27560
jmp 0x49788
jmp 0x49785
jmp 0x49785
movq %rax, %r14
movq %rbx, %rdi
callq 0x27070
movq %r14, %rdi
callq 0x28c10