// File: /workspace/github_c_makefile_star2_O1/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, -0xa8(%rbp)
movl %edx, -0x50(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rsi)
movq 0x5108(%r9), %rsi
movq 0x5110(%r9), %rdi
movslq 0x38(%r9), %rdx
callq 0x39b7
movq 0x5118(%r14), %rdi
movslq 0x20(%r14), %rsi
xorl %edx, %edx
callq 0x3304
cmpb $0x1, 0x69(%r14)
jne 0x72ce
movq %r14, %rdi
callq 0x57d9
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 0x3304
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x60(%rbp)
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
callq 0xb7e2
cmpq $0x0, 0x28(%r14)
je 0x7739
movq %rax, %r8
movl -0x30(%rbp), %eax
andl $0x1, %eax
movl $0x2, %ecx
subl %eax, %ecx
addl $-0x2, -0x50(%rbp)
movl %ecx, -0x9c(%rbp)
movl %ecx, %eax
shlb $0x6, %al
movb %al, -0x29(%rbp)
movq $0x0, -0x98(%rbp)
xorl %ebx, %ebx
movq $0x0, -0x88(%rbp)
movq $0x0, -0x38(%rbp)
movq %r15, -0x48(%rbp)
movq 0x50e8(%r14), %rax
movzbl (%rax,%rbx), %ecx
movl %ecx, %edx
shrl $0x6, %edx
movl -0x9c(%rbp), %esi
xorl %edx, %esi
cmpl $0x3, %esi
jne 0x7394
orb $-0x40, %cl
movb %cl, (%rax,%rbx)
movb $0x1, %r13b
movb $0x1, %r12b
jmp 0x739f
cmpb $0x3, %dl
sete %r12b
setne %r13b
movl %r13d, %eax
andb %r12b, %al
cmpb $0x1, %al
jne 0x73cf
cmpb $0x1, 0x3e(%r14)
jne 0x73cf
movq %rbx, %rdi
movl -0x50(%rbp), %esi
movq %r14, %rdx
movq %r8, %r15
callq 0x3a94
movq %r15, %r8
movq -0x48(%rbp), %r15
incq -0x88(%rbp)
testb %r12b, %r12b
je 0x7518
testb %r13b, %r13b
jne 0x7434
cmpb $0x1, -0x68(%rbp)
jne 0x7417
leaq -0x1(%rbx), %rax
cmpq %rax, -0x80(%rbp)
jne 0x7417
movq %rbx, -0x78(%rbp)
movq %rbx, 0x8(%r15)
movq 0x20(%r15), %rax
movslq -0x5c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r15), %rcx
movslq -0x60(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r15)
jmp 0x7463
testb %r13b, %r13b
jne 0x7434
cmpb $0x1, -0x68(%rbp)
jne 0x7434
cmpb $0x0, 0x3e(%r14)
jne 0x7434
cmpb $0x0, 0x69(%r14)
je 0x771a
cmpb $0x0, 0x2c(%r15)
jne 0x745c
movq -0x38(%rbp), %rdi
movq %r15, %rsi
movq %r8, %rdx
movq -0x40(%rbp), %rcx
movq %r8, %r15
callq 0x676c
movq %r15, %r8
movq -0x48(%rbp), %r15
movq %rax, -0x38(%rbp)
movq %rbx, 0x8(%r15)
movq %rbx, (%r15)
testq %r8, %r8
je 0x74be
cmpq %rbx, (%r8)
jne 0x74be
movq %r8, %rdi
movq %r14, %rsi
movq %r8, -0x90(%rbp)
callq 0xb96b
movq -0x90(%rbp), %rdx
movq 0x8(%rdx), %rbx
movq -0x38(%rbp), %r13
testq %r13, %r13
je 0x76b3
movq 0x8(%r13), %rax
cmpq (%r15), %rax
jne 0x76b3
movq %r13, %rdi
movq %r15, %rsi
movq -0x40(%rbp), %r12
movq %r12, %rcx
callq 0xbb2a
movq %r13, %rdx
jmp 0x76f7
testb %r13b, %r13b
je 0x74d0
movb $0x0, -0x68(%rbp)
movq %rbx, -0x98(%rbp)
jmp 0x7518
movq %rbx, -0x80(%rbp)
movb $0x1, -0x68(%rbp)
cmpb $0x0, 0x69(%r14)
jne 0x7518
movq 0x50e8(%r14), %rax
movzbl (%rax,%rbx), %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)
movq 0x50e8(%r14), %rax
movzbl (%rax,%rbx), %eax
movl %eax, %ecx
shrb $0x3, %cl
cmpl $0x0, -0x30(%rbp)
movzbl %cl, %r12d
cmovel %eax, %r12d
andl $0x7, %r12d
movl $0x0, -0x4c(%rbp)
cmpb $0x1, 0x69(%r14)
jne 0x758c
movq %r8, %r15
movq 0x50a0(%r14), %rax
movq (%rax,%r12,8), %rcx
movl $0x1, %esi
movl $0x1, %edx
leaq -0x4c(%rbp), %rdi
callq 0x20a0
cmpl $0x1, %eax
je 0x7578
leaq 0x8b26(%rip), %rdi      # 0x10099
callq 0x3973
movq 0x5118(%r14), %rax
incq (%rax,%r12,8)
movq %r15, %r8
movq -0x48(%rbp), %r15
jmp 0x75b5
movq (%r14), %rax
leal (,%r12,8), %ecx
movq (%rax,%rcx), %rax
movq 0x5118(%r14), %rdx
movq (%rdx,%rcx), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, (%rdx,%rcx)
movzbl (%rax,%rsi), %eax
movl %eax, -0x4c(%rbp)
movslq -0x4c(%rbp), %rax
movl %eax, -0x60(%rbp)
movl %r12d, -0x5c(%rbp)
cmpb $0x0, 0x69(%r14)
jne 0x75d1
cmpl %r12d, -0x64(%rbp)
je 0x75d1
movb $0x0, -0x68(%rbp)
testl %eax, %eax
je 0x76a1
movq 0x5110(%r14), %rdx
movq (%rdx,%rax,8), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdx,%rax,8)
movq 0x50e8(%r14), %rdx
movb (%rdx,%rcx), %dl
cmpl $0x0, -0x30(%rbp)
je 0x7604
andb $-0x8, %dl
orb %r12b, %dl
jmp 0x7611
andb $-0x39, %dl
movl %r12d, %esi
shlb $0x3, %sil
orb %sil, %dl
movq 0x50e8(%r14), %rsi
movb %dl, (%rsi,%rcx)
cmpb $0x1, 0x3c(%r14)
jne 0x765d
movq -0xa8(%rbp), %rdx
cmpb $0x0, (%rdx)
jne 0x765d
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 %r12b, %sil
je 0x765d
movq -0xa8(%rbp), %rdx
movb $0x1, (%rdx)
movq -0xb0(%rbp), %rdx
movq -0x98(%rbp), %rsi
cmpq %rsi, (%rdx,%rax,8)
je 0x76a1
testb %r13b, %r13b
je 0x768f
movq 0x50e8(%r14), %rdx
movb (%rdx,%rcx), %sil
cmpb $0x3f, %sil
ja 0x768f
orb -0x29(%rbp), %sil
movb %sil, (%rdx,%rcx)
movq -0xb0(%rbp), %rcx
movq -0x98(%rbp), %rdx
movq %rdx, (%rcx,%rax,8)
incq %rbx
cmpq 0x28(%r14), %rbx
jb 0x7369
jmp 0x774e
cmpb $0x0, 0x2c(%r15)
jne 0x76d1
movq -0x48(%rbp), %rdi
movq %rdx, %rsi
movq -0x40(%rbp), %rdx
callq 0x689e
movq -0x90(%rbp), %rdx
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x76ef
movq -0x40(%rbp), %r12
movq %r12, %rsi
callq 0x6950
movq -0x90(%rbp), %rdx
jmp 0x76f3
movq -0x40(%rbp), %r12
movq -0x48(%rbp), %r15
movq %rdx, %r13
movq %rbx, 0x8(%r15)
movq %rbx, (%r15)
movq %r12, %rdi
callq 0xb7e2
movq %rax, %r8
movq %r13, -0x38(%rbp)
movq %rax, 0x18(%r13)
movb $0x0, -0x68(%rbp)
jmp 0x76a4
movq %rbx, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
movq %r15, %rsi
movq %r8, %r15
callq 0xb920
movq %r15, %r8
movq -0x48(%rbp), %r15
jmp 0x7463
movq $0x0, -0x38(%rbp)
movq $0x0, -0x88(%rbp)
xorl %ebx, %ebx
movb -0x68(%rbp), %al
cmpb $0x1, %al
jne 0x77ab
leaq -0x1(%rbx), %rcx
cmpq %rcx, -0x80(%rbp)
jne 0x7789
movq %rbx, -0x78(%rbp)
movq %rbx, 0x8(%r15)
movq 0x20(%r15), %rax
movslq -0x5c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r15), %rcx
movslq -0x60(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r15)
jmp 0x77ab
testb %al, %al
je 0x77ab
cmpb $0x0, 0x3e(%r14)
jne 0x77ab
cmpb $0x0, 0x69(%r14)
jne 0x77ab
leaq -0x80(%rbp), %rdi
movq %rbx, 0x8(%rdi)
movq %r15, %rsi
callq 0xb920
cmpb $0x0, 0x2c(%r15)
je 0x77b8
movq -0x38(%rbp), %r15
jmp 0x77cd
movq -0x38(%rbp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq -0x40(%rbp), %rcx
callq 0x676c
movq %rax, %r15
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
sete %al
orb 0x3c(%r14), %al
jne 0x77eb
incq %rdi
movq %r14, %rsi
callq 0x3afe
testq %r15, %r15
je 0x77f6
cmpq $0x0, (%r15)
je 0x77fa
xorl %ebx, %ebx
jmp 0x7805
movq 0x8(%r15), %rax
cmpq 0x28(%r14), %rax
sete %bl
testq %r15, %r15
je 0x7816
movq %r15, %rdi
movq -0x40(%rbp), %rsi
callq 0x6950
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq