// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc8, %rsp
movl %r8d, 0x8(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdi, %rax
orq %rsi, %rax
sete %al
movq %rdx, 0x38(%rsp)
testq %rdx, %rdx
sete %cl
xorl %r14d, %r14d
orb %al, %cl
jne 0xa2b5
movl %r9d, %ebx
movq %rdi, %r15
testq %rdi, %rdi
movq %rsi, 0x30(%rsp)
movq %rdi, 0x40(%rsp)
je 0xa041
movl $0x78, %edi
callq 0x3260
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
callq 0x13394
xorl %r12d, %r12d
movq %r13, %rdi
xorl %esi, %esi
movl 0x8(%rsp), %edx
callq 0x13f52
movq %rax, %r14
shrq $0x20, %r14
incl %r14d
jmp 0xa04b
movb $0x1, %r12b
movl 0x8(%rsp), %eax
xorl %r13d, %r13d
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %r15
testq %r15, %r15
je 0xa08c
movl $0x78, %edi
callq 0x3260
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
callq 0x13394
movq %rbp, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
callq 0x1368a
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %r15
jmp 0xa09f
movq $0x0, 0x10(%rsp)
movq 0x18(%rsp), %r15
movl %r15d, 0xc(%rsp)
leaq 0x50(%rsp), %rdi
movl %ebx, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x14082
testl %r15d, %r15d
je 0xa1a9
movl %r15d, %eax
movq %rax, 0x48(%rsp)
xorl %ebx, %ebx
movq 0x28(%rsp), %rdx
movq %r13, 0x20(%rsp)
testb $0x1, %r12b
jne 0xa165
movl %r12d, %ebp
movl %r14d, %r13d
leal (%rdx,%rbx), %esi
movq 0x38(%rsp), %rax
movl (%rax,%rbx,4), %r15d
subl %esi, %r15d
jbe 0xa15f
movq %rdx, %r14
cmpl %r13d, %r15d
movl %r13d, %r12d
cmovbl %r15d, %r12d
leaq 0x50(%rsp), %rdi
movl %r12d, %edx
callq 0x1425a
movq %r14, %rdx
addl %r12d, %edx
movl %r13d, %r14d
subl %r12d, %r14d
cmpl %r15d, %r13d
ja 0xa14e
movq 0x20(%rsp), %rcx
movl 0x14(%rcx), %eax
addl 0xc(%rcx), %eax
movq (%rcx), %rcx
movl 0x4(%rcx), %ecx
decl %ecx
movb $0x1, %r12b
cmpl %ecx, %eax
jae 0xa151
movq 0x20(%rsp), %rdi
movl 0x8(%rsp), %esi
callq 0x13f88
movq %rax, %rdx
movq %rdx, %r14
shrq $0x20, %r14
incl %r14d
movl %ebp, %r12d
movl %r12d, %ebp
movl %r14d, %r13d
testb $0x1, %r12b
je 0xa0dd
jmp 0xa165
movl %r13d, %r14d
movl %ebp, %r12d
movl 0xc(%rsp), %eax
cmpq %rax, %rbx
movq 0x20(%rsp), %r13
jne 0xa199
movq %rdx, %r15
movq 0x38(%rsp), %rax
movl (%rax,%rbx,4), %esi
leaq 0x50(%rsp), %rdi
callq 0x14250
movq 0x10(%rsp), %rdi
callq 0x13900
movl %eax, 0xc(%rsp)
movq %r15, %rdx
incq %rbx
cmpq 0x48(%rsp), %rbx
jne 0xa0cd
jmp 0xa1ae
movq 0x28(%rsp), %rdx
testb $0x1, %r12b
jne 0xa20c
leaq 0x50(%rsp), %rbx
movq 0x18(%rsp), %rax
movq %rdx, %r15
leal (%rdx,%rax), %esi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x1425a
movl 0x14(%r13), %ecx
addl 0xc(%r13), %ecx
movq (%r13), %rax
movl 0x4(%rax), %edx
decl %edx
movb $0x1, %al
cmpl %edx, %ecx
movq %r15, %rdx
jae 0xa205
movq %r13, %rdi
movl 0x8(%rsp), %esi
callq 0x13f88
movq %rax, %rdx
movq %rdx, %r14
shrq $0x20, %r14
incl %r14d
movl %r12d, %eax
movl %eax, %r12d
testb $0x1, %al
je 0xa1b9
testq %r13, %r13
je 0xa226
movq %r13, %rdi
callq 0x1339a
movl $0x78, %esi
movq %r13, %rdi
callq 0x3270
movq 0x10(%rsp), %r15
testq %r15, %r15
movq 0x30(%rsp), %rbx
movq 0x40(%rsp), %r14
je 0xa24f
movq %r15, %rdi
callq 0x1339a
movl $0x78, %esi
movq %r15, %rdi
callq 0x3270
testq %r14, %r14
je 0xa269
movq %r14, %rdi
callq 0x13382
movl $0x48, %esi
movq %r14, %rdi
callq 0x3270
testq %rbx, %rbx
je 0xa283
movq %rbx, %rdi
callq 0x13382
movl $0x48, %esi
movq %rbx, %rdi
callq 0x3270
leaq 0x50(%rsp), %rdi
callq 0x14294
movl $0x48, %edi
callq 0x3260
movq %rax, %r14
leaq 0x50(%rsp), %rsi
movq %rax, %rdi
movl 0x8(%rsp), %edx
callq 0x1337c
leaq 0x50(%rsp), %rdi
callq 0x1409a
movq %r14, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x78, %esi
movq %rbp, %rdi
jmp 0xa2e2
movq %rax, %rbx
movl $0x78, %esi
movq %r13, %rdi
callq 0x3270
jmp 0xa312
movq %rax, %rbx
movl $0x48, %esi
movq %r14, %rdi
callq 0x3270
jmp 0xa308
jmp 0xa305
jmp 0xa305
jmp 0xa305
jmp 0xa305
jmp 0xa305
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x1409a
movq %rbx, %rdi
callq 0x3460