// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/util.c
// Function: mergeBWT128
void mergeBWT128(g_data *g, bool lastRound)
{
  if(sizeof(symbol)>2) die("Sorry, alphabet is too large (mergeBWT16)");
  symbol *bwtout = (symbol *) g->mergeColor16;   // merged BWT stored in mergeColor16
  assert(!g->lcpMerge);
  FILE *daOutFile=NULL;

  check_g_data(g);
  array_clear(g->inCnt,g->numBwt,0);
  if(g->outputDA && lastRound)
    daOutFile = openDAFile(g);  

  int shift = 7; (void) shift;  // shift is not used outside assertions 
  for (customInt i = 0; i < g->mergeLen; ++i) {
    int currentColor = g->mergeColor16[i] & 0x7F;
    assert(currentColor == ((g->mergeColor16[i]>>shift) & 0x7F)); // we must have mergeColor==newMergeColor
    assert(currentColor < g->numBwt);
    // check that all LCP values have been obtained
    if(g->lcpCompute && lastRound)
      assert( ((g->mergeColor16[i]>>2*shift) & 3)==3);
    // if requested output merge array
    if(g->outputDA && lastRound) 
      if(fputc(currentColor, daOutFile)==EOF)
        die("mergeBWT128: Error writing to Document Array file");   
    // save new BWT char overwriting mergeColor16[i]
    bwtout[i] = g->bws[currentColor][g->inCnt[currentColor]];
    g->inCnt[currentColor]++; // one more char read from currentColor BWT
  }
  // merging done: copy back to g->bws[0]
  if(lastRound)
    for(customInt i=0;i<g->mergeLen;i++)
      g->bws[0][i] = alpha_enlarge(bwtout[i]); // if last round remap while copying 
  else  
    memcpy(g->bws[0],bwtout,g->mergeLen*sizeof(symbol));  
  // final check
  for(int i=0;i<g->numBwt;i++) assert(g->inCnt[i]==g->bwtLen[i]);
  // close document array file 
  if(g->outputDA && lastRound)
      if(fclose(daOutFile)!=0) die("mergeBWT128: Error closing Document Array file");   
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x50f8(%rdi), %r14
movq 0x5118(%rdi), %rax
movslq 0x20(%rdi), %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x44a2
andq $0x0, (%rax,%rdx,8)
incq %rdx
jmp 0x4493
cmpl $0x0, 0x44(%rbx)
sete %al
movl %esi, %r13d
xorb $0x1, %r13b
orb %r13b, %al
movl %esi, 0x4(%rsp)
jne 0x44c6
movq %rbx, %rdi
callq 0x3b96
movq %rax, %r15
jmp 0x44c9
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x28(%rbx), %rdx
cmpq %rdx, %rbp
jae 0x4531
movq 0x50f8(%rbx), %rax
movzwl (%rax,%rbp,2), %r12d
andl $0x7f, %r12d
cmpl $0x0, 0x44(%rbx)
sete %al
orb %r13b, %al
jne 0x4504
movl %r12d, %edi
movq %r15, %rsi
callq 0x2180
cmpl $-0x1, %eax
je 0x45a0
movq (%rbx), %rax
shll $0x3, %r12d
movq (%rax,%r12), %rax
movq 0x5118(%rbx), %rcx
movq (%rcx,%r12), %rcx
movb (%rax,%rcx), %al
movb %al, (%r14,%rbp)
movq 0x5118(%rbx), %rax
incq (%rax,%r12)
incq %rbp
jmp 0x44cb
cmpb $0x0, 0x4(%rsp)
je 0x455d
xorl %r12d, %r12d
cmpq %rdx, %r12
jae 0x4579
movzbl (%r14,%r12), %edi
callq 0xbb6a
movq (%rbx), %rcx
movq (%rcx), %rcx
movb %al, (%rcx,%r12)
incq %r12
movq 0x28(%rbx), %rdx
jmp 0x453b
movq (%rbx), %rax
movq (%rax), %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21f0
cmpl $0x0, 0x44(%rbx)
sete %al
orb %r13b, %al
jne 0x4591
movq %r15, %rdi
callq 0x20e0
testl %eax, %eax
jne 0x45ac
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9933(%rip), %rdi      # 0xdeda
callq 0x35b7
leaq 0x9959(%rip), %rdi      # 0xdf0c
callq 0x35b7