// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/./merge256.h
// Function: gap256
void gap256(g_data *g, bool lastRound) 
{
  assert(g->lcpCompute && !g->mwXMerge); // does not make sense to be here unless we want compute LCP from scratch  with multiway mergesort 
  // extra check we can really use 8 bits for Z and newZ and we are not interested in merging 
  assert(g->numBwt<=256 &&  !g->lcpMerge);
  if(g->verbose>0) puts("BWT merging with gap256");
  assert(sizeof(lcpInt) <= 2);
  assert(!g->lcpMerge);
  assert(!g->extMem);
  if(g->lcpCompute)       // we compute LCP values only if we are at the last round 
    assert(!g->bwtOnly && lastRound);
  else assert(g->bwtOnly); 

  // init local global vars
  check_g_data(g);
  // allocate Z (merge) Znew and B and clear them
  alloc_array32(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_arrays256(g);
  else init_arrays256_largealpha(g);  
  uint32_t prefixLength = 1;
  
  #ifdef USE_MMAP_ADVISE
  // advise on g->array32
  for (int i = 0; i < g->sizeOfAlpha-1; ++i) {
    madvise(g->array32 + g->firstColumn[i], (g->firstColumn[i+1]-g->firstColumn[i])*4, MADV_SEQUENTIAL);
  }
  madvise(g->array32 + g->firstColumn[g->sizeOfAlpha-1], (g->mergeLen-g->firstColumn[g->sizeOfAlpha-1])*4, 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
  int round=0;
  if(g->numBwt>1) {
    bool merge_completed; 
    do {
      prefixLength+= 1;
      if(prefixLength> 0xFFFF ) {fprintf(stderr,"prefixLength too large: %u\n", prefixLength);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=addCharToPrefix256(ibList,liquid,prefixLength,&mergeChanged,round,g);
      #if MALLOC_COUNT_FLAG
        if (g->verbose>1 && lastRound) {
          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
      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("Merge256 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("Merge256 completed (%d bwts). Mem: %zu peak, %zu current\n", g->numBwt, malloc_count_peak(),
             malloc_count_current());
    #else
      printf("Merge256 completed (%d bwts).\n", g->numBwt);
    #endif
  }
  liquid_free(liquid);
  ibHead_free(ibList);

  // computation complete, do the merging. Write the merged BWT to g->array32
  // the LCP if requested is in the high 16 bits of g->array32 and is written to the output file  
  mergeBWTandLCP256(g,lastRound);

  free(g->F); // last five arrays deallocated
  free(g->firstColumn); 
  free(g->inCnt);
  free_array32(g);
  assert(g->blockBeginsAt==NULL); // lcp values are not here!
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %r13d
movq %rdi, %rbx
cmpl $0x0, 0x94(%rdi)
jle 0x9507
leaq 0x587b(%rip), %rdi      # 0xed7d
callq 0x2090
movq %rbx, %rdi
callq 0x3610
movq %rbx, %rdi
callq 0x524f
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 0x956c
testq %r14, %r14
setne %cl
testq %rax, %rax
setne %al
testb %al, %cl
jne 0x9578
leaq 0x581b(%rip), %rdi      # 0xed8e
callq 0x35b7
cmpb $0x1, 0x68(%rbx)
jne 0x958b
movq %rbx, %rdi
callq 0x9a24
jmp 0x963e
movslq 0x20(%rbx), %rdi
shlq $0x3, %rdi
callq 0xbce0
movq %rax, 0x18(%rbx)
testq %rax, %rax
jne 0x95ad
leaq 0x5c76(%rip), %rdi      # 0xf21e
callq 0x35b7
xorl %r12d, %r12d
pushq $0x8
popq %r14
leaq 0x5c63(%rip), %r15      # 0xf21e
movslq 0x20(%rbx), %rax
cmpq %rax, %r12
jge 0x960e
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 0x95f5
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 0x95bb
movq %rbx, %rdi
callq 0x9a24
xorl %r14d, %r14d
movslq 0x20(%rbx), %rax
movq 0x18(%rbx), %rdi
cmpq %rax, %r14
jge 0x9634
movq (%rdi,%r14,8), %rdi
callq 0xbdb4
incq %r14
jmp 0x9619
callq 0xbdb4
andq $0x0, 0x18(%rbx)
xorl %r14d, %r14d
pushq $0x2
popq %rbp
movslq 0x38(%rbx), %rcx
leaq -0x1(%rcx), %rsi
movq 0x5100(%rbx), %rax
movq 0x5108(%rbx), %rdx
cmpq %rsi, %r14
jge 0x967f
movq (%rdx,%r14,8), %rcx
movq 0x8(%rdx,%r14,8), %rsi
leaq (%rax,%rcx,4), %rdi
incq %r14
subq %rcx, %rsi
shlq $0x2, %rsi
movl %ebp, %edx
callq 0x22e0
jmp 0x9644
movq -0x8(%rdx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rdi
movq 0x28(%rbx), %rsi
subq %rcx, %rsi
shlq $0x2, %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)
cmpl $0x2, 0x20(%rbx)
jl 0x98b8
pushq $0x1
popq %rax
movl %r13d, 0x14(%rsp)
movl %r13d, %ecx
xorb $0x1, %cl
movb %cl, 0x20(%rsp)
xorl %ebp, %ebp
movl %eax, %r12d
leal 0x1(%r12), %r13d
cmpl $0x10000, %r13d         # imm = 0x10000
jae 0x9854
movb $0x0, 0x7(%rsp)
movq 0x80(%rbx), %rdi
callq 0x58cd
movq 0x8(%rsp), %rdi
movq %rax, 0x8(%rdi)
movq 0x18(%rsp), %rsi
movl %r13d, %edx
leaq 0x7(%rsp), %rcx
movl %ebp, %r8d
movq %rbx, %r9
callq 0x9aa5
movl %eax, %r15d
cmpl $0x2, 0x94(%rbx)
setl %al
orb 0x20(%rsp), %al
jne 0x9808
callq 0xbc94
movq %rax, %xmm0
movq 0x38cd(%rip), %xmm3     # 0xd010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x38d1(%rip), %xmm4     # 0xd020
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm2
movq 0x28(%rbx), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %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 0x3878(%rip), %xmm2     # 0xd010
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x387c(%rip), %xmm3     # 0xd020
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm4
movq 0x28(%rbx), %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm3, %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 0x51f8(%rip), %rdi      # 0xe9e8
movl %r12d, %esi
movaps 0x40(%rsp), %xmm0
movapd 0x30(%rsp), %xmm1
movq %rax, %rdx
movb $0x2, %al
callq 0x2100
cmpb $0x1, 0x3c(%rbx)
jne 0x9815
cmpb $0x0, 0x7(%rsp)
je 0x9880
movq 0x8(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x9827
callq 0x20e0
pushq $0x1
popq %r12
subl %ebp, %r12d
movq 0x8(%rsp), %rbp
movq 0x8(%rbp), %r14
movq %r14, %rdi
callq 0x2120
movq %r14, (%rbp)
movl %r12d, %ebp
movl %r13d, %r12d
testb %r15b, %r15b
je 0x96d6
jmp 0x98a6
movq 0x979d(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x5530(%rip), %rsi      # 0xed95
movl %r13d, %edx
xorl %eax, %eax
callq 0x21c0
leaq 0x5518(%rip), %rdi      # 0xed8e
callq 0x35b7
jmp 0x96e8
cmpl $0x2, 0x94(%rbx)
jl 0x9895
leaq 0x51c4(%rip), %rdi      # 0xea54
callq 0x2090
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rdi
callq 0x20e0
movq (%r14), %r14
testq %r14, %r14
movl 0x14(%rsp), %r13d
je 0x98b8
movq %r14, %rdi
callq 0x20e0
movl 0x94(%rbx), %eax
testl %eax, %eax
jle 0x99ca
testb %r13b, %r13b
je 0x999f
movl 0x20(%rbx), %ebp
callq 0xbc94
movq %rax, %r14
callq 0xbc8c
movq %rax, %r12
callq 0xbc94
movq %rax, %xmm0
movq 0x371c(%rip), %xmm3     # 0xd010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x3720(%rip), %xmm4     # 0xd020
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm2
movq 0x28(%rbx), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %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 0x36c7(%rip), %xmm2     # 0xd010
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x36cb(%rip), %xmm3     # 0xd020
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq 0x28(%rbx), %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
leaq 0x5428(%rip), %rdi      # 0xedb1
movl %ebp, %esi
movq %r14, %rdx
movq %r12, %rcx
movaps 0x20(%rsp), %xmm0
movb $0x2, %al
callq 0x2100
jmp 0x99ca
cmpl $0x1, %eax
je 0x99ca
movl 0x20(%rbx), %ebp
callq 0xbc94
movq %rax, %r14
callq 0xbc8c
leaq 0x544a(%rip), %rdi      # 0xee05
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 %r13b, %esi
movq %rbx, %rdi
callq 0x4b96
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 0x52bc