// File: /workspace/github_cpp_makefile_star2_O3/adamnovak[P]rlcsa/bits/rlevector.cpp
// Function: CSA::RLEEncoder::setRun(unsigned int, unsigned int)
void
RLEEncoder::setRun(usint start, usint len)
{
  if(this->items == 0)
  {
    this->setFirstBit(start);
    if(len > 1)
    {
      this->RLEncode(1, len - 1);
    }
    return;
  }
  if(start < this->size || len == 0) { return; }

  // Write as much into the buffer as possible.
  usint diff = start + 1 - this->size;
  usint free_bits = this->buffer->bitsLeft();
  usint code_bits = this->buffer->deltaCodeLength(diff);
  if(free_bits > code_bits) // At least a part of the run fits into the block.
  {
    free_bits -= code_bits;
    usint run_bits = this->buffer->deltaCodeLength(len);
    if(run_bits <= free_bits)
    {
      this->RLEncode(diff, len);
      return;
    }

    // Encode as much as possible and let the rest spill.
    usint llen = 1;
    while(llen + 2 * length(llen + 1) - 1 <= free_bits) { llen++; }
    llen = ((usint)1 << llen) - 1;

    this->RLEncode(diff, llen);
    len -= llen;

    // A new sample will be added.
    this->size++;
    this->items++;
  }
  else
  {
    this->size = start + 1;
    this->items++;
  }

  // Didn't fit into the block. A new sample & block required.
  this->addNewBlock();
  if(len > 1)
  {
    this->RLEncode(1, len - 1);
  }
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movl %edx, %ebx
movq %rdi, %r14
movl 0x4(%rdi), %edx
testl %edx, %edx
je 0x1471f
movl (%r14), %edi
cmpl %esi, %edi
seta %al
testl %ebx, %ebx
sete %cl
orb %al, %cl
jne 0x147ce
leal 0x1(%rsi), %ecx
movl %ecx, %eax
subl %edi, %eax
bsrl %eax, %r9d
xorl $-0x20, %r9d
addl $0x21, %r9d
xorl %r10d, %r10d
cmpl %edi, %ecx
cmovel %r10d, %r9d
bsrl %r9d, %eax
xorl $0x1f, %eax
addl %eax, %eax
movl $0x40, %r11d
subl %eax, %r11d
movl %ecx, %eax
subl %edi, %eax
movq 0x18(%r14), %rdi
movl 0x18(%rdi), %r8d
cmovel %r10d, %r11d
notl %r8d
addl 0x8(%rdi), %r8d
shll $0x5, %r8d
addl 0x1c(%rdi), %r8d
addl %r11d, %r9d
addl $-0x2, %r9d
subl %r9d, %r8d
jbe 0x14729
bsrl %ebx, %ecx
xorl $0x1f, %ecx
movl $0x20, %r9d
subl %ecx, %r9d
bsrl %r9d, %r9d
xorl $0x1f, %r9d
leal (%rcx,%r9,2), %ecx
movl $0x5e, %r9d
subl %ecx, %r9d
cmpl %r8d, %r9d
jbe 0x147d3
movl $0x2, %ecx
bsrl %ecx, %r9d
xorl $0x1f, %r9d
addl %r9d, %r9d
movl $0x40, %r10d
subl %r9d, %r10d
testl %ecx, %ecx
cmovel %ecx, %r10d
movl %ecx, %r9d
incl %ecx
addl %r10d, %r9d
addl $-0x2, %r9d
cmpl %r8d, %r9d
jbe 0x146c0
addb $-0x2, %cl
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
shll %cl, %ebp
notl %ebp
addl %ebp, %esi
movl %esi, (%r14)
addl %ebp, %edx
movl %edx, 0x4(%r14)
movl %eax, %esi
callq 0x10f3e
movq 0x18(%r14), %rdi
movl %ebp, %esi
callq 0x10f3e
subl %ebp, %ebx
incl (%r14)
movl 0x4(%r14), %edx
jmp 0x1472c
movq %r14, %rdi
callq 0x12a70
jmp 0x1473a
movl %ecx, (%r14)
incl %edx
movl %edx, 0x4(%r14)
movq %r14, %rdi
callq 0x12964
cmpl $0x2, %ebx
jb 0x147ce
decl %ebx
movq (%r14), %xmm0
movd %ebx, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, (%r14)
movq 0x18(%r14), %rdi
movl 0x18(%rdi), %edx
movl 0x1c(%rdi), %eax
movl %edx, %ecx
notl %ecx
addl 0x8(%rdi), %ecx
shll $0x5, %ecx
addl %eax, %ecx
je 0x147e9
movq (%rdi), %rsi
cmpl $0x1, %eax
ja 0x147a6
movb $0x1, %cl
subb %al, %cl
movl $0x1, %r8d
shrl %cl, %r8d
orl %r8d, (%rsi,%rdx,4)
movl 0x18(%rdi), %edx
incl %edx
movl %edx, 0x18(%rdi)
movl $0x20, 0x1c(%rdi)
cmpl $0x1, %eax
movl $0x20, %eax
je 0x147e9
decl %eax
movl %eax, 0x1c(%rdi)
movl $0x1, %r8d
movl %eax, %ecx
shll %cl, %r8d
movl %edx, %eax
orl %r8d, (%rsi,%rax,4)
cmpl $0x0, 0x1c(%rdi)
jne 0x147e9
incl 0x18(%rdi)
movl $0x20, 0x1c(%rdi)
jmp 0x147e9
popq %rbx
popq %r14
popq %rbp
retq
addl %ebx, %esi
movl %esi, (%r14)
addl %ebx, %edx
movl %edx, 0x4(%r14)
movl %eax, %esi
callq 0x10f3e
movq 0x18(%r14), %rdi
movl %ebx, %esi
popq %rbx
popq %r14
popq %rbp
jmp 0x10f3e