// File: /workspace/github_c_makefile_star2_O1/felipelouza[P]egap/util.c
// Function: mergeBWTandLCP256
void mergeBWTandLCP256(g_data *g, bool lastRound)
{
  if(sizeof(symbol)>sizeof(g->array32[0])) die("Sorry, alphabet is too large (mergeBWTandLCP256)");
  symbol *bwtout = (symbol *) g->array32;   // merged BWT stored in g->array32
  assert(!g->lcpMerge);
  FILE *daOutFile=NULL;
  FILE *lcpfile = NULL;
  int shift = 8;

  // if last round open file for LCP values if requested
  if(lastRound && g->lcpCompute) {
    char filename[Filename_size];
    snprintf(filename,Filename_size,"%s.%s",g->outPath,LCP_EXT);
    lcpfile = fopen(filename,"wb");
    if(lcpfile==NULL) {perror("Unable to open LCP output file"); die(__func__);}
  }
  
  check_g_data(g);
  array_clear(g->inCnt,g->numBwt,0);
  if(g->outputDA && lastRound)
    daOutFile = openDAFile(g);  
  for (customInt i = 0; i < g->mergeLen; ++i) {
    int currentColor = (g->array32[i]) & 0xFF;
    assert(currentColor == ((g->array32[i]>>shift) & 0xFF));// merge and newMerge should be identical 
    assert(currentColor < g->numBwt);
    // if requested output merge array
    if(g->outputDA && lastRound) 
      if(fputc(currentColor, daOutFile)==EOF)
        die("mergeBWTandLCP256: Error writing to Document Array file");       
    // get next BWT char
    symbol c = g->bws[currentColor][g->inCnt[currentColor]];
    // write LCP to file if requested
    if(lcpfile!=NULL) {
      uint64_t curB = (g->array32[i]>>(2*shift)) & 0xFFFF;
      if(curB==0) die("Illegal value in B-array");
      curB -= 1; // obtain true LCP
      if(curB > MAX_LCP_SIZE) die("LCP value too large for requested lcp size");  
      // only valid for little endian architectures!!!!
      size_t e = fwrite(&curB,BSIZE,1,lcpfile);
      if(e!=1) {perror("Error writing LCP values"); die(__func__);}
    }
    // save new BWT char overwriting array32[i]
    bwtout[i] =  c; 
    g->inCnt[currentColor]++; // one more char read from currentColor BWT
  }
  // merging done
  if(lcpfile!=NULL) fclose(lcpfile);
  // copy the merged bwt back to 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("mergeBWTandLCP256: Error closing Document Array file");   

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1028, %rsp           # imm = 0x1028
movl %esi, %ebp
movq %rdi, %rbx
movq 0x5100(%rdi), %r13
testl %esi, %esi
je 0x502a
cmpb $0x1, 0x3e(%rbx)
jne 0x502a
movq 0x80(%rbx), %rcx
leaq 0x992c(%rip), %rdx      # 0xe91e
leaq 0x9ccc(%rip), %r8       # 0xecc5
leaq 0x20(%rsp), %r15
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
xorl %eax, %eax
callq 0x2140
leaq 0x991a(%rip), %rsi      # 0xe92e
movq %r15, %rdi
callq 0x21e0
testq %rax, %rax
je 0x51fb
movq %rax, %r12
jmp 0x502d
xorl %r12d, %r12d
movslq 0x20(%rbx), %rdx
testq %rdx, %rdx
je 0x5048
movq 0x5118(%rbx), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2150
cmpl $0x0, 0x44(%rbx)
sete %al
movl %ebp, %ecx
xorb $0x1, %cl
movb %cl, 0xf(%rsp)
orb %cl, %al
movl %ebp, 0x1c(%rsp)
jne 0x506f
movq %rbx, %rdi
callq 0x3f6f
movq %rax, 0x10(%rsp)
jmp 0x5078
movq $0x0, 0x10(%rsp)
cmpq $0x0, 0x28(%rbx)
je 0x5149
xorl %r15d, %r15d
movq 0x5100(%rbx), %rax
movzbl (%rax,%r15,4), %ebp
cmpl $0x0, 0x44(%rbx)
sete %al
orb 0xf(%rsp), %al
jne 0x50b4
movl %ebp, %edi
movq 0x10(%rsp), %rsi
callq 0x2180
cmpl $-0x1, %eax
je 0x51e3
leal (,%rbp,8), %eax
movq (%rbx), %rcx
movq (%rcx,%rax), %rcx
movq 0x5118(%rbx), %rdx
movq (%rdx,%rax), %rax
movb (%rcx,%rax), %r14b
testq %r12, %r12
je 0x512c
movq 0x5100(%rbx), %rax
movl (%rax,%r15,4), %ecx
movl %ecx, %eax
shrl $0x10, %eax
movq %rax, 0x20(%rsp)
cmpl $0xffff, %ecx           # imm = 0xFFFF
jbe 0x51c6
decq %rax
movq %rax, 0x20(%rsp)
cmpq $0x100, %rax            # imm = 0x100
jae 0x51d2
movl $0x1, %esi
movl $0x1, %edx
leaq 0x20(%rsp), %rdi
movq %r12, %rcx
callq 0x2390
cmpq $0x1, %rax
jne 0x51de
movb %r14b, (%r13,%r15)
movq 0x5118(%rbx), %rax
incq (%rax,%rbp,8)
incq %r15
cmpq 0x28(%rbx), %r15
jb 0x5086
testq %r12, %r12
je 0x5156
movq %r12, %rdi
callq 0x20e0
cmpb $0x0, 0x1c(%rsp)
je 0x5187
cmpq $0x0, 0x28(%rbx)
je 0x5199
xorl %r14d, %r14d
movzbl (%r13,%r14), %edi
callq 0xc4f3
movq (%rbx), %rcx
movq (%rcx), %rcx
movb %al, (%rcx,%r14)
incq %r14
cmpq 0x28(%rbx), %r14
jb 0x5167
jmp 0x5199
movq (%rbx), %rax
movq 0x28(%rbx), %rdx
movq (%rax), %rdi
movq %r13, %rsi
callq 0x21f0
cmpl $0x0, 0x44(%rbx)
sete %al
orb 0xf(%rsp), %al
jne 0x51b4
movq 0x10(%rsp), %rdi
callq 0x20e0
testl %eax, %eax
jne 0x51ef
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9f59(%rip), %rdi      # 0xf126
callq 0x3973
leaq 0x9f66(%rip), %rdi      # 0xf13f
callq 0x3973
callq 0x24b5
leaq 0x9f04(%rip), %rdi      # 0xf0ee
callq 0x3973
leaq 0x9f8d(%rip), %rdi      # 0xf183
callq 0x3973
callq 0x24ce