// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movb %sil, %al
movq %rdi, 0x40(%rsp)
andb $0x1, %al
movb %al, 0x3f(%rsp)
movq 0x40(%rsp), %rax
movq 0x50f8(%rax), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x40(%rsp), %rdi
callq 0x3dd0
movq 0x40(%rsp), %rax
movq 0x5118(%rax), %rdi
movq 0x40(%rsp), %rax
movslq 0x20(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x3310
movq 0x40(%rsp), %rax
cmpl $0x0, 0x44(%rax)
je 0x5695
testb $0x1, 0x3f(%rsp)
je 0x5695
movq 0x40(%rsp), %rdi
callq 0x4870
movq %rax, 0x28(%rsp)
movl $0x7, 0x24(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq 0x40(%rsp), %rcx
cmpq 0x28(%rcx), %rax
jae 0x5783
movq 0x40(%rsp), %rax
movq 0x50f8(%rax), %rax
movq 0x18(%rsp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x7f, %eax
movl %eax, 0x14(%rsp)
movq 0x40(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0x56ea
testb $0x1, 0x3f(%rsp)
je 0x56ea
jmp 0x56ea
movq 0x40(%rsp), %rax
cmpl $0x0, 0x44(%rax)
je 0x571d
testb $0x1, 0x3f(%rsp)
je 0x571d
movl 0x14(%rsp), %edi
movq 0x28(%rsp), %rsi
callq 0x2170
cmpl $-0x1, %eax
jne 0x571b
leaq 0xf7e8(%rip), %rdi      # 0x14efe
callq 0x3ce0
jmp 0x571d
movq 0x40(%rsp), %rax
movq (%rax), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x40(%rsp), %rcx
movq 0x5118(%rcx), %rcx
movslq 0x14(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movb (%rax,%rcx), %dl
movq 0x30(%rsp), %rax
movq 0x18(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x40(%rsp), %rax
movq 0x5118(%rax), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
jmp 0x56a6
testb $0x1, 0x3f(%rsp)
je 0x57dd
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq 0x40(%rsp), %rcx
cmpq 0x28(%rcx), %rax
jae 0x57db
movq 0x30(%rsp), %rax
movq 0x8(%rsp), %rcx
movzbl (%rax,%rcx), %edi
callq 0x124e0
movb %al, %dl
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x8(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x8(%rsp)
jmp 0x5793
jmp 0x57ff
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %rax
movq 0x28(%rax), %rdx
shlq $0x0, %rdx
callq 0x21e0
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x5824
jmp 0x5817
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x5807
movq 0x40(%rsp), %rax
cmpl $0x0, 0x44(%rax)
je 0x5853
testb $0x1, 0x3f(%rsp)
je 0x5853
movq 0x28(%rsp), %rdi
callq 0x20d0
cmpl $0x0, %eax
je 0x5851
leaq 0xf6e4(%rip), %rdi      # 0x14f30
callq 0x3ce0
jmp 0x5853
addq $0x48, %rsp
retq
nopl (%rax,%rax)