// File: /workspace/github_c_makefile_star2_O3/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, -0xa0(%rbp)
movq %rdx, -0x88(%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 0x3a40
movq 0x5118(%rbx), %rdi
movslq 0x20(%rbx), %rsi
xorl %edx, %edx
callq 0x33a4
cmpb $0x1, 0x69(%rbx)
jne 0xb247
movq %rbx, %rdi
callq 0x5883
movq %rbx, %rdi
callq 0xbd8b
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, -0xa8(%rbp)
callq 0x33a4
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x60(%rbp)
movq %r15, %rdi
callq 0xb878
cmpq $0x0, 0x28(%rbx)
je 0xb755
movq %rax, %rsi
movq %r15, -0x30(%rbp)
andl $0x1, %r14d
movl $0x2, %ecx
subl %r14d, %ecx
movq -0x88(%rbp), %rax
leaq -0x1(%rax), %rdx
movq %rdx, -0xb8(%rbp)
addl $-0x2, %eax
movl %eax, -0x94(%rbp)
movl %ecx, -0x98(%rbp)
movl %ecx, %eax
movq %rax, -0xb0(%rbp)
xorl %r9d, %r9d
movq $0x0, -0x90(%rbp)
xorl %r13d, %r13d
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq %r12, -0xc0(%rbp)
movq %rsi, %r14
movq 0x98(%rbx), %rax
cmpb $0x1, 0x3d(%rbx)
jne 0xb338
movzbl (%rax,%r13), %eax
testq %rax, %rax
setne %cl
cmpq -0x88(%rbp), %rax
setb %dl
movb $0x1, %r15b
testb %dl, %cl
je 0xb4ba
cmpq %rax, -0xb8(%rbp)
jbe 0xb376
jmp 0xb3a2
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 -0x98(%rbp), %r8d
cmpl $0x3, %r8d
jne 0xb396
movl $0x3, %edi
shlq %cl, %rdi
orq %rdi, %rsi
movq %rsi, (%rax,%rdx,8)
movb $0x1, %r15b
cmpb $0x1, 0x3e(%rbx)
jne 0xb3f6
movq %r13, %rdi
movl -0x94(%rbp), %esi
movq %rbx, %rdx
callq 0x3b1c
incq -0x48(%rbp)
jmp 0xb3f6
movb $0x1, %r15b
cmpl $0x3, %edi
jne 0xb4ba
testb $0x1, %r9b
je 0xb3f3
leaq -0x1(%r13), %rax
cmpq %rax, -0x80(%rbp)
jne 0xb3e3
movq %r13, -0x78(%rbp)
movq %r13, 0x8(%r12)
movq 0x20(%r12), %rax
movslq -0x5c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r12), %rcx
movslq -0x60(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r12)
xorl %r15d, %r15d
jmp 0xb41e
cmpb $0x0, 0x3e(%rbx)
jne 0xb3f3
cmpb $0x0, 0x69(%rbx)
je 0xb702
xorl %r15d, %r15d
cmpb $0x0, 0x2c(%r12)
jne 0xb415
movq -0x38(%rbp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movq -0x30(%rbp), %rcx
callq 0x6839
movq %rax, -0x38(%rbp)
movq %r13, 0x8(%r12)
movq %r13, (%r12)
testq %r14, %r14
je 0xb46c
cmpq %r13, (%r14)
jne 0xb46c
movq %r14, %rdi
movq %rbx, %rsi
callq 0xba02
movq 0x8(%r14), %r13
movq -0x38(%rbp), %r15
testq %r15, %r15
je 0xb637
movq 0x8(%r15), %rax
cmpq (%r12), %rax
jne 0xb637
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq -0x30(%rbp), %r14
movq %r14, %rcx
callq 0xbbcb
jmp 0xb6dd
testb %r15b, %r15b
je 0xb481
movb $0x0, -0x68(%rbp)
xorl %r9d, %r9d
movq %r13, -0x90(%rbp)
jmp 0xb4ba
movq %r13, -0x80(%rbp)
movb $0x1, -0x68(%rbp)
movb $0x1, %r9b
cmpb $0x0, 0x69(%rbx)
jne 0xb4ba
movq 0x50e8(%rbx), %rax
movzbl (%rax,%r13), %eax
movl %eax, -0x64(%rbp)
movq (%rbx), %rcx
shll $0x3, %eax
movq (%rcx,%rax), %rcx
movq 0x5118(%rbx), %rdx
addq (%rdx,%rax), %rcx
movq %rcx, -0x70(%rbp)
movl %r15d, -0x4c(%rbp)
movl $0x0, -0x3c(%rbp)
movb 0x69(%rbx), %al
cmpb $0x1, %al
jne 0xb523
movq %r9, %r12
movq 0x50c8(%rbx), %rdi
callq 0xbe6d
movq 0x50a0(%rbx), %rcx
movslq %eax, %r15
movq (%rcx,%r15,8), %rcx
movl $0x1, %esi
movl $0x1, %edx
leaq -0x3c(%rbp), %rdi
callq 0x20a0
cmpl $0x1, %eax
je 0xb50d
leaq 0x4cd4(%rip), %rdi      # 0x101dc
callq 0x39fc
movq 0x5118(%rbx), %rax
incq (%rax,%r15,8)
movl -0x3c(%rbp), %ecx
movb 0x69(%rbx), %al
movq %r12, %r9
jmp 0xb558
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, -0x3c(%rbp)
movl %ecx, -0x60(%rbp)
movl %r15d, -0x5c(%rbp)
testb $0x1, %al
movq %r14, -0xc8(%rbp)
jne 0xb577
cmpl -0x64(%rbp), %r15d
je 0xb577
movb $0x0, -0x68(%rbp)
xorl %r9d, %r9d
testl %ecx, %ecx
je 0xb6bc
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 0xb5c2
movq %r12, %rdi
shlq $0x5, %rdi
addq 0x50d0(%rbx), %rdi
movzbl %r15b, %esi
movq %r9, %r15
callq 0x5f89
movq %r15, %r9
movq -0xa0(%rbp), %rax
movb $0x1, (%rax)
jmp 0xb5f0
movq 0x50f0(%rbx), %rax
movb %r15b, (%rax,%r14)
cmpb $0x1, 0x3c(%rbx)
jne 0xb5f0
movq -0xa0(%rbp), %rax
cmpb $0x0, (%rax)
jne 0xb5f0
movq 0x50e8(%rbx), %rax
movzbl (%rax,%r14), %eax
cmpl %eax, %r15d
jne 0xb5b6
movq -0xa8(%rbp), %rax
movq -0x90(%rbp), %rcx
cmpq %rcx, (%rax,%r12,8)
je 0xb6bc
cmpb $0x0, 0x3d(%rbx)
je 0xb668
cmpb $0x0, -0x4c(%rbp)
je 0xb6aa
movq 0x98(%rbx), %rax
cmpb $0x0, (%rax,%r14)
jne 0xb6aa
movq -0x88(%rbp), %rcx
movb %cl, (%rax,%r14)
jmp 0xb6aa
cmpb $0x0, 0x2c(%r12)
jne 0xb64e
movq %r12, %rdi
movq %r14, %rsi
movq -0x30(%rbp), %rdx
callq 0x6967
movq %r14, %r15
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0xb6d9
movq -0x30(%rbp), %r14
movq %r14, %rsi
callq 0x6a18
jmp 0xb6dd
cmpb $0x0, -0x4c(%rbp)
je 0xb6aa
movq 0x98(%rbx), %rax
movq %r14, %rdx
shrq $0x5, %rdx
movq (%rax,%rdx,8), %rsi
addl %r14d, %r14d
andl $0x3e, %r14d
movq %rsi, %rdi
movl %r14d, %ecx
shrq %cl, %rdi
testb $0x3, %dil
jne 0xb6aa
movq -0xb0(%rbp), %rdi
movl %r14d, %ecx
shlq %cl, %rdi
orq %rdi, %rsi
movq %rsi, (%rax,%rdx,8)
movq -0xa8(%rbp), %rax
movq -0x90(%rbp), %rcx
movq %rcx, (%rax,%r12,8)
incq %r13
movq -0xc0(%rbp), %r12
movq -0xc8(%rbp), %r14
cmpq 0x28(%rbx), %r13
jb 0xb300
jmp 0xb717
movq -0x30(%rbp), %r14
movq %r13, 0x8(%r12)
movq %r13, (%r12)
movq %r14, %rdi
callq 0xb878
movq %rax, %r14
movq %r15, -0x38(%rbp)
movq %rax, 0x18(%r15)
movb $0x0, -0x68(%rbp)
xorl %r9d, %r9d
jmp 0xb6cd
movq %r13, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
movq %r12, %rsi
callq 0xb9b7
jmp 0xb3de
testb $0x1, %r9b
je 0xb779
leaq -0x1(%r13), %rax
cmpq %rax, -0x80(%rbp)
jne 0xb75d
movq %r13, -0x78(%rbp)
movq %r13, 0x8(%r12)
movq 0x20(%r12), %rax
movslq -0x5c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r12), %rcx
movslq -0x60(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r12)
jmp 0xb779
xorl %r14d, %r14d
xorl %r13d, %r13d
jmp 0xb785
cmpb $0x0, 0x3e(%rbx)
jne 0xb779
cmpb $0x0, 0x69(%rbx)
jne 0xb779
leaq -0x80(%rbp), %rdi
movq %r13, 0x8(%rdi)
movq %r12, %rsi
callq 0xb9b7
movq -0x30(%rbp), %r15
movq -0x48(%rbp), %r14
movq -0x38(%rbp), %r13
cmpb $0x0, 0x2c(%r12)
jne 0xb7a0
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0x6839
movq %rax, %r13
cmpb $0x1, 0x3e(%rbx)
jne 0xb7b9
testq %r14, %r14
je 0xb7b9
incq %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3b86
testq %r13, %r13
je 0xb7ca
cmpq $0x0, (%r13)
je 0xb7cf
xorl %r14d, %r14d
jmp 0xb7db
xorl %r14d, %r14d
jmp 0xb7e6
movq 0x8(%r13), %rax
cmpq 0x28(%rbx), %rax
sete %r14b
movq %r13, %rdi
movq %r15, %rsi
callq 0x6a18
cmpb $0x1, 0x69(%rbx)
jne 0xb800
movq %rbx, %rdi
callq 0xbea2
movl $0x50e0, %eax           # imm = 0x50E0
movl $0x50d8, %ecx           # imm = 0x50D8
jmp 0xb80a
movl $0x50f0, %eax           # imm = 0x50F0
movl $0x50e8, %ecx           # imm = 0x50E8
movq (%rbx,%rcx), %rdx
movq (%rbx,%rax), %rsi
movq %rsi, (%rbx,%rcx)
movq %rdx, (%rbx,%rax)
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq