// File: /workspace/github_c_makefile_star2_O1/felipelouza[P]egap/./merge128.h
// Function: addCharToPrefix128
static bool addCharToPrefix128(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);
  // 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_set16(g->mergeColor16,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->bwtOnly && !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; cblock.color =  get_mergeColor16(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_mergeColor16(k,round);   // g->mergeColor[k] b in pseudocode
    int currentChar = g->bws[currentColor][g->inCnt[currentColor]++]; // c in pseudocode
    cblock.lastChar =  currentChar; // save lastchar, only useful for singleton blocks
    cblock.lastColor =  currentColor; // save lastcolor, only useful for singleton blocks
    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_mergeColor16(positionToUpdate,currentColor,round); // g->newMergeColor[positionToUpdate] = currentColor;
      if(g->bwtOnly && !*mergeChanged && get_mergeColor16(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_mark16_if0(g->mergeColor16,positionToUpdate,m);
        blockID[currentChar] = id; // update block id, always!
      }
    }
    k++;
  } // end main loop
  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->bwtOnly && !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 $0x78, %rsp
movq %r9, %r14
movl %r8d, %r13d
movq %rcx, -0x90(%rbp)
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
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 %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, -0x98(%rbp)
callq 0x3304
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
movq $0x0, -0x40(%rbp)
movq %rbx, -0x38(%rbp)
movq %rbx, %rdi
callq 0xb7e2
cmpq $0x0, 0x28(%r14)
je 0x8f32
movq %rax, %r9
movl %r13d, %eax
andl $0x1, %eax
movl $0x2, %ecx
subl %eax, %ecx
addl $-0x2, %r12d
movl %r12d, -0x80(%rbp)
movl %ecx, -0x84(%rbp)
shll $0xe, %ecx
movl %ecx, -0x7c(%rbp)
movq $0x0, -0x78(%rbp)
xorl %r12d, %r12d
movq $0x0, -0x68(%rbp)
xorl %r10d, %r10d
movq %r15, -0x70(%rbp)
movl %r13d, %r11d
movl %r13d, -0x2c(%rbp)
movq %r14, -0xa0(%rbp)
movq 0x50f8(%r14), %rax
movzwl (%rax,%r12,2), %ecx
movl %ecx, %edx
shrl $0xe, %edx
movl -0x84(%rbp), %esi
xorl %edx, %esi
cmpl $0x3, %esi
jne 0x8beb
orl $0xc000, %ecx           # imm = 0xC000
movw %cx, (%rax,%r12,2)
movb $0x1, %bl
movb $0x1, %r13b
jmp 0x8bf6
cmpw $0x3, %dx
sete %r13b
setne %bl
movl %ebx, %eax
andb %r13b, %al
cmpb $0x1, %al
jne 0x8c39
cmpb $0x1, 0x3e(%r14)
jne 0x8c39
movq %r12, %rdi
movl -0x80(%rbp), %esi
movq %r14, %rdx
movq %r10, %r15
movl %ebx, %r14d
movq %r9, %rbx
callq 0x3a94
movq %rbx, %r9
movl %r14d, %ebx
movq -0xa0(%rbp), %r14
movl -0x2c(%rbp), %r11d
movq %r15, %r10
movq -0x70(%rbp), %r15
incq -0x68(%rbp)
testb %r13b, %r13b
je 0x8d71
testb %bl, %bl
jne 0x8c9d
cmpb $0x1, -0x48(%rbp)
jne 0x8c81
leaq -0x1(%r12), %rax
cmpq %rax, -0x60(%rbp)
jne 0x8c81
movq %r12, -0x58(%rbp)
movq %r12, 0x8(%r15)
movq 0x20(%r15), %rax
movslq -0x3c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r15), %rcx
movslq -0x40(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r15)
jmp 0x8cd0
testb %bl, %bl
jne 0x8c9d
cmpb $0x1, -0x48(%rbp)
jne 0x8c9d
cmpb $0x1, 0x3c(%r14)
jne 0x8c9d
cmpb $0x0, 0x69(%r14)
je 0x8efd
cmpb $0x0, 0x2c(%r15)
jne 0x8cc9
movq %r10, %rdi
movq %r15, %rsi
movq %r9, %rdx
movq -0x38(%rbp), %rcx
movl %ebx, %r13d
movq %r9, %rbx
callq 0x676c
movq %rbx, %r9
movl %r13d, %ebx
movl -0x2c(%rbp), %r11d
movq %rax, %r10
movq %r12, 0x8(%r15)
movq %r12, (%r15)
testq %r9, %r9
je 0x8d25
cmpq %r12, (%r9)
jne 0x8d25
movq %r9, %rdi
movq %r14, %rsi
movq %r10, %rbx
movq %r9, %r13
callq 0xb96b
movq %r13, %rdx
movq %rbx, %rdi
movq 0x8(%r13), %r12
testq %rbx, %rbx
je 0x8e99
movq 0x8(%rdi), %rax
cmpq (%r15), %rax
jne 0x8e99
movq %r15, %rsi
movq -0x38(%rbp), %rbx
movq %rbx, %rcx
movq %rdi, %r15
callq 0xbb2a
movq %r15, %rdx
jmp 0x8ecf
testb %bl, %bl
je 0x8d33
movb $0x0, -0x48(%rbp)
movq %r12, -0x78(%rbp)
jmp 0x8d71
movq %r12, -0x60(%rbp)
movb $0x1, -0x48(%rbp)
movq 0x50f8(%r14), %rax
movzwl (%rax,%r12,2), %eax
movl %eax, %ecx
shrl $0x7, %ecx
testl %r11d, %r11d
cmovel %eax, %ecx
andl $0x7f, %ecx
movl %ecx, -0x44(%rbp)
movq (%r14), %rax
shll $0x3, %ecx
movq (%rax,%rcx), %rax
movq 0x5118(%r14), %rdx
addq (%rdx,%rcx), %rax
movq %rax, -0x50(%rbp)
movq 0x50f8(%r14), %rax
movzwl (%rax,%r12,2), %eax
movl %eax, %ecx
shrl $0x7, %ecx
testl %r11d, %r11d
cmovel %eax, %ecx
andl $0x7f, %ecx
movq (%r14), %rax
leal (,%rcx,8), %edx
movq (%rax,%rdx), %rax
movq 0x5118(%r14), %rsi
movq (%rsi,%rdx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%rsi,%rdx)
movzbl (%rax,%rdi), %eax
movl %eax, -0x40(%rbp)
movl %ecx, -0x3c(%rbp)
cmpl %ecx, -0x44(%rbp)
je 0x8dbf
movb $0x0, -0x48(%rbp)
testq %rax, %rax
je 0x8e87
movq 0x5110(%r14), %rsi
movq (%rsi,%rax,8), %rdx
leaq 0x1(%rdx), %rdi
movq %rdi, (%rsi,%rax,8)
movq 0x50f8(%r14), %rsi
movzwl (%rsi,%rdx,2), %esi
movzwl %cx, %edi
testl %r11d, %r11d
je 0x8df5
andl $-0x80, %esi
orl %edi, %esi
jmp 0x8e02
andl $0xffffc07f, %esi       # imm = 0xFFFFC07F
shll $0x7, %edi
orl %esi, %edi
movl %edi, %esi
movq 0x50f8(%r14), %rdi
movw %si, (%rdi,%rdx,2)
cmpb $0x1, 0x3c(%r14)
jne 0x8e4d
movq -0x90(%rbp), %r8
cmpb $0x0, (%r8)
jne 0x8e4d
movzwl %si, %r8d
shrl $0x7, %r8d
testl %r11d, %r11d
movq %r9, %r13
movl %esi, %r9d
cmovnel %r8d, %r9d
andl $0x7f, %r9d
cmpw %cx, %r9w
movq %r13, %r9
je 0x8e4d
movq -0x90(%rbp), %rcx
movb $0x1, (%rcx)
movq -0x98(%rbp), %rcx
movq -0x78(%rbp), %r8
cmpq %r8, (%rcx,%rax,8)
je 0x8e87
movzwl %si, %ecx
cmpl $0x4000, %ecx           # imm = 0x4000
setb %cl
andb %cl, %bl
cmpb $0x1, %bl
jne 0x8e78
orl -0x7c(%rbp), %esi
movw %si, (%rdi,%rdx,2)
movq -0x98(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq %rdx, (%rcx,%rax,8)
incq %r12
cmpq 0x28(%r14), %r12
jb 0x8bbb
jmp 0x8f40
cmpb $0x0, 0x2c(%r15)
jne 0x8eb5
movq %r15, %rdi
movq %rdx, %rsi
movq -0x38(%rbp), %rdx
callq 0x689e
movq %r13, %rdx
movq %rbx, %rdi
testq %rdi, %rdi
je 0x8ecb
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x6950
movq %r13, %rdx
jmp 0x8ecf
movq -0x38(%rbp), %rbx
movq %rdx, %r15
movq -0x70(%rbp), %r13
movq %r12, 0x8(%r13)
movq %r12, (%r13)
movq %rbx, %rdi
callq 0xb7e2
movq %r15, %r10
movq %r13, %r15
movq %rax, %r9
movq %rax, 0x18(%r10)
movb $0x0, -0x48(%rbp)
movl -0x2c(%rbp), %r11d
jmp 0x8e8a
movq %r12, -0x58(%rbp)
leaq -0x60(%rbp), %rdi
movq %r15, %rsi
movq %r10, %r15
movq %r14, %r13
movl %ebx, %r14d
movq %r9, %rbx
callq 0xb920
movq %rbx, %r9
movl %r14d, %ebx
movq %r13, %r14
movl -0x2c(%rbp), %r11d
movq %r15, %r10
movq -0x70(%rbp), %r15
jmp 0x8cd0
movq $0x0, -0x68(%rbp)
xorl %r10d, %r10d
xorl %r12d, %r12d
movb -0x48(%rbp), %al
cmpb $0x1, %al
jne 0x8fa4
leaq -0x1(%r12), %rcx
cmpq %rcx, -0x60(%rbp)
jne 0x8f7c
movq %r12, -0x58(%rbp)
movq %r12, 0x8(%r15)
movq 0x20(%r15), %rax
movslq -0x3c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r15), %rcx
movslq -0x40(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r15)
jmp 0x8fa4
testb %al, %al
je 0x8fa4
cmpb $0x1, 0x3c(%r14)
jne 0x8fa4
cmpb $0x0, 0x69(%r14)
jne 0x8fa4
leaq -0x60(%rbp), %rdi
movq %r12, 0x8(%rdi)
movq %r15, %rsi
movq %r10, %rbx
callq 0xb920
movq %rbx, %r10
cmpb $0x0, 0x2c(%r15)
je 0x8fb1
movq -0x38(%rbp), %r15
jmp 0x8fc8
movq %r10, %rdi
movq %r15, %rsi
xorl %edx, %edx
movq -0x38(%rbp), %r15
movq %r15, %rcx
callq 0x676c
movq %rax, %r10
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
sete %al
orb 0x3c(%r14), %al
jne 0x8fe9
incq %rdi
movq %r14, %rsi
movq %r10, %rbx
callq 0x3afe
movq %rbx, %r10
testq %r10, %r10
je 0x8ff4
cmpq $0x0, (%r10)
je 0x8ff8
xorl %ebx, %ebx
jmp 0x9003
movq 0x8(%r10), %rax
cmpq 0x28(%r14), %rax
sete %bl
testq %r10, %r10
je 0x9013
movq %r10, %rdi
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