// File: /workspace/github_c_makefile_star2_O3/felipelouza[P]egap/util.c
// Function: mergeBWTandLCP
void mergeBWTandLCP(g_data *g, bool lastRound)
{
  if(sizeof(symbol)>sizeof(palette)) die("Sorry, alphabet is too large (mergeBWTandLCP");
  assert(!g->lcpMerge || g->blockBeginsAt!=NULL); // if lcpMerge we need g->blockBeginsAt
  FILE *daOutFile=NULL;

  check_g_data(g);
  array_clear(g->inCnt,g->numBwt,0);
  if(g->outputDA && lastRound)
    daOutFile = openDAFile(g);
  if(g->extMem) {
    open_bw_files(g);// open files in read mode
    rewind_bw_files(g);   // set file pointers at the beginning of each BWT
    // mmap merge values
    int fd = open(g->merge_fname,O_RDWR);
    if(fd == -1) die(__func__);
    g->mergeColor = mmap(NULL,g->mergeLen*sizeof(palette),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    if(g->mergeColor == MAP_FAILED) die(__func__);
    #ifdef USE_MMAP_ADVISE
    madvise(g->mergeColor, g->mergeLen*sizeof(palette), MADV_SEQUENTIAL);
    #endif
    if(close(fd)!=0) die(__func__);
  }
  symbol *bwtout = (symbol *) g->mergeColor;   // merged BWT stored in mergeColor
  for (customInt i = 0; i < g->mergeLen; ++i) {
    int currentColor = g->mergeColor[i];
    assert(currentColor < g->numBwt);
    // check that all LCP values have been obtained
    if(g->lcpCompute && lastRound)
      assert( tba_get(g->bitB,i)==3 ); 
    // if requested output merge array
    if(g->outputDA && lastRound) 
      if(fputc(currentColor, daOutFile)==EOF)
        die("mergeBWTandLCP: Error writing to Document Array file");   
    // save new BWT char overwriting mergeColor[i]
    if(g->extMem) {
      int e = fread(bwtout+i,sizeof(symbol),1,g->bwf[currentColor]);
      if(e!=1) die(__func__);
    }      
    else bwtout[i] = g->bws[currentColor][g->inCnt[currentColor]];
    if(lastRound) bwtout[i] = alpha_enlarge(bwtout[i]); 
    
    if(g->lcpMerge) { // compute lcp value and store it to blockBeginsAt[]
      lcpInt tmp = g->lcps[currentColor][g->inCnt[currentColor]]; // lcp retrieved from file (suffixes from the same string)      
      if(g->blockBeginsAt[i]>0)
        g->blockBeginsAt[i] -= 1;  // lcp computed by the merging algorithm 
      else 
        g->blockBeginsAt[i] = tmp; // lcp retrieved from file (suffixes from the same string) 
    }
    g->inCnt[currentColor]++; // one more char read from currentColor BWT/LCP
  }  
  // final check on the merging 
  for(int i=0;i<g->numBwt;i++) assert(g->inCnt[i]==g->bwtLen[i]);
  if(g->extMem) close_bw_files(g);

  // merging done: copy back to g->bws[0] or to file
  if(g->extMem) {
    // printf("Copy back to file. Bytes: %ld, offset %ld\n",g->mergeLen, g->symb_offset);
    int fd = open(g->bwfname,O_WRONLY);
    if(fd == -1) die(__func__);
    huge_pwrite(fd, bwtout,sizeof(symbol)*g->mergeLen,sizeof(symbol)*g->symb_offset);
    if(close(fd)!=0) die(__func__);
    // unmap mergeColor
    fd = munmap(g->mergeColor,g->mergeLen*sizeof(palette));
    if(fd == -1) die(__func__);
    g->mergeColor=NULL;  
  }
  else 
    memcpy(g->bws[0],bwtout,g->mergeLen*sizeof(symbol));
  // close document array file 
  if(g->outputDA && lastRound)
      if(fclose(daOutFile)!=0) die("mergeBWTandLCP: Error closing Document Array file");   
  //copy merged values back to g->lcps[0]
  if(g->lcpMerge)
      memcpy(g->lcps[0],g->blockBeginsAt,g->mergeLen*sizeof(lcpInt)); // copy lcp values back to g->lcps[0]
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
movslq 0x20(%rdi), %rdx
testq %rdx, %rdx
je 0x3d3f
movq 0x5118(%rbx), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2150
cmpl $0x0, 0x44(%rbx)
sete %al
movl %ebp, %r13d
xorb $0x1, %r13b
orb %r13b, %al
jne 0x3d60
movq %rbx, %rdi
callq 0x3fea
movq %rax, (%rsp)
jmp 0x3d68
movq $0x0, (%rsp)
cmpb $0x1, 0x69(%rbx)
jne 0x3dea
movq %rbx, %rdi
callq 0x57ec
movq %rbx, %rdi
callq 0x5883
movq 0x50d8(%rbx), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x22f0
cmpl $-0x1, %eax
je 0x3fc6
movl %eax, %r15d
movq 0x28(%rbx), %rsi
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x2230
movq %rax, 0x50e8(%rbx)
cmpq $-0x1, %rax
je 0x3fc6
movq 0x28(%rbx), %rsi
movq %rax, %rdi
movl $0x2, %edx
callq 0x22e0
movl %r15d, %edi
callq 0x2160
testl %eax, %eax
jne 0x3fc6
movq 0x50e8(%rbx), %r15
cmpq $0x0, 0x28(%rbx)
je 0x3ee7
xorl %r14d, %r14d
movq 0x50e8(%rbx), %rax
movzbl (%rax,%r14), %r12d
cmpl $0x0, 0x44(%rbx)
sete %al
orb %r13b, %al
jne 0x3e2c
movl %r12d, %edi
movq (%rsp), %rsi
callq 0x2180
cmpl $-0x1, %eax
je 0x3fd2
cmpb $0x1, 0x69(%rbx)
jne 0x3e5a
leaq (%r15,%r14), %rdi
movq 0x50a0(%rbx), %rax
movq (%rax,%r12,8), %rcx
movl $0x1, %esi
movl $0x1, %edx
callq 0x20a0
cmpl $0x1, %eax
je 0x3e7b
jmp 0x3fc6
movq (%rbx), %rax
leal (,%r12,8), %ecx
movq (%rax,%rcx), %rax
movq 0x5118(%rbx), %rdx
movq (%rdx,%rcx), %rcx
movb (%rax,%rcx), %al
movb %al, (%r15,%r14)
testb %bpl, %bpl
je 0x3e8e
movzbl (%r15,%r14), %edi
callq 0xc5af
movb %al, (%r15,%r14)
cmpb $0x1, 0x3d(%rbx)
jne 0x3ecc
movq 0x98(%rbx), %rax
movb (%rax,%r14), %cl
testb %cl, %cl
je 0x3ea7
decb %cl
jmp 0x3ec8
movq 0x88(%rbx), %rcx
leal (,%r12,8), %edx
movq (%rcx,%rdx), %rcx
movq 0x5118(%rbx), %rsi
movq (%rsi,%rdx), %rdx
movb (%rcx,%rdx), %cl
movb %cl, (%rax,%r14)
movq 0x5118(%rbx), %rax
movl %r12d, %ecx
incq (%rax,%rcx,8)
incq %r14
cmpq 0x28(%rbx), %r14
jb 0x3dff
movb 0x69(%rbx), %al
testb %al, %al
je 0x3ef9
movq %rbx, %rdi
callq 0x58d9
movb 0x69(%rbx), %al
testb $0x1, %al
je 0x3f5e
leaq 0xa0(%rbx), %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x22f0
cmpl $-0x1, %eax
je 0x3fc6
movl %eax, %ebp
movq 0x28(%rbx), %rdx
movq 0x30(%rbx), %rcx
movl %eax, %edi
movq %r15, %rsi
callq 0x5ed1
movl %ebp, %edi
callq 0x2160
testl %eax, %eax
jne 0x3fc6
movq 0x50e8(%rbx), %rdi
movq 0x28(%rbx), %rsi
callq 0x2290
cmpl $-0x1, %eax
je 0x3fc6
movq $0x0, 0x50e8(%rbx)
jmp 0x3f70
movq (%rbx), %rax
movq 0x28(%rbx), %rdx
movq (%rax), %rdi
movq %r15, %rsi
callq 0x21f0
cmpl $0x0, 0x44(%rbx)
sete %al
orb %r13b, %al
jne 0x3f89
movq (%rsp), %rdi
callq 0x20e0
testl %eax, %eax
jne 0x3fde
cmpb $0x1, 0x3d(%rbx)
jne 0x3fb7
movq 0x28(%rbx), %rdx
movq 0x88(%rbx), %rax
movq (%rax), %rdi
movq 0x98(%rbx), %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21f0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xacfe(%rip), %rdi      # 0xeccb
callq 0x39fc
leaq 0xad01(%rip), %rdi      # 0xecda
callq 0x39fc
leaq 0xad2a(%rip), %rdi      # 0xed0f
callq 0x39fc