// File: /workspace/github_c_makefile_star2_O3/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, %ebx
movq %rdi, %r14
movq 0x5100(%rdi), %r13
testl %esi, %esi
je 0x50c8
cmpb $0x1, 0x3e(%r14)
jne 0x50c8
movq 0x80(%r14), %rcx
leaq 0x988e(%rip), %rdx      # 0xe91e
leaq 0x9c2e(%rip), %r8       # 0xecc5
leaq 0x20(%rsp), %r15
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
xorl %eax, %eax
callq 0x2140
leaq 0x987c(%rip), %rsi      # 0xe92e
movq %r15, %rdi
callq 0x21e0
testq %rax, %rax
je 0x5295
movq %rax, %r15
jmp 0x50cb
xorl %r15d, %r15d
movslq 0x20(%r14), %rdx
testq %rdx, %rdx
je 0x50e6
movq 0x5118(%r14), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2150
cmpl $0x0, 0x44(%r14)
sete %al
movl %ebx, %ecx
xorb $0x1, %cl
movb %cl, 0xf(%rsp)
orb %cl, %al
movl %ebx, 0x1c(%rsp)
jne 0x510e
movq %r14, %rdi
callq 0x3fea
movq %rax, 0x10(%rsp)
jmp 0x5117
movq $0x0, 0x10(%rsp)
cmpq $0x0, 0x28(%r14)
je 0x51e1
xorl %r12d, %r12d
movq 0x5100(%r14), %rax
movzbl (%rax,%r12,4), %ebp
cmpl $0x0, 0x44(%r14)
sete %al
orb 0xf(%rsp), %al
jne 0x5154
movl %ebp, %edi
movq 0x10(%rsp), %rsi
callq 0x2180
cmpl $-0x1, %eax
je 0x5289
leal (,%rbp,8), %eax
movq (%r14), %rcx
movq (%rcx,%rax), %rcx
movq 0x5118(%r14), %rdx
movq (%rdx,%rax), %rax
movb (%rcx,%rax), %bl
testq %r15, %r15
je 0x51c4
movq 0x5100(%r14), %rax
movl (%rax,%r12,4), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
jbe 0x526c
shrl $0x10, %eax
decq %rax
movq %rax, 0x20(%rsp)
cmpq $0x100, %rax            # imm = 0x100
jae 0x5278
movl $0x1, %esi
movl $0x1, %edx
leaq 0x20(%rsp), %rdi
movq %r15, %rcx
callq 0x2390
cmpq $0x1, %rax
jne 0x5284
movb %bl, (%r13,%r12)
movq 0x5118(%r14), %rax
incq (%rax,%rbp,8)
incq %r12
cmpq 0x28(%r14), %r12
jb 0x5125
testq %r15, %r15
je 0x51ee
movq %r15, %rdi
callq 0x20e0
cmpb $0x0, 0x1c(%rsp)
je 0x5249
cmpq $0x0, 0x28(%r14)
je 0x521b
xorl %ebx, %ebx
movzbl (%r13,%rbx), %edi
callq 0xc5af
movq (%r14), %rcx
movq (%rcx), %rcx
movb %al, (%rcx,%rbx)
incq %rbx
cmpq 0x28(%r14), %rbx
jb 0x51fe
cmpl $0x0, 0x44(%r14)
sete %al
orb 0xf(%rsp), %al
jne 0x5237
movq 0x10(%rsp), %rdi
callq 0x20e0
testl %eax, %eax
jne 0x529a
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rax
movq 0x28(%r14), %rdx
movq (%rax), %rdi
movq %r13, %rsi
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21f0
leaq 0x9eb3(%rip), %rdi      # 0xf126
callq 0x39fc
leaq 0x9ec0(%rip), %rdi      # 0xf13f
callq 0x39fc
callq 0x24b5
leaq 0x9e5e(%rip), %rdi      # 0xf0ee
callq 0x39fc
callq 0x24ce
leaq 0x9ee2(%rip), %rdi      # 0xf183
callq 0x39fc