// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movq %rdi, %rbx
cmpl $0x0, 0x94(%rdi)
jle 0x904c
leaq 0x6c99(%rip), %rdi      # 0xfce0
callq 0x2090
cmpb $0x1, 0x3e(%rbx)
jne 0x906f
movq %rbx, %rdi
callq 0x39d0
cmpl $0x0, 0x94(%rbx)
jle 0x906f
leaq 0x699e(%rip), %rdi      # 0xfa08
callq 0x2090
movq %rbx, %rdi
callq 0x39cf
movq %rbx, %rdi
callq 0x5748
movq 0x28(%rbx), %rsi
movq 0x80(%rbx), %rdx
xorl %ecx, %ecx
testb %bpl, %bpl
je 0x909a
cmpb $0x0, 0x3e(%rbx)
jne 0x909a
movl 0x40(%rbx), %ecx
leaq 0x50(%rsp), %rdi
callq 0x6010
movq %rbx, %rdi
callq 0x5200
movslq 0x20(%rbx), %rdi
shlq $0x3, %rdi
callq 0xc63a
movq %rax, %r15
movq %rax, 0x5118(%rbx)
movslq 0x38(%rbx), %r12
shlq $0x3, %r12
movq %r12, %rdi
callq 0xc63a
movq %rax, %r14
movq %rax, 0x5108(%rbx)
movq %r12, %rdi
callq 0xc63a
movq %rax, 0x5110(%rbx)
testq %r15, %r15
je 0x9101
testq %r14, %r14
setne %cl
testq %rax, %rax
setne %al
testb %al, %cl
jne 0x910d
leaq 0x6be9(%rip), %rdi      # 0xfcf1
callq 0x3973
movq 0x50e0(%rbx), %rdi
leaq 0x5813(%rip), %rsi      # 0xe92e
callq 0x21e0
movq %rax, %r14
testq %rax, %rax
jne 0x9134
leaq 0x701d(%rip), %rdi      # 0x1014c
callq 0x3973
movl %ebp, 0x4(%rsp)
movq 0x50d8(%rbx), %rdi
leaq 0x57e8(%rip), %rsi      # 0xe92e
callq 0x21e0
movq %rax, %r15
testq %rax, %rax
jne 0x915f
leaq 0x7016(%rip), %rdi      # 0x10170
callq 0x3973
cmpl $0x0, 0x38(%rbx)
jle 0x91f6
xorl %r13d, %r13d
xorl %edx, %edx
movq 0x5108(%rbx), %rax
movq %rdx, (%rax,%r13,8)
cmpl $0x0, 0x20(%rbx)
jle 0x91e6
movb $0x1, %al
xorl %r12d, %r12d
movq 0x18(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
cmpq $0x0, (%rcx,%r13,8)
je 0x91da
movq %rdx, 0x8(%rsp)
xorl %ebp, %ebp
testq %r13, %r13
jne 0x91ac
movl %r12d, %edi
movq %r14, %rsi
callq 0xbcb2
movb $0x1, %al
shlb $0x7, %al
movzbl %al, %edi
orl %r12d, %edi
movq %r15, %rsi
callq 0xbcb2
incq %rbp
movq 0x18(%rbx), %rax
movq (%rax,%r12,8), %rcx
xorl %eax, %eax
cmpq (%rcx,%r13,8), %rbp
jb 0x919a
movq 0x8(%rsp), %rdx
addq %rbp, %rdx
xorl %eax, %eax
incq %r12
movslq 0x20(%rbx), %rcx
cmpq %rcx, %r12
jl 0x9184
incq %r13
movslq 0x38(%rbx), %rax
cmpq %rax, %r13
jl 0x916e
movq %r15, %rdi
callq 0x20e0
testl %eax, %eax
je 0x920e
leaq 0x6f88(%rip), %rdi      # 0x10191
callq 0x3973
movq %r14, %rdi
callq 0x20e0
testl %eax, %eax
je 0x9226
leaq 0x6f89(%rip), %rdi      # 0x101aa
callq 0x3973
movq %rbx, %rdi
callq 0x6534
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0x65d8
movq %rax, %r15
xorl %r13d, %r13d
movl 0x4(%rsp), %eax
xorb $0x1, %al
movb %al, 0x3(%rsp)
movq $0x0, 0x8(%rsp)
movq %r15, 0x10(%rsp)
cmpb $0x1, 0x3e(%rbx)
jne 0x926c
cmpl $0x100, %r13d           # imm = 0x100
jae 0x946a
leal 0x1(%r13), %r12d
addl $0x2, %r13d
movq 0x80(%rbx), %rdi
callq 0x5d78
movq %rax, 0x8(%r15)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movl %r13d, %edx
leaq 0x50(%rsp), %rcx
movq %rbx, %r8
callq 0x96be
movb %al, 0x20(%rsp)
cmpl $0x2, 0x94(%rbx)
setl %al
orb 0x3(%rsp), %al
jne 0x9403
callq 0xc614
movq %rax, %xmm0
movq 0x4d4a(%rip), %xmm4     # 0xe010
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
movapd 0x4d4e(%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 0xc60c
movq %rax, %xmm0
movq 0x4cf5(%rip), %xmm3     # 0xe010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x4cf9(%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), %rdi
callq 0x2190
leaq 0x66c2(%rip), %rdi      # 0xfa30
movl %r12d, %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 0x9403
callq 0x2190
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
movq %rdx, %r14
shrq $0x2, %r14
movq 0x58(%rbx), %rdi
callq 0x2190
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x653e(%rip), %xmm0     # 0xf900
movq 0x28(%rbx), %rax
addq %rax, %rax
leaq (%rax,%rax,2), %rax
movq %rax, %xmm1
punpckldq 0x4c36(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4c3e(%rip), %xmm1     # 0xe020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x667c(%rip), %rdi      # 0xfa75
movq %r14, %rsi
movb $0x1, %al
callq 0x2100
movq 0x8(%r15), %r14
movq %r14, %rdi
callq 0x21d0
movq %rax, %rbp
movq (%r15), %r15
testq %r15, %r15
je 0x942d
movq %r15, %rdi
callq 0x21d0
addq %rax, %rbp
movq %r15, %rdi
callq 0x20e0
movq 0x8(%rsp), %rax
cmpq %rax, %rbp
cmovaq %rbp, %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x2120
movq 0x10(%rsp), %r15
movq %r14, (%r15)
cmpb $0x0, 0x20(%rsp)
jne 0x9477
movzbl 0x3e(%rbx), %eax
addl 0x40(%rbx), %eax
cmpl %eax, %r13d
movl %r12d, %r13d
jne 0x9259
jmp 0x9477
movl %r13d, %edi
callq 0x2585
jmp 0x926c
testq %r14, %r14
je 0x9484
movq %r14, %rdi
callq 0x20e0
movl 0x94(%rbx), %eax
testl %eax, %eax
movl 0x4(%rsp), %r15d
jle 0x95f9
testb %r15b, %r15b
je 0x95ce
movl 0x20(%rbx), %ebp
callq 0xc614
movq %rax, %r14
callq 0xc60c
movq %rax, %r13
callq 0xc614
movq %rax, %xmm0
movq 0x4b4b(%rip), %xmm4     # 0xe010
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
movapd 0x4b4f(%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, 0x20(%rsp)
callq 0xc60c
movq %rax, %xmm0
movq 0x4af6(%rip), %xmm3     # 0xe010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x4afa(%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 0x67a1(%rip), %rdi      # 0xfcfb
movl %ebp, %esi
movq %r14, %rdx
movq %r13, %rcx
movl 0x4(%rsp), %r15d
movaps 0x20(%rsp), %xmm0
movb $0x2, %al
callq 0x2100
movq 0x8(%rsp), %rsi
movq %rsi, %xmm1
movq 0x4a8b(%rip), %xmm3     # 0xe010
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x4a8f(%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 0x678d(%rip), %rdi      # 0xfd52
movb $0x1, %al
callq 0x2100
jmp 0x95f9
cmpl $0x1, %eax
je 0x95f9
movl 0x20(%rbx), %ebp
callq 0xc614
movq %rax, %r14
callq 0xc60c
leaq 0x679e(%rip), %rdi      # 0xfd88
movl %ebp, %esi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2100
movq 0x18(%rsp), %r14
movq 0x20(%r14), %rdi
callq 0xc6e6
movq %r14, %rdi
callq 0xc6e6
movq 0x10(%rsp), %rdi
callq 0x6626
leaq 0x50(%rsp), %rdi
callq 0x611d
movq %rbx, %rdi
callq 0x582f
testb %r15b, %r15b
je 0x9654
movl 0x40(%rbx), %ecx
testl %ecx, %ecx
jle 0x9654
cmpb $0x0, 0x3e(%rbx)
jne 0x9654
movq 0x50d8(%rbx), %rdi
movq 0x28(%rbx), %rsi
movq 0x80(%rbx), %rdx
callq 0x6293
movzbl %r15b, %esi
movq %rbx, %rdi
callq 0x3fe3
cmpb $0x1, 0x3e(%rbx)
jne 0x9683
movq %rbx, %rdi
callq 0x3b7a
cmpl $0x0, 0x94(%rbx)
jle 0x9683
leaq 0x6c19(%rip), %rdi      # 0x10297
callq 0x2090
movq 0x5110(%rbx), %rdi
callq 0xc6e6
movq 0x5108(%rbx), %rdi
callq 0xc6e6
movq 0x5118(%rbx), %rdi
callq 0xc6e6
movq %rbx, %rdi
callq 0x539f
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq