// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xb8, %rsp
movb %sil, %al
movq %rdi, 0xb0(%rsp)
andb $0x1, %al
movb %al, 0xaf(%rsp)
movq 0xb0(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0xdb28
leaq 0x82b8(%rip), %rdi      # 0x15ddb
callq 0x2080
movq 0xb0(%rsp), %rax
testb $0x1, 0x3e(%rax)
je 0xdb38
jmp 0xdb3a
jmp 0xdb3a
movq 0xb0(%rsp), %rdi
callq 0x3dd0
movq 0xb0(%rsp), %rdi
callq 0x6c40
movq 0xb0(%rsp), %rax
movslq 0x20(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x5118(%rax)
movq 0xb0(%rsp), %rax
movslq 0x38(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x5108(%rax)
movq 0xb0(%rsp), %rax
movslq 0x38(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x5110(%rax)
movq 0xb0(%rsp), %rax
cmpq $0x0, 0x5118(%rax)
je 0xdbff
movq 0xb0(%rsp), %rax
cmpq $0x0, 0x5108(%rax)
je 0xdbff
movq 0xb0(%rsp), %rax
cmpq $0x0, 0x5110(%rax)
jne 0xdc0b
leaq 0x81e6(%rip), %rdi      # 0x15dec
callq 0x3ce0
movq 0xb0(%rsp), %rax
testb $0x1, 0x68(%rax)
je 0xdc28
movq 0xb0(%rsp), %rdi
callq 0xe240
jmp 0xdc35
movq 0xb0(%rsp), %rdi
callq 0xe3a0
movl $0x1, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movq 0xb0(%rsp), %rcx
movl 0x38(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xdcf8
movq 0xb0(%rsp), %rax
movq 0x5100(%rax), %rdi
movq 0xb0(%rsp), %rax
movq 0x5108(%rax), %rax
movslq 0xa4(%rsp), %rcx
movq (%rax,%rcx,8), %rax
shlq $0x2, %rax
addq %rax, %rdi
movq 0xb0(%rsp), %rax
movq 0x5108(%rax), %rax
movl 0xa4(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
movq 0xb0(%rsp), %rax
movq 0x5108(%rax), %rax
movslq 0xa4(%rsp), %rcx
subq (%rax,%rcx,8), %rsi
shlq $0x2, %rsi
movl $0x2, %edx
callq 0x22d0
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0xdc4b
movq 0xb0(%rsp), %rax
movq 0x5100(%rax), %rdi
movq 0xb0(%rsp), %rax
movq 0x5108(%rax), %rax
movq 0xb0(%rsp), %rcx
movl 0x38(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
shlq $0x2, %rax
addq %rax, %rdi
movq 0xb0(%rsp), %rax
movq 0x28(%rax), %rsi
movq 0xb0(%rsp), %rax
movq 0x5108(%rax), %rax
movq 0xb0(%rsp), %rcx
movl 0x38(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
subq (%rax,%rcx,8), %rsi
shlq $0x2, %rsi
movl $0x2, %edx
callq 0x22d0
movq 0xb0(%rsp), %rdi
callq 0x84d0
movq %rax, 0x98(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x8640
movq %rax, 0x90(%rsp)
movl $0x0, 0x8c(%rsp)
movq 0xb0(%rsp), %rax
cmpl $0x1, 0x20(%rax)
jle 0xe055
jmp 0xddb9
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
cmpl $0xffff, 0xa8(%rsp) # imm = 0xFFFF
jbe 0xde02
movq 0xb21a(%rip), %rax      # 0x18ff8
movq (%rax), %rdi
movl 0xa8(%rsp), %edx
leaq 0x8004(%rip), %rsi      # 0x15df3
movb $0x0, %al
callq 0x21b0
leaq 0x7fef(%rip), %rdi      # 0x15dec
callq 0x3ce0
movb $0x0, 0x8a(%rsp)
movq 0xb0(%rsp), %rax
movq 0x80(%rax), %rdi
callq 0x7750
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x90(%rsp), %rdi
movq 0x98(%rsp), %rsi
movl 0xa8(%rsp), %edx
movl 0x8c(%rsp), %r8d
movq 0xb0(%rsp), %r9
leaq 0x8a(%rsp), %rcx
callq 0xe4f0
andb $0x1, %al
movb %al, 0x8b(%rsp)
movq 0xb0(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0xdf83
testb $0x1, 0xaf(%rsp)
je 0xdf83
movl 0xa8(%rsp), %eax
decl %eax
movl %eax, 0x74(%rsp)
callq 0x12730
movq %rax, %xmm0
movaps 0x6165(%rip), %xmm3     # 0x14010
movaps %xmm3, 0x50(%rsp)
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x6164(%rip), %xmm2     # 0x14020
movaps %xmm2, 0x60(%rsp)
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq 0xb0(%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, 0x78(%rsp)
callq 0x12720
movaps 0x50(%rsp), %xmm3
movaps 0x60(%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 0xb0(%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, 0x80(%rsp)
movq 0x90(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x2180
movl 0x74(%rsp), %esi
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movq %rax, %rdx
leaq 0x7a94(%rip), %rdi      # 0x15a10
movb $0x2, %al
callq 0x20f0
movl $0x1, %eax
subl 0x8c(%rsp), %eax
movl %eax, 0x8c(%rsp)
movq 0xb0(%rsp), %rax
testb $0x1, 0x3c(%rax)
je 0xdfde
testb $0x1, 0x8a(%rsp)
jne 0xdfde
movq 0xb0(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0xdfcb
leaq 0x7ab6(%rip), %rdi      # 0x15a7c
callq 0x2080
movq 0x90(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x20d0
jmp 0xe035
movq 0x90(%rsp), %rax
cmpq $0x0, (%rax)
je 0xdffc
movq 0x90(%rsp), %rax
movq (%rax), %rdi
callq 0x20d0
movq 0x90(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x2110
movq 0x90(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x90(%rsp), %rax
movq %rcx, (%rax)
movb 0x8b(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xddb9
movq 0x90(%rsp), %rax
cmpq $0x0, (%rax)
je 0xe053
movq 0x90(%rsp), %rax
movq (%rax), %rdi
callq 0x20d0
jmp 0xe055
movq 0xb0(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0xe1b7
testb $0x1, 0xaf(%rsp)
je 0xe16a
movq 0xb0(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x34(%rsp)
callq 0x12730
movq %rax, 0x38(%rsp)
callq 0x12720
movq %rax, 0x40(%rsp)
callq 0x12730
movq %rax, %xmm0
movaps 0x5f64(%rip), %xmm3     # 0x14010
movaps %xmm3, 0x10(%rsp)
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x5f63(%rip), %xmm2     # 0x14020
movaps %xmm2, 0x20(%rsp)
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq 0xb0(%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, 0x48(%rsp)
callq 0x12720
movaps 0x10(%rsp), %xmm4
movaps 0x20(%rsp), %xmm3
movl 0x34(%rsp), %esi
movq 0x38(%rsp), %rdx
movq 0x40(%rsp), %rcx
movsd 0x48(%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 0xb0(%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 0x7cae(%rip), %rdi      # 0x15e0f
movb $0x2, %al
callq 0x20f0
jmp 0xe1b5
movq 0xb0(%rsp), %rax
cmpl $0x1, 0x94(%rax)
jle 0xe1b3
movq 0xb0(%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 0x7cb7(%rip), %rdi      # 0x15e63
movb $0x0, %al
callq 0x20f0
jmp 0xe1b5
jmp 0xe1b7
movq 0x98(%rsp), %rdi
callq 0x8620
movq 0x90(%rsp), %rdi
callq 0x86d0
movq 0xb0(%rsp), %rdi
movb 0xaf(%rsp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x6040
movq 0xb0(%rsp), %rax
movq 0x5110(%rax), %rdi
callq 0x12940
movq 0xb0(%rsp), %rax
movq 0x5108(%rax), %rdi
callq 0x12940
movq 0xb0(%rsp), %rax
movq 0x5118(%rax), %rdi
callq 0x12940
movq 0xb0(%rsp), %rdi
callq 0x6d20
addq $0xb8, %rsp
retq
nopl (%rax,%rax)