// File: /workspace/github_cpp_makefile_star2_O1/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
movl %r9d, 0x34(%rsp)
movq %rsi, 0x48(%rsp)
cmpb $0x0, (%rsi)
jne 0xb9ce
movq %rdx, %rbx
cmpb $0x1, (%rdx)
jne 0xba21
movq 0x1361b(%rip), %rbx     # 0x1eff0
leaq 0xbcab(%rip), %rsi      # 0x17687
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 %r8d, %r15d
movq %rcx, %r13
movq %rdi, 0x58(%rsp)
movq 0x48(%rsp), %r14
movq 0x10(%r14), %rsi
leaq 0x68(%rsp), %rdi
callq 0x12744
movq 0x10(%rbx), %rsi
leaq 0x150(%rsp), %rdi
callq 0x12744
movq 0x18(%r14), %rbp
movq %rbx, 0x40(%rsp)
movq 0x18(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x128f8
movl %eax, %ebx
leaq 0x150(%rsp), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x128f8
movl %eax, 0x30(%rsp)
movq 0x48(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x54(%rsp)
movabsq $0x2000000000, %rax     # imm = 0x2000000000
movq %rax, 0x18(%rbp)
movl %r14d, 0x20(%rbp)
movq 0x38(%rsp), %rcx
movq %rax, 0x18(%rcx)
movl %r14d, 0x20(%rcx)
leaq 0xe0(%rsp), %rdi
movl $0x10, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x13026
movq 0x58(%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 0x11092
testl %r15d, %r15d
movl %r15d, 0x50(%rsp)
je 0xbd33
movl %r15d, %eax
movq %rax, 0x60(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
testb $0x1, %r15b
jne 0xbc25
movl %r15d, %r12d
movl (%r13,%r14,4), %eax
subl 0x34(%rsp), %eax
cmpl %eax, %ebx
jae 0xbc22
leaq 0xe0(%rsp), %rdi
movl %ebx, %esi
callq 0x13036
incl 0x20(%rbp)
movl 0xc(%rbp), %eax
movl 0x1c(%rbp), %ecx
xorl %esi, %esi
movl %eax, %edx
subl %ecx, %edx
jb 0xbb68
movq (%rbp), %rax
movl 0x18(%rbp), %edi
movl (%rax,%rdi,4), %esi
negb %cl
shll %cl, %esi
shrl %cl, %esi
movl %edx, %ecx
shll %cl, %esi
leal 0x1(%rdi), %eax
movl %eax, 0x18(%rbp)
movl $0x20, 0x1c(%rbp)
movl %edx, %eax
testl %eax, %eax
je 0xbb8a
movl 0x1c(%rbp), %ecx
subl %eax, %ecx
movl %ecx, 0x1c(%rbp)
movq (%rbp), %rdx
movl 0x18(%rbp), %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
jb 0xbbc0
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 %eax, %ecx
testl %ecx, %ecx
je 0xbbe4
movl 0x24(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x24(%rsp)
negb %cl
shll %cl, %esi
shrl %cl, %esi
movl %eax, %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
movb $0x1, %r15b
cmpl %ecx, %eax
jae 0xbc13
leaq 0x68(%rsp), %rdi
callq 0x12a58
movl %eax, %ebx
addl %r14d, %ebx
movl %r12d, %r15d
movl %r15d, %r12d
testb $0x1, %r15b
je 0xbb14
jmp 0xbc25
movl %r12d, %r15d
movl 0x30(%rsp), %eax
cmpq %rax, %r14
jne 0xbd21
movl (%r13,%r14,4), %esi
subl 0x34(%rsp), %esi
leaq 0xe0(%rsp), %rdi
callq 0x13036
movq 0x38(%rsp), %rcx
incl 0x20(%rcx)
movl 0xc(%rcx), %eax
movl 0x1c(%rcx), %ecx
xorl %esi, %esi
movl %eax, %edx
subl %ecx, %edx
jb 0xbc88
movq 0x38(%rsp), %r8
movq (%r8), %rax
movl 0x18(%r8), %edi
movl (%rax,%rdi,4), %esi
negb %cl
shll %cl, %esi
shrl %cl, %esi
movl %edx, %ecx
shll %cl, %esi
leal 0x1(%rdi), %eax
movl %eax, 0x18(%r8)
movl $0x20, 0x1c(%r8)
movl %edx, %eax
testl %eax, %eax
je 0xbcae
movq 0x38(%rsp), %rdi
movl 0x1c(%rdi), %ecx
subl %eax, %ecx
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 0x54(%rsp), %esi
movl 0x14(%rsp), %ecx
movl 0x24(%rsp), %edx
movl %ecx, %eax
subl %edx, %eax
jb 0xbce8
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 %eax, %ecx
testl %ecx, %ecx
je 0xbd0c
movl 0x24(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x24(%rsp)
negb %cl
shll %cl, %esi
shrl %cl, %esi
movl %eax, %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 0x12a58
movl %eax, 0x30(%rsp)
incq %r14
incl %ebx
cmpq 0x60(%rsp), %r14
jne 0xbb07
jmp 0xbd36
xorl %r15d, %r15d
testb $0x1, %r15b
movq 0x48(%rsp), %r13
jne 0xbe3e
leaq 0xe0(%rsp), %r14
leaq 0x68(%rsp), %r12
movq %r14, %rdi
movl %ebx, %esi
callq 0x13036
incl 0x20(%rbp)
movl 0xc(%rbp), %eax
movl 0x1c(%rbp), %ecx
xorl %esi, %esi
movl %eax, %edx
subl %ecx, %edx
jb 0xbd90
movq (%rbp), %rax
movl 0x18(%rbp), %edi
movl (%rax,%rdi,4), %esi
negb %cl
shll %cl, %esi
shrl %cl, %esi
movl %edx, %ecx
shll %cl, %esi
leal 0x1(%rdi), %eax
movl %eax, 0x18(%rbp)
movl $0x20, 0x1c(%rbp)
movl %edx, %eax
testl %eax, %eax
je 0xbdb2
movl 0x1c(%rbp), %ecx
subl %eax, %ecx
movl %ecx, 0x1c(%rbp)
movq (%rbp), %rdx
movl 0x18(%rbp), %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
jb 0xbde8
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 %eax, %ecx
testl %ecx, %ecx
je 0xbe0c
movl 0x24(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x24(%rsp)
negb %cl
shll %cl, %esi
shrl %cl, %esi
movl %eax, %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 0xbe3e
movq %r12, %rdi
callq 0x12a58
movl %eax, %ebx
addl 0x50(%rsp), %ebx
testb $0x1, %r15b
je 0xbd52
movq 0x10(%r13), %rbx
testq %rbx, %rbx
je 0xbe5c
movq %rbx, %rdi
callq 0x12732
movl $0x48, %esi
movq %rbx, %rdi
callq 0x3270
movq $0x0, 0x10(%r13)
movq 0x18(%r13), %rbx
testq %rbx, %rbx
je 0xbe82
movq %rbx, %rdi
callq 0x10f96
movl $0x28, %esi
movq %rbx, %rdi
callq 0x3270
movq $0x0, 0x18(%r13)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rbx
testq %rbx, %rbx
je 0xbead
movq %rbx, %rdi
callq 0x12732
movl $0x48, %esi
movq %rbx, %rdi
callq 0x3270
movq 0x40(%rsp), %rax
movq $0x0, 0x10(%rax)
movq 0x18(%rax), %rbx
testq %rbx, %rbx
je 0xbed8
movq %rbx, %rdi
callq 0x10f96
movl $0x28, %esi
movq %rbx, %rdi
callq 0x3270
movq 0x40(%rsp), %rax
movq $0x0, 0x18(%rax)
movl $0x48, %edi
callq 0x3260
movq 0x58(%rsp), %rbx
movq %rax, %r14
movl 0x8(%rbx), %edx
leaq 0xe0(%rsp), %rsi
movq %rax, %rdi
callq 0x1272c
movq %r14, 0x10(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x11156
movq %rax, 0x18(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x11142
leaq 0xe0(%rsp), %rdi
callq 0x13030
leaq 0x150(%rsp), %rdi
callq 0x1274a
leaq 0x68(%rsp), %rdi
callq 0x1274a
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x48, %esi
movq %r14, %rdi
callq 0x3270
jmp 0xbf8a
movq %rax, %rbx
jmp 0xbf94
jmp 0xbf77
jmp 0xbf77
movq %rax, %rbx
jmp 0xbfa1
movq %rax, %rbx
jmp 0xbfae
jmp 0xbf87
jmp 0xbf87
jmp 0xbf87
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x11142
leaq 0xe0(%rsp), %rdi
callq 0x13030
leaq 0x150(%rsp), %rdi
callq 0x1274a
leaq 0x68(%rsp), %rdi
callq 0x1274a
movq %rbx, %rdi
callq 0x3460