// File: /workspace/github_c_makefile_star2_O1/felipelouza[P]egap/./merge16.h
// Function: gap16
void gap16(g_data *g, bool lastRound) {

  // check we can really use 4 bits
  assert(g->numBwt<=16);
  if(g->numBwt<=8 && g->bwtOnly && !g->algorithm) return gap8(g,lastRound);
  if(g->verbose>0) puts("BWT merging with gap16");
  assert(!g->lcpCompute);
  assert(!g->extMem);
  // init local global vars
  check_g_data(g);
  // allocate and clear bit/int array B 
  if(!g->lcpMerge) g->bitB = tba_alloc(g->mergeLen, g->mmapB);
  else alloc0_B_array(g); // alloc and clear blockBeginsAt array
    
  // allocate Z (merge) Znew 
  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_arrays16(g);
  else init_arrays16_largealpha(g);  
  customInt prefixLength = 1;
  
  // 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>MAX_LCP_SIZE && !g->bwtOnly) {fprintf(stderr,"LCP too large\n");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=addCharToPrefix16(ibList,liquid,prefixLength,&mergeChanged,round,g);
      #if MALLOC_COUNT_FLAG
        if (g->verbose>1 && lastRound) {
          printf("Lcp: "CUSTOM_FORMAT". Memory: %zu peak, %zu current, %.4lf/%.4lf bytes/symbol\n", 
               prefixLength-1, malloc_count_peak(),
               malloc_count_current(), (double)malloc_count_peak()/g->mergeLen,
               (double)malloc_count_current()/g->mergeLen);
        }
      #endif
      if(g->bwtOnly && !mergeChanged) {
        if(g->verbose>1) puts("Gap bwt-only early termination");
        fclose(ibList->fout);
        break;
      }
      round  = 1 - round; // change round parity
      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("Merge16 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("Merge16 completed (%d bwts). Mem: %zu peak, %zu current\n", g->numBwt, malloc_count_peak(),
             malloc_count_current());
      #else
        printf("Merge16 completed (%d bwts).\n", g->numBwt);
      #endif
  }
  liquid_free(liquid);
  ibHead_free(ibList);  

  // normalize mergeColor
  if(round!=0) for(customInt i=0;i<g->mergeLen;i++) g->mergeColor[i] >>=4;
  else for(customInt i=0;i<g->mergeLen;i++) g->mergeColor[i] &= 0x0F;
  // computation complete, do the merging. The following call writes the
  // (possibly remapped) merged BWT back to g->bws[0]; and if lcpMerge==true the merged LCP to g->lcps[0]     
  mergeBWTandLCP(g,lastRound);

  // free B array 
  if(!g->lcpMerge) tba_free(g->bitB, g->mergeLen, g->mmapB);
  else free_B_array(g); 
  free(g->F); // last five arrays deallocated
  free(g->firstColumn); 
  free(g->inCnt);
  free_merge_array(g);// contains merge and newMerge
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %ebp
movq %rdi, %rbx
cmpl $0x8, 0x20(%rdi)
jg 0x7850
cmpb $0x1, 0x3c(%rbx)
jne 0x7850
cmpl $0x0, 0x6c(%rbx)
je 0x7dac
cmpl $0x0, 0x94(%rbx)
jle 0x7865
leaq 0x82e5(%rip), %rdi      # 0xfb45
callq 0x2090
movq %rbx, %rdi
callq 0x39cf
cmpb $0x0, 0x3d(%rbx)
je 0x787d
movq %rbx, %rdi
callq 0x5535
jmp 0x7891
movq 0x28(%rbx), %rdi
movzbl 0x71(%rbx), %esi
callq 0x6aef
movq %rax, 0x98(%rbx)
movq %rbx, %rdi
callq 0x547e
movslq 0x20(%rbx), %rdi
shlq $0x3, %rdi
callq 0xc63a
movq %rax, %r15
movq %rax, 0x5118(%rbx)
movslq 0x38(%rbx), %r12
shlq $0x3, %r12
movq %r12, %rdi
callq 0xc63a
movq %rax, %r14
movq %rax, 0x5108(%rbx)
movq %r12, %rdi
callq 0xc63a
movq %rax, 0x5110(%rbx)
testq %r15, %r15
je 0x78ee
testq %r14, %r14
setne %cl
testq %rax, %rax
setne %al
testb %al, %cl
jne 0x78fa
leaq 0x8261(%rip), %rdi      # 0xfb56
callq 0x3973
cmpb $0x1, 0x68(%rbx)
jne 0x790d
movq %rbx, %rdi
callq 0x7dc6
jmp 0x79cb
movslq 0x20(%rbx), %rdi
shlq $0x3, %rdi
callq 0xc63a
movq %rax, 0x18(%rbx)
testq %rax, %rax
jne 0x792f
leaq 0x87ef(%rip), %rdi      # 0x10119
callq 0x3973
cmpl $0x0, 0x20(%rbx)
jle 0x7990
xorl %r15d, %r15d
leaq 0x87da(%rip), %r14      # 0x10119
movslq 0x38(%rbx), %rdi
movl $0x8, %esi
callq 0xc78c
movq 0x18(%rbx), %rcx
movq %rax, (%rcx,%r15,8)
movq 0x18(%rbx), %rax
cmpq $0x0, (%rax,%r15,8)
jne 0x7968
movq %r14, %rdi
callq 0x3973
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq (%rax,%r15,8), %rdi
movq (%rcx,%r15,8), %rsi
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rdx
callq 0xc41b
incq %r15
movslq 0x20(%rbx), %rax
cmpq %rax, %r15
jl 0x793f
movq %rbx, %rdi
callq 0x7dc6
cmpl $0x0, 0x20(%rbx)
jle 0x79ba
xorl %r14d, %r14d
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rdi
callq 0xc6e6
incq %r14
movslq 0x20(%rbx), %rax
cmpq %rax, %r14
jl 0x79a1
movq 0x18(%rbx), %rdi
callq 0xc6e6
movq $0x0, 0x18(%rbx)
movq %rbx, %rdi
callq 0x6534
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0x65d8
movq %rax, %r15
movb $0x1, %r13b
cmpl $0x2, 0x20(%rbx)
jl 0x7bc3
movl %ebp, %eax
movl $0x1, %ebp
xorl %r12d, %r12d
movl %eax, 0x10(%rsp)
xorb $0x1, %al
movb %al, 0x20(%rsp)
leaq 0x1(%rbp), %r13
cmpq $0x100, %r13            # imm = 0x100
jb 0x7a1b
cmpb $0x0, 0x3c(%rbx)
je 0x7ba1
movb $0x0, 0xf(%rsp)
movq 0x80(%rbx), %rdi
callq 0x5d78
movq %rax, 0x8(%r15)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
leaq 0xf(%rsp), %rcx
movl %r12d, %r8d
movq %rbx, %r9
callq 0x7ec7
movl %eax, %r14d
cmpl $0x2, 0x94(%rbx)
setl %al
orb 0x20(%rsp), %al
jne 0x7b3c
callq 0xc614
movq %rax, 0x38(%rsp)
callq 0xc60c
movl %r12d, 0x14(%rsp)
movq %rax, %r12
callq 0xc614
movq %rax, %xmm0
movq 0x6585(%rip), %xmm3     # 0xe010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x6589(%rip), %xmm4     # 0xe020
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 0xc60c
movq %rax, %xmm0
movq 0x6530(%rip), %xmm2     # 0xe010
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x6534(%rip), %xmm3     # 0xe020
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 0x804b(%rip), %rdi      # 0xfb6b
movq %rbp, %rsi
movq 0x38(%rsp), %rdx
movq %r12, %rcx
movl 0x14(%rsp), %r12d
movaps 0x40(%rsp), %xmm0
movb $0x2, %al
callq 0x2100
cmpb $0x1, 0x3c(%rbx)
jne 0x7b49
cmpb $0x0, 0xf(%rsp)
je 0x7b72
movq (%r15), %rdi
testq %rdi, %rdi
je 0x7b56
callq 0x20e0
movl $0x1, %ebp
subl %r12d, %ebp
movq 0x8(%r15), %r12
movq %r12, %rdi
callq 0x2120
movq %r12, (%r15)
movl %ebp, %r12d
jmp 0x7b93
cmpl $0x2, 0x94(%rbx)
jl 0x7b87
leaq 0x7f1a(%rip), %rdi      # 0xfa9c
callq 0x2090
movq 0x8(%r15), %rdi
callq 0x20e0
movb $0x1, %r14b
movq %r13, %rbp
testb %r14b, %r14b
je 0x7a04
jmp 0x7bab
callq 0x2533
jmp 0x7a1b
movq (%r15), %rdi
testq %rdi, %rdi
je 0x7bb8
callq 0x20e0
testl %r12d, %r12d
sete %r13b
movl 0x10(%rsp), %ebp
movl 0x94(%rbx), %eax
testl %eax, %eax
jle 0x7ce3
testb %bpl, %bpl
je 0x7cb2
movl %ebp, 0x10(%rsp)
movl 0x20(%rbx), %ebp
callq 0xc614
movq %rax, %r14
callq 0xc60c
movq %rax, %r12
callq 0xc614
movq %rax, %xmm0
movq 0x640d(%rip), %xmm3     # 0xe010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x6411(%rip), %xmm4     # 0xe020
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 0xc60c
movq %rax, %xmm0
movq 0x63b8(%rip), %xmm2     # 0xe010
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x63bc(%rip), %xmm3     # 0xe020
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 0x7f16(%rip), %rdi      # 0xfbae
movl %ebp, %esi
movl 0x10(%rsp), %ebp
movq %r14, %rdx
movq %r12, %rcx
movaps 0x20(%rsp), %xmm0
movb $0x2, %al
callq 0x2100
jmp 0x7ce3
cmpl $0x1, %eax
je 0x7ce3
movl %ebp, %r12d
movl 0x20(%rbx), %ebp
callq 0xc614
movq %rax, %r14
callq 0xc60c
leaq 0x7f30(%rip), %rdi      # 0xfc01
movl %ebp, %esi
movl %r12d, %ebp
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2100
movq 0x18(%rsp), %r14
movq 0x20(%r14), %rdi
callq 0xc6e6
movq %r14, %rdi
callq 0xc6e6
movq %r15, %rdi
callq 0x6626
movq 0x28(%rbx), %rax
testb %r13b, %r13b
je 0x7d27
testq %rax, %rax
je 0x7d42
xorl %eax, %eax
movq 0x50e8(%rbx), %rcx
andb $0xf, (%rcx,%rax)
incq %rax
cmpq 0x28(%rbx), %rax
jb 0x7d11
jmp 0x7d42
testq %rax, %rax
je 0x7d42
xorl %eax, %eax
movq 0x50e8(%rbx), %rcx
shrb $0x4, (%rcx,%rax)
incq %rax
cmpq 0x28(%rbx), %rax
jb 0x7d2e
movzbl %bpl, %esi
movq %rbx, %rdi
callq 0x3c93
cmpb $0x0, 0x3d(%rbx)
je 0x7d5e
movq %rbx, %rdi
callq 0x5594
jmp 0x7d72
movq 0x28(%rbx), %rsi
movq 0x98(%rbx), %rdi
movzbl 0x71(%rbx), %edx
callq 0x6b59
movq 0x5110(%rbx), %rdi
callq 0xc6e6
movq 0x5108(%rbx), %rdi
callq 0xc6e6
movq 0x5118(%rbx), %rdi
callq 0xc6e6
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x54f9
movzbl %bpl, %esi
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6b9b