// File: /workspace/github_c_makefile_star2_O3/felipelouza[P]egap/./merge16.h
// Function: addCharToPrefix16
static bool addCharToPrefix16(solidBlockFile *solidHead, liquidBlock *liquid, customInt prefixLength, bool *mergeChanged, const int round, g_data *g) {
  assert(prefixLength <= MAX_LCP_SIZE);
  assert(liquid->empty);
  liquid->beginsAt = liquid->endsAt = 0;  
  for(int i=0;i<liquid->occ_size;i++) assert(liquid->occ[i]==0);
  // copy first column to F 
  array_copy(g->F, g->firstColumn, g->sizeOfAlpha); //initialize char positions
  // pointer inside each BWT (k_0 & k_1 in the pseudocode)
  array_clear(g->inCnt,g->numBwt,0);
  // id for each character, init with an invalid id
  customInt blockID[g->sizeOfAlpha];
  array_clear(blockID,g->sizeOfAlpha, g->mergeLen);  // mergeLen is an invalid id
  customInt id = 0, k; 
  int m = (round%2==1) ? 1 : 2;  // mask for the bitB array

  protoBlock cblock = {.mono = false};
  solidBlock *next = readBlock(solidHead); // first block
  solidBlock *last = NULL;
  
  for (k = 0; k < g->mergeLen; ) { 
    assert(next==NULL || k <= next->beginsAt);  // we did not pass next block
    assert(last==NULL || last->nextBlock == next); // last is the immediately preceeding block

    // check if we are entering a block, and if the block is at least 2 iterations old
    bool start_block, last_block_recent=true; // for k=0 a new block starts, so last id properly initialized
    if(!g->bwtOnly) {
      start_block = (g->blockBeginsAt[k]>0) && (g->blockBeginsAt[k] < prefixLength);
      if(start_block) last_block_recent = g->blockBeginsAt[k]>=prefixLength-1;
    }
    else {
      start_block = tba_block_test_set(g->bitB,k,m,&last_block_recent);    
    }
    if (start_block) {
      // check if the block we just left is not recent and monochrome 
      if(cblock.mono==true && !last_block_recent) {
        cblock.endsAt = k;           // solidifiable monochorome block just ended
        add_proto2liquid(&cblock,liquid);  // add proto to liquid that remains active 
      }
      else { // proto block cannot be added, close current liquid
        if(!liquid->empty)
          last = finalize_liquid(last,liquid,next,solidHead); // this is the only point where a new block is created
        assert(liquid->empty);
        liquid->beginsAt=liquid->endsAt=k; // start empty liquid block
      }
      assert(liquid->endsAt==k);
      // block ending at k considered, now look forward 
      if(next!=NULL && next->beginsAt==k) { // entering an irrelevant block
        skip(next, g);                      // skip block 
        k = next->endsAt;                   // update k
        // merge liquid with next block and possibly previous 
        if(last==NULL || last->endsAt!=liquid->beginsAt) {
            if(!liquid->empty) merge_liquid(liquid,next,solidHead); // simple merge 
            if(last!=NULL) writeBlock(last,solidHead);  // save current last  
            last = next;   // advance last           
        } 
        else  //three way merge, next is freed last does not change
          merge_sls(last,liquid,next,solidHead); // only point where a solid block can be destroyed 
        assert(liquid->empty);
        liquid->beginsAt=liquid->endsAt=k; // start empty liquid block
        next = readBlock(solidHead);       // next has become last, update next (was: next = last->nextBlock; )
        last->nextBlock = next;
        assert(k==last->endsAt);
        cblock.mono = false;        // prevent re-adding the just skipped block
        continue;                   // resume from the end of the block 
      }
      // we are entering a relevant block, unless it is a recent one make it a candidate for solidification  
      if( !last_block_recent ) {
        cblock.beginsAt = k; cblock.mono=true; cblock.color =  get_mergeColor(k,round);     // g->mergeColor[k];
        cblock.start = &g->bws[cblock.color][g->inCnt[cblock.color]]; // bwt-position of first char in block
      }
      else cblock.mono = false; // not a candidate for solid block, wait next iteration 
      if(last_block_recent) 
         id = k;    // id of the new block 
    }
    // processing a char in a relevant block
    int currentColor = get_mergeColor(k,round);   // g->mergeColor[k] b in pseudocode
    int currentChar = g->bws[currentColor][g->inCnt[currentColor]++]; // c in pseudocode
    if(currentColor != cblock.color) cblock.mono = false;       // block is not monochrome
    // write color in new Z array, except 0 chars
    if(currentChar!=0) {
      customInt positionToUpdate = g->F[currentChar]++;
      set_mergeColor(positionToUpdate,currentColor,round); // g->newMergeColor[positionToUpdate] = currentColor;
      if(g->bwtOnly && !*mergeChanged && get_mergeColor(positionToUpdate,round)!=currentColor)
        *mergeChanged=true;  // remember there is a difference from the previous ieration 
      // create new block?
      if (blockID[currentChar] != id) {
        if(g->bwtOnly) { // no lcp just mark B array 
          if(last_block_recent) tba_mark_if0(g->bitB,positionToUpdate,m);
        } else    // update lcp
          if(last_block_recent && g->blockBeginsAt[positionToUpdate]==0) // only 0 values in B are overwritten
            g->blockBeginsAt[positionToUpdate] = prefixLength;
        blockID[currentChar] = id; // update block id, always!
      }
    }
    k++;
  } // end main loop
  assert(next==NULL); 
  if(cblock.mono==true) { // final mono block can be created 
    cblock.endsAt = k;
    add_proto2liquid(&cblock,liquid);  // add proto to liquid that remains active
    assert(!liquid->empty);
  }
  if(!liquid->empty) 
    last = finalize_liquid(last,liquid,NULL,solidHead); // a new block could be created 
  assert(liquid->empty);
  // check if all sequence has become irrelevant
  bool everything_irrelevant = false;
  if(last!=NULL && last->beginsAt==0 && last->endsAt==g->mergeLen)
    everything_irrelevant = true;
  // save last block   
  if(last!=NULL) writeBlock(last,solidHead);
  // check we have read all chars from all BWT's 
  for(int i=0; i<g->numBwt; i++)
    assert(g->inCnt[i]==g->bwtLen[i]);

  return everything_irrelevant;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, %r14
movl %r8d, -0x2c(%rbp)
movq %rcx, -0x68(%rbp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rsi)
movq 0x5108(%r9), %rsi
movq 0x5110(%r9), %rdi
movslq 0x38(%r9), %rdx
callq 0x3a40
movq 0x5118(%r14), %rdi
movslq 0x20(%r14), %rsi
movq $0x0, -0x38(%rbp)
xorl %edx, %edx
callq 0x33a4
movslq 0x38(%r14), %rsi
movl %esi, %eax
movq %rsp, %r15
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq 0x28(%r14), %rdx
movq %r15, %rdi
callq 0x33a4
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movq $0x0, -0x80(%rbp)
movq %rbx, %rdi
callq 0xb878
movq %rax, -0x58(%rbp)
cmpq $0x0, 0x28(%r14)
je 0x840e
movq %rbx, -0x40(%rbp)
movl -0x2c(%rbp), %r11d
movl %r11d, %eax
andl $0x1, %eax
movl $0x2, %ecx
subl %eax, %ecx
movq %r12, -0x70(%rbp)
leaq -0x1(%r12), %rax
movq %rax, -0xb8(%rbp)
movl %ecx, -0x5c(%rbp)
movl %ecx, %eax
movq %rax, -0xa8(%rbp)
xorl %eax, %eax
movq $0x0, -0x50(%rbp)
xorl %r12d, %r12d
movq $0x0, -0x38(%rbp)
movq %r13, -0xb0(%rbp)
movq %r15, -0x48(%rbp)
movq 0x98(%r14), %rdx
cmpb $0x0, 0x3c(%r14)
je 0x80e2
movq %r12, %rsi
shrq $0x5, %rsi
movq (%rdx,%rsi,8), %rdi
leal (%r12,%r12), %ecx
andl $0x3e, %ecx
movq %rdi, %r8
shrq %cl, %r8
andl $0x3, %r8d
movl %r8d, %r9d
xorl -0x5c(%rbp), %r9d
cmpl $0x3, %r9d
jne 0x810d
movl $0x3, %eax
shlq %cl, %rax
orq %rax, %rdi
movq %rdi, (%rdx,%rsi,8)
movb $0x1, %bl
jmp 0x813f
movzbl (%rdx,%r12), %ecx
testq %rcx, %rcx
sete %sil
cmpq -0x70(%rbp), %rcx
setae %dil
movb $0x1, %dl
orb %sil, %dil
jne 0x822a
cmpq %rcx, -0xb8(%rbp)
setbe %bl
jmp 0x811b
movb $0x1, %dl
cmpl $0x3, %r8d
jne 0x822a
xorl %ebx, %ebx
testb %bl, %bl
jne 0x813f
testb $0x1, %al
je 0x813f
movq %r13, %rsi
movq %r12, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xb9b7
movl -0x2c(%rbp), %r11d
jmp 0x816a
cmpb $0x0, 0x2c(%r13)
jne 0x8162
movq -0x38(%rbp), %rdi
movq %r13, %rsi
movq -0x58(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0x6839
movl -0x2c(%rbp), %r11d
movq %rax, -0x38(%rbp)
movq %r12, 0x8(%r13)
movq %r12, (%r13)
movq -0x58(%rbp), %r15
testq %r15, %r15
je 0x81bc
cmpq %r12, (%r15)
jne 0x81bc
movq %r15, %rdi
movq %r14, %rsi
callq 0xba02
movq 0x8(%r15), %r12
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x835c
movq 0x8(%rdi), %rax
cmpq (%r13), %rax
jne 0x835c
movq %r13, %rsi
movq %r15, %rdx
movq -0x40(%rbp), %rbx
movq %rbx, %rcx
movq %rdi, %r15
callq 0xbbcb
jmp 0x83bd
testb %bl, %bl
je 0x81d1
movb $0x0, -0x88(%rbp)
movb $0x1, %dl
xorl %eax, %eax
movq %r12, -0x50(%rbp)
jmp 0x8226
movq %r12, -0xa0(%rbp)
movb $0x1, -0x88(%rbp)
movq 0x50e8(%r14), %rax
movb (%rax,%r12), %al
movl %eax, %ecx
shrb $0x4, %cl
andb $0xf, %al
testl %r11d, %r11d
movzbl %al, %eax
movzbl %cl, %ecx
cmovel %eax, %ecx
movzbl %cl, %eax
movl %eax, -0x84(%rbp)
movq (%r14), %rcx
shll $0x3, %eax
movq (%rcx,%rax), %rcx
movq 0x5118(%r14), %rdx
addq (%rdx,%rax), %rcx
movq %rcx, -0x90(%rbp)
movb $0x1, %al
xorl %edx, %edx
movq -0x48(%rbp), %r15
movq 0x50e8(%r14), %r8
movb (%r8,%r12), %cl
movl %ecx, %esi
shrb $0x4, %sil
andb $0xf, %cl
testl %r11d, %r11d
movzbl %cl, %ecx
movzbl %sil, %edi
cmovel %ecx, %edi
movzbl %dil, %r9d
movq (%r14), %rcx
leal (,%r9,8), %r10d
movq (%rcx,%r10), %rcx
movq 0x5118(%r14), %r11
movq (%r11,%r10), %rsi
leaq 0x1(%rsi), %rbx
movq %rbx, (%r11,%r10)
cmpl %r9d, -0x84(%rbp)
je 0x8283
movb $0x0, -0x88(%rbp)
xorl %eax, %eax
movzbl (%rcx,%rsi), %esi
testq %rsi, %rsi
movl -0x2c(%rbp), %r11d
je 0x83aa
movq 0x5110(%r14), %r9
movq (%r9,%rsi,8), %rcx
leaq 0x1(%rcx), %r10
movq %r10, (%r9,%rsi,8)
movb (%r8,%rcx), %r9b
testl %r11d, %r11d
je 0x82b9
andb $-0x10, %r9b
orb %dil, %r9b
jmp 0x82c7
andb $0xf, %r9b
movl %edi, %r10d
shlb $0x4, %r10b
orb %r10b, %r9b
movb %r9b, (%r8,%rcx)
cmpb $0x1, 0x3c(%r14)
jne 0x830d
movq -0x68(%rbp), %r8
cmpb $0x0, (%r8)
jne 0x830d
movq 0x50e8(%r14), %r8
movb (%r8,%rcx), %r8b
movl %r8d, %r9d
shrb $0x4, %r9b
andb $0xf, %r8b
testl %r11d, %r11d
movzbl %r8b, %r8d
movzbl %r9b, %r9d
cmovel %r8d, %r9d
cmpb %dil, %r9b
je 0x830d
movq -0x68(%rbp), %rdi
movb $0x1, (%rdi)
movq -0x50(%rbp), %rdi
cmpq %rdi, (%r15,%rsi,8)
je 0x83aa
cmpb $0x1, 0x3c(%r14)
jne 0x8389
testb %dl, %dl
je 0x83a2
movq 0x98(%r14), %rdx
movq %rcx, %rdi
shrq $0x5, %rdi
movq (%rdx,%rdi,8), %r8
addl %ecx, %ecx
andl $0x3e, %ecx
movq %r8, %r9
shrq %cl, %r9
testb $0x3, %r9b
jne 0x83a2
movq -0xa8(%rbp), %r9
shlq %cl, %r9
orq %r9, %r8
movq %r8, (%rdx,%rdi,8)
jmp 0x83a2
cmpb $0x0, 0x2c(%r13)
jne 0x8376
movq %r13, %rdi
movq %r15, %rsi
movq -0x40(%rbp), %rdx
callq 0x6967
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x83b9
movq -0x40(%rbp), %rbx
movq %rbx, %rsi
callq 0x6a18
jmp 0x83bd
testb %dl, %dl
je 0x83a2
movq 0x98(%r14), %rdx
cmpb $0x0, (%rdx,%rcx)
jne 0x83a2
movq -0x70(%rbp), %rdi
movb %dil, (%rdx,%rcx)
movq -0x50(%rbp), %rcx
movq %rcx, (%r15,%rsi,8)
incq %r12
cmpq 0x28(%r14), %r12
jb 0x8098
jmp 0x83f3
movq -0x40(%rbp), %rbx
movq -0xb0(%rbp), %r13
movq %r12, 0x8(%r13)
movq %r12, (%r13)
movq %rbx, %rdi
callq 0xb878
movq %r15, -0x38(%rbp)
movq %rax, -0x58(%rbp)
movq %rax, 0x18(%r15)
movb $0x0, -0x88(%rbp)
xorl %eax, %eax
movl -0x2c(%rbp), %r11d
movq -0x48(%rbp), %r15
jmp 0x83ad
testb $0x1, %al
je 0x840a
leaq -0xa0(%rbp), %rdi
movq %r12, 0x8(%rdi)
movq %r13, %rsi
callq 0xb9b7
movq -0x40(%rbp), %rbx
cmpb $0x0, 0x2c(%r13)
je 0x841b
movq -0x38(%rbp), %rdi
jmp 0x842f
movq -0x38(%rbp), %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x6839
movq %rax, %rdi
testq %rdi, %rdi
je 0x843f
cmpq $0x0, (%rdi)
je 0x8444
xorl %r14d, %r14d
jmp 0x8450
xorl %r14d, %r14d
jmp 0x8458
movq 0x8(%rdi), %rax
cmpq 0x28(%r14), %rax
sete %r14b
movq %rbx, %rsi
callq 0x6a18
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq