// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/./merge128.h
// Function: addCharToPrefix128
static bool addCharToPrefix128(solidBlockFile *solidHead, liquidBlock *liquid, uint32_t prefixLength, bool *mergeChanged, const int round, g_data *g) {
  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 (now inside mergeColor)
  uint64_t lcpWritten =0;

  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
    start_block = tba_block_test_set16(g->mergeColor16,k,m,&last_block_recent);
    if(start_block && last_block_recent && g->lcpCompute)
      {writeLcp(k,prefixLength-2,g); lcpWritten++;} // save lcp value found in previous iteration
    if (start_block) {
      // if the block we just left is a singleton we add it to liquid that remains active
      if(!last_block_recent && cblock.mono==true && (cblock.beginsAt==k-1)) {
        cblock.endsAt = k;           // solidifiable singleton block just ended
        add_singleton2liquid(&cblock, liquid);
      }
      // if the block we left is not recent, monochrome, we are only computing BWTs and not using extMem add it  
      else if(!last_block_recent && cblock.mono==true && g->bwtOnly && !g->extMem) {
        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_mergeColor16(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_mergeColor16(k,round);   // g->mergeColor[k] b in pseudocode
    int currentChar = g->bws[currentColor][g->inCnt[currentColor]++]; // c in pseudocode
    cblock.lastChar =  currentChar; // save lastchar, only useful for singleton blocks
    cblock.lastColor =  currentColor; // save lastcolor, only useful for singleton blocks
    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_mergeColor16(positionToUpdate,currentColor,round); // g->newMergeColor[positionToUpdate] = currentColor;
      if(g->bwtOnly && !*mergeChanged && get_mergeColor16(positionToUpdate,round)!=currentColor)
        *mergeChanged=true;  // remember there is a difference from the previous iteration 
      // create new block?
      if (blockID[currentChar] != id) {
        if(last_block_recent) tba_mark16_if0(g->mergeColor16,positionToUpdate,m);
        blockID[currentChar] = id; // update block id, always!
      }
    }
    k++;
  } // end main loop
  assert(k==g->mergeLen);
  assert(next==NULL); 
  if(cblock.mono==true && cblock.beginsAt==k-1) {
    cblock.endsAt = k;           // solidifiable singleton block just ended
    add_singleton2liquid(&cblock, liquid);
    assert(!liquid->empty);
  }
  else if(cblock.mono==true && g->bwtOnly && !g->extMem) {
    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);
  // add EOF value to lcp file and entry to .size file 
  if(!g->bwtOnly && lcpWritten>0)
    writeLcp_EOF(++lcpWritten, g);
  // 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, %r13d
movq %rcx, -0xa8(%rbp)
movl %edx, %r15d
movq %rdi, %rbx
xorps %xmm0, %xmm0
movq %rsi, -0x30(%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
movq $0x0, -0x50(%rbp)
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, -0xb0(%rbp)
callq 0x2fbc
movl %r13d, %eax
andl $0x1, %eax
pushq $0x2
popq %r12
subl %eax, %r12d
leaq -0xa0(%rbp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
andq $0x0, 0x20(%rax)
movq %rbx, -0x38(%rbp)
movq %rbx, %rdi
callq 0xaea5
movq %rax, %rcx
addl $-0x2, %r15d
movl %r15d, -0x68(%rbp)
movl %r12d, -0x6c(%rbp)
shll $0xe, %r12d
movl %r12d, -0x64(%rbp)
movq $0x0, -0x60(%rbp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movl %r13d, -0x3c(%rbp)
movq %rcx, -0x48(%rbp)
decq %r15
xorl %eax, %eax
leaq 0x1(%r15), %rbx
cmpq 0x28(%r14), %rbx
jae 0x8820
movq 0x50f8(%r14), %rcx
movzwl 0x2(%rcx,%r15,2), %edx
movl %edx, %esi
shrl $0xe, %esi
movl -0x6c(%rbp), %r13d
xorl %esi, %r13d
cmpl $0x3, %r13d
jne 0x852b
orl $0xc000, %edx           # imm = 0xC000
movw %dx, 0x2(%rcx,%r15,2)
cmpb $0x1, 0x3e(%r14)
jne 0x856c
movq %rbx, %rdi
movl -0x68(%rbp), %esi
movq %r14, %rdx
callq 0x36c8
incq -0x60(%rbp)
jmp 0x856c
movb $0x1, %cl
cmpw $0x3, %si
jne 0x85c4
testb $0x1, %al
je 0x856c
cmpq -0xa0(%rbp), %r15
movq -0x30(%rbp), %rax
jne 0x8738
movq %r12, -0x58(%rbp)
movq %rbx, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
movq %rax, %rsi
callq 0xafd6
movq -0x30(%rbp), %rax
jmp 0x859d
movq -0x30(%rbp), %rax
cmpb $0x0, 0x2c(%rax)
jne 0x8592
movq -0x50(%rbp), %rdi
movq %rax, %rsi
movq -0x48(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x628f
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq %r12, -0x58(%rbp)
movq %rbx, 0x8(%rax)
movq %rbx, (%rax)
movq -0x48(%rbp), %r12
testq %r12, %r12
je 0x85b0
cmpq (%r12), %rbx
je 0x876d
cmpl $0x3, %r13d
jne 0x85ca
movb $0x0, -0x88(%rbp)
movb $0x1, %cl
xorl %eax, %eax
movq %rbx, %r12
movl -0x3c(%rbp), %r13d
jmp 0x8621
movq %rbx, -0xa0(%rbp)
movb $0x1, -0x88(%rbp)
movq 0x50f8(%r14), %rax
movzwl 0x2(%rax,%r15,2), %eax
movl %eax, %ecx
shrl $0x7, %ecx
movl -0x3c(%rbp), %r13d
testl %r13d, %r13d
cmovel %eax, %ecx
andl $0x7f, %ecx
movl %ecx, -0x84(%rbp)
movq (%r14), %rax
shll $0x3, %ecx
movq (%rax,%rcx), %rax
movq 0x5118(%r14), %rdx
addq (%rdx,%rcx), %rax
movq %rax, -0x90(%rbp)
movb $0x1, %al
xorl %ecx, %ecx
movq -0x58(%rbp), %r12
movq 0x50f8(%r14), %rdx
movzwl 0x2(%rdx,%r15,2), %esi
movl %esi, %edi
shrl $0x7, %edi
testl %r13d, %r13d
cmovel %esi, %edi
andl $0x7f, %edi
movq (%r14), %rsi
leal (,%rdi,8), %r8d
movq (%rsi,%r8), %rsi
movq 0x5118(%r14), %r9
movq (%r9,%r8), %r10
leaq 0x1(%r10), %r11
movq %r11, (%r9,%r8)
movzbl (%rsi,%r10), %esi
movl %esi, -0x80(%rbp)
movl %edi, -0x7c(%rbp)
cmpl %edi, -0x84(%rbp)
je 0x867a
movb $0x0, -0x88(%rbp)
xorl %eax, %eax
testq %rsi, %rsi
je 0x8730
movq 0x5110(%r14), %r9
movq (%r9,%rsi,8), %r8
leaq 0x1(%r8), %r10
movq %r10, (%r9,%rsi,8)
movzwl (%rdx,%r8,2), %r9d
movzwl %di, %r10d
testl %r13d, %r13d
je 0x86ad
andl $-0x80, %r9d
orl %r10d, %r9d
jmp 0x86be
andl $0xffffc07f, %r9d       # imm = 0xFFFFC07F
shll $0x7, %r10d
orl %r9d, %r10d
movl %r10d, %r9d
movw %r9w, (%rdx,%r8,2)
cmpb $0x1, 0x3c(%r14)
jne 0x86fd
movq -0xa8(%rbp), %r10
cmpb $0x0, (%r10)
jne 0x86fd
movzwl %r9w, %r10d
shrl $0x7, %r10d
testl %r13d, %r13d
movl %r9d, %r11d
cmovnel %r10d, %r11d
andl $0x7f, %r11d
cmpw %di, %r11w
je 0x86fd
movq -0xa8(%rbp), %rdi
movb $0x1, (%rdi)
movq -0xb0(%rbp), %rdi
cmpq %r12, (%rdi,%rsi,8)
je 0x8730
cmpw $0x4000, %r9w           # imm = 0x4000
setb %dil
andb %dil, %cl
cmpb $0x1, %cl
jne 0x8725
orl -0x64(%rbp), %r9d
movw %r9w, (%rdx,%r8,2)
movq -0xb0(%rbp), %rcx
movq %r12, (%rcx,%rsi,8)
incq %r15
jmp 0x84d7
cmpb $0x1, 0x3c(%r14)
jne 0x8570
cmpb $0x0, 0x69(%r14)
jne 0x8570
movq %r12, -0x58(%rbp)
movq %rbx, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
movq %rax, %rsi
callq 0xaffe
jmp 0x8566
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %r13
callq 0xb041
movq 0x8(%r12), %r15
movq -0x50(%rbp), %rbx
testq %rbx, %rbx
je 0x87b9
movq 0x8(%rbx), %rax
cmpq (%r13), %rax
jne 0x87b9
movq %rbx, %rdi
movq %r13, %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %r12
movq -0x38(%rbp), %rbx
movq %rbx, %rcx
callq 0xb1e2
movq %r12, -0x48(%rbp)
movq %r13, %rdi
movq -0x58(%rbp), %r12
jmp 0x87f6
cmpb $0x0, 0x2c(%r13)
movq %r13, %rdi
movq -0x58(%rbp), %r12
jne 0x87d8
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x63c9
movq -0x30(%rbp), %rdi
testq %rbx, %rbx
je 0x87f2
movq %rbx, %rdi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x648d
movq -0x30(%rbp), %rdi
jmp 0x87f6
movq -0x38(%rbp), %rbx
movq -0x48(%rbp), %r13
movq %r15, 0x8(%rdi)
movq %r15, (%rdi)
movq %rbx, %rdi
callq 0xaea5
movq %rax, %rcx
movq %r13, -0x50(%rbp)
movq %rax, 0x18(%r13)
movb $0x0, -0x88(%rbp)
jmp 0x84ce
testb $0x1, %al
movq -0x50(%rbp), %rbx
je 0x886d
leaq 0x1(%r15), %rax
cmpq %r15, -0xa0(%rbp)
jne 0x884b
leaq -0xa0(%rbp), %rdi
movq %rax, 0x8(%rdi)
movq -0x30(%rbp), %rsi
callq 0xafd6
jmp 0x886d
cmpb $0x1, 0x3c(%r14)
jne 0x886d
cmpb $0x0, 0x69(%r14)
jne 0x886d
leaq -0xa0(%rbp), %rdi
movq %rax, 0x8(%rdi)
movq -0x30(%rbp), %rsi
callq 0xaffe
movq -0x30(%rbp), %rsi
cmpb $0x0, 0x2c(%rsi)
je 0x887d
movq -0x38(%rbp), %r15
jmp 0x8891
movq %rbx, %rdi
xorl %edx, %edx
movq -0x38(%rbp), %r15
movq %r15, %rcx
callq 0x628f
movq %rax, %rbx
movq -0x60(%rbp), %rdi
cmpb $0x0, 0x3c(%r14)
jne 0x88ac
testq %rdi, %rdi
je 0x88ac
incq %rdi
movq %r14, %rsi
callq 0x3748
testq %rbx, %rbx
je 0x88be
cmpq $0x0, (%rbx)
je 0x88c2
movq %rbx, %rdi
xorl %ebx, %ebx
jmp 0x88d0
xorl %ebx, %ebx
jmp 0x88d8
movq 0x8(%rbx), %rax
cmpq 0x28(%r14), %rax
movq %rbx, %rdi
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