// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movq %r9, %r14
movl %r8d, -0x2c(%rbp)
movq %rcx, -0x98(%rbp)
movl %edx, %r12d
movq %rsi, %rbx
movq %rdi, %r15
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
movq $0x0, -0x40(%rbp)
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, -0xa0(%rbp)
callq 0x33a4
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movq $0x0, -0x70(%rbp)
movq %r15, %rdi
callq 0xb878
movq %rbx, %rsi
cmpq $0x0, 0x28(%r14)
je 0xa735
movq %rax, -0x50(%rbp)
movq %r15, -0x48(%rbp)
leal -0x1(%r12), %eax
movl %eax, -0x54(%rbp)
movl %r12d, %eax
shll $0x10, %eax
movl %eax, -0x64(%rbp)
xorl %ecx, %ecx
movq $0x0, -0x60(%rbp)
xorl %r13d, %r13d
movq $0x0, -0x40(%rbp)
movq %rsi, -0x38(%rbp)
movq 0x5100(%r14), %rax
movl (%rax,%r13,4), %eax
movl %eax, %r15d
shrl $0x10, %r15d
cmpl $0x10000, %eax          # imm = 0x10000
setb %al
movq %r12, %rbx
cmpl %r12d, %r15d
setae %dl
movb $0x1, %r12b
orb %al, %dl
jne 0xa599
cmpl -0x54(%rbp), %r15d
jae 0xa4bf
testb $0x1, %cl
je 0xa4bf
leaq -0x1(%r13), %rax
cmpq %rax, -0x90(%rbp)
je 0xa4a6
cmpb $0x1, 0x3c(%r14)
jne 0xa4bf
movq %r13, -0x88(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xb9b7
movq -0x38(%rbp), %rsi
jmp 0xa4e5
cmpb $0x0, 0x2c(%rsi)
jne 0xa4de
movq -0x40(%rbp), %rdi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
callq 0x6839
movq -0x38(%rbp), %rsi
movq %rax, -0x40(%rbp)
movq %r13, 0x8(%rsi)
movq %r13, (%rsi)
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0xa541
cmpq %r13, (%rdi)
jne 0xa541
movq %r14, %rsi
callq 0xba02
movq -0x50(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq 0x8(%rdx), %r13
movq -0x40(%rbp), %r12
testq %r12, %r12
je 0xa6b2
movq 0x8(%r12), %rax
cmpq (%rsi), %rax
jne 0xa6b2
movq %r12, %rdi
movq -0x48(%rbp), %r15
movq %r15, %rcx
callq 0xbbcb
movq -0x38(%rbp), %rsi
movq %r12, -0x50(%rbp)
movq %rbx, %r12
jmp 0xa6ed
cmpl -0x54(%rbp), %r15d
jae 0xa58f
movq %r13, -0x90(%rbp)
movb $0x1, -0x78(%rbp)
movq 0x5100(%r14), %rax
movl (%rax,%r13,4), %eax
movl %eax, %ecx
shrl $0x8, %ecx
cmpl $0x0, -0x2c(%rbp)
cmovel %eax, %ecx
movzbl %cl, %eax
movl %eax, -0x74(%rbp)
movq (%r14), %rcx
shll $0x3, %eax
movq (%rcx,%rax), %rcx
movq 0x5118(%r14), %rdx
addq (%rdx,%rax), %rcx
movq %rcx, -0x80(%rbp)
movb $0x1, %cl
xorl %r12d, %r12d
jmp 0xa599
movb $0x0, -0x78(%rbp)
xorl %ecx, %ecx
movq %r13, -0x60(%rbp)
movq 0x5100(%r14), %rax
movl (%rax,%r13,4), %edx
movl %edx, %edi
shrl $0x8, %edi
cmpl $0x0, -0x2c(%rbp)
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 -0x74(%rbp), %r11d
je 0xa5e2
movb $0x0, -0x78(%rbp)
xorl %ecx, %ecx
movzbl (%rdx,%rdi), %edx
testq %rdx, %rdx
je 0xa6a0
movq 0x5110(%r14), %r8
movq (%r8,%rdx,8), %rdi
leaq 0x1(%rdi), %r9
movq %r9, (%r8,%rdx,8)
movl (%rax,%rdi,4), %r8d
cmpl $0x0, -0x2c(%rbp)
je 0xa618
andl $0xffffff00, %r8d       # imm = 0xFFFFFF00
orl %r11d, %r8d
jmp 0xa629
andl $0xffff00ff, %r8d       # imm = 0xFFFF00FF
movl %r11d, %r9d
shll $0x8, %r9d
orl %r9d, %r8d
movl %r8d, (%rax,%rdi,4)
cmpb $0x1, 0x3c(%r14)
jne 0xa664
movq -0x98(%rbp), %r9
cmpb $0x0, (%r9)
jne 0xa664
movl %r8d, %r9d
shrl $0x8, %r9d
cmpl $0x0, -0x2c(%rbp)
cmovel %r8d, %r9d
movzbl %r9b, %r9d
cmpl %r11d, %r9d
je 0xa664
movq -0x98(%rbp), %r9
movb $0x1, (%r9)
movq -0xa0(%rbp), %r9
movq -0x60(%rbp), %r10
cmpq %r10, (%r9,%rdx,8)
je 0xa6a0
cmpl $0x10000, %r8d          # imm = 0x10000
setb %r9b
andb %r9b, %r12b
cmpb $0x1, %r12b
jne 0xa691
orl -0x64(%rbp), %r8d
movl %r8d, (%rax,%rdi,4)
movq -0xa0(%rbp), %rax
movq -0x60(%rbp), %rdi
movq %rdi, (%rax,%rdx,8)
incq %r13
movq %rbx, %r12
cmpq 0x28(%r14), %r13
jb 0xa459
jmp 0xa718
cmpb $0x0, 0x2c(%rsi)
movq %rbx, %r12
jne 0xa6ce
movq %rsi, %rdi
movq %rdx, %rsi
movq -0x48(%rbp), %rdx
callq 0x6967
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0xa6e9
movq -0x48(%rbp), %r15
movq %r15, %rsi
callq 0x6a18
movq -0x38(%rbp), %rsi
jmp 0xa6ed
movq -0x48(%rbp), %r15
movq %r13, 0x8(%rsi)
movq %r13, (%rsi)
movq %r15, %rdi
callq 0xb878
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq %rax, 0x18(%rdx)
movb $0x0, -0x78(%rbp)
xorl %ecx, %ecx
movq %rdx, -0x40(%rbp)
movq %rax, -0x50(%rbp)
jmp 0xa6a6
testb $0x1, %cl
je 0xa731
leaq -0x90(%rbp), %rdi
movq %r13, 0x8(%rdi)
callq 0xb9b7
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %r15
cmpb $0x0, 0x2c(%rsi)
je 0xa741
movq -0x40(%rbp), %rdi
jmp 0xa752
movq -0x40(%rbp), %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x6839
movq %rax, %rdi
testq %rdi, %rdi
je 0xa761
cmpq $0x0, (%rdi)
je 0xa765
xorl %ebx, %ebx
jmp 0xa770
xorl %ebx, %ebx
jmp 0xa778
movq 0x8(%rdi), %rax
cmpq 0x28(%r14), %rax
sete %bl
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