// File: /workspace/github_c_makefile_star2_O1/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, -0x88(%rbp)
movq %rdx, -0x40(%rbp)
movq %rsi, %r15
movq %rdi, %r12
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 %ebx, %ebx
xorl %edx, %edx
callq 0x3304
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, -0x90(%rbp)
callq 0x3304
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x60(%rbp)
movq %r12, -0x38(%rbp)
movq %r12, %rdi
callq 0xb7e2
cmpq $0x0, 0x28(%r14)
je 0xa608
movq %rax, %r13
movq -0x40(%rbp), %r10
leal -0x1(%r10), %eax
movl %eax, -0x50(%rbp)
movl %r10d, %eax
shll $0x10, %eax
movl %eax, -0x4c(%rbp)
movq $0x0, -0x48(%rbp)
xorl %ebx, %ebx
xorl %r11d, %r11d
movl -0x2c(%rbp), %r9d
movq 0x5100(%r14), %rax
movl (%rax,%rbx,4), %eax
movl %eax, %ecx
shrl $0x10, %ecx
cmpl $0x10000, %eax          # imm = 0x10000
setb %al
cmpl %r10d, %ecx
setae %dl
orb %al, %dl
cmpl -0x50(%rbp), %ecx
setae %r12b
orb %dl, %r12b
testb %dl, %dl
jne 0xa4a3
testb %r12b, %r12b
jne 0xa3dd
cmpb $0x1, -0x68(%rbp)
jne 0xa3dd
leaq -0x1(%rbx), %rax
cmpq %rax, -0x80(%rbp)
je 0xa3ad
cmpb $0x1, 0x3c(%r14)
jne 0xa3dd
movq %rbx, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
movq %r15, %rsi
movq %r13, -0x98(%rbp)
movq %r15, %r13
movq %r11, %r15
callq 0xb920
movq %r15, %r11
movq %r13, %r15
movq -0x98(%rbp), %r13
movl -0x2c(%rbp), %r9d
jmp 0xa404
cmpb $0x0, 0x2c(%r15)
jne 0xa3fd
movq %r11, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq -0x38(%rbp), %rcx
callq 0x676c
movl -0x2c(%rbp), %r9d
movq %rax, %r11
movq %rbx, 0x8(%r15)
movq %rbx, (%r15)
testq %r13, %r13
je 0xa454
cmpq %rbx, (%r13)
jne 0xa454
movq %r13, %rdi
movq %r14, %rsi
movq %r11, %r12
callq 0xb96b
movq %r12, %rdi
movq 0x8(%r13), %rbx
testq %r12, %r12
je 0xa5b1
movq 0x8(%rdi), %rax
cmpq (%r15), %rax
jne 0xa5b1
movq %r15, %rsi
movq %r13, %rdx
movq -0x38(%rbp), %r12
movq %r12, %rcx
movq %rdi, %r13
callq 0xbb2a
jmp 0xa5e1
testb %r12b, %r12b
je 0xa463
movb $0x0, -0x68(%rbp)
movq %rbx, -0x48(%rbp)
jmp 0xa49f
movq %rbx, -0x80(%rbp)
movb $0x1, -0x68(%rbp)
movq 0x5100(%r14), %rax
movl (%rax,%rbx,4), %eax
movl %eax, %ecx
shrl $0x8, %ecx
testl %r9d, %r9d
cmovel %eax, %ecx
movzbl %cl, %eax
movl %eax, -0x64(%rbp)
movq (%r14), %rcx
shll $0x3, %eax
movq (%rcx,%rax), %rcx
movq 0x5118(%r14), %rdx
addq (%rdx,%rax), %rcx
movq %rcx, -0x70(%rbp)
movq -0x40(%rbp), %r10
movq 0x5100(%r14), %rax
movl (%rax,%rbx,4), %eax
movl %eax, %ecx
shrl $0x8, %ecx
testl %r9d, %r9d
cmovel %eax, %ecx
movzbl %cl, %ecx
movq (%r14), %rax
leal (,%rcx,8), %esi
movq (%rax,%rsi), %rax
movq 0x5118(%r14), %rdi
movq (%rdi,%rsi), %rdx
leaq 0x1(%rdx), %r8
movq %r8, (%rdi,%rsi)
cmpl -0x64(%rbp), %ecx
je 0xa4e5
movb $0x0, -0x68(%rbp)
movzbl (%rax,%rdx), %eax
testq %rax, %rax
je 0xa5a2
movq 0x5110(%r14), %rsi
movq (%rsi,%rax,8), %rdx
leaq 0x1(%rdx), %rdi
movq %rdi, (%rsi,%rax,8)
movq 0x5100(%r14), %rsi
movl (%rsi,%rdx,4), %esi
testl %r9d, %r9d
je 0xa51e
andl $0xffffff00, %esi       # imm = 0xFFFFFF00
orl %ecx, %esi
jmp 0xa52b
andl $0xffff00ff, %esi       # imm = 0xFFFF00FF
movl %ecx, %edi
shll $0x8, %edi
orl %edi, %esi
movq 0x5100(%r14), %rdi
movl %esi, (%rdi,%rdx,4)
cmpb $0x1, 0x3c(%r14)
jne 0xa56a
movq -0x88(%rbp), %r8
cmpb $0x0, (%r8)
jne 0xa56a
movl %esi, %r8d
shrl $0x8, %r8d
testl %r9d, %r9d
cmovel %esi, %r8d
movzbl %r8b, %r8d
cmpl %ecx, %r8d
je 0xa56a
movq -0x88(%rbp), %rcx
movb $0x1, (%rcx)
movq -0x90(%rbp), %rcx
movq -0x48(%rbp), %r8
cmpq %r8, (%rcx,%rax,8)
je 0xa5a2
cmpl $0x10000, %esi          # imm = 0x10000
setb %cl
andb %cl, %r12b
cmpb $0x1, %r12b
jne 0xa593
orl -0x4c(%rbp), %esi
movl %esi, (%rdi,%rdx,4)
movq -0x90(%rbp), %rcx
movq -0x48(%rbp), %rdx
movq %rdx, (%rcx,%rax,8)
incq %rbx
cmpq 0x28(%r14), %rbx
jb 0xa360
jmp 0xa60b
cmpb $0x0, 0x2c(%r15)
jne 0xa5ca
movq %r15, %rdi
movq %r13, %rsi
movq -0x38(%rbp), %rdx
callq 0x689e
movq %r12, %rdi
testq %rdi, %rdi
je 0xa5dd
movq -0x38(%rbp), %r12
movq %r12, %rsi
callq 0x6950
jmp 0xa5e1
movq -0x38(%rbp), %r12
movq %rbx, 0x8(%r15)
movq %rbx, (%r15)
movq %r12, %rdi
callq 0xb7e2
movq %rax, 0x18(%r13)
movb $0x0, -0x68(%rbp)
movq %r13, %r11
movq %rax, %r13
movl -0x2c(%rbp), %r9d
movq -0x40(%rbp), %r10
jmp 0xa5a5
xorl %r11d, %r11d
cmpb $0x1, -0x68(%rbp)
jne 0xa627
leaq -0x80(%rbp), %rdi
movq %rbx, 0x8(%rdi)
movq %r15, %rsi
movq %r11, %rbx
callq 0xb920
movq %rbx, %r11
cmpb $0x0, 0x2c(%r15)
je 0xa634
movq -0x38(%rbp), %rbx
jmp 0xa64b
movq %r11, %rdi
movq %r15, %rsi
xorl %edx, %edx
movq -0x38(%rbp), %rbx
movq %rbx, %rcx
callq 0x676c
movq %rax, %r11
testq %r11, %r11
je 0xa656
cmpq $0x0, (%r11)
je 0xa65b
xorl %r14d, %r14d
jmp 0xa667
movq 0x8(%r11), %rax
cmpq 0x28(%r14), %rax
sete %r14b
testq %r11, %r11
je 0xa677
movq %r11, %rdi
movq %rbx, %rsi
callq 0x6950
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq