// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %r12
pushq %rbx
movl %edx, %ebx
movq %rdi, %r14
movl 0x4(%rdi), %edx
testl %edx, %edx
je 0x141c5
movl (%r14), %edi
cmpl %esi, %edi
seta %al
testl %ebx, %ebx
sete %cl
orb %al, %cl
jne 0x14247
leal 0x1(%rsi), %ecx
movl %ecx, %eax
subl %edi, %eax
bsrl %eax, %r8d
xorl $-0x20, %r8d
addl $0x21, %r8d
xorl %r9d, %r9d
cmpl %edi, %ecx
cmovel %r9d, %r8d
bsrl %r8d, %eax
xorl $0x1f, %eax
addl %eax, %eax
movl $0x40, %r10d
subl %eax, %r10d
movl %ecx, %eax
subl %edi, %eax
cmovel %r9d, %r10d
movq 0x18(%r14), %rdi
movl 0x18(%rdi), %r15d
notl %r15d
addl 0x8(%rdi), %r15d
shll $0x5, %r15d
addl 0x1c(%rdi), %r15d
addl %r10d, %r8d
addl $-0x2, %r8d
subl %r8d, %r15d
jbe 0x141e1
bsrl %ebx, %ecx
xorl $0x1f, %ecx
movl $0x20, %r8d
subl %ecx, %r8d
bsrl %r8d, %r8d
xorl $0x1f, %r8d
leal (%rcx,%r8,2), %ecx
movl $0x5e, %r12d
subl %ecx, %r12d
cmpl %r15d, %r12d
jbe 0x141ec
movl $0x2, %ecx
bsrl %ecx, %r8d
xorl $0x1f, %r8d
addl %r8d, %r8d
movl $0x40, %r9d
subl %r8d, %r9d
testl %ecx, %ecx
cmovel %ecx, %r9d
movl %ecx, %r8d
incl %ecx
addl %r9d, %r8d
addl $-0x2, %r8d
cmpl %r15d, %r8d
jbe 0x14166
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 0x10c1e
movq 0x18(%r14), %rdi
movl %ebp, %esi
callq 0x10c1e
incl (%r14)
subl %ebp, %ebx
incl 0x4(%r14)
jmp 0x14209
movq %r14, %rdi
callq 0x126f4
cmpl $0x2, %ebx
jb 0x14247
decl %ebx
addl %ebx, (%r14)
addl %ebx, 0x4(%r14)
addq $0x18, %r14
jmp 0x14228
movl %ecx, (%r14)
incl %edx
movl %edx, 0x4(%r14)
jmp 0x1420e
addl %ebx, %esi
movl %esi, (%r14)
addl %ebx, %edx
movl %edx, 0x4(%r14)
movl %eax, %esi
callq 0x10c1e
movq 0x18(%r14), %rdi
movl %ebx, %esi
callq 0x10c1e
cmpl %r15d, %r12d
jbe 0x14247
movq %r14, %rdi
callq 0x125fa
cmpl $0x2, %ebx
jb 0x14247
decl %ebx
addl %ebx, (%r14)
addl %ebx, 0x4(%r14)
addq $0x18, %r14
movq (%r14), %rdi
movl $0x1, %esi
callq 0x10c1e
movq (%r14), %rdi
movl %ebx, %esi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x10c1e
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq