// File: /workspace/github_c_makefile_star2_O1/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
movl %esi, %ebp
movq %rdi, %rbx
movq 0x50f8(%rdi), %r14
movslq 0x20(%rdi), %rdx
testq %rdx, %rdx
je 0x4899
movq 0x5118(%rbx), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2150
cmpl $0x0, 0x44(%rbx)
sete %al
movl %ebp, %r13d
xorb $0x1, %r13b
orb %r13b, %al
movl %ebp, 0x4(%rsp)
jne 0x48bd
movq %rbx, %rdi
callq 0x3f6f
movq %rax, %r15
jmp 0x48c0
xorl %r15d, %r15d
movq 0x28(%rbx), %rdx
testq %rdx, %rdx
je 0x492f
xorl %ebp, %ebp
movq 0x50f8(%rbx), %rax
movzwl (%rax,%rbp,2), %r12d
andl $0x7f, %r12d
cmpl $0x0, 0x44(%rbx)
sete %al
orb %r13b, %al
jne 0x48fb
movl %r12d, %edi
movq %r15, %rsi
callq 0x2180
cmpl $-0x1, %eax
je 0x4994
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
movq 0x28(%rbx), %rdx
cmpq %rdx, %rbp
jb 0x48cb
cmpb $0x0, 0x4(%rsp)
je 0x495f
cmpq $0x0, 0x28(%rbx)
je 0x496d
xorl %r12d, %r12d
movzbl (%r14,%r12), %edi
callq 0xc4f3
movq (%rbx), %rcx
movq (%rcx), %rcx
movb %al, (%rcx,%r12)
incq %r12
cmpq 0x28(%rbx), %r12
jb 0x4940
jmp 0x496d
movq (%rbx), %rax
movq (%rax), %rdi
movq %r14, %rsi
callq 0x21f0
cmpl $0x0, 0x44(%rbx)
sete %al
orb %r13b, %al
jne 0x4985
movq %r15, %rdi
callq 0x20e0
testl %eax, %eax
jne 0x49a0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa57a(%rip), %rdi      # 0xef15
callq 0x3973
leaq 0xa5a0(%rip), %rdi      # 0xef47
callq 0x3973