// File: /workspace/github_c_makefile_star2_O2/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 $0x88, %rsp
movq %r9, %r14
movl %r8d, %r15d
movq %rcx, -0x60(%rbp)
movq %rdx, %r13
movq %rdi, %rbx
xorps %xmm0, %xmm0
movq %rsi, -0x48(%rbp)
movups %xmm0, (%rsi)
movq 0x5108(%r9), %rsi
movq 0x5110(%r9), %rdi
movslq 0x38(%r9), %rdx
callq 0x35fb
movq 0x5118(%r14), %rdi
movslq 0x20(%r14), %rsi
xorl %r12d, %r12d
xorl %edx, %edx
callq 0x2fbc
movslq 0x38(%r14), %rsi
movl %esi, %eax
movq %rsp, %rdi
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
movq 0x28(%r14), %rdx
movq %rdi, -0x78(%rbp)
callq 0x2fbc
movl %r15d, -0x30(%rbp)
andl $0x1, %r15d
pushq $0x2
popq %rcx
subl %r15d, %ecx
movq %rcx, -0x70(%rbp)
leaq -0xa0(%rbp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
andq $0x0, 0x20(%rax)
movq %rbx, -0x50(%rbp)
movq %rbx, %rdi
callq 0xaea5
movq %rax, %rdx
movq %r13, -0x68(%rbp)
leaq -0x1(%r13), %rax
movq %rax, -0xa8(%rbp)
leaq -0x29(%rbp), %r13
movq $0x0, -0x38(%rbp)
xorl %ebx, %ebx
movq $0x0, -0x58(%rbp)
cmpq 0x28(%r14), %rbx
jae 0x7c80
movb $0x1, -0x29(%rbp)
movq 0x98(%r14), %rdi
cmpb $0x0, 0x3c(%r14)
movq %rdx, -0x40(%rbp)
je 0x79b0
movq %rbx, %rsi
movq -0x70(%rbp), %rdx
movq %r13, %rcx
callq 0xb34d
movq -0x40(%rbp), %rdx
testb %al, %al
jne 0x79d6
jmp 0x7aef
movzbl (%rdi,%rbx), %eax
testq %rax, %rax
sete %cl
cmpq -0x68(%rbp), %rax
setae %sil
orb %cl, %sil
jne 0x7aef
cmpq %rax, -0xa8(%rbp)
setbe -0x29(%rbp)
testb $0x1, %r12b
je 0x79e2
cmpb $0x0, -0x29(%rbp)
je 0x7a10
movq -0x48(%rbp), %r12
cmpb $0x0, 0x2c(%r12)
movq -0x50(%rbp), %r15
jne 0x7a05
movq -0x38(%rbp), %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x628f
movq %rax, -0x38(%rbp)
movq %rbx, 0x8(%r12)
movq %rbx, (%r12)
jmp 0x7a2e
movq %rbx, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
movq -0x48(%rbp), %r12
movq %r12, %rsi
callq 0xaffe
movq -0x50(%rbp), %r15
movq -0x40(%rbp), %rdx
testq %rdx, %rdx
je 0x7a85
cmpq %rbx, (%rdx)
jne 0x7a85
movq %rdx, %rdi
movq %r14, %rsi
callq 0xb041
movq -0x40(%rbp), %rdx
movq 0x8(%rdx), %rbx
movq -0x38(%rbp), %r13
testq %r13, %r13
je 0x7c26
movq 0x8(%r13), %rax
cmpq (%r12), %rax
jne 0x7c26
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0xb1e2
movq %r13, -0x40(%rbp)
leaq -0x29(%rbp), %r13
jmp 0x7c51
cmpb $0x0, -0x29(%rbp)
je 0x7a9b
movb $0x0, -0x88(%rbp)
xorl %r12d, %r12d
movq %rbx, -0x58(%rbp)
jmp 0x7aef
movq %rbx, -0xa0(%rbp)
movb $0x1, -0x88(%rbp)
movq 0x50e8(%r14), %rax
movb (%rax,%rbx), %al
movl %eax, %ecx
shrb $0x4, %cl
andb $0xf, %al
cmpl $0x0, -0x30(%rbp)
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), %rsi
addq (%rsi,%rax), %rcx
movq %rcx, -0x90(%rbp)
movb $0x1, %r12b
movq 0x50e8(%r14), %rcx
movb (%rcx,%rbx), %al
movl %eax, %edi
shrb $0x4, %dil
andb $0xf, %al
cmpl $0x0, -0x30(%rbp)
movzbl %al, %esi
movzbl %dil, %eax
cmovel %esi, %eax
movzbl %al, %edi
movq (%r14), %rsi
leal (,%rdi,8), %r8d
movq (%rsi,%r8), %r11
movq 0x5118(%r14), %r9
movq (%r9,%r8), %rsi
leaq 0x1(%rsi), %r10
movq %r10, (%r9,%r8)
cmpl %edi, -0x84(%rbp)
je 0x7b46
movb $0x0, -0x88(%rbp)
xorl %r12d, %r12d
movzbl (%r11,%rsi), %r15d
testq %r15, %r15
je 0x7c1e
movq 0x5110(%r14), %r8
movq (%r8,%r15,8), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, (%r8,%r15,8)
movb (%rcx,%rsi), %r8b
cmpl $0x0, -0x30(%rbp)
je 0x7b7a
andb $-0x10, %r8b
orb %al, %r8b
jmp 0x7b87
andb $0xf, %r8b
movl %eax, %edi
shlb $0x4, %dil
orb %dil, %r8b
movb %r8b, (%rcx,%rsi)
cmpb $0x1, 0x3c(%r14)
jne 0x7bc8
movq -0x60(%rbp), %rcx
cmpb $0x0, (%rcx)
jne 0x7bc8
movq 0x50e8(%r14), %rcx
movb (%rcx,%rsi), %cl
movl %ecx, %edi
shrb $0x4, %dil
andb $0xf, %cl
cmpl $0x0, -0x30(%rbp)
movzbl %cl, %ecx
movzbl %dil, %edi
cmovel %ecx, %edi
cmpb %al, %dil
je 0x7bc8
movq -0x60(%rbp), %rax
movb $0x1, (%rax)
movq -0x78(%rbp), %rax
movq -0x58(%rbp), %rcx
cmpq %rcx, (%rax,%r15,8)
je 0x7c1e
movb -0x29(%rbp), %al
cmpb $0x1, 0x3c(%r14)
jne 0x7bfa
testb %al, %al
je 0x7c12
movq 0x98(%r14), %rdi
movq -0x70(%rbp), %rdx
callq 0xb397
movq -0x40(%rbp), %rdx
jmp 0x7c12
testb %al, %al
je 0x7c12
movq 0x98(%r14), %rax
cmpb $0x0, (%rax,%rsi)
jne 0x7c12
movq -0x68(%rbp), %rcx
movb %cl, (%rax,%rsi)
movq -0x78(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rcx, (%rax,%r15,8)
incq %rbx
jmp 0x7974
cmpb $0x0, 0x2c(%r12)
jne 0x7c3c
movq %r12, %rdi
movq %rdx, %rsi
movq %r15, %rdx
callq 0x63c9
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
leaq -0x29(%rbp), %r13
je 0x7c51
movq %r15, %rsi
callq 0x648d
movq %rbx, 0x8(%r12)
movq %rbx, (%r12)
movq %r15, %rdi
callq 0xaea5
movq -0x40(%rbp), %rcx
movq %rax, 0x18(%rcx)
movb $0x0, -0x88(%rbp)
xorl %r12d, %r12d
movq %rcx, -0x38(%rbp)
movq %rax, %rdx
jmp 0x7974
testb $0x1, %r12b
movq -0x48(%rbp), %r12
je 0x7c9d
leaq -0xa0(%rbp), %rdi
movq %rbx, 0x8(%rdi)
movq %r12, %rsi
callq 0xaffe
cmpb $0x0, 0x2c(%r12)
movq -0x50(%rbp), %r15
movq -0x38(%rbp), %rdi
jne 0x7cbd
movq %r12, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0x628f
movq %rax, %rdi
testq %rdi, %rdi
je 0x7ccc
cmpq $0x0, (%rdi)
je 0x7cd0
xorl %ebx, %ebx
jmp 0x7cdb
xorl %ebx, %ebx
jmp 0x7ce3
movq 0x8(%rdi), %rax
cmpq 0x28(%r14), %rax
sete %bl
movq %r15, %rsi
callq 0x648d
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq