// File: /workspace/github_c_makefile_star2_O3/felipelouza[P]egap/./merge8.h
// Function: addCharToPrefix8
static bool addCharToPrefix8(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);
  if(g->extMem) rewind_bw_files(g); // set file pointers at the beginning of each BWT 
  // 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_set8(g->mergeColor,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->lcpCompute && !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; 
        if(!g->extMem) {
          cblock.color =  get_mergeColor8(k,round);
          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_mergeColor8(k,round);   // g->mergeColor[k] b in pseudocode
    int currentChar=0;  // c in pseudocode: next char in current BWT 
    if(g->extMem) {
      int e = fread(&currentChar,sizeof(symbol),1,g->bwf[currentColor]);
      if(e!=1) {die(__func__);} g->inCnt[currentColor]++;
    }
    else 
      currentChar =  g->bws[currentColor][g->inCnt[currentColor]++]; // c in pseudocode
    // add currentChar/Color to proto block  
    cblock.lastChar =  currentChar;   // save lastchar, only useful for singleton blocks
    cblock.lastColor =  currentColor; // save lastcolor, only useful for singleton blocks
    if(!g->extMem)
      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_mergeColor8(positionToUpdate,currentColor,round); // g->newMergeColor[positionToUpdate] = currentColor;
      if(g->bwtOnly && !*mergeChanged && get_mergeColor8(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_mark8_if0(g->mergeColor,positionToUpdate,m);
        blockID[currentChar] = id; // update block id, always!
      }
    }
    k++;
  } // end main loop, check block that just ended  
  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->lcpCompute && !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, -0x30(%rbp)
movq %rcx, -0xa0(%rbp)
movl %edx, -0x50(%rbp)
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
cmpb $0x1, 0x69(%r14)
jne 0x73a2
movq %r14, %rdi
callq 0x5883
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, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x60(%rbp)
movq %rbx, %rdi
callq 0xb878
cmpq $0x0, 0x28(%r14)
movq %r12, -0x58(%rbp)
je 0x783c
movq %rax, %r9
movq %rbx, -0x38(%rbp)
movl -0x30(%rbp), %eax
andl $0x1, %eax
movl $0x2, %ecx
subl %eax, %ecx
addl $-0x2, -0x50(%rbp)
movl %ecx, -0x98(%rbp)
movl %ecx, %eax
shlb $0x6, %al
movb %al, -0x29(%rbp)
xorl %r10d, %r10d
movq $0x0, -0x90(%rbp)
movq %r15, -0xa8(%rbp)
xorl %r13d, %r13d
movq $0x0, -0x88(%rbp)
movq $0x0, -0x40(%rbp)
movq 0x50e8(%r14), %rax
movzbl (%rax,%r13), %ecx
movl %ecx, %edx
shrl $0x6, %edx
movl -0x98(%rbp), %ebx
xorl %edx, %ebx
cmpl $0x3, %ebx
movq %r9, -0x48(%rbp)
jne 0x748f
orb $-0x40, %cl
movb %cl, (%rax,%r13)
cmpb $0x1, 0x3e(%r14)
jne 0x74eb
movq %r13, %rdi
movl -0x50(%rbp), %esi
movq %r14, %rdx
callq 0x3b1c
movq -0x48(%rbp), %r9
incq -0x88(%rbp)
jmp 0x74eb
movb $0x1, %r11b
cmpb $0x3, %dl
jne 0x75d3
testb $0x1, %r10b
je 0x74eb
leaq -0x1(%r13), %rax
cmpq %rax, -0x80(%rbp)
jne 0x74d9
movq %r13, -0x78(%rbp)
movq %r13, 0x8(%r12)
movq 0x20(%r12), %rax
movslq -0x5c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r12), %rcx
movslq -0x60(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r12)
jmp 0x7517
cmpb $0x0, 0x3e(%r14)
jne 0x74eb
cmpb $0x0, 0x69(%r14)
je 0x77df
cmpb $0x0, 0x2c(%r12)
jne 0x750e
movq -0x40(%rbp), %rdi
movq %r12, %rsi
movq %r9, %rdx
movq -0x38(%rbp), %rcx
callq 0x6839
movq -0x48(%rbp), %r9
movq %rax, -0x40(%rbp)
movq %r13, 0x8(%r12)
movq %r13, (%r12)
testq %r9, %r9
je 0x756c
cmpq %r13, (%r9)
jne 0x756c
movq %r9, %rdi
movq %r14, %rsi
callq 0xba02
movq -0x48(%rbp), %r15
movq 0x8(%r15), %r13
movq %r12, %rsi
movq -0x40(%rbp), %r12
testq %r12, %r12
je 0x7774
movq 0x8(%r12), %rax
cmpq (%rsi), %rax
jne 0x7774
movq %r12, %rdi
movq %r15, %rdx
movq -0x38(%rbp), %rbx
movq %rbx, %rcx
callq 0xbbcb
movq %r12, %r15
jmp 0x77ac
cmpl $0x3, %ebx
jne 0x7584
movb $0x0, -0x68(%rbp)
movb $0x1, %r11b
xorl %r10d, %r10d
movq %r13, -0x90(%rbp)
jmp 0x75d3
movq %r13, -0x80(%rbp)
movb $0x1, -0x68(%rbp)
movb $0x1, %r10b
cmpb $0x0, 0x69(%r14)
jne 0x75d0
movq 0x50e8(%r14), %rax
movzbl (%rax,%r13), %eax
movl %eax, %ecx
shrb $0x3, %cl
cmpl $0x0, -0x30(%rbp)
movzbl %cl, %ecx
cmovel %eax, %ecx
andl $0x7, %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)
xorl %r11d, %r11d
movq 0x50e8(%r14), %rax
movzbl (%rax,%r13), %eax
movl %eax, %ecx
shrb $0x3, %cl
cmpl $0x0, -0x30(%rbp)
movzbl %cl, %ebx
cmovel %eax, %ebx
andl $0x7, %ebx
movl $0x0, -0x4c(%rbp)
movb 0x69(%r14), %cl
cmpb $0x1, %cl
jne 0x7662
movl %r11d, -0x94(%rbp)
movq %r12, %r15
movq %r10, %r12
movq 0x50a0(%r14), %rax
movq (%rax,%rbx,8), %rcx
movl $0x1, %esi
movl $0x1, %edx
leaq -0x4c(%rbp), %rdi
callq 0x20a0
cmpl $0x1, %eax
je 0x763d
leaq 0x8a61(%rip), %rdi      # 0x10099
callq 0x39fc
movq 0x5118(%r14), %rax
incq (%rax,%rbx,8)
movl -0x4c(%rbp), %eax
movb 0x69(%r14), %cl
movq -0x48(%rbp), %r9
movq %r12, %r10
movq %r15, %r12
movl -0x94(%rbp), %r11d
jmp 0x768a
movq (%r14), %rax
leal (,%rbx,8), %edx
movq (%rax,%rdx), %rax
movq 0x5118(%r14), %rsi
movq (%rsi,%rdx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%rsi,%rdx)
movzbl (%rax,%rdi), %eax
movl %eax, -0x4c(%rbp)
movl %eax, -0x60(%rbp)
movl %ebx, -0x5c(%rbp)
testb $0x1, %cl
jne 0x76a1
cmpl %ebx, -0x64(%rbp)
je 0x76a1
movb $0x0, -0x68(%rbp)
xorl %r10d, %r10d
testl %eax, %eax
je 0x776f
movq 0x5110(%r14), %rdx
cltq
movq (%rdx,%rax,8), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdx,%rax,8)
movq 0x50e8(%r14), %rdx
movb (%rdx,%rcx), %sil
cmpl $0x0, -0x30(%rbp)
je 0x76d8
andb $-0x8, %sil
orb %bl, %sil
jmp 0x76e5
andb $-0x39, %sil
movl %ebx, %edi
shlb $0x3, %dil
orb %dil, %sil
movb %sil, (%rdx,%rcx)
cmpb $0x1, 0x3c(%r14)
jne 0x772b
movq -0xa0(%rbp), %rdx
cmpb $0x0, (%rdx)
jne 0x772b
movq 0x50e8(%r14), %rdx
movzbl (%rdx,%rcx), %edx
movl %edx, %esi
shrb $0x3, %sil
cmpl $0x0, -0x30(%rbp)
movzbl %sil, %esi
cmovel %edx, %esi
andb $0x7, %sil
cmpb %bl, %sil
je 0x772b
movq -0xa0(%rbp), %rdx
movb $0x1, (%rdx)
movq -0x90(%rbp), %rdx
movq -0xa8(%rbp), %rsi
cmpq %rdx, (%rsi,%rax,8)
je 0x776f
testb %r11b, %r11b
je 0x775d
movq 0x50e8(%r14), %rdx
movb (%rdx,%rcx), %sil
cmpb $0x3f, %sil
ja 0x775d
orb -0x29(%rbp), %sil
movb %sil, (%rdx,%rcx)
movq -0x90(%rbp), %rcx
movq -0xa8(%rbp), %rdx
movq %rcx, (%rdx,%rax,8)
incq %r13
jmp 0x77d3
cmpb $0x0, 0x2c(%rsi)
jne 0x778d
movq %rsi, %rdi
movq %r15, %rsi
movq -0x38(%rbp), %rdx
callq 0x6967
movq -0x48(%rbp), %r15
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x77a8
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x6a18
movq -0x48(%rbp), %r15
jmp 0x77ac
movq -0x38(%rbp), %rbx
movq -0x58(%rbp), %r12
movq %r13, 0x8(%r12)
movq %r13, (%r12)
movq %rbx, %rdi
callq 0xb878
movq %rax, %r9
movq %r15, -0x40(%rbp)
movq %rax, 0x18(%r15)
movb $0x0, -0x68(%rbp)
xorl %r10d, %r10d
cmpq 0x28(%r14), %r13
jb 0x7444
jmp 0x77f8
movq %r13, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
movq %r12, %rsi
callq 0xb9b7
movq -0x48(%rbp), %r9
jmp 0x7517
testb $0x1, %r10b
je 0x7844
leaq -0x1(%r13), %rax
cmpq %rax, -0x80(%rbp)
movq -0x88(%rbp), %r12
jne 0x7851
movq %r13, -0x78(%rbp)
movq -0x58(%rbp), %rsi
movq %r13, 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 0x7870
xorl %r12d, %r12d
xorl %r13d, %r13d
jmp 0x7878
movq -0x38(%rbp), %rbx
movq -0x88(%rbp), %r12
jmp 0x7874
cmpb $0x0, 0x3e(%r14)
jne 0x7870
cmpb $0x0, 0x69(%r14)
jne 0x7870
leaq -0x80(%rbp), %rdi
movq %r13, 0x8(%rdi)
movq -0x58(%rbp), %rsi
callq 0xb9b7
movq -0x38(%rbp), %rbx
movq -0x40(%rbp), %r13
movq -0x58(%rbp), %rsi
cmpb $0x0, 0x2c(%rsi)
jne 0x7892
movq %r13, %rdi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x6839
movq %rax, %r13
cmpb $0x0, 0x3c(%r14)
jne 0x78ac
testq %r12, %r12
je 0x78ac
incq %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x3b86
testq %r13, %r13
je 0x78bd
cmpq $0x0, (%r13)
je 0x78c2
xorl %r14d, %r14d
jmp 0x78ce
xorl %r14d, %r14d
jmp 0x78d9
movq 0x8(%r13), %rax
cmpq 0x28(%r14), %rax
sete %r14b
movq %r13, %rdi
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