// File: cristian-bicheru[P]arith/./arith.h
// Function: arith::CompressBuffer(std::vector<unsigned char, std::allocator<unsigned char>>&, arith::CodecProbabilityTable&, buffer::CodecByteStream&)
void CompressBuffer(std::vector<unsigned char>& InputBuffer,
                        CodecProbabilityTable& ProbabilityTable,
                        buffer::CodecByteStream& OutputBuffer) {
        uint64_t high = MAXVAL;
        uint64_t low = 0ull;
        uint8_t shift = ProbabilityTable.GetShift();
        buffer::CodecBufferWrapper Buffer(InputBuffer, shift);
        OutputBuffer.WriteByte(shift);
        OutputBuffer.WriteByte((InputBuffer[InputBuffer.size()-1]<<shift)+(InputBuffer[0]>>(8u-shift))); // residual due to shift
        uint64_t pLow, pUp, pDenom, range;

        // Rate Stuff
        auto StartTime = std::chrono::high_resolution_clock::now();
        uint64_t BufSizePct = Buffer.Size()/100ull;


        for (uint64_t i = 0; i < Buffer.Size(); i++) {

            if ((i<<44ll>>44ull) == 0ull) {
                std::cout << "\33[2K\r";
                std::cout << "Compressing... " << i/BufSizePct+1 << "%  @" << double(i)/std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - StartTime).count() << " Bytes/Second." << std::flush;
            }

            range = high - low + 1ull;
            std::tie(pLow, pUp, pDenom) = ProbabilityTable.GetProbability(Buffer[i]);
            high = low + (range * pUp / pDenom) - 1ull;
            low = low + (range * pLow / pDenom);

            while (true) {
                if (high < HALF) {
                    OutputBuffer.WriteBitBuffered(0u);
                } else if (low >= HALF) {
                    OutputBuffer.WriteBitBuffered(1u);
                } else if (high < THREE_QUARTERS && low >= QUARTER) {
                    OutputBuffer.IncPendingBits();
                    low -= QUARTER;
                    high -= QUARTER;
                } else {
                    break;
                }
                high <<= 1ull;
                high++;
                low <<= 1ull;
                high &= MAXVAL;
                low &= MAXVAL;
            }
        }

        OutputBuffer.IncPendingBits();
        if (low < QUARTER) {
            OutputBuffer.WriteBitBuffered(0);
        } else {
            OutputBuffer.WriteBitBuffered(1);
        }

        std::cout << std::endl;
    }

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x170(%rsp)
movq %rsi, 0x168(%rsp)
movq %rdx, 0x160(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x158(%rsp)
movq $0x0, 0x150(%rsp)
movq 0x168(%rsp), %rdi
callq 0x4b40
movb %al, 0x14f(%rsp)
movq 0x170(%rsp), %rsi
movzbl 0x14f(%rsp), %edx
leaq 0x120(%rsp), %rdi
callq 0x4b60
movq 0x160(%rsp), %rdi
movzbl 0x14f(%rsp), %esi
callq 0x4c10
jmp 0x2c20
movq 0x160(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x170(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x4b20
movq 0x80(%rsp), %rdi
movq %rax, %rsi
decq %rsi
callq 0x4ae0
movzbl (%rax), %eax
movb 0x14f(%rsp), %cl
shll %cl, %eax
movl %eax, 0x94(%rsp)
movq 0x170(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4ae0
movq 0x88(%rsp), %rdi
movq %rax, %rcx
movl 0x94(%rsp), %eax
movzbl (%rcx), %edx
movb 0x14f(%rsp), %sil
movb $0x8, %cl
subb %sil, %cl
shrl %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
movzbl %al, %esi
callq 0x4c10
jmp 0x2cae
callq 0x2030
movq %rax, 0xe8(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x4c90
movq %rax, 0x78(%rsp)
jmp 0x2ccf
movq 0x78(%rsp), %rax
movl $0x64, %ecx
xorl %edx, %edx
divq %rcx
movq %rax, 0xe0(%rsp)
movq $0x0, 0xd8(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x4c90
movq %rax, 0x70(%rsp)
jmp 0x2d13
movq 0x68(%rsp), %rax
movq 0x70(%rsp), %rcx
cmpq %rcx, %rax
jae 0x3118
movq 0xd8(%rsp), %rax
shlq $0x2c, %rax
shrq $0x2c, %rax
cmpq $0x0, %rax
jne 0x2ec7
movq 0x1f279(%rip), %rdi     # 0x21fc0
leaq 0x1530a(%rip), %rsi     # 0x18058
callq 0x21a0
jmp 0x2d55
movq 0x1f264(%rip), %rdi     # 0x21fc0
leaq 0x152fb(%rip), %rsi     # 0x1805e
callq 0x21a0
movq %rax, 0x60(%rsp)
jmp 0x2d6f
movq 0x60(%rsp), %rdi
movq 0xd8(%rsp), %rax
movq 0xe0(%rsp), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
incq %rsi
callq 0x2060
movq %rax, 0x58(%rsp)
jmp 0x2d9b
movq 0x58(%rsp), %rdi
leaq 0x152c7(%rip), %rsi     # 0x1806e
callq 0x21a0
movq %rax, 0x50(%rsp)
jmp 0x2db3
movaps 0x15256(%rip), %xmm1    # 0x18010
movq 0xd8(%rsp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x15251(%rip), %xmm1    # 0x18020
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
callq 0x2030
movq %rax, 0xc0(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x4cd0
movq %rax, 0x48(%rsp)
jmp 0x2e0d
movq 0x48(%rsp), %rax
movq %rax, 0xc8(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x4ca0
movsd %xmm0, 0x38(%rsp)
jmp 0x2e2f
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x4d20
movsd %xmm0, 0x30(%rsp)
jmp 0x2e53
movq 0x50(%rsp), %rdi
movsd 0x30(%rsp), %xmm1
movsd 0x40(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x2340
movq %rax, 0x28(%rsp)
jmp 0x2e74
movq 0x28(%rsp), %rdi
leaq 0x151f3(%rip), %rsi     # 0x18073
callq 0x21a0
movq %rax, 0x20(%rsp)
jmp 0x2e8c
movq 0x20(%rsp), %rdi
movq 0x1f130(%rip), %rsi     # 0x21fc8
callq 0x21d0
jmp 0x2e9f
jmp 0x2ec7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x4ef0
jmp 0x3188
movq 0x158(%rsp), %rax
movq 0x150(%rsp), %rcx
subq %rcx, %rax
incq %rax
movq %rax, 0xf0(%rsp)
movq 0x168(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xd8(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x4d90
movb %al, 0x1f(%rsp)
jmp 0x2f0d
movq 0x10(%rsp), %rsi
movb 0x1f(%rsp), %al
movzbl %al, %edx
leaq 0xb4(%rsp), %rdi
callq 0x4d30
jmp 0x2f28
leaq 0x98(%rsp), %rdi
leaq 0x108(%rsp), %rsi
leaq 0x100(%rsp), %rdx
leaq 0xf8(%rsp), %rcx
callq 0x4df0
leaq 0x98(%rsp), %rdi
leaq 0xb4(%rsp), %rsi
callq 0x4e30
movq 0x150(%rsp), %rax
movq %rax, (%rsp)
movq 0xf0(%rsp), %rax
imulq 0x100(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0xf8(%rsp)
movq %rax, %rcx
movq (%rsp), %rax
addq %rcx, %rax
subq $0x1, %rax
movq %rax, 0x158(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xf0(%rsp), %rax
imulq 0x108(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0xf8(%rsp)
movq %rax, %rcx
movq 0x8(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x150(%rsp)
jmp 0x2fe0
movl $0x80000000, %eax       # imm = 0x80000000
cmpq %rax, 0x158(%rsp)
jae 0x3005
movq 0x160(%rsp), %rdi
xorl %esi, %esi
callq 0x4e70
jmp 0x3000
jmp 0x308a
movl $0x80000000, %eax       # imm = 0x80000000
cmpq %rax, 0x150(%rsp)
jb 0x302a
movq 0x160(%rsp), %rdi
movl $0x1, %esi
callq 0x4e70
jmp 0x3028
jmp 0x3088
movl $0xc0000000, %eax       # imm = 0xC0000000
cmpq %rax, 0x158(%rsp)
jae 0x3084
cmpq $0x40000000, 0x150(%rsp) # imm = 0x40000000
jb 0x3084
movq 0x160(%rsp), %rdi
callq 0x4ed0
jmp 0x3056
movq 0x150(%rsp), %rax
subq $0x40000000, %rax       # imm = 0x40000000
movq %rax, 0x150(%rsp)
movq 0x158(%rsp), %rax
subq $0x40000000, %rax       # imm = 0x40000000
movq %rax, 0x158(%rsp)
jmp 0x3086
jmp 0x30fd
jmp 0x3088
jmp 0x308a
movq 0x158(%rsp), %rax
shlq %rax
movq %rax, 0x158(%rsp)
movq 0x158(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x158(%rsp)
movq 0x150(%rsp), %rax
shlq %rax
movq %rax, 0x150(%rsp)
movabsq $0xffffffff, %rax       # imm = 0xFFFFFFFF
andq 0x158(%rsp), %rax
movq %rax, 0x158(%rsp)
movabsq $0xffffffff, %rax       # imm = 0xFFFFFFFF
andq 0x150(%rsp), %rax
movq %rax, 0x150(%rsp)
jmp 0x2fde
jmp 0x30ff
movq 0xd8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd8(%rsp)
jmp 0x2cf2
movq 0x160(%rsp), %rdi
callq 0x4ed0
jmp 0x3127
cmpq $0x40000000, 0x150(%rsp) # imm = 0x40000000
jae 0x3148
movq 0x160(%rsp), %rdi
xorl %esi, %esi
callq 0x4e70
jmp 0x3146
jmp 0x315e
movq 0x160(%rsp), %rdi
movl $0x1, %esi
callq 0x4e70
jmp 0x315c
jmp 0x315e
movq 0x1ee5b(%rip), %rdi     # 0x21fc0
movq 0x1ee34(%rip), %rsi     # 0x21fa0
callq 0x21d0
jmp 0x3173
leaq 0x120(%rsp), %rdi
callq 0x4ef0
addq $0x178, %rsp            # imm = 0x178
retq
movq 0x118(%rsp), %rdi
callq 0x2310
nopw %cs:(%rax,%rax)