// File: /workspace/github_c_makefile_star2_O3/ijleesw[P]data-compression/src/arithmetic/arithmetic_encoder.h
// Function: arithmetic_encoder::encode(unsigned int, unsigned int, unsigned int)
inline void encode(DWORD lower_bound, DWORD upper_bound, DWORD total)
  {    
    bool e3_cond = false;
    bool eql_cond;

    QWORD range = (QWORD)(u - l) + 1;

    ASSERT(l < u);
    ASSERT((lower_bound <= upper_bound) && (upper_bound <= total));
    
    u = l + (DWORD)(((range * upper_bound) / total) - 1);
    l = l + (DWORD)((range * lower_bound) / total);

    while( (eql_cond = (MSB(u) == MSB(l))) ||
           (e3_cond  = (M2SB(l) == 1) && (M2SB(u) == 0)) )
    {  
      if(eql_cond)
      {
        bit bit_to_send = ((MSB(u) == 0) ? 0 : 1);
        m_output->send(bit_to_send);
        
        l <<= 1;
        u <<= 1;
        u |= 1;

        while (scale3 > 0)
        {
          m_output->send(!bit_to_send);
          --scale3;
        }
      }
      else if (e3_cond)
      {
        l <<= 1;
        u <<= 1;
        u |= 1;
        
        l += MSB_MASK; 
        u += MSB_MASK;

        ++scale3;
      }
    }
    ASSERT(l < u);
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl 0xc(%rdi), %edi
movl 0x10(%rbx), %r8d
subl %edi, %r8d
incq %r8
movl %edx, %eax
imulq %r8, %rax
movl %ecx, %r9d
xorl %edx, %edx
divq %r9
leal (%rdi,%rax), %ecx
decl %ecx
movl %ecx, 0x10(%rbx)
movl %esi, %eax
imulq %r8, %rax
xorl %edx, %edx
divq %r9
addl %edi, %eax
movl %eax, 0xc(%rbx)
movl %ecx, %r14d
movl %eax, %ecx
xorl %r14d, %ecx
js 0x2e92
movq (%rbx), %r15
movl 0x1c(%r15), %ebp
movb 0x10(%r15), %al
testl %ebp, %ebp
je 0x2ec5
decl %ebp
addb %al, %al
jmp 0x2f03
btl $0x1e, %r14d
jb 0x2fb3
movl %eax, %ecx
andl $0x40000000, %ecx       # imm = 0x40000000
je 0x2fb3
addl %eax, %eax
andl $0x7ffffffe, %eax       # imm = 0x7FFFFFFE
movl %eax, 0xc(%rbx)
leal -0x7fffffff(,%r14,2), %ecx
movl %ecx, 0x10(%rbx)
incl 0x14(%rbx)
jmp 0x2e73
movq 0x8(%r15), %rcx
movl 0x14(%r15), %edx
movb %al, (%rcx,%rdx)
movl 0x14(%r15), %eax
incl %eax
movl %eax, 0x14(%r15)
movl $0x7, %ebp
cmpl 0x18(%r15), %eax
jne 0x2f01
movl (%r15), %edi
movq 0x8(%r15), %rsi
movl %eax, %edx
callq 0x2040
movl $0x0, 0x14(%r15)
cmpl %eax, 0x18(%r15)
jne 0x2f13
xorl %eax, %eax
movl %ebp, 0x1c(%r15)
movl %r14d, %ecx
shrl $0x1f, %ecx
orb %al, %cl
movb %cl, 0x10(%r15)
movl 0xc(%rbx), %eax
movl 0x10(%rbx), %ecx
addl %eax, %eax
movl %eax, 0xc(%rbx)
leal 0x1(,%rcx,2), %ecx
movl %ecx, 0x10(%rbx)
cmpl $0x0, 0x14(%rbx)
jle 0x2e73
testl %r14d, %r14d
setns %bpl
movq (%rbx), %r14
movl 0x1c(%r14), %r15d
movb 0x10(%r14), %al
testl %r15d, %r15d
je 0x2f50
decl %r15d
addb %al, %al
jmp 0x2f8f
movq 0x8(%r14), %rcx
movl 0x14(%r14), %edx
movb %al, (%rcx,%rdx)
movl 0x14(%r14), %eax
incl %eax
movl %eax, 0x14(%r14)
movl $0x7, %r15d
cmpl 0x18(%r14), %eax
jne 0x2f8d
movl (%r14), %edi
movq 0x8(%r14), %rsi
movl %eax, %edx
callq 0x2040
movl $0x0, 0x14(%r14)
cmpl %eax, 0x18(%r14)
jne 0x2f9a
xorl %eax, %eax
orb %bpl, %al
movl %r15d, 0x1c(%r14)
movb %al, 0x10(%r14)
movl 0x14(%rbx), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x14(%rbx)
cmpl $0x1, %eax
jg 0x2f39
movl 0xc(%rbx), %eax
movl 0x10(%rbx), %ecx
jmp 0x2e73
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq