// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x1068, %rsp           # imm = 0x1068
movb %sil, %al
movq %rdi, 0x1060(%rsp)
andb $0x1, %al
movb %al, 0x105f(%rsp)
movq 0x1060(%rsp), %rax
movq 0x5100(%rax), %rax
movq %rax, 0x1050(%rsp)
movq $0x0, 0x1048(%rsp)
movq $0x0, 0x1040(%rsp)
movl $0x8, 0x103c(%rsp)
testb $0x1, 0x105f(%rsp)
je 0x6119
movq 0x1060(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0x6119
leaq 0x30(%rsp), %rdi
movq 0x1060(%rsp), %rax
movq 0x80(%rax), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0xea12(%rip), %rdx      # 0x14adf
leaq 0xebda(%rip), %r8       # 0x14cae
movb $0x0, %al
callq 0x2130
leaq 0x30(%rsp), %rdi
leaq 0xea08(%rip), %rsi      # 0x14aef
callq 0x21d0
movq %rax, 0x1040(%rsp)
cmpq $0x0, 0x1040(%rsp)
jne 0x6117
leaq 0xefa0(%rip), %rdi      # 0x150a6
callq 0x2310
leaq 0xefb3(%rip), %rdi      # 0x150c5
callq 0x3ce0
jmp 0x6119
movq 0x1060(%rsp), %rdi
callq 0x3dd0
movq 0x1060(%rsp), %rax
movq 0x5118(%rax), %rdi
movq 0x1060(%rsp), %rax
movslq 0x20(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x3310
movq 0x1060(%rsp), %rax
cmpl $0x0, 0x44(%rax)
je 0x6177
testb $0x1, 0x105f(%rsp)
je 0x6177
movq 0x1060(%rsp), %rdi
callq 0x4870
movq %rax, 0x1048(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq 0x1060(%rsp), %rcx
cmpq 0x28(%rcx), %rax
jae 0x6324
movq 0x1060(%rsp), %rax
movq 0x5100(%rax), %rax
movq 0x28(%rsp), %rcx
movl (%rax,%rcx,4), %eax
andl $0xff, %eax
movl %eax, 0x24(%rsp)
movq 0x1060(%rsp), %rax
cmpl $0x0, 0x44(%rax)
je 0x61f3
testb $0x1, 0x105f(%rsp)
je 0x61f3
movl 0x24(%rsp), %edi
movq 0x1048(%rsp), %rsi
callq 0x2170
cmpl $-0x1, %eax
jne 0x61f1
leaq 0xeeeb(%rip), %rdi      # 0x150d7
callq 0x3ce0
jmp 0x61f3
movq 0x1060(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x1060(%rsp), %rcx
movq 0x5118(%rcx), %rcx
movslq 0x24(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movb (%rax,%rcx), %al
movb %al, 0x23(%rsp)
cmpq $0x0, 0x1040(%rsp)
je 0x62dd
movq 0x1060(%rsp), %rax
movq 0x5100(%rax), %rax
movq 0x28(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl 0x103c(%rsp), %ecx
shll %ecx
shrl %cl, %eax
andl $0xffff, %eax           # imm = 0xFFFF
movl %eax, %eax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x6277
leaq 0xee9d(%rip), %rdi      # 0x1510f
callq 0x3ce0
movq 0x18(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x18(%rsp)
cmpq $0xff, 0x18(%rsp)
jbe 0x629c
leaq 0xee91(%rip), %rdi      # 0x15128
callq 0x3ce0
movq 0x1040(%rsp), %rcx
leaq 0x18(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2380
movq %rax, 0x10(%rsp)
cmpq $0x1, 0x10(%rsp)
je 0x62db
leaq 0xee89(%rip), %rdi      # 0x15153
callq 0x2310
leaq 0xedef(%rip), %rdi      # 0x150c5
callq 0x3ce0
jmp 0x62dd
movb 0x23(%rsp), %dl
movq 0x1050(%rsp), %rax
movq 0x28(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x1060(%rsp), %rax
movq 0x5118(%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x6180
cmpq $0x0, 0x1040(%rsp)
je 0x633c
movq 0x1040(%rsp), %rdi
callq 0x20d0
testb $0x1, 0x105f(%rsp)
je 0x63a2
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq 0x1060(%rsp), %rcx
cmpq 0x28(%rcx), %rax
jae 0x63a0
movq 0x1050(%rsp), %rax
movq 0x8(%rsp), %rcx
movzbl (%rax,%rcx), %edi
callq 0x124e0
movb %al, %dl
movq 0x1060(%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 0x634f
jmp 0x63cd
movq 0x1060(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq 0x1050(%rsp), %rsi
movq 0x1060(%rsp), %rax
movq 0x28(%rax), %rdx
shlq $0x0, %rdx
callq 0x21e0
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
movq 0x1060(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x63f5
jmp 0x63e8
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x63d5
movq 0x1060(%rsp), %rax
cmpl $0x0, 0x44(%rax)
je 0x642d
testb $0x1, 0x105f(%rsp)
je 0x642d
movq 0x1048(%rsp), %rdi
callq 0x20d0
cmpl $0x0, %eax
je 0x642b
leaq 0xed46(%rip), %rdi      # 0x1516c
callq 0x3ce0
jmp 0x642d
addq $0x1068, %rsp           # imm = 0x1068
retq
nopw %cs:(%rax,%rax)