// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/./merge128.h
// Function: gap128
void gap128(g_data *g, bool lastRound) {
  // check we can really use 7 bits
  assert(g->numBwt<=128 && g->lcpMerge==false && !g->extMem);
  if(g->numBwt<=8 && !g->algorithm) // if numBwt is small use gap8 or gap16 (they use less space for Z) 
    return gap8(g,lastRound);
  if(g->bwtOnly && g->numBwt <=16 && !g->algorithm) 
    return gap16(g,lastRound); 
  if(g->verbose>0) puts("BWT merging with gap128");
  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); 

  // init local global vars
  check_g_data(g);    
  // allocate an array containing Z (merge) Znew and B 
  // together they use 16 bits 1+1+7+7 
  assert(g->blockBeginsAt==NULL);
  alloc_merge_array16(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
  if(g->smallAlpha) init_arrays128(g);
  else init_arrays128_largealpha(g);
  // now we are ready to give mmap advise
  #ifdef USE_MMAP_ADVISE
  if(g->mmapZ) { // advise on g->mergeColor16
    for (int i = 0; i < g->sizeOfAlpha-1; ++i)
      madvise(g->mergeColor16 + g->firstColumn[i], (g->firstColumn[i+1]-g->firstColumn[i])*2, MADV_SEQUENTIAL);
    madvise(g->mergeColor16 + g->firstColumn[g->sizeOfAlpha-1], (g->mergeLen-g->firstColumn[g->sizeOfAlpha-1])*2, MADV_SEQUENTIAL);
  }
  #endif

  // 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);

  // main loop
  uint32_t prefixLength = 1;
  int lcpSize = POS_SIZE + BSIZE;   // number of bytes for each pos,lcp pair, see writeLcp()
  int round=0;
  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__);}
    bool mergeChanged = false; // the Z vector has changed in this iteration (used when g->bwtOnly)
    ibList->fout = gap_tmpfile(g->outPath);
    merge_completed=addCharToPrefix128(ibList,liquid,prefixLength,&mergeChanged,round,g);
    if (g->verbose>1 && lastRound) {
      #if MALLOC_COUNT_FLAG
        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));
    }
    round  = 1 - round; // change round parity
    if(g->bwtOnly && !mergeChanged) {
      if(g->verbose>1) puts("Gap bwt-only early termination");
      fclose(ibList->fout);
      break;
    }
    if(ibList->fin!=NULL) fclose(ibList->fin);
    rewind(ibList->fout);
    ibList->fin = ibList->fout;
  } while(!merge_completed);  // end main loop
  if(ibList->fin!=NULL) fclose(ibList->fin);

  if (g->verbose>0) {
    #if MALLOC_COUNT_FLAG
      if(lastRound)
        printf("Merge128 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);
      else if(g->verbose>1)
        printf("Merge128 completed (%d bwts). Mem: %zu peak, %zu current\n", g->numBwt, malloc_count_peak(),
             malloc_count_current());
    #else
      printf("Merge128 completed (%d bwts).\n", g->numBwt);
    #endif
  }
  liquid_free(liquid);
  ibHead_free(ibList);

  // computation complete, do the merging. 
  // The following call writes the merged BWT back to g->bws[0]
  mergeBWT128(g,lastRound);
  if(g->lcpCompute) {
    assert(lastRound);
    // close lcp file (and merge them?)
    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_array16(g);// merge and newMerge
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %r13d
movq %rdi, %rbx
movl 0x20(%rdi), %eax
cmpl $0x8, %eax
jg 0x7d1a
cmpl $0x0, 0x6c(%rbx)
je 0x8316
cmpl $0x10, %eax
jg 0x7d2f
cmpb $0x0, 0x3c(%rbx)
je 0x7d2f
cmpl $0x0, 0x6c(%rbx)
je 0x8330
cmpl $0x0, 0x94(%rbx)
jle 0x7d44
leaq 0x6eb3(%rip), %rdi      # 0xebf2
callq 0x2090
cmpb $0x1, 0x3e(%rbx)
jne 0x7d67
movq %rbx, %rdi
callq 0x3611
cmpl $0x0, 0x94(%rbx)
jle 0x7d67
leaq 0x6c5e(%rip), %rdi      # 0xe9c0
callq 0x2090
movq %rbx, %rdi
callq 0x3610
movq %rbx, %rdi
callq 0x51a0
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 0x7dcc
testq %r14, %r14
setne %cl
testq %rax, %rax
setne %al
testb %al, %cl
jne 0x7dd8
leaq 0x6e30(%rip), %rdi      # 0xec03
callq 0x35b7
cmpb $0x1, 0x68(%rbx)
jne 0x7deb
movq %rbx, %rdi
callq 0x834a
jmp 0x7e9e
movslq 0x20(%rbx), %rdi
shlq $0x3, %rdi
callq 0xbce0
movq %rax, 0x18(%rbx)
testq %rax, %rax
jne 0x7e0d
leaq 0x72e2(%rip), %rdi      # 0xf0ea
callq 0x35b7
xorl %r12d, %r12d
pushq $0x8
popq %r14
leaq 0x72cf(%rip), %r15      # 0xf0ea
movslq 0x20(%rbx), %rax
cmpq %rax, %r12
jge 0x7e6e
movslq 0x38(%rbx), %rdi
movq %r14, %rsi
callq 0xbe60
movq 0x18(%rbx), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x18(%rbx), %rax
movq (%rax,%r12,8), %rdx
testq %rdx, %rdx
jne 0x7e55
movq %r15, %rdi
callq 0x35b7
movq 0x18(%rbx), %rax
movq (%rax,%r12,8), %rdx
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq (%rax,%r12,8), %rdi
movq (%rcx,%r12,8), %rsi
callq 0xbad1
incq %r12
jmp 0x7e1b
movq %rbx, %rdi
callq 0x834a
xorl %r14d, %r14d
movslq 0x20(%rbx), %rax
movq 0x18(%rbx), %rdi
cmpq %rax, %r14
jge 0x7e94
movq (%rdi,%r14,8), %rdi
callq 0xbdb4
incq %r14
jmp 0x7e79
callq 0xbdb4
andq $0x0, 0x18(%rbx)
cmpb $0x1, 0x70(%rbx)
jne 0x7eff
xorl %r14d, %r14d
pushq $0x2
popq %rbp
movslq 0x38(%rbx), %rcx
leaq -0x1(%rcx), %rsi
movq 0x50f8(%rbx), %rax
movq 0x5108(%rbx), %rdx
cmpq %rsi, %r14
jge 0x7ee4
movq (%rdx,%r14,8), %rcx
movq 0x8(%rdx,%r14,8), %rsi
leaq (%rax,%rcx,2), %rdi
incq %r14
subq %rcx, %rsi
addq %rsi, %rsi
movl %ebp, %edx
callq 0x22e0
jmp 0x7eaa
movq -0x8(%rdx,%rcx,8), %rcx
leaq (%rax,%rcx,2), %rdi
movq 0x28(%rbx), %rsi
subq %rcx, %rsi
addq %rsi, %rsi
pushq $0x2
popq %rdx
callq 0x22e0
movq %rbx, %rdi
callq 0x606e
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0x610b
movq %rax, 0x8(%rsp)
movl %r13d, 0x14(%rsp)
movl %r13d, %eax
xorb $0x1, %al
movb %al, 0x20(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpb $0x1, 0x3e(%rbx)
jne 0x7f40
cmpl $0x100, %r14d           # imm = 0x100
jae 0x811d
leal 0x1(%r14), %ebp
addl $0x2, %r14d
movb $0x0, 0x7(%rsp)
movq 0x80(%rbx), %rdi
callq 0x58cd
movq 0x8(%rsp), %rdi
movq %rax, 0x8(%rdi)
movq 0x18(%rsp), %rsi
movl %r14d, %edx
leaq 0x7(%rsp), %rcx
movl %r13d, %r8d
movq %rbx, %r9
callq 0x83e9
movl %eax, %r15d
cmpl $0x2, 0x94(%rbx)
setl %al
orb 0x20(%rsp), %al
jne 0x80d2
callq 0xbc94
movq %rax, %xmm0
movq 0x506d(%rip), %xmm4     # 0xd010
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
movapd 0x5071(%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 0x5018(%rip), %xmm3     # 0xd010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x501c(%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(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x2190
leaq 0x6998(%rip), %rdi      # 0xe9e8
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 0x80d2
callq 0x2190
xorl %edx, %edx
pushq $0x6
popq %rcx
divq %rcx
movq %rax, %r14
movq 0x58(%rbx), %rdi
callq 0x2190
cvtsi2sd %rax, %xmm0
imulq $0x6, 0x28(%rbx), %rax
mulsd 0x681c(%rip), %xmm0     # 0xe8b8
movq %rax, %xmm1
punpckldq 0x4f67(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4f6f(%rip), %xmm1     # 0xd020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x6965(%rip), %rdi      # 0xea2d
movq %r14, %rsi
movb $0x1, %al
callq 0x2100
cmpb $0x1, 0x3c(%rbx)
jne 0x80df
cmpb $0x0, 0x7(%rsp)
je 0x8149
movl %ebp, %r14d
movq 0x8(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x80f4
callq 0x20e0
pushq $0x1
popq %rbp
subl %r13d, %ebp
movq 0x8(%rsp), %r13
movq 0x8(%r13), %r12
movq %r12, %rdi
callq 0x2120
movq %r12, (%r13)
movl %ebp, %r13d
testb %r15b, %r15b
je 0x7f2d
jmp 0x816f
movq 0xaed4(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x68a7(%rip), %rsi      # 0xe9d5
movl %r14d, %edx
xorl %eax, %eax
callq 0x21c0
leaq 0x6ac4(%rip), %rdi      # 0xec03
callq 0x35b7
jmp 0x7f40
cmpl $0x2, 0x94(%rbx)
jl 0x815e
leaq 0x68fb(%rip), %rdi      # 0xea54
callq 0x2090
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rdi
callq 0x20e0
movq (%r14), %r12
testq %r12, %r12
je 0x817c
movq %r12, %rdi
callq 0x20e0
movl 0x94(%rbx), %eax
testl %eax, %eax
movl 0x14(%rsp), %r12d
jle 0x8299
testb %r12b, %r12b
je 0x826e
movl 0x20(%rbx), %ebp
callq 0xbc94
movq %rax, %r12
callq 0xbc8c
movq %rax, %r13
callq 0xbc94
movq %rax, %xmm0
movq 0x4e53(%rip), %xmm4     # 0xd010
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
movapd 0x4e57(%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, 0x20(%rsp)
callq 0xbc8c
movq %rax, %xmm0
movq 0x4dfe(%rip), %xmm3     # 0xd010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x4e02(%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 0x69b8(%rip), %rdi      # 0xec0a
movl %ebp, %esi
movq %r12, %rdx
movq %r13, %rcx
movl 0x14(%rsp), %r12d
movapd 0x20(%rsp), %xmm0
movb $0x2, %al
callq 0x2100
jmp 0x8299
cmpl $0x1, %eax
je 0x8299
movl 0x20(%rbx), %ebp
callq 0xbc94
movq %rax, %r14
callq 0xbc8c
leaq 0x69d4(%rip), %rdi      # 0xec5e
movl %ebp, %esi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2100
movq 0x18(%rsp), %rdi
callq 0x60f5
movq 0x8(%rsp), %rdi
callq 0x6154
movzbl %r12b, %esi
movq %rbx, %rdi
callq 0x4471
cmpb $0x1, 0x3e(%rbx)
jne 0x82dc
movq %rbx, %rdi
callq 0x37b5
cmpl $0x0, 0x94(%rbx)
jle 0x82dc
leaq 0x6f78(%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
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5213
movzbl %r13b, %esi
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6643
movzbl %r13b, %esi
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7228