// File: /workspace/github_c_makefile_star2_O2/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), %rax
movq %rax, 0x10(%rsp)
testl %esi, %esi
je 0x4c09
cmpb $0x1, 0x3e(%r14)
jne 0x4c09
movq 0x80(%r14), %rcx
leaq 0x8d4d(%rip), %rdx      # 0xd91e
leaq 0x90b2(%rip), %r8       # 0xdc8a
leaq 0x20(%rsp), %r15
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
xorl %eax, %eax
callq 0x2140
leaq 0x8d3b(%rip), %rsi      # 0xd92e
movq %r15, %rdi
callq 0x21e0
testq %rax, %rax
je 0x4de0
movq %rax, %r15
jmp 0x4c0c
xorl %r15d, %r15d
movq 0x5118(%r14), %rax
movslq 0x20(%r14), %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x4c28
andq $0x0, (%rax,%rdx,8)
incq %rdx
jmp 0x4c19
cmpl $0x0, 0x44(%r14)
sete %al
movl %ebx, %r12d
xorb $0x1, %r12b
orb %r12b, %al
movl %ebx, 0x1c(%rsp)
jne 0x4c4f
movq %r14, %rdi
callq 0x3b96
movq %rax, 0x8(%rsp)
jmp 0x4c58
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
cmpq 0x28(%r14), %rbp
jae 0x4d1a
movq 0x5100(%r14), %rax
movzbl (%rax,%rbp,4), %r13d
cmpl $0x0, 0x44(%r14)
sete %al
orb %r12b, %al
jne 0x4c93
movl %r13d, %edi
movq 0x8(%rsp), %rsi
callq 0x2180
cmpl $-0x1, %eax
je 0x4dd4
leal (,%r13,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 0x4cff
movq 0x5100(%r14), %rax
movl (%rax,%rbp,4), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
jbe 0x4dc8
shrl $0x10, %eax
decq %rax
movq %rax, 0x20(%rsp)
cmpq $0x100, %rax            # imm = 0x100
jae 0x4dbc
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq %r15, %rcx
callq 0x2390
cmpq $0x1, %rax
jne 0x4da4
movq 0x10(%rsp), %rax
movb %bl, (%rax,%rbp)
movq 0x5118(%r14), %rax
incq (%rax,%r13,8)
incq %rbp
jmp 0x4c5a
testq %r15, %r15
je 0x4d27
movq %r15, %rdi
callq 0x20e0
cmpb $0x0, 0x1c(%rsp)
je 0x4d52
xorl %ebx, %ebx
cmpq 0x28(%r14), %rbx
jae 0x4d77
movq 0x10(%rsp), %rax
movzbl (%rax,%rbx), %edi
callq 0xbb6a
movq (%r14), %rcx
movq (%rcx), %rcx
movb %al, (%rcx,%rbx)
incq %rbx
jmp 0x4d30
movq (%r14), %rax
movq 0x28(%r14), %rdx
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21f0
cmpl $0x0, 0x44(%r14)
sete %al
orb %r12b, %al
jne 0x4d92
movq 0x8(%rsp), %rdi
callq 0x20e0
testl %eax, %eax
jne 0x4de9
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9384(%rip), %rdi      # 0xe12f
callq 0x2320
leaq 0x92ea(%rip), %rdi      # 0xe0a1
callq 0x35b7
leaq 0x9341(%rip), %rdi      # 0xe104
callq 0x35b7
leaq 0x931c(%rip), %rdi      # 0xe0eb
callq 0x35b7
leaq 0x92d8(%rip), %rdi      # 0xe0b3
callq 0x35b7
leaq 0x929b(%rip), %rdi      # 0xe082
jmp 0x4dab
leaq 0x9358(%rip), %rdi      # 0xe148
callq 0x35b7