// File: /workspace/github_c_makefile_star2_O0/felipelouza[P]egap/./merge8.h
// Function: gap8
void gap8(g_data *g, bool lastRound) {
  // check we can really use 4 bits
  assert(g->numBwt<=8 && g->lcpMerge==false);
  if(g->verbose>0) puts("BWT merging with gap8");
  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);    
  if(g->extMem) open_bw_files(g);
  // allocate an array containing Z (merge) Znew and B 
  // together they use 8 bits 1+1+3+3 
  assert(g->blockBeginsAt==NULL);
  alloc_merge_array(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_arrays8(g);
  else init_arrays8_largealpha(g);
  // now we are ready to give mmap advise
  #ifdef USE_MMAP_ADVISE
  if(g->mmapZ) { // advise on g->mergeColor
    for (int i = 0; i < g->sizeOfAlpha-1; ++i)
      madvise(g->mergeColor + g->firstColumn[i], (g->firstColumn[i+1]-g->firstColumn[i])*sizeof(palette), MADV_SEQUENTIAL);
    madvise(g->mergeColor + g->firstColumn[g->sizeOfAlpha-1], (g->mergeLen-g->firstColumn[g->sizeOfAlpha-1])*sizeof(palette), 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=addCharToPrefix8(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));
      // also EOF are written to unsortedLcp file so percentages are not accurate     
      #endif
      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 MALLOC_COUNT_FLAG
    if (g->verbose>0) {
      if(lastRound)
        printf("Merge8 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("Merge8 completed (%d bwts). Mem: %zu peak, %zu current\n", g->numBwt, malloc_count_peak(),
             malloc_count_current());
    }
  #else
    if (g->verbose>0) {
        printf("Merge8 completed (%d bwts).\n", g->numBwt);
    }
  #endif
  liquid_free(liquid);
  ibHead_free(ibList);  
  if(g->extMem) close_bw_files(g);

  // computation complete, do the merging. 
  // The following call writes the merged BWT back to g->bws[0] (if extMem to the BWTs file)
  mergeBWT8(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_array(g);
}

// Assembly:
subq $0xc8, %rsp
movb %sil, %al
movq %rdi, 0xc0(%rsp)
andb $0x1, %al
movb %al, 0xbf(%rsp)
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0x9188
leaq 0xc84f(%rip), %rdi      # 0x159d2
callq 0x2080
movq 0xc0(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0x91c2
movq 0xc0(%rsp), %rdi
callq 0x3ed0
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0x91c0
leaq 0xc82d(%rip), %rdi      # 0x159e8
callq 0x2080
jmp 0x91c4
jmp 0x91c4
movq 0xc0(%rsp), %rdi
callq 0x3dd0
movq 0xc0(%rsp), %rax
testb $0x1, 0x69(%rax)
je 0x91ec
movq 0xc0(%rsp), %rdi
callq 0x6da0
movq 0xc0(%rsp), %rdi
callq 0x6830
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 0x92a4
movq 0xc0(%rsp), %rax
cmpq $0x0, 0x5108(%rax)
je 0x92a4
movq 0xc0(%rsp), %rax
cmpq $0x0, 0x5110(%rax)
jne 0x92b0
leaq 0xc738(%rip), %rdi      # 0x159e3
callq 0x3ce0
movq 0xc0(%rsp), %rax
testb $0x1, 0x68(%rax)
je 0x92cd
movq 0xc0(%rsp), %rdi
callq 0x9a10
jmp 0x92da
movq 0xc0(%rsp), %rdi
callq 0x9b40
movq 0xc0(%rsp), %rax
testb $0x1, 0x70(%rax)
je 0x940e
movl $0x0, 0xb8(%rsp)
movl 0xb8(%rsp), %eax
movq 0xc0(%rsp), %rcx
movl 0x38(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x939d
movq 0xc0(%rsp), %rax
movq 0x50e8(%rax), %rdi
movq 0xc0(%rsp), %rax
movq 0x5108(%rax), %rax
movslq 0xb8(%rsp), %rcx
addq (%rax,%rcx,8), %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 $0x0, %rsi
movl $0x2, %edx
callq 0x22d0
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x92f7
movq 0xc0(%rsp), %rax
movq 0x50e8(%rax), %rdi
movq 0xc0(%rsp), %rax
movq 0x5108(%rax), %rax
movq 0xc0(%rsp), %rcx
movl 0x38(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addq (%rax,%rcx,8), %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 $0x0, %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 0x94ba
movl 0xa4(%rsp), %eax
subl $0x2, %eax
movl %eax, %eax
cmpq $0xff, %rax
jbe 0x94ba
movq 0xfb65(%rip), %rax      # 0x18ff8
movq (%rax), %rdi
movl 0xa4(%rsp), %edx
subl $0x2, %edx
leaq 0xc556(%rip), %rsi      # 0x159fd
movb $0x0, %al
callq 0x21b0
leaq 0xc52e(%rip), %rdi      # 0x159e3
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 0x9c90
andb $0x1, %al
movb %al, 0x9b(%rsp)
movq 0xc0(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0x96f7
testb $0x1, 0xbf(%rsp)
je 0x96f7
movl 0xa4(%rsp), %eax
decl %eax
movl %eax, 0x84(%rsp)
callq 0x12730
movq %rax, %xmm0
movaps 0xaaaa(%rip), %xmm3     # 0x14010
movaps %xmm3, 0x60(%rsp)
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0xaaa9(%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 0xc3d0(%rip), %rdi      # 0x15a10
movb $0x2, %al
callq 0x20f0
movq 0xc0(%rsp), %rax
cmpq $0x0, 0x58(%rax)
je 0x96f5
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 0xc240(%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 0xa948(%rip), %xmm2     # 0x14010
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0xa94c(%rip), %xmm2     # 0x14020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
leaq 0xc367(%rip), %rdi      # 0x15a55
movb $0x1, %al
callq 0x20f0
jmp 0x96f7
movl $0x1, %eax
subl 0x9c(%rsp), %eax
movl %eax, 0x9c(%rsp)
movq 0xc0(%rsp), %rax
testb $0x1, 0x3c(%rax)
je 0x9752
testb $0x1, 0x9a(%rsp)
jne 0x9752
movq 0xc0(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0x973f
leaq 0xc342(%rip), %rdi      # 0x15a7c
callq 0x2080
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x20d0
jmp 0x97a9
movq 0xa8(%rsp), %rax
cmpq $0x0, (%rax)
je 0x9770
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 0x9459
movq 0xa8(%rsp), %rax
cmpq $0x0, (%rax)
je 0x97c7
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
callq 0x20d0
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0x9929
testb $0x1, 0xbf(%rsp)
je 0x98dc
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 0xa7f2(%rip), %xmm3     # 0x14010
movaps %xmm3, 0x10(%rsp)
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0xa7f1(%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 0xc1c8(%rip), %rdi      # 0x15a9b
movb $0x2, %al
callq 0x20f0
jmp 0x9927
movq 0xc0(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0x9925
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 0xc1cf(%rip), %rdi      # 0x15aed
movb $0x0, %al
callq 0x20f0
jmp 0x9927
jmp 0x9929
movq 0xb0(%rsp), %rdi
callq 0x8620
movq 0xa8(%rsp), %rdi
callq 0x86d0
movq 0xc0(%rsp), %rax
testb $0x1, 0x69(%rax)
je 0x995e
movq 0xc0(%rsp), %rdi
callq 0x6f10
movq 0xc0(%rsp), %rdi
movb 0xbf(%rsp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x5860
movq 0xc0(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0x99b3
movq 0xc0(%rsp), %rdi
callq 0x4180
movq 0xc0(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0x99b1
leaq 0xc17b(%rip), %rdi      # 0x15b25
movb $0x0, %al
callq 0x20f0
jmp 0x99b3
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 0x6930
addq $0xc8, %rsp
retq
nopw %cs:(%rax,%rax)