// File: /workspace/github_cpp_makefile_star2_O2/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, 0x4(%rsp)
movl %ecx, %r15d
movq %rdi, %rax
orq %rsi, %rax
sete %al
movq %rdx, 0x30(%rsp)
testq %rdx, %rdx
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x86f8
movl %r9d, %r13d
movq %rsi, 0x28(%rsp)
testq %rdi, %rdi
movq %rdi, 0x38(%rsp)
je 0x84ce
movq %rdi, %rbx
pushq $0x78
popq %rdi
callq 0x3220
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0xfd04
movl $0x0, 0x8(%rsp)
movq %r12, %rdi
xorl %esi, %esi
movl 0x4(%rsp), %edx
callq 0x1004a
movq %rax, %r14
movq %rax, %rbp
shrq $0x20, %rbp
incl %ebp
jmp 0x84de
movb $0x1, %al
movl %eax, 0x8(%rsp)
movl 0x4(%rsp), %eax
movl %eax, %r14d
xorl %r12d, %r12d
movq 0x28(%rsp), %rbx
testq %rbx, %rbx
je 0x851d
movl %r13d, 0x18(%rsp)
pushq $0x78
popq %rdi
callq 0x3220
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
callq 0xfd04
movq %r13, 0x10(%rsp)
movq %r13, %rdi
xorl %esi, %esi
callq 0xfdfc
movl %eax, 0xc(%rsp)
movl 0x18(%rsp), %r13d
jmp 0x852b
movq $0x0, 0x10(%rsp)
movl %r15d, 0xc(%rsp)
leaq 0x40(%rsp), %rdi
movl %r13d, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x10178
movl %r15d, 0x24(%rsp)
movl %r15d, %eax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
cmpq 0x18(%rsp), %r15
je 0x8603
movl %ebp, %eax
movl %eax, %ebp
testb $0x1, 0x8(%rsp)
jne 0x85d1
leal (%r14,%r15), %esi
movq 0x30(%rsp), %rax
movl (%rax,%r15,4), %r13d
subl %esi, %r13d
jbe 0x85d1
cmpl %ebp, %r13d
movl %ebp, %ebx
cmovbl %r13d, %ebx
leaq 0x40(%rsp), %rdi
movl %ebx, %edx
callq 0x10294
addl %ebx, %r14d
movl %ebp, %eax
subl %ebx, %eax
cmpl %r13d, %ebp
movl %eax, %ebp
ja 0x855c
movl 0x14(%r12), %ecx
addl 0xc(%r12), %ecx
movq (%r12), %rdx
movl 0x4(%rdx), %edx
decl %edx
cmpl %edx, %ecx
jae 0x85c9
movq %r12, %rdi
movl 0x4(%rsp), %esi
callq 0x10092
movq %rax, %r14
movq %rax, %rbp
shrq $0x20, %rbp
incl %ebp
jmp 0x855c
movb $0x1, %cl
movl %ecx, 0x8(%rsp)
jmp 0x855a
movl 0xc(%rsp), %eax
cmpq %rax, %r15
jne 0x85fb
movq 0x30(%rsp), %rax
movl (%rax,%r15,4), %esi
leaq 0x40(%rsp), %rdi
callq 0x1028c
movq 0x10(%rsp), %rdi
callq 0xfe84
movl %eax, 0xc(%rsp)
incq %r15
jmp 0x854d
leaq 0x40(%rsp), %r15
addl 0x24(%rsp), %r14d
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rsp)
testb $0x1, %al
jne 0x8659
movq %r15, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x10294
movl 0x14(%r12), %ecx
addl 0xc(%r12), %ecx
movq (%r12), %rax
movl 0x4(%rax), %edx
decl %edx
movb $0x1, %al
cmpl %edx, %ecx
jae 0x8611
movq %r12, %rdi
movl 0x4(%rsp), %esi
callq 0x10092
movq %rax, %r14
movq %rax, %rbp
shrq $0x20, %rbp
incl %ebp
jmp 0x8608
testq %r12, %r12
je 0x8666
movq %r12, %rdi
callq 0xfd0a
pushq $0x78
popq %rsi
movq %r12, %rdi
callq 0x3230
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x8683
movq %rbx, %rdi
callq 0xfd0a
pushq $0x78
popq %rsi
movq %rbx, %rdi
callq 0x3230
movq 0x38(%rsp), %r14
testq %r14, %r14
movq 0x28(%rsp), %rbx
je 0x86a5
movq %r14, %rdi
callq 0xfcf2
pushq $0x48
popq %rsi
movq %r14, %rdi
callq 0x3230
testq %rbx, %rbx
je 0x86bd
movq %rbx, %rdi
callq 0xfcf2
pushq $0x48
popq %rsi
movq %rbx, %rdi
callq 0x3230
leaq 0x40(%rsp), %rdi
callq 0x102cc
pushq $0x48
popq %rdi
callq 0x3220
movq %rax, %rbp
leaq 0x40(%rsp), %rsi
movq %rax, %rdi
movl 0x4(%rsp), %edx
callq 0xfcec
leaq 0x40(%rsp), %rdi
callq 0x1018e
movq %rbp, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x78
popq %rsi
movq %r13, %rdi
jmp 0x8721
movq %rax, %rbx
pushq $0x78
popq %rsi
movq %r12, %rdi
callq 0x3230
jmp 0x874f
jmp 0x8742
movq %rax, %rbx
pushq $0x48
popq %rsi
movq %rbp, %rdi
callq 0x3230
jmp 0x8745
jmp 0x8742
jmp 0x8742
jmp 0x8742
jmp 0x8742
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x1018e
movq %rbx, %rdi
callq 0x3410
nop