// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/./merge256.h
// Function: addCharToPrefix256
static bool addCharToPrefix256(solidBlockFile *solidHead, liquidBlock *liquid, uint32_t prefixLength, bool *mergeChanged, const int round, g_data *g) {
  assert(prefixLength <= 65535);// lengths are stored in 16 bits in g->array32
  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; 

  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
    int bk = get_blockBeginsAt(k);
    start_block = (bk>0) && (bk < prefixLength);// if bk==prefixLength the value has been written during this iteration 
    if(start_block) last_block_recent = (bk >=prefixLength-1);
    if (start_block) {
      // check if the block we just left is not recent and monochrome and if(unsortedLcp) singleton  
      if( !last_block_recent && cblock.mono && ((cblock.beginsAt==k-1)||g->bwtOnly)) {
        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_mergeColor(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_mergeColor(k,round);   // g->mergeColor[k] b in pseudocode
    int currentChar = g->bws[currentColor][g->inCnt[currentColor]++]; // c in pseudocode
    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_mergeColor(positionToUpdate,currentColor,round); // g->newMergeColor[positionToUpdate] = currentColor;
      if(g->bwtOnly && !*mergeChanged && get_mergeColor(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 && get_blockBeginsAt(positionToUpdate)==0) // only 0 values in B are overwritten
           set_blockBeginsAt(positionToUpdate, prefixLength);
        blockID[currentChar] = id; // update block id, always!
      }
    }
    k++;
  } // end main loop
  assert(next==NULL); 
  if(cblock.mono==true) { // final mono block can be created 
    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);
  // 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, -0x68(%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, -0x38(%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, -0x70(%rbp)
callq 0x2fbc
leaq -0xa0(%rbp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
andq $0x0, 0x20(%rax)
movq %rbx, -0x48(%rbp)
movq %rbx, %rdi
callq 0xaea5
movq %rax, -0x40(%rbp)
leal -0x1(%r15), %eax
movl %eax, -0x4c(%rbp)
movq %r15, %rax
movq %r15, -0xa8(%rbp)
shll $0x10, %eax
movl %eax, -0x5c(%rbp)
xorl %ebx, %ebx
movq $0x0, -0x58(%rbp)
xorl %eax, %eax
movq -0x30(%rbp), %rsi
cmpq 0x28(%r14), %rbx
jae 0x9e2e
movq 0x5100(%r14), %rcx
movl (%rcx,%rbx,4), %ecx
movl %ecx, %r15d
shrl $0x10, %r15d
cmpl $0x10000, %ecx          # imm = 0x10000
setb %cl
cmpl -0xa8(%rbp), %r15d
setae %dl
movb $0x1, %r13b
orb %cl, %dl
jne 0x9c83
cmpl -0x4c(%rbp), %r15d
jae 0x9be9
testb $0x1, %al
je 0x9be9
leaq -0x1(%rbx), %rax
cmpq -0xa0(%rbp), %rax
je 0x9bd0
cmpb $0x1, 0x3c(%r14)
jne 0x9be9
movq %rbx, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xaffe
movq -0x30(%rbp), %rsi
jmp 0x9c0f
cmpb $0x0, 0x2c(%rsi)
jne 0x9c08
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
callq 0x628f
movq -0x30(%rbp), %rsi
movq %rax, -0x38(%rbp)
movq %rbx, 0x8(%rsi)
movq %rbx, (%rsi)
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x9c21
cmpq (%rdi), %rbx
je 0x9d88
cmpl -0x4c(%rbp), %r15d
jae 0x9c76
movq %rbx, -0xa0(%rbp)
movb $0x1, -0x88(%rbp)
movq 0x5100(%r14), %rax
movl (%rax,%rbx,4), %eax
movl %eax, %ecx
shrl $0x8, %ecx
testl %r12d, %r12d
cmovel %eax, %ecx
movzbl %cl, %eax
movl %eax, -0x84(%rbp)
movq (%r14), %rcx
shll $0x3, %eax
movq (%rcx,%rax), %rcx
movq 0x5118(%r14), %rdx
addq (%rdx,%rax), %rcx
movq %rcx, -0x90(%rbp)
movb $0x1, %al
xorl %r13d, %r13d
jmp 0x9c83
movb $0x0, -0x88(%rbp)
xorl %eax, %eax
movq %rbx, -0x58(%rbp)
movq 0x5100(%r14), %rcx
movl (%rcx,%rbx,4), %edx
movl %edx, %edi
shrl $0x8, %edi
testl %r12d, %r12d
cmovel %edx, %edi
movzbl %dil, %r11d
movq (%r14), %rdx
leal (,%r11,8), %r8d
movq (%rdx,%r8), %rdx
movq 0x5118(%r14), %r9
movq (%r9,%r8), %rdi
leaq 0x1(%rdi), %r10
movq %r10, (%r9,%r8)
cmpl -0x84(%rbp), %r11d
je 0x9cd0
movb $0x0, -0x88(%rbp)
xorl %eax, %eax
movzbl (%rdx,%rdi), %edx
testq %rdx, %rdx
je 0x9d80
movq 0x5110(%r14), %r8
movq (%r8,%rdx,8), %rdi
leaq 0x1(%rdi), %r9
movq %r9, (%r8,%rdx,8)
movl (%rcx,%rdi,4), %r8d
testl %r12d, %r12d
je 0x9d05
andl $0xffffff00, %r8d       # imm = 0xFFFFFF00
orl %r11d, %r8d
jmp 0x9d16
andl $0xffff00ff, %r8d       # imm = 0xFFFF00FF
movl %r11d, %r9d
shll $0x8, %r9d
orl %r9d, %r8d
movl %r8d, (%rcx,%rdi,4)
cmpb $0x1, 0x3c(%r14)
jne 0x9d4a
movq -0x68(%rbp), %r9
cmpb $0x0, (%r9)
jne 0x9d4a
movl %r8d, %r9d
shrl $0x8, %r9d
testl %r12d, %r12d
cmovel %r8d, %r9d
movzbl %r9b, %r9d
cmpl %r11d, %r9d
je 0x9d4a
movq -0x68(%rbp), %r9
movb $0x1, (%r9)
movq -0x70(%rbp), %r9
movq -0x58(%rbp), %r10
cmpq %r10, (%r9,%rdx,8)
je 0x9d80
cmpl $0x10000, %r8d          # imm = 0x10000
setb %r9b
andb %r9b, %r13b
cmpb $0x1, %r13b
jne 0x9d74
orl -0x5c(%rbp), %r8d
movl %r8d, (%rcx,%rdi,4)
movq -0x70(%rbp), %rcx
movq -0x58(%rbp), %rdi
movq %rdi, (%rcx,%rdx,8)
incq %rbx
jmp 0x9b79
movq %r14, %rsi
callq 0xb041
movq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rsi
movq 0x8(%rdx), %rbx
movq -0x38(%rbp), %r13
testq %r13, %r13
je 0x9dcb
movq 0x8(%r13), %rax
cmpq (%rsi), %rax
movq -0x40(%rbp), %rdx
jne 0x9dcb
movq %r13, %rdi
movq -0x48(%rbp), %r15
movq %r15, %rcx
callq 0xb1e2
movq -0x30(%rbp), %rsi
movq %r13, -0x40(%rbp)
jmp 0x9e03
cmpb $0x0, 0x2c(%rsi)
jne 0x9de4
movq %rsi, %rdi
movq %rdx, %rsi
movq -0x48(%rbp), %rdx
callq 0x63c9
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x9dff
movq -0x48(%rbp), %r15
movq %r15, %rsi
callq 0x648d
movq -0x30(%rbp), %rsi
jmp 0x9e03
movq -0x48(%rbp), %r15
movq %rbx, 0x8(%rsi)
movq %rbx, (%rsi)
movq %r15, %rdi
callq 0xaea5
movq -0x40(%rbp), %rcx
movq %rax, 0x18(%rcx)
movb $0x0, -0x88(%rbp)
movq %rcx, -0x38(%rbp)
movq %rax, -0x40(%rbp)
jmp 0x9b73
testb $0x1, %al
je 0x9e46
leaq -0xa0(%rbp), %rdi
movq %rbx, 0x8(%rdi)
callq 0xaffe
movq -0x30(%rbp), %rsi
cmpb $0x0, 0x2c(%rsi)
movq -0x48(%rbp), %r15
je 0x9e56
movq -0x38(%rbp), %rdi
jmp 0x9e67
movq -0x38(%rbp), %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x628f
movq %rax, %rdi
testq %rdi, %rdi
je 0x9e76
cmpq $0x0, (%rdi)
je 0x9e7a
xorl %ebx, %ebx
jmp 0x9e85
xorl %ebx, %ebx
jmp 0x9e8d
movq 0x8(%rdi), %rax
cmpq 0x28(%r14), %rax
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