// File: /workspace/github_c_makefile_star2_O3/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 0x4921
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
jne 0x4941
movq %rbx, %rdi
callq 0x3fea
movq %rax, %r15
jmp 0x4944
xorl %r15d, %r15d
cmpq $0x0, 0x28(%rbx)
je 0x49e7
movl %ebp, 0x4(%rsp)
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 0x4985
movl %r12d, %edi
movq %r15, %rsi
callq 0x2180
cmpl $-0x1, %eax
je 0x4a31
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 0x4955
cmpb $0x0, 0x4(%rsp)
je 0x4a15
testq %rdx, %rdx
je 0x49ec
xorl %r12d, %r12d
movzbl (%r14,%r12), %edi
callq 0xc5af
movq (%rbx), %rcx
movq (%rcx), %rcx
movb %al, (%rcx,%r12)
incq %r12
cmpq 0x28(%rbx), %r12
jb 0x49c8
jmp 0x49ec
testb %bpl, %bpl
je 0x4a13
cmpl $0x0, 0x44(%rbx)
sete %al
orb %r13b, %al
jne 0x4a04
movq %r15, %rdi
callq 0x20e0
testl %eax, %eax
jne 0x4a3d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edx, %edx
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
leaq 0xa4dd(%rip), %rdi      # 0xef15
callq 0x39fc
leaq 0xa503(%rip), %rdi      # 0xef47
callq 0x39fc