// File: /workspace/github_c_makefile_star2_O1/felipelouza[P]egap/./merge16.h
// Function: addCharToPrefix16
static bool addCharToPrefix16(solidBlockFile *solidHead, liquidBlock *liquid, customInt prefixLength, bool *mergeChanged, const int round, g_data *g) {
  assert(prefixLength <= MAX_LCP_SIZE);
  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; 
  int m = (round%2==1) ? 1 : 2;  // mask for the bitB array

  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
    if(!g->bwtOnly) {
      start_block = (g->blockBeginsAt[k]>0) && (g->blockBeginsAt[k] < prefixLength);
      if(start_block) last_block_recent = g->blockBeginsAt[k]>=prefixLength-1;
    }
    else {
      start_block = tba_block_test_set(g->bitB,k,m,&last_block_recent);    
    }
    if (start_block) {
      // check if the block we just left is not recent and monochrome 
      if(cblock.mono==true && !last_block_recent) {
        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 ieration 
      // create new block?
      if (blockID[currentChar] != id) {
        if(g->bwtOnly) { // no lcp just mark B array 
          if(last_block_recent) tba_mark_if0(g->bitB,positionToUpdate,m);
        } else    // update lcp
          if(last_block_recent && g->blockBeginsAt[positionToUpdate]==0) // only 0 values in B are overwritten
            g->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, -0x2c(%rbp)
movq %rcx, -0x98(%rbp)
movq %rdx, %r12
movq %rdi, %rbx
xorps %xmm0, %xmm0
movq %rsi, -0x48(%rbp)
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 %r13d, %r13d
xorl %edx, %edx
callq 0x3304
movslq 0x38(%r14), %rsi
movl %esi, %eax
movq %rsp, %r15
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq 0x28(%r14), %rdx
movq %r15, %rdi
callq 0x3304
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movq $0x0, -0x70(%rbp)
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
callq 0xb7e2
movq %rax, -0x58(%rbp)
cmpq $0x0, 0x28(%r14)
je 0x8346
movl -0x2c(%rbp), %r10d
movl %r10d, %eax
andl $0x1, %eax
movl $0x2, %ecx
subl %eax, %ecx
leaq -0x1(%r12), %rax
movq %rax, -0xb0(%rbp)
movl %ecx, -0x64(%rbp)
movl %ecx, %eax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x60(%rbp)
xorl %r13d, %r13d
movq $0x0, -0x38(%rbp)
movq %r12, %r11
movq %r12, -0x50(%rbp)
movq -0x48(%rbp), %rbx
movq %r15, -0xa8(%rbp)
cmpb $0x0, 0x3c(%r14)
je 0x801b
movq 0x98(%r14), %rax
movq %r13, %rdx
shrq $0x5, %rdx
movq (%rax,%rdx,8), %rsi
leal (,%r13,2), %ecx
andl $0x3e, %ecx
movq %rsi, %rdi
shrq %cl, %rdi
andl $0x3, %edi
movl %edi, %r8d
xorl -0x64(%rbp), %r8d
cmpl $0x3, %r8d
jne 0x804c
movl $0x3, %edi
shlq %cl, %rdi
orq %rdi, %rsi
movq %rsi, (%rax,%rdx,8)
movb $0x1, %r12b
movb $0x1, %al
jmp 0x8056
movq 0x98(%r14), %rax
movb (%rax,%r13), %al
testb %al, %al
setne %cl
movzbl %al, %edx
cmpq %r11, %rdx
setb %al
andb %cl, %al
cmpq %rdx, -0xb0(%rbp)
setbe %cl
movl %eax, %r12d
xorb $0x1, %r12b
orb %cl, %r12b
jmp 0x8056
cmpl $0x3, %edi
sete %al
setne %r12b
testb %al, %al
je 0x81a4
movb -0x78(%rbp), %al
xorb $0x1, %al
orb %r12b, %al
je 0x809d
cmpb $0x0, 0x2c(%rbx)
jne 0x808e
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
movq -0x58(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0x676c
movq -0x50(%rbp), %r11
movl -0x2c(%rbp), %r10d
movq %rax, -0x38(%rbp)
movq %rbx, %rax
movq %r15, %rbx
movq %r13, 0x8(%rax)
movq %r13, (%rax)
jmp 0x80be
movq %rbx, %rsi
movq %r15, %rbx
movq %r13, -0x88(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xb920
movq -0x50(%rbp), %r11
movl -0x2c(%rbp), %r10d
movq -0x58(%rbp), %r15
testq %r15, %r15
je 0x8108
cmpq %r13, (%r15)
jne 0x8108
movq %r15, %rdi
movq %r14, %rsi
callq 0xb96b
movq 0x8(%r15), %r13
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
movq -0x48(%rbp), %rsi
je 0x8141
movq 0x8(%rdi), %rax
cmpq (%rsi), %rax
jne 0x8141
movq %r15, %rdx
movq -0x40(%rbp), %r12
movq %r12, %rcx
movq %rdi, %r15
callq 0xbb2a
jmp 0x8312
testb %r12b, %r12b
movq %rbx, %r15
je 0x8121
movb $0x0, -0x78(%rbp)
movq %r13, -0x60(%rbp)
movq -0x48(%rbp), %rbx
jmp 0x81a4
movq %r13, -0x90(%rbp)
movb $0x1, -0x78(%rbp)
testl %r10d, %r10d
je 0x8174
movq 0x50e8(%r14), %rax
movb (%rax,%r13), %al
shrb $0x4, %al
jmp 0x8181
cmpb $0x0, 0x2c(%rsi)
jne 0x815a
movq %rsi, %rdi
movq %r15, %rsi
movq -0x40(%rbp), %rdx
callq 0x689e
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x830e
movq -0x40(%rbp), %r12
movq %r12, %rsi
callq 0x6950
jmp 0x8312
movq 0x50e8(%r14), %rax
movb (%rax,%r13), %al
andb $0xf, %al
movq -0x48(%rbp), %rbx
movzbl %al, %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)
testl %r10d, %r10d
je 0x81b9
movq 0x50e8(%r14), %rax
movb (%rax,%r13), %dl
shrb $0x4, %dl
jmp 0x81c7
movq 0x50e8(%r14), %rax
movb (%rax,%r13), %dl
andb $0xf, %dl
movzbl %dl, %esi
movq (%r14), %rax
leal (,%rsi,8), %edi
movq (%rax,%rdi), %rax
movq 0x5118(%r14), %r8
movq (%r8,%rdi), %rcx
leaq 0x1(%rcx), %r9
movq %r9, (%r8,%rdi)
cmpl %esi, -0x74(%rbp)
je 0x81f4
movb $0x0, -0x78(%rbp)
movzbl (%rax,%rcx), %eax
testq %rax, %rax
je 0x82ff
movq 0x5110(%r14), %rsi
movq (%rsi,%rax,8), %rcx
leaq 0x1(%rcx), %rdi
movq %rdi, (%rsi,%rax,8)
testl %r10d, %r10d
je 0x822d
movq 0x50e8(%r14), %rsi
movb (%rsi,%rcx), %sil
andb $-0x10, %sil
orb %dl, %sil
jmp 0x8245
movq 0x50e8(%r14), %rsi
movb (%rsi,%rcx), %dil
andb $0xf, %dil
movl %edx, %esi
shlb $0x4, %sil
orb %dil, %sil
movq 0x50e8(%r14), %rdi
movb %sil, (%rdi,%rcx)
cmpb $0x1, 0x3c(%r14)
jne 0x8295
movq -0x98(%rbp), %rsi
cmpb $0x0, (%rsi)
jne 0x8295
movq 0x50e8(%r14), %rsi
movb (%rsi,%rcx), %sil
movl %esi, %edi
shrb $0x4, %dil
andb $0xf, %sil
testl %r10d, %r10d
movzbl %sil, %esi
movzbl %dil, %edi
cmovel %esi, %edi
cmpb %dl, %dil
je 0x8295
movq -0x98(%rbp), %rdx
movb $0x1, (%rdx)
movq -0x60(%rbp), %rdx
cmpq %rdx, (%r15,%rax,8)
je 0x82ff
cmpb $0x1, 0x3c(%r14)
jne 0x82e1
testb %r12b, %r12b
je 0x82f7
movq 0x98(%r14), %rdx
movq %rcx, %rsi
shrq $0x5, %rsi
movq (%rdx,%rsi,8), %rdi
addl %ecx, %ecx
andl $0x3e, %ecx
movq %rdi, %r8
shrq %cl, %r8
testb $0x3, %r8b
jne 0x82f7
movq -0xa0(%rbp), %r8
shlq %cl, %r8
orq %r8, %rdi
movq %rdi, (%rdx,%rsi,8)
jmp 0x82f7
testb %r12b, %r12b
je 0x82f7
movq 0x98(%r14), %rdx
cmpb $0x0, (%rdx,%rcx)
jne 0x82f7
movb %r11b, (%rdx,%rcx)
movq -0x60(%rbp), %rcx
movq %rcx, (%r15,%rax,8)
incq %r13
cmpq 0x28(%r14), %r13
jb 0x7fcb
jmp 0x8352
movq -0x40(%rbp), %r12
movq -0x48(%rbp), %rbx
movq %r13, 0x8(%rbx)
movq %r13, (%rbx)
movq %r12, %rdi
callq 0xb7e2
movq %r15, -0x38(%rbp)
movq %rax, -0x58(%rbp)
movq %rax, 0x18(%r15)
movb $0x0, -0x78(%rbp)
movl -0x2c(%rbp), %r10d
movq -0x50(%rbp), %r11
movq -0xa8(%rbp), %r15
jmp 0x8302
movq $0x0, -0x38(%rbp)
movq -0x48(%rbp), %rbx
cmpb $0x1, -0x78(%rbp)
jne 0x836b
leaq -0x90(%rbp), %rdi
movq %r13, 0x8(%rdi)
movq %rbx, %rsi
callq 0xb920
cmpb $0x0, 0x2c(%rbx)
je 0x837b
movq -0x40(%rbp), %r15
movq -0x38(%rbp), %rdi
jmp 0x8393
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq -0x40(%rbp), %r15
movq %r15, %rcx
callq 0x676c
movq %rax, %rdi
testq %rdi, %rdi
je 0x839e
cmpq $0x0, (%rdi)
je 0x83a2
xorl %ebx, %ebx
jmp 0x83ad
movq 0x8(%rdi), %rax
cmpq 0x28(%r14), %rax
sete %bl
testq %rdi, %rdi
je 0x83ba
movq %r15, %rsi
callq 0x6950
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq