// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x118, %rsp            # imm = 0x118
movb %sil, %al
movq %rdi, 0x110(%rsp)
andb $0x1, %al
movb %al, 0x10f(%rsp)
movq 0x110(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0xcab8
leaq 0x9243(%rip), %rdi      # 0x15cf6
callq 0x2080
movq 0x110(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0xcaf2
movq 0x110(%rsp), %rdi
callq 0x3ed0
movq 0x110(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0xcaf0
leaq 0x8efd(%rip), %rdi      # 0x159e8
callq 0x2080
jmp 0xcaf4
jmp 0xcaf4
movq 0x110(%rsp), %rdi
callq 0x3dd0
movq 0x110(%rsp), %rdi
callq 0x6da0
movq 0x110(%rsp), %rax
movq 0x28(%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0x110(%rsp), %rax
movq 0x80(%rax), %rax
movq %rax, 0xa8(%rsp)
testb $0x1, 0x10f(%rsp)
je 0xcb51
movq 0x110(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0xcb5c
xorl %eax, %eax
movl %eax, 0x9c(%rsp)
jmp 0xcb6e
movq 0x110(%rsp), %rax
movl 0x40(%rax), %eax
movl %eax, 0x9c(%rsp)
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rsi
movl 0x9c(%rsp), %ecx
leaq 0xe0(%rsp), %rdi
callq 0x7c10
movq 0x110(%rsp), %rdi
callq 0x6440
movq 0x110(%rsp), %rax
movslq 0x20(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0x110(%rsp), %rax
movq %rcx, 0x5118(%rax)
movq 0x110(%rsp), %rax
movslq 0x38(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0x110(%rsp), %rax
movq %rcx, 0x5108(%rax)
movq 0x110(%rsp), %rax
movslq 0x38(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0x110(%rsp), %rax
movq %rcx, 0x5110(%rax)
movq 0x110(%rsp), %rax
cmpq $0x0, 0x5118(%rax)
je 0xcc4a
movq 0x110(%rsp), %rax
cmpq $0x0, 0x5108(%rax)
je 0xcc4a
movq 0x110(%rsp), %rax
cmpq $0x0, 0x5110(%rax)
jne 0xcc56
leaq 0x90b6(%rip), %rdi      # 0x15d07
callq 0x3ce0
movq 0x110(%rsp), %rdi
callq 0xd350
movq 0x110(%rsp), %rdi
callq 0x84d0
movq %rax, 0xd8(%rsp)
movq 0x110(%rsp), %rdi
callq 0x8640
movq %rax, 0xd0(%rsp)
movq $0x0, 0xc8(%rsp)
movl $0x1, 0xc4(%rsp)
movl $0x6, 0xc0(%rsp)
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
movq 0x110(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0xcd10
movl 0xc4(%rsp), %eax
subl $0x2, %eax
movl %eax, %eax
cmpq $0xff, %rax
jbe 0xcd10
movq 0xc30f(%rip), %rax      # 0x18ff8
movq (%rax), %rdi
movl 0xc4(%rsp), %edx
subl $0x2, %edx
leaq 0x8d00(%rip), %rsi      # 0x159fd
movb $0x0, %al
callq 0x21b0
leaq 0x8ffc(%rip), %rdi      # 0x15d07
callq 0x3ce0
movq 0x110(%rsp), %rax
movq 0x80(%rax), %rdi
callq 0x7750
movq %rax, %rcx
movq 0xd0(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0xd0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movl 0xc4(%rsp), %edx
movq 0x110(%rsp), %r8
leaq 0xe0(%rsp), %rcx
callq 0xd510
andb $0x1, %al
movb %al, 0xbf(%rsp)
movq 0x110(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0xcf3d
testb $0x1, 0x10f(%rsp)
je 0xcf3d
movl 0xc4(%rsp), %eax
decl %eax
movl %eax, 0x84(%rsp)
callq 0x12730
movq %rax, %xmm0
movaps 0x7264(%rip), %xmm3     # 0x14010
movaps %xmm3, 0x60(%rsp)
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x7263(%rip), %xmm2     # 0x14020
movaps %xmm2, 0x70(%rsp)
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq 0x110(%rsp), %rax
movq 0x28(%rax), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
callq 0x12720
movaps 0x60(%rsp), %xmm3
movaps 0x70(%rsp), %xmm2
movq %rax, %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq 0x110(%rsp), %rax
movq 0x28(%rax), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0xd0(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x2180
movl 0x84(%rsp), %esi
movsd 0x88(%rsp), %xmm0
movsd 0x90(%rsp), %xmm1
movq %rax, %rdx
leaq 0x8b8a(%rip), %rdi      # 0x15a10
movb $0x2, %al
callq 0x20f0
movq 0x110(%rsp), %rax
cmpq $0x0, 0x58(%rax)
je 0xcf3b
movq 0x110(%rsp), %rax
movq 0x58(%rax), %rdi
callq 0x2180
movslq 0xc0(%rsp), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, 0x58(%rsp)
movq 0x110(%rsp), %rax
movq 0x58(%rax), %rdi
callq 0x2180
movq 0x58(%rsp), %rsi
cvtsi2sd %rax, %xmm0
movsd 0x89fa(%rip), %xmm1     # 0x158e0
mulsd %xmm1, %xmm0
movslq 0xc0(%rsp), %rax
movq 0x110(%rsp), %rcx
movq 0x28(%rcx), %rcx
imulq %rcx, %rax
movq %rax, %xmm1
movaps 0x7102(%rip), %xmm2     # 0x14010
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x7106(%rip), %xmm2     # 0x14020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
leaq 0x8b21(%rip), %rdi      # 0x15a55
movb $0x1, %al
callq 0x20f0
jmp 0xcf3d
movq 0xd0(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x21c0
movq %rax, 0xb0(%rsp)
movq 0xd0(%rsp), %rax
cmpq $0x0, (%rax)
je 0xcf94
movq 0xd0(%rsp), %rax
movq (%rax), %rdi
callq 0x21c0
addq 0xb0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xd0(%rsp), %rax
movq (%rax), %rdi
callq 0x20d0
movq 0xb0(%rsp), %rax
cmpq 0xc8(%rsp), %rax
jbe 0xcfb6
movq 0xb0(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0xd0(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x2110
movq 0xd0(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0xd0(%rsp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
testb $0x1, 0xbf(%rsp)
movb %al, 0x57(%rsp)
jne 0xd025
movl 0xc4(%rsp), %eax
movq 0x110(%rsp), %rcx
movl 0x40(%rcx), %ecx
movq 0x110(%rsp), %rdx
movb 0x3e(%rdx), %dil
xorl %edx, %edx
movl $0x1, %esi
testb $0x1, %dil
cmovnel %esi, %edx
addl %edx, %ecx
cmpl %ecx, %eax
setne %al
movb %al, 0x57(%rsp)
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0xccaf
movq 0xd0(%rsp), %rax
cmpq $0x0, (%rax)
je 0xd04f
movq 0xd0(%rsp), %rax
movq (%rax), %rdi
callq 0x20d0
movq 0x110(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0xd20d
testb $0x1, 0x10f(%rsp)
je 0xd1c0
movq 0x110(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x14(%rsp)
callq 0x12730
movq %rax, 0x18(%rsp)
callq 0x12720
movq %rax, 0x20(%rsp)
callq 0x12730
movq %rax, %xmm0
movaps 0x6f6a(%rip), %xmm3     # 0x14010
movaps %xmm3, 0x30(%rsp)
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x6f69(%rip), %xmm2     # 0x14020
movaps %xmm2, 0x40(%rsp)
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq 0x110(%rsp), %rax
movq 0x28(%rax), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x12720
movl 0x14(%rsp), %esi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movsd 0x28(%rsp), %xmm0
movaps 0x30(%rsp), %xmm4
movaps 0x40(%rsp), %xmm3
movq %rax, %xmm1
punpckldq %xmm4, %xmm1    # xmm1 = xmm1[0],xmm4[0],xmm1[1],xmm4[1]
subpd %xmm3, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
movq 0x110(%rsp), %rax
movq 0x28(%rax), %xmm2
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
subpd %xmm3, %xmm2
movaps %xmm2, %xmm3
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
addsd %xmm3, %xmm2
divsd %xmm2, %xmm1
leaq 0x8bb6(%rip), %rdi      # 0x15d11
movb $0x2, %al
callq 0x20f0
movaps 0x30(%rsp), %xmm3
movaps 0x40(%rsp), %xmm2
movq 0xc8(%rsp), %rsi
movq %rsi, %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq 0x110(%rsp), %rax
movq 0x28(%rax), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
leaq 0x8bb1(%rip), %rdi      # 0x15d68
movb $0x1, %al
callq 0x20f0
jmp 0xd20b
movq 0x110(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0xd209
movq 0x110(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x4(%rsp)
callq 0x12730
movq %rax, 0x8(%rsp)
callq 0x12720
movl 0x4(%rsp), %esi
movq 0x8(%rsp), %rdx
movq %rax, %rcx
leaq 0x8b9c(%rip), %rdi      # 0x15d9e
movb $0x0, %al
callq 0x20f0
jmp 0xd20b
jmp 0xd20d
movq 0xd8(%rsp), %rdi
callq 0x8620
movq 0xd0(%rsp), %rdi
callq 0x86d0
leaq 0xe0(%rsp), %rdi
callq 0x7e00
movq 0x110(%rsp), %rdi
callq 0x6f10
testb $0x1, 0x10f(%rsp)
je 0xd2a1
movq 0x110(%rsp), %rax
cmpl $0x0, 0x40(%rax)
jle 0xd2a1
movq 0x110(%rsp), %rax
testb $0x1, 0x3e(%rax)
jne 0xd2a1
movq 0x110(%rsp), %rax
movq 0x50d8(%rax), %rdi
movq 0x110(%rsp), %rax
movq 0x28(%rax), %rsi
movq 0x110(%rsp), %rax
movq 0x80(%rax), %rdx
movq 0x110(%rsp), %rax
movl 0x40(%rax), %ecx
callq 0x80f0
movq 0x110(%rsp), %rdi
movb 0x10f(%rsp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x4910
movq 0x110(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0xd2f6
movq 0x110(%rsp), %rdi
callq 0x4180
movq 0x110(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0xd2f4
leaq 0x8838(%rip), %rdi      # 0x15b25
movb $0x0, %al
callq 0x20f0
jmp 0xd2f6
movq 0x110(%rsp), %rax
movq 0x5110(%rax), %rdi
callq 0x12940
movq 0x110(%rsp), %rax
movq 0x5108(%rax), %rdi
callq 0x12940
movq 0x110(%rsp), %rax
movq 0x5118(%rax), %rdi
callq 0x12940
movq 0x110(%rsp), %rdi
callq 0x66e0
addq $0x118, %rsp            # imm = 0x118
retq
nopw (%rax,%rax)