// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq 0x5118(%rdi), %rax
movslq 0x20(%rdi), %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x38f7
andq $0x0, (%rax,%rdx,8)
incq %rdx
jmp 0x38e8
cmpl $0x0, 0x44(%rbx)
sete %al
movl %ebp, %ecx
xorb $0x1, %cl
movb %cl, 0xf(%rsp)
orb %cl, %al
jne 0x391a
movq %rbx, %rdi
callq 0x3b96
movq %rax, 0x10(%rsp)
jmp 0x3923
movq $0x0, 0x10(%rsp)
cmpb $0x1, 0x69(%rbx)
jne 0x399d
movq %rbx, %rdi
callq 0x52fc
movq %rbx, %rdi
callq 0x5389
movq 0x50d8(%rbx), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x22f0
cmpl $-0x1, %eax
je 0x3b72
movl %eax, %r15d
movq 0x28(%rbx), %rsi
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x2230
movq %rax, 0x50e8(%rbx)
cmpq $-0x1, %rax
je 0x3b72
movq 0x28(%rbx), %rsi
pushq $0x2
popq %rdx
movq %rax, %rdi
callq 0x22e0
movl %r15d, %edi
callq 0x2160
testl %eax, %eax
jne 0x3b72
movq 0x50e8(%rbx), %r15
xorl %r14d, %r14d
pushq $0x1
popq %r12
cmpq 0x28(%rbx), %r14
jae 0x3a96
movq 0x50e8(%rbx), %rax
movzbl (%rax,%r14), %r13d
cmpl $0x0, 0x44(%rbx)
sete %al
orb 0xf(%rsp), %al
jne 0x39e4
movl %r13d, %edi
movq 0x10(%rsp), %rsi
callq 0x2180
cmpl $-0x1, %eax
je 0x3b7e
cmpb $0x1, 0x69(%rbx)
jne 0x3a0e
leaq (%r15,%r14), %rdi
movq 0x50a0(%rbx), %rax
movq (%rax,%r13,8), %rcx
movq %r12, %rsi
movq %r12, %rdx
callq 0x20a0
cmpl $0x1, %eax
je 0x3a2f
jmp 0x3b72
movq (%rbx), %rax
leal (,%r13,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 0x3a42
movzbl (%r15,%r14), %edi
callq 0xbb6a
movb %al, (%r15,%r14)
cmpb $0x1, 0x3d(%rbx)
jne 0x3a80
movq 0x98(%rbx), %rax
movb (%rax,%r14), %cl
testb %cl, %cl
je 0x3a5b
decb %cl
jmp 0x3a7c
movq 0x88(%rbx), %rcx
leal (,%r13,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 %r13d, %ecx
incq (%rax,%rcx,8)
incq %r14
jmp 0x39ab
movb 0x69(%rbx), %al
testb %al, %al
je 0x3aa8
movq %rbx, %rdi
callq 0x53db
movb 0x69(%rbx), %al
testb $0x1, %al
je 0x3b08
leaq 0xa0(%rbx), %rdi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x22f0
cmpl $-0x1, %eax
je 0x3b72
movl %eax, %ebp
movq 0x28(%rbx), %rdx
movq 0x30(%rbx), %rcx
movl %eax, %edi
movq %r15, %rsi
callq 0x5964
movl %ebp, %edi
callq 0x2160
testl %eax, %eax
jne 0x3b72
movq 0x50e8(%rbx), %rdi
movq 0x28(%rbx), %rsi
callq 0x2290
cmpl $-0x1, %eax
je 0x3b72
andq $0x0, 0x50e8(%rbx)
jmp 0x3b1a
movq (%rbx), %rax
movq 0x28(%rbx), %rdx
movq (%rax), %rdi
movq %r15, %rsi
callq 0x21f0
cmpl $0x0, 0x44(%rbx)
sete %al
orb 0xf(%rsp), %al
jne 0x3b35
movq 0x10(%rsp), %rdi
callq 0x20e0
testl %eax, %eax
jne 0x3b8a
cmpb $0x1, 0x3d(%rbx)
jne 0x3b63
movq 0x28(%rbx), %rdx
movq 0x88(%rbx), %rax
movq (%rax), %rdi
movq 0x98(%rbx), %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21f0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa117(%rip), %rdi      # 0xdc90
callq 0x35b7
leaq 0xa11a(%rip), %rdi      # 0xdc9f
callq 0x35b7
leaq 0xa143(%rip), %rdi      # 0xdcd4
callq 0x35b7