// File: /workspace/github_cpp_makefile_star2_O3/adamnovak[P]rlcsa/./bits/vectors.h
// Function: CSA::RLEVector* CSA::mergeVectors<CSA::RLEVector, CSA::RLEEncoder, CSA::RLEVector::Iterator>(CSA::RLEVector*, CSA::RLEVector*, unsigned int*, unsigned int, unsigned int, unsigned int)
V*
mergeVectors(V* first, V* second, usint* positions, usint n, usint size, usint block_size)
{
  if((first == 0 && second == 0) || positions == 0) { return 0; }

  I* first_iter = 0;
  I* second_iter = 0;

  pair_type first_run;
  bool first_finished;
  if(first == 0)
  {
    first_run = pair_type(size, 0);
    first_finished = true;
  }
  else
  {
    first_iter = new I(*first);
    first_run = first_iter->selectRun(0, size);
    first_run.second++;
    first_finished = false;
  }

  usint second_bit;
  if(second == 0)
  {
    second_bit = n;
  }
  else
  {
    second_iter = new I(*second);
    second_bit = second_iter->select(0);
  }

  E encoder(block_size);
  for(usint i = 0; i < n; i++)
  {
    while(!first_finished && first_run.first + i < positions[i])
    {
      usint bits = std::min(first_run.second, positions[i] - i - first_run.first);
      encoder.addRun(first_run.first + i, bits);
      first_run.first += bits;
      first_run.second -= bits;
      if(first_run.second == 0)
      {
        if(first_iter->hasNext())
        {
          first_run = first_iter->selectNextRun(size);
          first_run.second++;
        }
        else { first_finished = true; }
      }
    }

    if(i == second_bit) // positions[i] is one
    {
      encoder.addBit(positions[i]);
      second_bit = second_iter->selectNext();
    }
  }

  while(!first_finished)
  {
    encoder.addRun(first_run.first + n, first_run.second);
    if(first_iter->hasNext())
    {
      first_run = first_iter->selectNextRun(size);
      first_run.second++;
    }
    else { first_finished = true; }
  }

  delete first_iter; delete second_iter;
  delete first; delete second;
  encoder.flush();
  return new V(encoder, size);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %r8d, 0xc(%rsp)
movl %ecx, 0x10(%rsp)
movq %rdi, %rax
orq %rsi, %rax
sete %al
movq %rdx, 0x28(%rsp)
testq %rdx, %rdx
sete %cl
xorl %r14d, %r14d
orb %al, %cl
jne 0xa3c3
movl %r9d, %r13d
movq %rsi, %rbx
testq %rdi, %rdi
movq %rdi, 0x30(%rsp)
je 0xa188
movq %rdi, %r15
movl $0x78, %edi
callq 0x3260
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x1375c
movl $0x0, 0x8(%rsp)
movq %r12, %rdi
xorl %esi, %esi
movl 0xc(%rsp), %edx
callq 0x14468
movq %rax, %rbp
movq %rax, %r14
shrq $0x20, %r14
incl %r14d
jmp 0xa197
movb $0x1, %al
movl %eax, 0x8(%rsp)
movl 0xc(%rsp), %eax
movl %eax, %ebp
xorl %r12d, %r12d
testq %rbx, %rbx
je 0xa1ce
movl $0x78, %edi
callq 0x3260
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1375c
movq %r15, 0x18(%rsp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x13a48
movl %eax, 0x14(%rsp)
movl 0x10(%rsp), %r15d
jmp 0xa1e1
movq $0x0, 0x18(%rsp)
movl 0x10(%rsp), %r15d
movl %r15d, 0x14(%rsp)
movq %rbx, 0x20(%rsp)
leaq 0x40(%rsp), %rdi
movl %r13d, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x145e0
testl %r15d, %r15d
je 0xa2bf
movl %r15d, %eax
movq %rax, 0x38(%rsp)
xorl %ebx, %ebx
testb $0x1, 0x8(%rsp)
jne 0xa288
leal (%rbx,%rbp), %esi
movq 0x28(%rsp), %rax
movl (%rax,%rbx,4), %r15d
subl %esi, %r15d
jbe 0xa288
cmpl %r14d, %r15d
movl %r14d, %r13d
cmovbl %r15d, %r13d
leaq 0x40(%rsp), %rdi
movl %r13d, %edx
callq 0x147fe
addl %r13d, %ebp
movl %r14d, %eax
subl %r13d, %eax
cmpl %r15d, %r14d
jbe 0xa24d
movl %eax, %r14d
jmp 0xa20b
movl 0x14(%r12), %ecx
addl 0xc(%r12), %ecx
movq (%r12), %rdx
movl 0x4(%rdx), %edx
decl %edx
cmpl %edx, %ecx
jae 0xa27f
movq %r12, %rdi
movl 0xc(%rsp), %esi
callq 0x144b0
movq %rax, %rbp
movq %rax, %r14
shrq $0x20, %r14
incl %r14d
jmp 0xa20b
movb $0x1, %cl
movl %ecx, 0x8(%rsp)
movl %eax, %r14d
movl 0x14(%rsp), %eax
cmpq %rax, %rbx
jne 0xa2b1
movq 0x28(%rsp), %rax
movl (%rax,%rbx,4), %esi
leaq 0x40(%rsp), %rdi
callq 0x147f4
movq 0x18(%rsp), %rdi
callq 0x13cb8
movl %eax, 0x14(%rsp)
incq %rbx
cmpq 0x38(%rsp), %rbx
jne 0xa20b
testb $0x1, 0x8(%rsp)
je 0xa2d2
testq %r12, %r12
movq 0x20(%rsp), %rbx
jne 0xa324
jmp 0xa32c
leaq 0x40(%rsp), %r15
movq 0x20(%rsp), %rbx
addl 0x10(%rsp), %ebp
movq %r15, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x147fe
movl 0x14(%r12), %eax
addl 0xc(%r12), %eax
movq (%r12), %rcx
movl 0x4(%rcx), %ecx
decl %ecx
cmpl %ecx, %eax
jae 0xa324
movq %r12, %rdi
movl 0xc(%rsp), %esi
callq 0x144b0
movq %rax, %rbp
movq %rax, %r14
shrq $0x20, %r14
incl %r14d
testb $0x1, 0x8(%rsp)
je 0xa2dc
movq %r12, %rdi
callq 0x13762
movl $0x78, %esi
movq %r12, %rdi
callq 0x3270
movq 0x18(%rsp), %r14
testq %r14, %r14
je 0xa34b
movq %r14, %rdi
callq 0x13762
movl $0x78, %esi
movq %r14, %rdi
callq 0x3270
movq 0x30(%rsp), %r14
testq %r14, %r14
je 0xa36a
movq %r14, %rdi
callq 0x1374a
movl $0x48, %esi
movq %r14, %rdi
callq 0x3270
testq %rbx, %rbx
je 0xa384
movq %rbx, %rdi
callq 0x1374a
movl $0x48, %esi
movq %rbx, %rdi
callq 0x3270
leaq 0x40(%rsp), %rdi
callq 0x14836
movl $0x48, %edi
callq 0x3260
movq %rax, %r14
leaq 0x40(%rsp), %rsi
movq %rax, %rdi
movl 0xc(%rsp), %edx
callq 0x13744
leaq 0x40(%rsp), %rdi
callq 0x145f8
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x78, %esi
movq %r15, %rdi
jmp 0xa3f0
movq %rax, %rbx
movl $0x78, %esi
movq %r12, %rdi
callq 0x3270
jmp 0xa420
movq %rax, %rbx
movl $0x48, %esi
movq %r14, %rdi
callq 0x3270
jmp 0xa416
jmp 0xa413
jmp 0xa413
jmp 0xa413
jmp 0xa413
jmp 0xa413
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x145f8
movq %rbx, %rdi
callq 0x3460