// File: /workspace/github_cpp_makefile_star2_O3/adamnovak[P]rlcsa/sasamples.cpp
// Function: CSA::SASamples::mergeSamples(CSA::SASamples&, CSA::SASamples&, unsigned int*, unsigned int, unsigned int)
void
SASamples::mergeSamples(SASamples& index, SASamples& increment, usint* positions, usint n, usint skip)
{
  if(index.isWeighted() || increment.isWeighted())
  {
    std::cerr << "Error: Cannot merge weighted samples!" << std::endl;
    return;
  }

  SAVector::Iterator first(*(index.indexes));
  SAVector::Iterator second(*(increment.indexes));
  ReadBuffer* first_samples = index.samples;
  ReadBuffer* second_samples = increment.samples;

  usint first_bit = first.select(0);
  bool first_finished = false;
  usint second_bit = second.select(0);
  usint sum = index.items;
  first_samples->goToItem(0);
  second_samples->goToItem(0);

  SAVector::Encoder encoder(INDEX_BLOCK_SIZE);
  WriteBuffer sample_buffer(this->items, length(this->items - 1));
  for(usint i = 0; i < n; i++, first_bit++)
  {
    while(!first_finished && first_bit < positions[i] - skip)
    {
      encoder.setBit(first_bit);
      sample_buffer.writeItem(first_samples->readItem());
      if(first.hasNext())
      {
        first_bit = first.selectNext() + i;
      }
      else
      {
        first_finished = true;
      }
    }

    if(i == second_bit) // positions[i] is one
    {
      encoder.setBit(positions[i] - skip);
      sample_buffer.writeItem(second_samples->readItem() + sum);
      second_bit = second.selectNext();
    }
  }

  while(!first_finished)
  {
    encoder.setBit(first_bit);
    sample_buffer.writeItem(first_samples->readItem());
    if(!first.hasNext()) { break; }
    first_bit = first.selectNext() + n;
  }

  delete index.indexes; index.indexes = 0;
  delete index.samples; index.samples = 0;
  delete increment.indexes; increment.indexes = 0;
  delete increment.samples; increment.samples = 0;

  this->indexes = new SAVector(encoder, size);
  this->samples = sample_buffer.getReadBuffer();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rsi, 0x38(%rsp)
cmpb $0x0, (%rsi)
jne 0xbaeb
movq %rdx, %rbx
cmpb $0x1, (%rdx)
jne 0xbb3e
movq 0x144fe(%rip), %rbx     # 0x1fff0
leaq 0xcbdb(%rip), %rsi      # 0x186d4
movl $0x25, %edx
movq %rbx, %rdi
callq 0x32b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3190
movl %r9d, %r12d
movq %rcx, %r13
movl %r8d, 0x30(%rsp)
movq %rdi, 0x50(%rsp)
movq 0x38(%rsp), %r14
movq 0x10(%r14), %rsi
leaq 0x68(%rsp), %rdi
callq 0x12ac0
movq 0x10(%rbx), %rsi
leaq 0x150(%rsp), %rdi
callq 0x12ac0
movq 0x18(%r14), %r15
movq %rbx, 0x40(%rsp)
movq 0x18(%rbx), %r14
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x12c6e
movl %eax, %ebx
leaq 0x150(%rsp), %rdi
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x12c6e
movl %eax, 0x34(%rsp)
movq 0x38(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x4c(%rsp)
movabsq $0x2000000000, %rax     # imm = 0x2000000000
movq %rax, 0x18(%r15)
movl %ebp, 0x20(%r15)
movq %rax, 0x18(%r14)
movl %ebp, 0x20(%r14)
leaq 0xe0(%rsp), %rdi
movl $0x10, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x134f2
movq 0x50(%rsp), %rax
movl 0xc(%rax), %esi
leal -0x1(%rsi), %eax
bsrl %eax, %edx
xorl $-0x20, %edx
addl $0x21, %edx
testl %eax, %eax
cmovel %eax, %edx
leaq 0x8(%rsp), %rdi
callq 0x113b2
movl 0x30(%rsp), %eax
testl %eax, %eax
je 0xbf77
movq %r14, 0x58(%rsp)
movl %eax, %eax
movq %rax, 0x60(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
testb $0x1, %bpl
jne 0xbd38
movl (%r13,%r14,4), %eax
subl %r12d, %eax
cmpl %eax, %ebx
jae 0xbd38
leaq 0xe0(%rsp), %rdi
movl %ebx, %esi
callq 0x13502
incl 0x20(%r15)
movl 0xc(%r15), %edx
movl 0x1c(%r15), %ecx
xorl %esi, %esi
movl %edx, %eax
subl %ecx, %eax
jae 0xbc60
movl %edx, %eax
jmp 0xbc88
movq (%r15), %rdx
movl 0x18(%r15), %edi
movl (%rdx,%rdi,4), %esi
negb %cl
shll %cl, %esi
shrl %cl, %esi
movl %eax, %ecx
shll %cl, %esi
leal 0x1(%rdi), %ecx
movl %ecx, 0x18(%r15)
movl $0x20, 0x1c(%r15)
movl $0x20, %ecx
testl %eax, %eax
je 0xbca8
subl %eax, %ecx
movl %ecx, 0x1c(%r15)
movq (%r15), %rdx
movl 0x18(%r15), %edi
movl (%rdx,%rdi,4), %edx
shrl %cl, %edx
negb %al
movl %eax, %ecx
shll %cl, %edx
shrl %cl, %edx
orl %edx, %esi
movl 0x14(%rsp), %ecx
movl 0x24(%rsp), %edx
movl %ecx, %eax
subl %edx, %eax
jae 0xbcba
movl %ecx, %eax
jmp 0xbce5
movl %esi, %edi
movl %eax, %ecx
shrl %cl, %edi
negb %dl
movl %edx, %ecx
shll %cl, %edi
shrl %cl, %edi
movq 0x8(%rsp), %rcx
movl 0x20(%rsp), %edx
orl %edi, (%rcx,%rdx,4)
incl 0x20(%rsp)
movl $0x20, 0x24(%rsp)
movl $0x20, %edx
testl %eax, %eax
je 0xbd07
subl %eax, %edx
movl %edx, 0x24(%rsp)
negb %al
movl %eax, %ecx
shll %cl, %esi
shrl %cl, %esi
movl %edx, %ecx
shll %cl, %esi
movq 0x8(%rsp), %rax
movl 0x20(%rsp), %ecx
orl %esi, (%rax,%rcx,4)
incl 0x28(%rsp)
movl 0x7c(%rsp), %eax
addl 0x74(%rsp), %eax
movq 0x68(%rsp), %rcx
movl 0x4(%rcx), %ecx
decl %ecx
cmpl %ecx, %eax
jae 0xbd35
leaq 0x68(%rsp), %rdi
callq 0x12dce
movl %eax, %ebx
addl %r14d, %ebx
jmp 0xbc1f
movb $0x1, %bpl
movl 0x34(%rsp), %eax
cmpq %rax, %r14
jne 0xbe3a
movl (%r13,%r14,4), %esi
subl %r12d, %esi
leaq 0xe0(%rsp), %rdi
callq 0x13502
movq 0x58(%rsp), %r8
incl 0x20(%r8)
movl 0xc(%r8), %edx
movl 0x1c(%r8), %ecx
xorl %esi, %esi
movl %edx, %eax
subl %ecx, %eax
jae 0xbd77
movl %edx, %eax
jmp 0xbd9f
movq (%r8), %rdx
movl 0x18(%r8), %edi
movl (%rdx,%rdi,4), %esi
negb %cl
shll %cl, %esi
shrl %cl, %esi
movl %eax, %ecx
shll %cl, %esi
leal 0x1(%rdi), %ecx
movl %ecx, 0x18(%r8)
movl $0x20, 0x1c(%r8)
movl $0x20, %ecx
testl %eax, %eax
je 0xbdc2
subl %eax, %ecx
movq 0x58(%rsp), %rdi
movl %ecx, 0x1c(%rdi)
movq (%rdi), %rdx
movl 0x18(%rdi), %edi
movl (%rdx,%rdi,4), %edx
shrl %cl, %edx
negb %al
movl %eax, %ecx
shll %cl, %edx
shrl %cl, %edx
orl %edx, %esi
addl 0x4c(%rsp), %esi
movl 0x14(%rsp), %ecx
movl 0x24(%rsp), %edx
movl %ecx, %eax
subl %edx, %eax
jae 0xbdd8
movl %ecx, %eax
jmp 0xbe03
movl %esi, %edi
movl %eax, %ecx
shrl %cl, %edi
negb %dl
movl %edx, %ecx
shll %cl, %edi
shrl %cl, %edi
movq 0x8(%rsp), %rcx
movl 0x20(%rsp), %edx
orl %edi, (%rcx,%rdx,4)
incl 0x20(%rsp)
movl $0x20, 0x24(%rsp)
movl $0x20, %edx
testl %eax, %eax
je 0xbe25
subl %eax, %edx
movl %edx, 0x24(%rsp)
negb %al
movl %eax, %ecx
shll %cl, %esi
shrl %cl, %esi
movl %edx, %ecx
shll %cl, %esi
movq 0x8(%rsp), %rax
movl 0x20(%rsp), %ecx
orl %esi, (%rax,%rcx,4)
incl 0x28(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x12dce
movl %eax, 0x34(%rsp)
incq %r14
incl %ebx
cmpq 0x60(%rsp), %r14
jne 0xbc1f
testb $0x1, %bpl
movq 0x38(%rsp), %r13
je 0xbf7c
movq 0x10(%r13), %rbx
testq %rbx, %rbx
je 0xbe6a
movq %rbx, %rdi
callq 0x12aae
movl $0x48, %esi
movq %rbx, %rdi
callq 0x3270
movq $0x0, 0x10(%r13)
movq 0x18(%r13), %rbx
testq %rbx, %rbx
je 0xbe90
movq %rbx, %rdi
callq 0x112b6
movl $0x28, %esi
movq %rbx, %rdi
callq 0x3270
movq $0x0, 0x18(%r13)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rbx
testq %rbx, %rbx
je 0xbebb
movq %rbx, %rdi
callq 0x12aae
movl $0x48, %esi
movq %rbx, %rdi
callq 0x3270
movq 0x40(%rsp), %rax
movq $0x0, 0x10(%rax)
movq 0x18(%rax), %rbx
testq %rbx, %rbx
je 0xbee6
movq %rbx, %rdi
callq 0x112b6
movl $0x28, %esi
movq %rbx, %rdi
callq 0x3270
movq 0x40(%rsp), %rax
movq $0x0, 0x18(%rax)
movl $0x48, %edi
callq 0x3260
movq 0x50(%rsp), %rbx
movq %rax, %r14
movl 0x8(%rbx), %edx
leaq 0xe0(%rsp), %rsi
movq %rax, %rdi
callq 0x12aa8
movq %r14, 0x10(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x11476
movq %rax, 0x18(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x11462
leaq 0xe0(%rsp), %rdi
callq 0x134fc
leaq 0x150(%rsp), %rdi
callq 0x12ac6
leaq 0x68(%rsp), %rdi
callq 0x12ac6
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rsp), %r13
leaq 0xe0(%rsp), %r14
leaq 0x68(%rsp), %r12
movq %r14, %rdi
movl %ebx, %esi
callq 0x13502
incl 0x20(%r15)
movl 0xc(%r15), %edx
movl 0x1c(%r15), %ecx
xorl %esi, %esi
movl %edx, %eax
subl %ecx, %eax
jae 0xbfab
movl %edx, %eax
jmp 0xbfd3
movq (%r15), %rdx
movl 0x18(%r15), %edi
movl (%rdx,%rdi,4), %esi
negb %cl
shll %cl, %esi
shrl %cl, %esi
movl %eax, %ecx
shll %cl, %esi
leal 0x1(%rdi), %ecx
movl %ecx, 0x18(%r15)
movl $0x20, 0x1c(%r15)
movl $0x20, %ecx
testl %eax, %eax
je 0xbff3
subl %eax, %ecx
movl %ecx, 0x1c(%r15)
movq (%r15), %rdx
movl 0x18(%r15), %edi
movl (%rdx,%rdi,4), %edx
shrl %cl, %edx
negb %al
movl %eax, %ecx
shll %cl, %edx
shrl %cl, %edx
orl %edx, %esi
movl 0x14(%rsp), %ecx
movl 0x24(%rsp), %edx
movl %ecx, %eax
subl %edx, %eax
jae 0xc005
movl %ecx, %eax
jmp 0xc030
movl %esi, %edi
movl %eax, %ecx
shrl %cl, %edi
negb %dl
movl %edx, %ecx
shll %cl, %edi
shrl %cl, %edi
movq 0x8(%rsp), %rcx
movl 0x20(%rsp), %edx
orl %edi, (%rcx,%rdx,4)
incl 0x20(%rsp)
movl $0x20, 0x24(%rsp)
movl $0x20, %edx
testl %eax, %eax
je 0xc052
subl %eax, %edx
movl %edx, 0x24(%rsp)
negb %al
movl %eax, %ecx
shll %cl, %esi
shrl %cl, %esi
movl %edx, %ecx
shll %cl, %esi
movq 0x8(%rsp), %rax
movl 0x20(%rsp), %ecx
orl %esi, (%rax,%rcx,4)
incl 0x28(%rsp)
movl 0x7c(%rsp), %eax
addl 0x74(%rsp), %eax
movq 0x68(%rsp), %rcx
movl 0x4(%rcx), %ecx
decl %ecx
cmpl %ecx, %eax
jae 0xbe59
movq %r12, %rdi
callq 0x12dce
movl %eax, %ebx
addl 0x30(%rsp), %ebx
jmp 0xbf89
movq %rax, %rbx
movl $0x48, %esi
movq %r14, %rdi
callq 0x3270
jmp 0xc0b1
movq %rax, %rbx
jmp 0xc0bb
jmp 0xc09e
jmp 0xc09e
movq %rax, %rbx
jmp 0xc0c8
movq %rax, %rbx
jmp 0xc0d5
jmp 0xc0ae
jmp 0xc0ae
jmp 0xc0ae
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x11462
leaq 0xe0(%rsp), %rdi
callq 0x134fc
leaq 0x150(%rsp), %rdi
callq 0x12ac6
leaq 0x68(%rsp), %rdi
callq 0x12ac6
movq %rbx, %rdi
callq 0x3460
nop