// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/mergegap.c
// Function: addCharToPrefix
static bool addCharToPrefix(solidBlockFile *solidHead, liquidBlock *liquid, customInt 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);
  if(g->extMem) {
    rewind_bw_files(g);  // set file pointers at the beginning of each BWT 
    open_merge_files(g); // open merge file for reading and newmerge files for writing
  }
  // 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
  uint64_t lcpWritten =0;

  protoBlock cblock = {.mono = false};
  solidBlock *next = readBlock(solidHead); // first block
  solidBlock *last = NULL;                 // previous block

  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 is properly initialized
    if(g->lcpMerge) {
      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 && 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 not computing LCPs and not using extMem add it  
      else if(!last_block_recent && cblock.mono==true && !g->lcpCompute && !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; 
        if(!g->extMem) { // monochrome blocks of length>1 are not used in external memory  
          cblock.color = 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=0;   // b in pseudocode
    int currentChar=0;  // c in pseudocode: next char in current BWT 
    if(g->extMem) {
      assert(ftell(g->fmergeColor)==k*sizeof(palette));
      currentColor = fread_color(g->fmergeColor);
      assert(ftell(g->bwf[currentColor])==(g->inCnt[currentColor]+(g->bws[currentColor]-g->bws[0])+g->symb_offset)*sizeof(symbol)); 
      int e = fread(&currentChar,sizeof(symbol),1,g->bwf[currentColor]);
      if(e!=1) die("mergegap:addCharToPrefix:bwt[color]Read"); 
      g->inCnt[currentColor]++;
    }
    else {
      currentColor = g->mergeColor[k];
      currentChar  = g->bws[currentColor][g->inCnt[currentColor]++]; // c in pseudocode
    }
    // add currentChar/Color to proto block  
    cblock.lastChar =  currentChar;   // save lastchar, only useful for singleton blocks
    cblock.lastColor =  currentColor; // save lastcolor, only useful for singleton blocks
    if(!g->extMem)
      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]++;
      if(g->extMem) {
        cwriter_put(&g->fnewMergeColor[currentChar],currentColor);
        assert(cwriter_tell(&g->fnewMergeColor[currentChar])==g->F[currentChar]*sizeof(palette));
        *mergeChanged=true; // this could be a problem....
      }
      else {
        g->newMergeColor[positionToUpdate] = currentColor;
        if(g->bwtOnly && !*mergeChanged && g->mergeColor[positionToUpdate]!=currentColor)
          *mergeChanged=true;  // remember there is a difference from the previous iteration
      }
      // create new block?
      if (blockID[currentChar] != id) {
        if(!g->lcpMerge) { // 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(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->lcpCompute && !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->lcpCompute && 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]);
    
  // swap merge and newMerge
  if(g->extMem) { // close merge files and swap file names 
    assert(ftell(g->fmergeColor)==g->mergeLen*sizeof(palette));
    assert(g->F[g->sizeOfAlpha-1]==g->mergeLen);
    assert(cwriter_tell(&g->fnewMergeColor[g->sizeOfAlpha-1])==g->mergeLen*sizeof(palette));
    close_merge_files(g);
    char *tmp=g->merge_fname; g->merge_fname = g->newmerge_fname; g->newmerge_fname = tmp;
  } else { // swap arrays 
    palette *tmp=g->mergeColor; g->mergeColor=g->newMergeColor; g->newMergeColor=tmp;
  }
  return everything_irrelevant;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, %rbx
movl %r8d, %r14d
movq %rcx, -0xa8(%rbp)
movq %rdx, -0x60(%rbp)
movq %rsi, %r12
movq %rdi, %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rsi)
movq 0x5108(%r9), %rsi
movq 0x5110(%r9), %rdi
movslq 0x38(%r9), %rdx
callq 0x35fb
movq 0x5118(%rbx), %rdi
movslq 0x20(%rbx), %rsi
xorl %edx, %edx
callq 0x2fbc
cmpb $0x1, 0x69(%rbx)
jne 0xa901
movq %rbx, %rdi
callq 0x5389
movq %rbx, %rdi
callq 0xb3ef
movslq 0x38(%rbx), %rsi
movl %esi, %eax
movq %rsp, %rdi
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
movq 0x28(%rbx), %rdx
movq %rdi, -0xb8(%rbp)
callq 0x2fbc
andl $0x1, %r14d
pushq $0x2
popq %rax
subl %r14d, %eax
movq %rax, -0xb0(%rbp)
leaq -0xa0(%rbp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
andq $0x0, 0x20(%rax)
movq %r15, -0x38(%rbp)
movq %r15, %rdi
callq 0xaea5
movq %rax, %r14
movq -0x60(%rbp), %rax
leaq -0x1(%rax), %rcx
movq %rcx, -0xc0(%rbp)
addl $-0x2, %eax
movl %eax, -0x6c(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
xorl %r13d, %r13d
movq $0x0, -0x68(%rbp)
movq %r12, -0xc8(%rbp)
cmpq 0x28(%rbx), %r13
jae 0xad57
movb $0x1, -0x29(%rbp)
movq 0x98(%rbx), %rdi
cmpb $0x1, 0x3d(%rbx)
jne 0xaa12
movzbl (%rdi,%r13), %eax
testq %rax, %rax
setne %cl
cmpq -0x60(%rbp), %rax
setb %dl
testb %dl, %cl
je 0xab24
cmpq %rax, -0xc0(%rbp)
setbe -0x29(%rbp)
jbe 0xaa33
testb $0x1, -0x50(%rbp)
je 0xaa4b
leaq -0x1(%r13), %rax
cmpq %rax, -0xa0(%rbp)
jne 0xacf4
movq %r13, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
movq %r12, %rsi
callq 0xafd6
jmp 0xaa73
movq %r13, %rsi
movq -0xb0(%rbp), %rdx
leaq -0x29(%rbp), %rcx
callq 0xb34d
testb %al, %al
je 0xab24
testb $0x1, -0x29(%rbp)
je 0xa9e3
cmpb $0x1, 0x3e(%rbx)
jne 0xaa4b
movq %r13, %rdi
movl -0x6c(%rbp), %esi
movq %rbx, %rdx
callq 0x36c8
incq -0x58(%rbp)
cmpb $0x0, 0x2c(%r12)
jne 0xaa6a
movq -0x40(%rbp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movq -0x38(%rbp), %rcx
callq 0x628f
movq %rax, -0x40(%rbp)
movq %r13, 0x8(%r12)
movq %r13, (%r12)
testq %r14, %r14
je 0xaac1
cmpq %r13, (%r14)
jne 0xaac1
movq %r14, %rdi
movq %rbx, %rsi
callq 0xb041
movq 0x8(%r14), %r13
movq -0x40(%rbp), %r15
testq %r15, %r15
je 0xacc3
movq 0x8(%r15), %rax
cmpq (%r12), %rax
jne 0xacc3
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq -0x38(%rbp), %r14
movq %r14, %rcx
callq 0xb1e2
jmp 0xad27
cmpb $0x0, -0x29(%rbp)
je 0xaadc
movb $0x0, -0x88(%rbp)
movq $0x0, -0x50(%rbp)
movq %r13, -0x68(%rbp)
jmp 0xab24
movq %r13, -0xa0(%rbp)
movb $0x1, -0x88(%rbp)
movb $0x1, %al
movq %rax, -0x50(%rbp)
cmpb $0x0, 0x69(%rbx)
jne 0xab24
movq 0x50e8(%rbx), %rax
movzbl (%rax,%r13), %eax
movl %eax, -0x84(%rbp)
movq (%rbx), %rcx
shll $0x3, %eax
movq (%rcx,%rax), %rcx
movq 0x5118(%rbx), %rdx
addq (%rdx,%rax), %rcx
movq %rcx, -0x90(%rbp)
andl $0x0, -0x44(%rbp)
movb 0x69(%rbx), %al
cmpb $0x1, %al
jne 0xab7c
movq 0x50c8(%rbx), %rdi
callq 0xb4bd
movq 0x50a0(%rbx), %rcx
movslq %eax, %r15
movq (%rcx,%r15,8), %rcx
leaq -0x44(%rbp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
callq 0x20a0
cmpl $0x1, %eax
je 0xab69
leaq 0x4630(%rip), %rdi      # 0xf194
callq 0x35b7
movq 0x5118(%rbx), %rax
incq (%rax,%r15,8)
movl -0x44(%rbp), %ecx
movb 0x69(%rbx), %al
jmp 0xabb1
movq 0x50e8(%rbx), %rcx
movzbl (%rcx,%r13), %r15d
movq (%rbx), %rcx
leal (,%r15,8), %edx
movq (%rcx,%rdx), %rcx
movq 0x5118(%rbx), %rsi
movq (%rsi,%rdx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%rsi,%rdx)
movzbl (%rcx,%rdi), %ecx
movl %ecx, -0x44(%rbp)
movq %r14, -0xd0(%rbp)
movl %ecx, -0x80(%rbp)
movl %r15d, -0x7c(%rbp)
testb $0x1, %al
jne 0xabdb
cmpl -0x84(%rbp), %r15d
je 0xabdb
movb $0x0, -0x88(%rbp)
movq $0x0, -0x50(%rbp)
testl %ecx, %ecx
je 0xacad
movq 0x5110(%rbx), %rdx
movslq %ecx, %r12
movq (%rdx,%r12,8), %r14
leaq 0x1(%r14), %rcx
movq %rcx, (%rdx,%r12,8)
testb $0x1, %al
je 0xac20
movq %r12, %rdi
shlq $0x5, %rdi
addq 0x50d0(%rbx), %rdi
movzbl %r15b, %esi
callq 0x5a1c
movq -0xa8(%rbp), %rax
movb $0x1, (%rax)
jmp 0xac4e
movq 0x50f0(%rbx), %rax
movb %r15b, (%rax,%r14)
cmpb $0x1, 0x3c(%rbx)
jne 0xac4e
movq -0xa8(%rbp), %rax
cmpb $0x0, (%rax)
jne 0xac4e
movq 0x50e8(%rbx), %rax
movzbl (%rax,%r14), %eax
cmpl %eax, %r15d
jne 0xac14
movq -0xb8(%rbp), %rax
movq -0x68(%rbp), %rcx
cmpq %rcx, (%rax,%r12,8)
je 0xacad
movb -0x29(%rbp), %al
cmpb $0x0, 0x3d(%rbx)
je 0xac84
testb %al, %al
je 0xac9e
movq 0x98(%rbx), %rax
cmpb $0x0, (%rax,%r14)
jne 0xac9e
movq -0x60(%rbp), %rcx
movb %cl, (%rax,%r14)
jmp 0xac9e
testb %al, %al
je 0xac9e
movq 0x98(%rbx), %rdi
movq %r14, %rsi
movq -0xb0(%rbp), %rdx
callq 0xb397
movq -0xb8(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, (%rax,%r12,8)
incq %r13
movq -0xc8(%rbp), %r12
movq -0xd0(%rbp), %r14
jmp 0xa9a1
cmpb $0x0, 0x2c(%r12)
jne 0xacda
movq %r12, %rdi
movq %r14, %rsi
movq -0x38(%rbp), %rdx
callq 0x63c9
movq %r14, %r15
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0xad23
movq -0x38(%rbp), %r14
movq %r14, %rsi
callq 0x648d
jmp 0xad27
cmpb $0x0, 0x3e(%rbx)
jne 0xaa4b
cmpb $0x0, 0x69(%rbx)
jne 0xaa4b
movq %r13, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
movq %r12, %rsi
callq 0xaffe
jmp 0xaa73
movq -0x38(%rbp), %r14
movq %r13, 0x8(%r12)
movq %r13, (%r12)
movq %r14, %rdi
callq 0xaea5
movq %rax, %r14
movq %rax, 0x18(%r15)
movb $0x0, -0x88(%rbp)
movq $0x0, -0x50(%rbp)
movq %r15, -0x40(%rbp)
jmp 0xa9a1
testb $0x1, -0x50(%rbp)
je 0xad9e
leaq -0x1(%r13), %rax
cmpq %rax, -0xa0(%rbp)
jne 0xad7f
leaq -0xa0(%rbp), %rdi
movq %r13, 0x8(%rdi)
movq %r12, %rsi
callq 0xafd6
jmp 0xad9e
cmpb $0x0, 0x3e(%rbx)
jne 0xad9e
cmpb $0x0, 0x69(%rbx)
jne 0xad9e
leaq -0xa0(%rbp), %rdi
movq %r13, 0x8(%rdi)
movq %r12, %rsi
callq 0xaffe
cmpb $0x0, 0x2c(%r12)
je 0xadb4
movq -0x38(%rbp), %r15
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %r12
jmp 0xadd0
movq -0x40(%rbp), %rdi
movq %r12, %rsi
xorl %edx, %edx
movq -0x38(%rbp), %r15
movq %r15, %rcx
callq 0x628f
movq %rax, %r12
movq -0x58(%rbp), %rdi
cmpb $0x1, 0x3e(%rbx)
jne 0xade6
testq %rdi, %rdi
je 0xade6
incq %rdi
movq %rbx, %rsi
callq 0x3748
testq %r12, %r12
je 0xadf7
cmpq $0x0, (%r12)
je 0xadfc
xorl %r14d, %r14d
jmp 0xae09
xorl %r14d, %r14d
jmp 0xae14
movq 0x8(%r12), %rax
cmpq 0x28(%rbx), %rax
sete %r14b
movq %r12, %rdi
movq %r15, %rsi
callq 0x648d
cmpb $0x1, 0x69(%rbx)
jne 0xae39
movq %rbx, %rdi
callq 0xb4eb
movdqu 0x50d8(%rbx), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqu %xmm0, 0x50d8(%rbx)
jmp 0xae4e
movdqu 0x50e8(%rbx), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqu %xmm0, 0x50e8(%rbx)
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq