// File: /workspace/github_c_makefile_star2_O2/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 0x8913
leaq 0x638a(%rip), %rdi      # 0xec98
callq 0x2090
cmpb $0x1, 0x3e(%rbx)
jne 0x8936
movq %rbx, %rdi
callq 0x3611
cmpl $0x0, 0x94(%rbx)
jle 0x8936
leaq 0x608f(%rip), %rdi      # 0xe9c0
callq 0x2090
movq %rbx, %rdi
callq 0x3610
movq %rbx, %rdi
callq 0x52fc
movq 0x28(%rbx), %rsi
movq 0x80(%rbx), %rdx
xorl %ecx, %ecx
cmpb $0x0, 0x8(%rsp)
je 0x8963
cmpb $0x0, 0x3e(%rbx)
jne 0x8963
movl 0x40(%rbx), %ecx
leaq 0x50(%rsp), %rdi
callq 0x5b57
movq %rbx, %rdi
callq 0x4df5
movslq 0x20(%rbx), %rdi
shlq $0x3, %rdi
callq 0xbce0
movq %rax, %r15
movq %rax, 0x5118(%rbx)
movslq 0x38(%rbx), %r12
shlq $0x3, %r12
movq %r12, %rdi
callq 0xbce0
movq %rax, %r14
movq %rax, 0x5108(%rbx)
movq %r12, %rdi
callq 0xbce0
movq %rax, 0x5110(%rbx)
testq %r15, %r15
je 0x89ca
testq %r14, %r14
setne %cl
testq %rax, %rax
setne %al
testb %al, %cl
jne 0x89d6
leaq 0x62d8(%rip), %rdi      # 0xeca9
callq 0x35b7
movq 0x50e0(%rbx), %rdi
leaq 0x4f4a(%rip), %rsi      # 0xd92e
callq 0x21e0
movq %rax, %r14
testq %rax, %rax
jne 0x89fd
leaq 0x670c(%rip), %rdi      # 0xf104
callq 0x35b7
movq 0x50d8(%rbx), %rdi
leaq 0x4f23(%rip), %rsi      # 0xd92e
callq 0x21e0
movq %rax, %r15
testq %rax, %rax
jne 0x8a24
leaq 0x6709(%rip), %rdi      # 0xf128
callq 0x35b7
xorl %r13d, %r13d
xorl %edx, %edx
movslq 0x38(%rbx), %rax
cmpq %rax, %r13
jge 0x8aa1
movq 0x5108(%rbx), %rax
movq %rdx, (%rax,%r13,8)
movb $0x1, %al
xorl %r12d, %r12d
movslq 0x20(%rbx), %rcx
cmpq %rcx, %r12
jge 0x8a9c
movq %rdx, 0x10(%rsp)
xorl %ebp, %ebp
movq 0x18(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
cmpq (%rcx,%r13,8), %rbp
jae 0x8a8f
testq %r13, %r13
je 0x8a6d
shlb $0x7, %al
movzbl %al, %edi
jmp 0x8a7d
movl %r12d, %edi
movq %r14, %rsi
callq 0xb3c2
movl $0x80, %edi
orl %r12d, %edi
movq %r15, %rsi
callq 0xb3c2
incq %rbp
xorl %eax, %eax
jmp 0x8a52
incq %r12
movq 0x10(%rsp), %rdx
addq %rbp, %rdx
jmp 0x8a42
incq %r13
jmp 0x8a29
movq %r15, %rdi
callq 0x20e0
testl %eax, %eax
je 0x8ab9
leaq 0x6695(%rip), %rdi      # 0xf149
callq 0x35b7
movq %r14, %rdi
callq 0x20e0
testl %eax, %eax
je 0x8ad1
leaq 0x6696(%rip), %rdi      # 0xf162
callq 0x35b7
movq %rbx, %rdi
callq 0x606e
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
callq 0x610b
movq %rax, %r15
movb 0x3e(%rbx), %al
xorl %ebp, %ebp
xorl %r12d, %r12d
movq %r15, 0x28(%rsp)
testb $0x1, %al
je 0x8b06
cmpl $0x100, %ebp            # imm = 0x100
jae 0x8cf9
leal 0x1(%rbp), %r13d
addl $0x2, %ebp
movq 0x80(%rbx), %rdi
callq 0x58cd
movq %rax, 0x8(%r15)
movq %r15, %rdi
movq 0x20(%rsp), %rsi
movl %ebp, %edx
leaq 0x50(%rsp), %rcx
movq %rbx, %r8
callq 0x8f56
movb %al, 0x10(%rsp)
cmpl $0x2, 0x94(%rbx)
setge %al
testb %al, 0x8(%rsp)
movl %r13d, 0xc(%rsp)
je 0x8c98
callq 0xbc94
movq %rax, %xmm0
movq 0x44ad(%rip), %xmm4     # 0xd010
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
movapd 0x44b1(%rip), %xmm3     # 0xd020
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 0xbc8c
movq %rax, %xmm0
movq 0x4458(%rip), %xmm3     # 0xd010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x445c(%rip), %xmm2     # 0xd020
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 0x5dda(%rip), %rdi      # 0xe9e8
movl %r13d, %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 0x8c9c
callq 0x2190
xorl %edx, %edx
pushq $0x6
popq %rcx
divq %rcx
movq %rax, %r15
movq 0x58(%rbx), %rdi
callq 0x2190
cvtsi2sd %rax, %xmm0
imulq $0x6, 0x28(%rbx), %rax
mulsd 0x5c5d(%rip), %xmm0     # 0xe8b8
movq %rax, %xmm1
punpckldq 0x43a8(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x43b0(%rip), %xmm1     # 0xd020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x5da6(%rip), %rdi      # 0xea2d
movq %r15, %rsi
movq 0x28(%rsp), %r15
movb $0x1, %al
callq 0x2100
jmp 0x8c9c
movq 0x8(%r15), %r14
movq %r14, %rdi
callq 0x21d0
movq %rax, %r13
movq (%r15), %r15
testq %r15, %r15
je 0x8cc2
movq %r15, %rdi
callq 0x21d0
addq %rax, %r13
movq %r15, %rdi
callq 0x20e0
cmpq %r12, %r13
cmovaq %r13, %r12
movq %r14, %rdi
callq 0x2120
movq 0x28(%rsp), %r15
movq %r14, (%r15)
cmpb $0x0, 0x10(%rsp)
jne 0x8d24
movzbl 0x3e(%rbx), %eax
movl %eax, %ecx
addl 0x40(%rbx), %ecx
cmpl %ecx, %ebp
movl 0xc(%rsp), %ecx
movl %ecx, %ebp
jne 0x8af6
jmp 0x8d24
movq 0xa2f8(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x5ccb(%rip), %rsi      # 0xe9d5
movl %ebp, %edx
xorl %eax, %eax
callq 0x21c0
leaq 0x5f8f(%rip), %rdi      # 0xeca9
callq 0x35b7
jmp 0x8b06
testq %r14, %r14
je 0x8d31
movq %r14, %rdi
callq 0x20e0
movl 0x94(%rbx), %eax
testl %eax, %eax
jle 0x8e9c
cmpb $0x0, 0x8(%rsp)
je 0x8e71
movl 0x20(%rbx), %ebp
callq 0xbc94
movq %rax, %r14
callq 0xbc8c
movq %rax, %r13
callq 0xbc94
movq %rax, %xmm0
movq 0x42a1(%rip), %xmm4     # 0xd010
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
movapd 0x42a5(%rip), %xmm3     # 0xd020
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 0xbc8c
movq %rax, %xmm0
movq 0x424c(%rip), %xmm3     # 0xd010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x4250(%rip), %xmm2     # 0xd020
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 0x5eaf(%rip), %rdi      # 0xecb3
movl %ebp, %esi
movq %r14, %rdx
movq %r13, %rcx
movaps 0x10(%rsp), %xmm0
movb $0x2, %al
callq 0x2100
movq %r12, %xmm1
movq 0x41eb(%rip), %xmm3     # 0xd010
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x41ef(%rip), %xmm2     # 0xd020
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 0x5ea5(%rip), %rdi      # 0xed0a
movq %r12, %rsi
movb $0x1, %al
callq 0x2100
jmp 0x8e9c
cmpl $0x1, %eax
je 0x8e9c
movl 0x20(%rbx), %ebp
callq 0xbc94
movq %rax, %r14
callq 0xbc8c
leaq 0x5eb3(%rip), %rdi      # 0xed40
movl %ebp, %esi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2100
movq 0x20(%rsp), %rdi
callq 0x60f5
movq %r15, %rdi
callq 0x6154
leaq 0x50(%rsp), %rdi
callq 0x5c68
movq %rbx, %rdi
callq 0x53db
cmpb $0x0, 0x8(%rsp)
je 0x8eeb
movl 0x40(%rbx), %ecx
testl %ecx, %ecx
jle 0x8eeb
cmpb $0x0, 0x3e(%rbx)
jne 0x8eeb
movq 0x50d8(%rbx), %rdi
movq 0x28(%rbx), %rsi
movq 0x80(%rbx), %rdx
callq 0x5dd6
movzbl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0x3c0a
cmpb $0x1, 0x3e(%rbx)
jne 0x8f1b
movq %rbx, %rdi
callq 0x37b5
cmpl $0x0, 0x94(%rbx)
jle 0x8f1b
leaq 0x6339(%rip), %rdi      # 0xf24f
callq 0x2090
movq 0x5110(%rbx), %rdi
callq 0xbdb4
movq 0x5108(%rbx), %rdi
callq 0xbdb4
movq 0x5118(%rbx), %rdi
callq 0xbdb4
movq %rbx, %rdi
callq 0x4f88
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq