// File: /workspace/github_c_makefile_star2_O3/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, %r15d
movq %rcx, -0xa0(%rbp)
movl %edx, %r13d
movq %rsi, %r12
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
xorl %edx, %edx
callq 0x33a4
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, -0xa8(%rbp)
callq 0x33a4
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x60(%rbp)
movq %rbx, -0x30(%rbp)
movq %rbx, %rdi
callq 0xb878
cmpq $0x0, 0x28(%r14)
movq %r12, -0x50(%rbp)
je 0x9033
movq %rax, %rdx
movl %r15d, %eax
andl $0x1, %eax
movl $0x2, %ecx
subl %eax, %ecx
addl $-0x2, %r13d
movl %ecx, -0x98(%rbp)
shll $0xe, %ecx
movl %ecx, -0x8c(%rbp)
movq $0x0, -0x88(%rbp)
xorl %ebx, %ebx
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movl %r15d, -0x90(%rbp)
movl %r15d, %r11d
xorl %r15d, %r15d
movl %r13d, -0x94(%rbp)
movq 0x50f8(%r14), %rcx
movzwl (%rcx,%rbx,2), %edi
movl %edi, %esi
shrl $0xe, %esi
movl -0x98(%rbp), %r13d
xorl %esi, %r13d
cmpl $0x3, %r13d
movq %rdx, -0x40(%rbp)
jne 0x8ce8
orl $0xc000, %edi           # imm = 0xC000
movw %di, (%rcx,%rbx,2)
cmpb $0x1, 0x3e(%r14)
jne 0x8d44
movq %rbx, %rdi
movl -0x94(%rbp), %esi
movq %r14, %rdx
movq %r12, %r15
movl %r11d, %r12d
callq 0x3b1c
movl %r12d, %r11d
movq %r15, %r12
movq -0x40(%rbp), %rdx
incq -0x48(%rbp)
jmp 0x8d44
movb $0x1, %cl
cmpw $0x3, %si
jne 0x8e28
testb $0x1, %r15b
je 0x8d44
leaq -0x1(%rbx), %rax
cmpq %rax, -0x80(%rbp)
jne 0x8d32
movq %rbx, -0x78(%rbp)
movq %rbx, 0x8(%r12)
movq 0x20(%r12), %rax
movslq -0x5c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r12), %rcx
movslq -0x60(%rbp), %rsi
addq %rcx, %rsi
incq (%rax,%rsi,8)
movb $0x0, 0x2c(%r12)
jmp 0x8d79
cmpb $0x1, 0x3c(%r14)
jne 0x8d44
cmpb $0x0, 0x69(%r14)
je 0x8fc9
cmpb $0x0, 0x2c(%r12)
jne 0x8d70
movq -0x38(%rbp), %rdi
movq %r12, %rsi
movq -0x30(%rbp), %rcx
movq %r12, %r15
movl %r11d, %r12d
callq 0x6839
movl %r12d, %r11d
movq %r15, %r12
movq -0x40(%rbp), %rdx
movq %rax, -0x38(%rbp)
movq %rbx, 0x8(%r12)
movq %rbx, (%r12)
testq %rdx, %rdx
je 0x8dce
cmpq %rbx, (%rdx)
jne 0x8dce
movq %rdx, %rdi
movq %r14, %rsi
callq 0xba02
movq -0x40(%rbp), %r15
movq 0x8(%r15), %rbx
movq %r12, %rsi
movq -0x38(%rbp), %r12
testq %r12, %r12
je 0x8f57
movq 0x8(%r12), %rax
cmpq (%rsi), %rax
jne 0x8f57
movq %r12, %rdi
movq %r15, %rdx
movq -0x30(%rbp), %r13
movq %r13, %rcx
callq 0xbbcb
movq %r12, %r15
jmp 0x8f8f
cmpl $0x3, %r13d
jne 0x8de6
movb $0x0, -0x68(%rbp)
movb $0x1, %cl
xorl %r15d, %r15d
movq %rbx, -0x88(%rbp)
jmp 0x8e28
movq %rbx, -0x80(%rbp)
movb $0x1, -0x68(%rbp)
movq 0x50f8(%r14), %rax
movzwl (%rax,%rbx,2), %eax
movl %eax, %ecx
shrl $0x7, %ecx
testl %r11d, %r11d
cmovel %eax, %ecx
andl $0x7f, %ecx
movl %ecx, -0x64(%rbp)
movq (%r14), %rax
shll $0x3, %ecx
movq (%rax,%rcx), %rax
movq 0x5118(%r14), %rdx
addq (%rdx,%rcx), %rax
movq %rax, -0x70(%rbp)
movb $0x1, %r15b
xorl %ecx, %ecx
movq 0x50f8(%r14), %rdx
movzwl (%rdx,%rbx,2), %esi
movl %esi, %edi
shrl $0x7, %edi
movl %r11d, %eax
testl %r11d, %r11d
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, -0x60(%rbp)
movl %edi, -0x5c(%rbp)
cmpl %edi, -0x64(%rbp)
je 0x8e7d
movb $0x0, -0x68(%rbp)
xorl %r15d, %r15d
testq %rsi, %rsi
movl %eax, %r11d
je 0x8f4e
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 %r11d, %r11d
je 0x8eb3
andl $-0x80, %r9d
orl %r10d, %r9d
jmp 0x8ec4
andl $0xffffc07f, %r9d       # imm = 0xFFFFC07F
shll $0x7, %r10d
orl %r9d, %r10d
movl %r10d, %r9d
movw %r9w, (%rdx,%r8,2)
cmpb $0x1, 0x3c(%r14)
jne 0x8f06
movq -0xa0(%rbp), %r10
cmpb $0x0, (%r10)
jne 0x8f06
movzwl %r9w, %r10d
shrl $0x7, %r10d
testl %r11d, %r11d
movl %r9d, %r11d
cmovnel %r10d, %r11d
andl $0x7f, %r11d
cmpw %di, %r11w
movl %eax, %r11d
je 0x8f06
movq -0xa0(%rbp), %rdi
movb $0x1, (%rdi)
movq -0xa8(%rbp), %rdi
movq -0x88(%rbp), %rax
cmpq %rax, (%rdi,%rsi,8)
je 0x8f4e
movzwl %r9w, %edi
cmpl $0x4000, %edi           # imm = 0x4000
setb %dil
andb %dil, %cl
cmpb $0x1, %cl
jne 0x8f3c
orl -0x8c(%rbp), %r9d
movw %r9w, (%rdx,%r8,2)
movq -0xa8(%rbp), %rcx
movq -0x88(%rbp), %rax
movq %rax, (%rcx,%rsi,8)
incq %rbx
movq -0x40(%rbp), %rdx
jmp 0x8fbd
cmpb $0x0, 0x2c(%rsi)
jne 0x8f70
movq %rsi, %rdi
movq %r15, %rsi
movq -0x30(%rbp), %rdx
callq 0x6967
movq -0x40(%rbp), %r15
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x8f8b
movq -0x30(%rbp), %r13
movq %r13, %rsi
callq 0x6a18
movq -0x40(%rbp), %r15
jmp 0x8f8f
movq -0x30(%rbp), %r13
movq -0x50(%rbp), %r12
movq %rbx, 0x8(%r12)
movq %rbx, (%r12)
movq %r13, %rdi
callq 0xb878
movq %rax, %rdx
movq %r15, -0x38(%rbp)
movq %rax, 0x18(%r15)
movb $0x0, -0x68(%rbp)
xorl %r15d, %r15d
movl -0x90(%rbp), %r11d
cmpq 0x28(%r14), %rbx
jb 0x8c88
jmp 0x8fee
movq %rbx, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
movq %r12, %rsi
movq %r12, %r15
movl %r11d, %r12d
callq 0xb9b7
movl %r12d, %r11d
movq %r15, %r12
movq -0x40(%rbp), %rdx
jmp 0x8d79
testb $0x1, %r15b
je 0x903a
leaq -0x1(%rbx), %rax
cmpq %rax, -0x80(%rbp)
movq -0x38(%rbp), %r12
jne 0x90bc
movq %rbx, -0x78(%rbp)
movq -0x50(%rbp), %rsi
movq %rbx, 0x8(%rsi)
movq 0x20(%rsi), %rax
movslq -0x5c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%rsi), %rcx
movslq -0x60(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%rsi)
jmp 0x903e
xorl %ebx, %ebx
xorl %r12d, %r12d
jmp 0x9042
movq -0x38(%rbp), %r12
movq -0x48(%rbp), %rbx
movq -0x50(%rbp), %rsi
cmpb $0x0, 0x2c(%rsi)
je 0x9052
movq -0x30(%rbp), %r15
jmp 0x9066
movq %r12, %rdi
xorl %edx, %edx
movq -0x30(%rbp), %r15
movq %r15, %rcx
callq 0x6839
movq %rax, %r12
cmpb $0x0, 0x3c(%r14)
jne 0x9080
testq %rbx, %rbx
je 0x9080
incq %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3b86
testq %r12, %r12
je 0x9090
cmpq $0x0, (%r12)
je 0x9094
xorl %ebx, %ebx
jmp 0x90a0
xorl %ebx, %ebx
jmp 0x90ab
movq 0x8(%r12), %rax
cmpq 0x28(%r14), %rax
sete %bl
movq %r12, %rdi
movq %r15, %rsi
callq 0x6a18
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x1, 0x3c(%r14)
jne 0x903e
cmpb $0x0, 0x69(%r14)
jne 0x903e
leaq -0x80(%rbp), %rdi
movq %rbx, 0x8(%rdi)
movq -0x50(%rbp), %rsi
callq 0xb9b7
jmp 0x903e