// File: /workspace/github_c_makefile_star2_O3/felipelouza[P]egap/./merge128ext.h
// Function: gap128ext
void gap128ext(g_data *g, bool lastRound) {
  // we are specialized for external memory
  assert(g->extMem); 
  // check we can really use 7 bits
  assert(g->numBwt<=128 && g->lcpMerge==false);
  if(g->verbose>0) puts("BWT merging with gap128ext");
  if(g->lcpCompute) {       // we compute LCP values only if we are at the last round 
    assert(!g->bwtOnly && lastRound);
    open_unsortedLCP_files(g);
    if(g->verbose>0) puts("Computing LCP values");
  }
  else assert(g->bwtOnly); 

  check_g_data(g);
  // open files for BWTs (only reading)
  open_bw_files(g);
  // create 0 initialized bitfile (reading and writing using bitfile_* functions)
  bitfile b;
  // if it is the last round and g->dbOrder>0 and !lcp_compute the bitfile must be preserved, see comment at start of file
  bitfile_create(&b,g->mergeLen,g->outPath, (!lastRound || g->lcpCompute) ? 0 : g->dbOrder  );
  // allocate Z (merge) Znew (reading only Z, writing only newZ) 
  alloc_merge_arrays(g);
  
  // allocate other useful arrays
  g->inCnt = malloc(g->numBwt*sizeof(customInt)); 
  g->firstColumn = malloc(g->sizeOfAlpha*sizeof(customInt));
  g->F = malloc(g->sizeOfAlpha*sizeof(customInt));
  if(!g->inCnt || !g->firstColumn || !g->F) die(__func__);
  // init the above arrays
  init_arrays128ext(g);

  // init liquid block (containing list of allocated mem)
  liquidBlock *liquid = liquid_new(g); 
  // init list (on disk) of irrelevant blocks, initially empty 
  solidBlockFile *ibList = ibHead_new(g);
  uint64_t maxSolid = 0;   // maximum space used by a pair of solid block files 

  // main loop
  uint32_t prefixLength = 1;                      
  int lcpSize = POS_SIZE + BSIZE;   // number of bytes for each pos,lcp pair, see writeLcp()
  bool merge_completed;
  do {
    prefixLength+= 1;
    if(g->lcpCompute && prefixLength-2>MAX_LCP_SIZE) {fprintf(stderr,"LCP too large: %u\n", prefixLength-2);die(__func__);}
    ibList->fout = gap_tmpfile(g->outPath);
    merge_completed=addCharToPrefix128ext(ibList,liquid,prefixLength,&b,g);
    if (g->verbose>1 && lastRound) {
      #if MALLOC_COUNT_FLAG
        // at this iteration we are discovering suffixes with LCP=prefixLength-1
        printf("Lcp: %u. Memory peak/current: %.2lf/%.2lf bytes/symbol. ibList: %ju\n", 
           prefixLength-1, (double)malloc_count_peak()/g->mergeLen,
           (double)malloc_count_current()/g->mergeLen, (uintmax_t) ftello(ibList->fout));
      #endif
      // also EOF are written to unsortedLcp file so percentages are not accurate     
      if(g->unsortedLcp) printf("   unsorted lcp values: %ju (%.2lf%%)\n",  
      (uintmax_t) ftello(g->unsortedLcp)/lcpSize, (double) 100*ftello(g->unsortedLcp)/(lcpSize*g->mergeLen));
    }
    // compute current space usage for the gap files 
    uint64_t totSolid = ftell(ibList->fout);
    if(ibList->fin!=NULL) {totSolid += ftell(ibList->fin); fclose(ibList->fin);}
    if(totSolid>maxSolid) maxSolid = totSolid;
    // update solid block files:
    rewind(ibList->fout);
    ibList->fin = ibList->fout;
  } while(!merge_completed && (prefixLength!=g->dbOrder+(g->lcpCompute?1:0)));  // end main loop
  // stop at iteration g->dbOrder or,if interested in trucated LCP, g->dbOrder+1
  if(ibList->fin!=NULL) fclose(ibList->fin);

  if (g->verbose>0) {
    #if MALLOC_COUNT_FLAG
      if(lastRound) {
        printf("Merge128ext completed (%d bwts). Mem: %zu peak, %zu current, %.2lf/%.2lf bytes/symbol\n", g->numBwt, malloc_count_peak(),
             malloc_count_current(), (double)malloc_count_peak()/g->mergeLen,
             (double)malloc_count_current()/g->mergeLen);
        printf("Peak solid block disk space: %lu, %.2lf bytes/symbol\n", maxSolid, (double)maxSolid/g->mergeLen);
      }
      else if(g->verbose>1)
        printf("Merge128ext completed (%d bwts). Mem: %zu peak, %zu current\n", g->numBwt, malloc_count_peak(),
             malloc_count_current());
    #else
      printf("Merge128ext completed (%d bwts).\n", g->numBwt);
    #endif
           
  }
  liquid_free(liquid);
  ibHead_free(ibList);
  
  bitfile_destroy(&b);
  close_bw_files(g);
  
  // for dbGraph info we need to extract the hi bit from the merge array
  if(lastRound && g->dbOrder>0 && !g->lcpCompute) 
    extract_bitfile(g->merge_fname, g->mergeLen, g->outPath, g->dbOrder);
  
  // computation complete, do the merging. 
  // The following call writes the merged BWT back to file g->bwfname
  mergeBWT128ext(g,lastRound);
  if(g->lcpCompute) {
    assert(lastRound);
    close_unsortedLCP_files(g);
    if(g->verbose>0) printf("Remind to run lcpmerge to obtain the final LCP array\n"); 
  }
  
  free(g->F); // last five arrays deallocated
  free(g->firstColumn); 
  free(g->inCnt);
  free_merge_arrays(g);// merge and newMerge
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %esi, 0x8(%rsp)
movq %rdi, %rbx
cmpl $0x0, 0x94(%rdi)
jle 0x9112
leaq 0x6bd3(%rip), %rdi      # 0xfce0
callq 0x2090
cmpb $0x1, 0x3e(%rbx)
jne 0x9135
movq %rbx, %rdi
callq 0x3a59
cmpl $0x0, 0x94(%rbx)
jle 0x9135
leaq 0x68d8(%rip), %rdi      # 0xfa08
callq 0x2090
movq %rbx, %rdi
callq 0x3a58
movq %rbx, %rdi
callq 0x57ec
movq 0x28(%rbx), %rsi
movq 0x80(%rbx), %rdx
xorl %ecx, %ecx
cmpb $0x0, 0x8(%rsp)
je 0x9162
cmpb $0x0, 0x3e(%rbx)
jne 0x9162
movl 0x40(%rbx), %ecx
leaq 0x50(%rsp), %rdi
callq 0x60d4
movq %rbx, %rdi
callq 0x52a6
movslq 0x20(%rbx), %rdi
shlq $0x3, %rdi
callq 0xc70e
movq %rax, %r15
movq %rax, 0x5118(%rbx)
movslq 0x38(%rbx), %r12
shlq $0x3, %r12
movq %r12, %rdi
callq 0xc70e
movq %rax, %r14
movq %rax, 0x5108(%rbx)
movq %r12, %rdi
callq 0xc70e
movq %rax, 0x5110(%rbx)
testq %r15, %r15
je 0x91c9
testq %r14, %r14
setne %cl
testq %rax, %rax
setne %al
testb %al, %cl
jne 0x91d5
leaq 0x6b21(%rip), %rdi      # 0xfcf1
callq 0x39fc
movq 0x50e0(%rbx), %rdi
leaq 0x574b(%rip), %rsi      # 0xe92e
callq 0x21e0
movq %rax, %r14
testq %rax, %rax
jne 0x91fc
leaq 0x6f55(%rip), %rdi      # 0x1014c
callq 0x39fc
movq 0x50d8(%rbx), %rdi
leaq 0x5724(%rip), %rsi      # 0xe92e
callq 0x21e0
movq %rax, %r15
testq %rax, %rax
jne 0x9223
leaq 0x6f52(%rip), %rdi      # 0x10170
callq 0x39fc
movl 0x38(%rbx), %ecx
testl %ecx, %ecx
jle 0x92c3
movl 0x20(%rbx), %eax
xorl %r13d, %r13d
xorl %edi, %edi
movq 0x5108(%rbx), %rdx
movq %rdi, (%rdx,%r13,8)
testl %eax, %eax
jle 0x92b4
movq 0x18(%rbx), %rcx
movb $0x1, %dl
xorl %r12d, %r12d
movq (%rcx,%r12,8), %rsi
cmpq $0x0, (%rsi,%r13,8)
je 0x92a6
movq %rdi, 0x10(%rsp)
shlb $0x7, %dl
movzbl %dl, %edi
xorl %ebp, %ebp
testq %r13, %r13
jne 0x927b
movl %r12d, %edi
movq %r14, %rsi
callq 0xbd5a
movl $0x80, %edi
orl %r12d, %edi
movq %r15, %rsi
callq 0xbd5a
incq %rbp
movq 0x18(%rbx), %rcx
movq (%rcx,%r12,8), %rax
xorl %edi, %edi
cmpq (%rax,%r13,8), %rbp
jb 0x9266
movl 0x20(%rbx), %eax
movq 0x10(%rsp), %rdi
addq %rbp, %rdi
xorl %edx, %edx
incq %r12
movslq %eax, %rsi
cmpq %rsi, %r12
jl 0x924e
movl 0x38(%rbx), %ecx
incq %r13
movslq %ecx, %rdx
cmpq %rdx, %r13
jl 0x9236
movq %r15, %rdi
callq 0x20e0
testl %eax, %eax
je 0x92db
leaq 0x6ebb(%rip), %rdi      # 0x10191
callq 0x39fc
movq %r14, %rdi
callq 0x20e0
testl %eax, %eax
je 0x92f3
leaq 0x6ebc(%rip), %rdi      # 0x101aa
callq 0x39fc
movq %rbx, %rdi
callq 0x6604
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
callq 0x66a5
movq %rax, %r15
movb 0x3e(%rbx), %al
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %r15, 0x28(%rsp)
testb $0x1, %al
je 0x932a
cmpl $0x100, %r13d           # imm = 0x100
jae 0x9531
leal 0x1(%r13), %ebp
addl $0x2, %r13d
movq 0x80(%rbx), %rdi
callq 0x5e3a
movq %rax, 0x8(%r15)
movq %r15, %rdi
movq 0x20(%rsp), %rsi
movl %r13d, %edx
leaq 0x50(%rsp), %rcx
movq %rbx, %r8
callq 0x977c
movb %al, 0x10(%rsp)
cmpl $0x2, 0x94(%rbx)
setge %al
testb %al, 0x8(%rsp)
movl %ebp, 0xc(%rsp)
je 0x94ce
callq 0xc6e8
movq %rax, %xmm0
movq 0x4c88(%rip), %xmm4     # 0xe010
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
movapd 0x4c8c(%rip), %xmm3     # 0xe020
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm2
movq 0x28(%rbx), %xmm0
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movapd %xmm2, 0x40(%rsp)
callq 0xc6e0
movq %rax, %xmm0
movq 0x4c33(%rip), %xmm3     # 0xe010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x4c37(%rip), %xmm2     # 0xe020
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm4
movq 0x28(%rbx), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm4
movapd %xmm4, 0x30(%rsp)
movq 0x8(%r15), %r14
movq %r14, %rdi
callq 0x2190
leaq 0x65fd(%rip), %rdi      # 0xfa30
movl %ebp, %esi
movapd 0x40(%rsp), %xmm0
movapd 0x30(%rsp), %xmm1
movq %rax, %rdx
movb $0x2, %al
callq 0x2100
movq 0x58(%rbx), %rdi
testq %rdi, %rdi
je 0x94d2
callq 0x2190
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
movq %rdx, %r15
shrq $0x2, %r15
movq 0x58(%rbx), %rdi
callq 0x2190
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x647a(%rip), %xmm0     # 0xf900
movq 0x28(%rbx), %rax
addq %rax, %rax
leaq (%rax,%rax,2), %rax
movq %rax, %xmm1
punpckldq 0x4b72(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4b7a(%rip), %xmm1     # 0xe020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x65b8(%rip), %rdi      # 0xfa75
movq %r15, %rsi
movq 0x28(%rsp), %r15
movb $0x1, %al
callq 0x2100
jmp 0x94d2
movq 0x8(%r15), %r14
movq %r14, %rdi
callq 0x21d0
movq %rax, %rbp
movq (%r15), %r15
testq %r15, %r15
je 0x94f8
movq %r15, %rdi
callq 0x21d0
addq %rax, %rbp
movq %r15, %rdi
callq 0x20e0
cmpq %r12, %rbp
cmovaq %rbp, %r12
movq %r14, %rdi
callq 0x2120
movq 0x28(%rsp), %r15
movq %r14, (%r15)
cmpb $0x0, 0x10(%rsp)
jne 0x953e
movzbl 0x3e(%rbx), %eax
movl %eax, %ecx
addl 0x40(%rbx), %ecx
cmpl %ecx, %r13d
movl 0xc(%rsp), %ecx
movl %ecx, %r13d
jne 0x9319
jmp 0x953e
movl %r13d, %edi
callq 0x2585
jmp 0x932a
testq %r14, %r14
je 0x954b
movq %r14, %rdi
callq 0x20e0
movl 0x94(%rbx), %eax
testl %eax, %eax
jle 0x96b6
cmpb $0x0, 0x8(%rsp)
je 0x968b
movl 0x20(%rbx), %ebp
callq 0xc6e8
movq %rax, %r14
callq 0xc6e0
movq %rax, %r13
callq 0xc6e8
movq %rax, %xmm0
movq 0x4a87(%rip), %xmm4     # 0xe010
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
movapd 0x4a8b(%rip), %xmm3     # 0xe020
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm2
movq 0x28(%rbx), %xmm0
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
callq 0xc6e0
movq %rax, %xmm0
movq 0x4a32(%rip), %xmm3     # 0xe010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x4a36(%rip), %xmm2     # 0xe020
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq 0x28(%rbx), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
leaq 0x66dd(%rip), %rdi      # 0xfcfb
movl %ebp, %esi
movq %r14, %rdx
movq %r13, %rcx
movaps 0x10(%rsp), %xmm0
movb $0x2, %al
callq 0x2100
movq %r12, %xmm1
movq 0x49d1(%rip), %xmm3     # 0xe010
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x49d5(%rip), %xmm2     # 0xe020
subpd %xmm2, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq 0x28(%rbx), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x66d3(%rip), %rdi      # 0xfd52
movq %r12, %rsi
movb $0x1, %al
callq 0x2100
jmp 0x96b6
cmpl $0x1, %eax
je 0x96b6
movl 0x20(%rbx), %ebp
callq 0xc6e8
movq %rax, %r14
callq 0xc6e0
leaq 0x66e1(%rip), %rdi      # 0xfd88
movl %ebp, %esi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2100
movq 0x20(%rsp), %r14
movq 0x20(%r14), %rdi
callq 0xc7ba
movq %r14, %rdi
callq 0xc7ba
movq %r15, %rdi
callq 0x66f3
leaq 0x50(%rsp), %rdi
callq 0x61e5
movq %rbx, %rdi
callq 0x58d9
cmpb $0x0, 0x8(%rsp)
je 0x9711
movl 0x40(%rbx), %ecx
testl %ecx, %ecx
jle 0x9711
cmpb $0x0, 0x3e(%rbx)
jne 0x9711
movq 0x50d8(%rbx), %rdi
movq 0x28(%rbx), %rsi
movq 0x80(%rbx), %rdx
callq 0x635b
movzbl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0x405e
cmpb $0x1, 0x3e(%rbx)
jne 0x9741
movq %rbx, %rdi
callq 0x3c02
cmpl $0x0, 0x94(%rbx)
jle 0x9741
leaq 0x6b5b(%rip), %rdi      # 0x10297
callq 0x2090
movq 0x5110(%rbx), %rdi
callq 0xc7ba
movq 0x5108(%rbx), %rdi
callq 0xc7ba
movq 0x5118(%rbx), %rdi
callq 0xc7ba
movq %rbx, %rdi
callq 0x5445
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq