// File: /workspace/github_cpp_makefile_star2_O1/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
movl %esi, %ebx
movq %rdi, %r15
movq 0x30(%rdi), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movw $0x0, 0x10(%rax)
movl $0x4088b1f, (%rax)      # imm = 0x4088B1F
movw $0x6ff, 0x9(%rax)       # imm = 0x6FF
movw $0x4342, 0xc(%rax)      # imm = 0x4342
movb $0x2, 0xe(%rax)
movzbl 0x10(%rdi), %r12d
negl %r12d
movq %rax, 0x48(%rsp)
addq $0x12, %rax
movq %rax, 0x50(%rsp)
xorl %ebp, %ebp
leaq 0x58(%rsp), %r13
movl %esi, 0x44(%rsp)
xorps %xmm0, %xmm0
leaq 0x98(%rsp), %rax
movups %xmm0, (%rax)
movq %r15, %r14
movq 0x20(%r15), %rax
movq %rax, 0x58(%rsp)
movl %ebx, 0x60(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x70(%rsp)
movl $0xffe6, 0x78(%rsp)     # imm = 0xFFE6
movq %r13, %rdi
movl %r12d, %esi
movl $0x8, %edx
movl $0xfffffff1, %ecx       # imm = 0xFFFFFFF1
movl $0x8, %r8d
xorl %r9d, %r9d
pushq $0x70
leaq 0xda3d(%rip), %rax      # 0x56cfc
pushq %rax
callq 0x261a0
addq $0x10, %rsp
testl %eax, %eax
jne 0x493d6
movq %r13, %rdi
movl $0x4, %esi
callq 0x26900
movl %eax, %r15d
movq %r13, %rdi
callq 0x26d50
cmpl $0x1, %r15d
jne 0x49311
testl %eax, %eax
jne 0x49550
movq 0x80(%rsp), %rbp
addq $0x1a, %rbp
cmpq $0x10001, %rbp          # imm = 0x10001
jb 0x4932c
jmp 0x49454
testl %r15d, %r15d
jne 0x495ce
cmpl $0x3ff, %ebx            # imm = 0x3FF
jle 0x494d2
addl $0xfffffc00, %ebx       # imm = 0xFFFFFC00
cmpl $0x1, %r15d
movq %r14, %r15
jne 0x4926d
leal -0x1(%rbp), %eax
movq 0x48(%rsp), %r14
movw %ax, 0x10(%r14)
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x27060
movl %eax, %edi
movq 0x20(%r15), %rsi
movl %ebx, %edx
callq 0x27060
movq %r14, %rdx
movb %al, -0x8(%r14,%rbp)
movb %ah, -0x7(%rdx,%rbp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, -0x6(%r14,%rbp)
shrl $0x18, %eax
movb %al, -0x5(%r14,%rbp)
movb %bl, -0x4(%r14,%rbp)
movb %bh, -0x3(%rdx,%rbp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, -0x2(%r14,%rbp)
movl %ebx, %eax
shrl $0x18, %eax
movb %al, -0x1(%r14,%rbp)
movl 0x44(%rsp), %r14d
subl %ebx, %r14d
jle 0x493bd
cmpl %ebx, %r14d
jg 0x49649
movq 0x20(%r15), %rdi
movl %ebx, %esi
addq %rdi, %rsi
movl %r14d, %edx
callq 0x27260
movl %r14d, 0x4(%r15)
movq %rbp, %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 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd910(%rip), %rsi      # 0x56d03
leaq 0xd921(%rip), %rdx      # 0x56d1b
movq %rsp, %rdi
callq 0x26750
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd909(%rip), %rsi      # 0x56d1c
leaq 0xd91a(%rip), %rdx      # 0x56d34
leaq 0x20(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
movq %rsp, %rsi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x28490
xorl %ebp, %ebp
movq 0x21af8(%rip), %rsi     # 0x6af38
movq 0x21b31(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a30
jmp 0x496c2
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd892(%rip), %rsi      # 0x56d03
leaq 0xd8a3(%rip), %rdx      # 0x56d1b
movq %rsp, %rdi
callq 0x26750
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd8e6(%rip), %rsi      # 0x56d77
leaq 0xd8ef(%rip), %rdx      # 0x56d87
leaq 0x20(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
movq %rsp, %rsi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x28490
xorl %ebp, %ebp
movq 0x21a7a(%rip), %rsi     # 0x6af38
movq 0x21ab3(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a30
jmp 0x496c2
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd814(%rip), %rsi      # 0x56d03
leaq 0xd825(%rip), %rdx      # 0x56d1b
movq %rsp, %rdi
callq 0x26750
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd826(%rip), %rsi      # 0x56d35
leaq 0xd835(%rip), %rdx      # 0x56d4b
leaq 0x20(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
movq %rsp, %rsi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x28490
xorl %ebp, %ebp
movq 0x219fc(%rip), %rsi     # 0x6af38
movq 0x21a35(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a30
jmp 0x496c2
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd796(%rip), %rsi      # 0x56d03
leaq 0xd7a7(%rip), %rdx      # 0x56d1b
movq %rsp, %rdi
callq 0x26750
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd7d3(%rip), %rsi      # 0x56d60
leaq 0xd7e2(%rip), %rdx      # 0x56d76
leaq 0x20(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
movq %rsp, %rsi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x28490
xorl %ebp, %ebp
movq 0x2197e(%rip), %rsi     # 0x6af38
movq 0x219b7(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a30
jmp 0x496c2
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd718(%rip), %rsi      # 0x56d03
leaq 0xd729(%rip), %rdx      # 0x56d1b
movq %rsp, %rdi
callq 0x26750
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd741(%rip), %rsi      # 0x56d4c
leaq 0xd74d(%rip), %rdx      # 0x56d5f
leaq 0x20(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
movq %rsp, %rsi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x28490
xorl %ebp, %ebp
movq 0x21900(%rip), %rsi     # 0x6af38
movq 0x21939(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a30
jmp 0x496c2
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x68(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd69d(%rip), %rsi      # 0x56d03
leaq 0xd6ae(%rip), %rdx      # 0x56d1b
leaq 0x58(%rsp), %rdi
callq 0x26750
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xd700(%rip), %rsi      # 0x56d88
leaq 0xd71b(%rip), %rdx      # 0x56daa
movq %rsp, %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0x58(%rsp), %rsi
movq %rsp, %rdx
movq %rbx, %rdi
callq 0x28490
xorl %ebp, %ebp
movq 0x21885(%rip), %rsi     # 0x6af38
movq 0x218be(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a30
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x496db
callq 0x27560
jmp 0x496db
movq %rax, %r14
movb $0x1, %bpl
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x496f2
callq 0x27560
jmp 0x496f2
movq %rax, %r14
movb $0x1, %bpl
testb %bpl, %bpl
je 0x497e4
jmp 0x497dc
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x4971a
callq 0x27560
jmp 0x4971a
movq %rax, %r14
movb $0x1, %bpl
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x497d7
jmp 0x497c2
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x49746
callq 0x27560
jmp 0x49746
movq %rax, %r14
movb $0x1, %bpl
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x497d7
jmp 0x497c2
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x4976f
callq 0x27560
jmp 0x4976f
movq %rax, %r14
movb $0x1, %bpl
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x497d7
jmp 0x497c2
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x49794
callq 0x27560
jmp 0x49794
movq %rax, %r14
movb $0x1, %bpl
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x497d7
jmp 0x497c2
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x497b9
callq 0x27560
jmp 0x497b9
movq %rax, %r14
movb $0x1, %bpl
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x497d7
callq 0x27560
jmp 0x497d7
jmp 0x497d1
jmp 0x497d1
jmp 0x497d1
jmp 0x497d1
movq %rax, %r14
movb $0x1, %bpl
testb %bpl, %bpl
je 0x497e4
movq %rbx, %rdi
callq 0x27070
movq %r14, %rdi
callq 0x28c20