// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xc8, %rsp
movb %sil, %al
movq %rdi, 0xc0(%rsp)
andb $0x1, %al
movb %al, 0xbf(%rsp)
movq 0xc0(%rsp), %rax
cmpl $0x8, 0x20(%rax)
jg 0xb765
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x6c(%rax)
jne 0xb765
movq 0xc0(%rsp), %rdi
movb 0xbf(%rsp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x9150
jmp 0xc012
movq 0xc0(%rsp), %rax
testb $0x1, 0x3c(%rax)
je 0xb7ad
movq 0xc0(%rsp), %rax
cmpl $0x10, 0x20(%rax)
jg 0xb7ad
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x6c(%rax)
jne 0xb7ad
movq 0xc0(%rsp), %rdi
movb 0xbf(%rsp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0xa500
jmp 0xc012
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0xb7ca
leaq 0xa48b(%rip), %rdi      # 0x15c50
callq 0x2080
movq 0xc0(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0xb804
movq 0xc0(%rsp), %rdi
callq 0x3ed0
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0xb802
leaq 0xa1eb(%rip), %rdi      # 0x159e8
callq 0x2080
jmp 0xb806
jmp 0xb806
movq 0xc0(%rsp), %rdi
callq 0x3dd0
movq 0xc0(%rsp), %rdi
callq 0x6ae0
movq 0xc0(%rsp), %rax
movslq 0x20(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0xc0(%rsp), %rax
movq %rcx, 0x5118(%rax)
movq 0xc0(%rsp), %rax
movslq 0x38(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0xc0(%rsp), %rax
movq %rcx, 0x5108(%rax)
movq 0xc0(%rsp), %rax
movslq 0x38(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0xc0(%rsp), %rax
movq %rcx, 0x5110(%rax)
movq 0xc0(%rsp), %rax
cmpq $0x0, 0x5118(%rax)
je 0xb8cb
movq 0xc0(%rsp), %rax
cmpq $0x0, 0x5108(%rax)
je 0xb8cb
movq 0xc0(%rsp), %rax
cmpq $0x0, 0x5110(%rax)
jne 0xb8d7
leaq 0xa38f(%rip), %rdi      # 0x15c61
callq 0x3ce0
movq 0xc0(%rsp), %rax
testb $0x1, 0x68(%rax)
je 0xb8f4
movq 0xc0(%rsp), %rdi
callq 0xc020
jmp 0xb901
movq 0xc0(%rsp), %rdi
callq 0xc160
movq 0xc0(%rsp), %rax
testb $0x1, 0x70(%rax)
je 0xba3f
movl $0x0, 0xb8(%rsp)
movl 0xb8(%rsp), %eax
movq 0xc0(%rsp), %rcx
movl 0x38(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xb9c9
movq 0xc0(%rsp), %rax
movq 0x50f8(%rax), %rdi
movq 0xc0(%rsp), %rax
movq 0x5108(%rax), %rax
movslq 0xb8(%rsp), %rcx
movq (%rax,%rcx,8), %rax
shlq %rax
addq %rax, %rdi
movq 0xc0(%rsp), %rax
movq 0x5108(%rax), %rax
movl 0xb8(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
movq 0xc0(%rsp), %rax
movq 0x5108(%rax), %rax
movslq 0xb8(%rsp), %rcx
subq (%rax,%rcx,8), %rsi
shlq %rsi
movl $0x2, %edx
callq 0x22d0
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0xb91e
movq 0xc0(%rsp), %rax
movq 0x50f8(%rax), %rdi
movq 0xc0(%rsp), %rax
movq 0x5108(%rax), %rax
movq 0xc0(%rsp), %rcx
movl 0x38(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
shlq %rax
addq %rax, %rdi
movq 0xc0(%rsp), %rax
movq 0x28(%rax), %rsi
movq 0xc0(%rsp), %rax
movq 0x5108(%rax), %rax
movq 0xc0(%rsp), %rcx
movl 0x38(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
subq (%rax,%rcx,8), %rsi
shlq %rsi
movl $0x2, %edx
callq 0x22d0
movq 0xc0(%rsp), %rdi
callq 0x84d0
movq %rax, 0xb0(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x8640
movq %rax, 0xa8(%rsp)
movl $0x1, 0xa4(%rsp)
movl $0x6, 0xa0(%rsp)
movl $0x0, 0x9c(%rsp)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
movq 0xc0(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0xbaeb
movl 0xa4(%rsp), %eax
subl $0x2, %eax
movl %eax, %eax
cmpq $0xff, %rax
jbe 0xbaeb
movq 0xd534(%rip), %rax      # 0x18ff8
movq (%rax), %rdi
movl 0xa4(%rsp), %edx
subl $0x2, %edx
leaq 0x9f25(%rip), %rsi      # 0x159fd
movb $0x0, %al
callq 0x21b0
leaq 0xa17b(%rip), %rdi      # 0x15c61
callq 0x3ce0
movb $0x0, 0x9a(%rsp)
movq 0xc0(%rsp), %rax
movq 0x80(%rax), %rdi
callq 0x7750
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0xa8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movl 0xa4(%rsp), %edx
movl 0x9c(%rsp), %r8d
movq 0xc0(%rsp), %r9
leaq 0x9a(%rsp), %rcx
callq 0xc2b0
andb $0x1, %al
movb %al, 0x9b(%rsp)
movq 0xc0(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0xbd28
testb $0x1, 0xbf(%rsp)
je 0xbd28
movl 0xa4(%rsp), %eax
decl %eax
movl %eax, 0x84(%rsp)
callq 0x12730
movq %rax, %xmm0
movaps 0x8479(%rip), %xmm3     # 0x14010
movaps %xmm3, 0x60(%rsp)
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x8478(%rip), %xmm2     # 0x14020
movaps %xmm2, 0x70(%rsp)
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq 0xc0(%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 0xc0(%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 0xa8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x2180
movl 0x84(%rsp), %esi
movsd 0x88(%rsp), %xmm0
movsd 0x90(%rsp), %xmm1
movq %rax, %rdx
leaq 0x9d9f(%rip), %rdi      # 0x15a10
movb $0x2, %al
callq 0x20f0
movq 0xc0(%rsp), %rax
cmpq $0x0, 0x58(%rax)
je 0xbd26
movq 0xc0(%rsp), %rax
movq 0x58(%rax), %rdi
callq 0x2180
movslq 0xa0(%rsp), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, 0x58(%rsp)
movq 0xc0(%rsp), %rax
movq 0x58(%rax), %rdi
callq 0x2180
movq 0x58(%rsp), %rsi
cvtsi2sd %rax, %xmm0
movsd 0x9c0f(%rip), %xmm1     # 0x158e0
mulsd %xmm1, %xmm0
movslq 0xa0(%rsp), %rax
movq 0xc0(%rsp), %rcx
movq 0x28(%rcx), %rcx
imulq %rcx, %rax
movq %rax, %xmm1
movaps 0x8317(%rip), %xmm2     # 0x14010
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x831b(%rip), %xmm2     # 0x14020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
leaq 0x9d36(%rip), %rdi      # 0x15a55
movb $0x1, %al
callq 0x20f0
jmp 0xbd28
movl $0x1, %eax
subl 0x9c(%rsp), %eax
movl %eax, 0x9c(%rsp)
movq 0xc0(%rsp), %rax
testb $0x1, 0x3c(%rax)
je 0xbd83
testb $0x1, 0x9a(%rsp)
jne 0xbd83
movq 0xc0(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0xbd70
leaq 0x9d11(%rip), %rdi      # 0x15a7c
callq 0x2080
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x20d0
jmp 0xbdda
movq 0xa8(%rsp), %rax
cmpq $0x0, (%rax)
je 0xbda1
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
callq 0x20d0
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x2110
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0xa8(%rsp), %rax
movq %rcx, (%rax)
movb 0x9b(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xba8a
movq 0xa8(%rsp), %rax
cmpq $0x0, (%rax)
je 0xbdf8
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
callq 0x20d0
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0xbf5a
testb $0x1, 0xbf(%rsp)
je 0xbf0d
movq 0xc0(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x3c(%rsp)
callq 0x12730
movq %rax, 0x40(%rsp)
callq 0x12720
movq %rax, 0x48(%rsp)
callq 0x12730
movq %rax, %xmm0
movaps 0x81c1(%rip), %xmm3     # 0x14010
movaps %xmm3, 0x10(%rsp)
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x81c0(%rip), %xmm2     # 0x14020
movaps %xmm2, 0x20(%rsp)
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq 0xc0(%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, 0x50(%rsp)
callq 0x12720
movaps 0x10(%rsp), %xmm4
movaps 0x20(%rsp), %xmm3
movl 0x3c(%rsp), %esi
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
movsd 0x50(%rsp), %xmm0
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 0xc0(%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 0x9d64(%rip), %rdi      # 0x15c68
movb $0x2, %al
callq 0x20f0
jmp 0xbf58
movq 0xc0(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0xbf56
movq 0xc0(%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 0x9d6d(%rip), %rdi      # 0x15cbc
movb $0x0, %al
callq 0x20f0
jmp 0xbf58
jmp 0xbf5a
movq 0xb0(%rsp), %rdi
callq 0x8620
movq 0xa8(%rsp), %rdi
callq 0x86d0
movq 0xc0(%rsp), %rdi
movb 0xbf(%rsp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x5620
movq 0xc0(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0xbfc9
movq 0xc0(%rsp), %rdi
callq 0x4180
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0xbfc7
leaq 0x9b65(%rip), %rdi      # 0x15b25
movb $0x0, %al
callq 0x20f0
jmp 0xbfc9
movq 0xc0(%rsp), %rax
movq 0x5110(%rax), %rdi
callq 0x12940
movq 0xc0(%rsp), %rax
movq 0x5108(%rax), %rdi
callq 0x12940
movq 0xc0(%rsp), %rax
movq 0x5118(%rax), %rdi
callq 0x12940
movq 0xc0(%rsp), %rdi
callq 0x6bd0
addq $0xc8, %rsp
retq
nopw (%rax,%rax)