// File: /workspace/github_c_makefile_star2_O2/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, %r12d
movq %rcx, -0xa8(%rbp)
movl %edx, -0x54(%rbp)
movq %rdi, %r13
xorps %xmm0, %xmm0
movq %rsi, -0x38(%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 %edx, %edx
callq 0x2fbc
cmpb $0x1, 0x69(%r14)
jne 0x6d2c
movq %r14, %rdi
callq 0x5389
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 %r12d, %eax
andl $0x1, %eax
pushq $0x2
popq %rbx
subl %eax, %ebx
leaq -0xa0(%rbp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
andq $0x0, 0x20(%rax)
movq %r13, %rdi
callq 0xaea5
movq %rax, %r10
addl $-0x2, -0x54(%rbp)
movl %ebx, -0x74(%rbp)
movl %ebx, %eax
shlb $0x6, %al
movb %al, -0x29(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
xorl %ebx, %ebx
movq $0x0, -0x70(%rbp)
movq %r13, -0x60(%rbp)
decq %rbx
xorl %r11d, %r11d
movq -0x38(%rbp), %rsi
movq %r10, -0x50(%rbp)
leaq 0x1(%rbx), %r13
cmpq 0x28(%r14), %r13
jae 0x7167
movl %r12d, %r15d
movq 0x50e8(%r14), %rax
movzbl 0x1(%rax,%rbx), %ecx
movl %ecx, %edx
shrl $0x6, %edx
movl -0x74(%rbp), %r12d
xorl %edx, %r12d
cmpl $0x3, %r12d
jne 0x6e17
orb $-0x40, %cl
movb %cl, 0x1(%rax,%rbx)
cmpb $0x1, 0x3e(%r14)
jne 0x6e5d
movq %r13, %rdi
movl -0x54(%rbp), %esi
movq %r14, %rdx
callq 0x36c8
movq -0x50(%rbp), %r10
movq -0x38(%rbp), %rsi
incq -0x68(%rbp)
jmp 0x6e5d
cmpb $0x3, %dl
jne 0x6e44
testb $0x1, %r11b
je 0x6e5d
cmpq -0xa0(%rbp), %rbx
jne 0x6e4b
movq %r13, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xafd6
movq -0x38(%rbp), %rsi
jmp 0x6e82
movb $0x1, %al
movl %eax, -0x3c(%rbp)
jmp 0x6ead
cmpb $0x0, 0x3e(%r14)
jne 0x6e5d
cmpb $0x0, 0x69(%r14)
je 0x70a5
cmpb $0x0, 0x2c(%rsi)
jne 0x6e7b
movq -0x48(%rbp), %rdi
movq %r10, %rdx
movq -0x60(%rbp), %rcx
callq 0x628f
movq -0x38(%rbp), %rsi
movq %rax, -0x48(%rbp)
movq %r13, 0x8(%rsi)
movq %r13, (%rsi)
movq -0x50(%rbp), %r10
testq %r10, %r10
je 0x6e94
cmpq (%r10), %r13
je 0x70bd
cmpl $0x3, %r12d
jne 0x6eb2
movb $0x0, -0x88(%rbp)
movb $0x1, %al
movl %eax, -0x3c(%rbp)
xorl %r11d, %r11d
movq %r13, -0x70(%rbp)
movl %r15d, %r12d
jmp 0x6f13
movq %r13, -0xa0(%rbp)
movb $0x1, -0x88(%rbp)
movb $0x1, %r11b
cmpb $0x0, 0x69(%r14)
movl %r15d, %r12d
jne 0x6f0c
movq 0x50e8(%r14), %rax
movzbl 0x1(%rax,%rbx), %eax
movl %eax, %ecx
shrb $0x3, %cl
testl %r12d, %r12d
movzbl %cl, %ecx
cmovel %eax, %ecx
andl $0x7, %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)
movl $0x0, -0x3c(%rbp)
movq 0x50e8(%r14), %rax
movzbl 0x1(%rax,%rbx), %eax
movl %eax, %ecx
shrb $0x3, %cl
testl %r12d, %r12d
movzbl %cl, %r13d
cmovel %eax, %r13d
andl $0x0, -0x40(%rbp)
andl $0x7, %r13d
movb 0x69(%r14), %cl
cmpb $0x1, %cl
jne 0x6f8d
movl %r11d, %r15d
movq 0x50a0(%r14), %rax
movq (%rax,%r13,8), %rcx
leaq -0x40(%rbp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
callq 0x20a0
cmpl $0x1, %eax
je 0x6f6e
leaq 0x80e8(%rip), %rdi      # 0xf051
callq 0x35b7
movq 0x5118(%r14), %rax
incq (%rax,%r13,8)
movl -0x40(%rbp), %eax
movb 0x69(%r14), %cl
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %r10
movl %r15d, %r11d
jmp 0x6fb6
movq (%r14), %rax
leal (,%r13,8), %edx
movq (%rax,%rdx), %rax
movq 0x5118(%r14), %r9
movq (%r9,%rdx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%r9,%rdx)
movzbl (%rax,%rdi), %eax
movl %eax, -0x40(%rbp)
movl %eax, -0x80(%rbp)
movl %r13d, -0x7c(%rbp)
testb $0x1, %cl
jne 0x6fd5
cmpl %r13d, -0x84(%rbp)
je 0x6fd5
movb $0x0, -0x88(%rbp)
xorl %r11d, %r11d
testl %eax, %eax
je 0x709d
movq 0x5110(%r14), %rdx
cltq
movq (%rdx,%rax,8), %rcx
leaq 0x1(%rcx), %rdi
movq %rdi, (%rdx,%rax,8)
movq 0x50e8(%r14), %rdx
movb (%rdx,%rcx), %r8b
testl %r12d, %r12d
je 0x700b
andb $-0x8, %r8b
orb %r13b, %r8b
jmp 0x7019
andb $-0x39, %r8b
movl %r13d, %edi
shlb $0x3, %dil
orb %dil, %r8b
movb %r8b, (%rdx,%rcx)
cmpb $0x1, 0x3c(%r14)
jne 0x705e
movq -0xa8(%rbp), %rdx
cmpb $0x0, (%rdx)
jne 0x705e
movq 0x50e8(%r14), %rdx
movzbl (%rdx,%rcx), %edx
movl %edx, %edi
shrb $0x3, %dil
testl %r12d, %r12d
movzbl %dil, %edi
cmovel %edx, %edi
andb $0x7, %dil
cmpb %r13b, %dil
je 0x705e
movq -0xa8(%rbp), %rdx
movb $0x1, (%rdx)
movq -0xb0(%rbp), %rdx
movq -0x70(%rbp), %rdi
cmpq %rdi, (%rdx,%rax,8)
je 0x709d
cmpb $0x0, -0x3c(%rbp)
je 0x708e
movq 0x50e8(%r14), %rdx
movb (%rdx,%rcx), %dil
cmpb $0x3f, %dil
ja 0x708e
orb -0x29(%rbp), %dil
movb %dil, (%rdx,%rcx)
movq -0xb0(%rbp), %rcx
movq -0x70(%rbp), %rdx
movq %rdx, (%rcx,%rax,8)
incq %rbx
jmp 0x6dbe
movq %r13, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xaffe
jmp 0x6e3e
movq %r10, %rdi
movq %r14, %rsi
callq 0xb041
movq -0x50(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq 0x8(%rdx), %rbx
movq -0x48(%rbp), %r12
testq %r12, %r12
je 0x7102
movq 0x8(%r12), %rax
cmpq (%rsi), %rax
jne 0x7102
movq %r12, %rdi
movq -0x60(%rbp), %r13
movq %r13, %rcx
callq 0xb1e2
movq -0x38(%rbp), %rsi
movq %r12, %rdx
movl %r15d, %r12d
jmp 0x713e
cmpb $0x0, 0x2c(%rsi)
movq -0x60(%rbp), %r13
movl %r15d, %r12d
jne 0x7125
movq %rsi, %rdi
movq %rdx, %rsi
movq %r13, %rdx
callq 0x63c9
movq -0x50(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x713e
movq %r13, %rsi
callq 0x648d
movq -0x50(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq %rdx, %r15
movq %rbx, 0x8(%rsi)
movq %rbx, (%rsi)
movq %r13, %rdi
callq 0xaea5
movq %rax, %r10
movq %r15, -0x48(%rbp)
movq %rax, 0x18(%r15)
movb $0x0, -0x88(%rbp)
jmp 0x6db0
testb $0x1, %r11b
movq -0x60(%rbp), %r15
movq -0x48(%rbp), %r12
je 0x71a6
leaq 0x1(%rbx), %rax
cmpq %rbx, -0xa0(%rbp)
jne 0x7198
leaq -0xa0(%rbp), %rdi
movq %rax, 0x8(%rdi)
callq 0xafd6
movq -0x38(%rbp), %rsi
jmp 0x71a6
cmpb $0x0, 0x3e(%r14)
jne 0x71a6
cmpb $0x0, 0x69(%r14)
je 0x7213
cmpb $0x0, 0x2c(%rsi)
jne 0x71bc
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x628f
movq %rax, %r12
movq -0x68(%rbp), %rdi
cmpb $0x0, 0x3c(%r14)
jne 0x71d7
testq %rdi, %rdi
je 0x71d7
incq %rdi
movq %r14, %rsi
callq 0x3748
testq %r12, %r12
je 0x71e7
cmpq $0x0, (%r12)
je 0x71eb
xorl %ebx, %ebx
jmp 0x71f7
xorl %ebx, %ebx
jmp 0x7202
movq 0x8(%r12), %rax
cmpq 0x28(%r14), %rax
sete %bl
movq %r12, %rdi
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
leaq -0xa0(%rbp), %rdi
movq %rax, 0x8(%rdi)
callq 0xaffe
jmp 0x7192