// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdi, -0x30(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsi)
movq 0x5108(%r9), %rsi
movq 0x5110(%r9), %rdi
movslq 0x38(%r9), %rdx
callq 0x39b7
movq 0x5118(%rbx), %rdi
movslq 0x20(%rbx), %rsi
xorl %edx, %edx
callq 0x3304
cmpb $0x1, 0x69(%rbx)
jne 0xb15a
movq %rbx, %rdi
callq 0x57d9
movq %rbx, %rdi
callq 0xbce3
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 0x3304
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x30(%rbp), %rdi
callq 0xb7e2
cmpq $0x0, 0x28(%rbx)
je 0xb671
movq %rax, %r12
andl $0x1, %r14d
movl $0x2, %ecx
subl %r14d, %ecx
movq -0x88(%rbp), %rax
leaq -0x1(%rax), %rdx
movq %rdx, -0xc0(%rbp)
addl $-0x2, %eax
movl %eax, -0x94(%rbp)
movl %ecx, -0x98(%rbp)
movl %ecx, %eax
movq %rax, -0xb8(%rbp)
movq $0x0, -0x90(%rbp)
xorl %r9d, %r9d
xorl %r10d, %r10d
movq $0x0, -0x38(%rbp)
movq %r15, -0xc8(%rbp)
movq %rbx, -0x80(%rbp)
cmpb $0x1, 0x3d(%rbx)
jne 0xb246
movq 0x98(%rbx), %rax
movb (%rax,%r9), %al
testb %al, %al
setne %cl
movzbl %al, %eax
cmpq -0x88(%rbp), %rax
setb %r14b
andb %cl, %r14b
cmpq %rax, -0xc0(%rbp)
setbe %al
movl %r14d, %r13d
xorb $0x1, %r13b
orb %al, %r13b
jmp 0xb29a
movq 0x98(%rbx), %rax
movq %r9, %rdx
shrq $0x5, %rdx
movq (%rax,%rdx,8), %rsi
leal (%r9,%r9), %ecx
andl $0x3e, %ecx
movq %rsi, %rdi
shrq %cl, %rdi
andl $0x3, %edi
movl %edi, %r8d
xorl -0x98(%rbp), %r8d
cmpl $0x3, %r8d
jne 0xb28f
movl $0x3, %edi
shlq %cl, %rdi
orq %rdi, %rsi
movq %rsi, (%rax,%rdx,8)
movb $0x1, %r13b
movb $0x1, %r14b
jmp 0xb29a
cmpl $0x3, %edi
sete %r14b
setne %r13b
testb %r13b, %r13b
movq %r12, -0x78(%rbp)
je 0xb2dc
testb %r14b, %r14b
je 0xb2dc
cmpb $0x1, 0x3e(%rbx)
jne 0xb2dc
movq %r9, %rdi
movl -0x94(%rbp), %esi
movq %rbx, %rdx
movq %r9, %r12
movq %r15, %rbx
movq %r10, %r15
callq 0x3a94
movq %r15, %r10
movq %rbx, %r15
movq -0x80(%rbp), %rbx
movq %r12, %r9
movq -0x78(%rbp), %r12
incq %r10
testb %r14b, %r14b
movq %r10, -0xb0(%rbp)
je 0xb411
testb %r13b, %r13b
jne 0xb346
cmpb $0x1, -0x58(%rbp)
jne 0xb32b
leaq -0x1(%r9), %rax
cmpq %rax, -0x70(%rbp)
jne 0xb32b
movq %r9, -0x68(%rbp)
movq %r9, 0x8(%r15)
movq 0x20(%r15), %rax
movslq -0x4c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r15), %rcx
movslq -0x50(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r15)
jmp 0xb375
testb %r13b, %r13b
jne 0xb346
cmpb $0x1, -0x58(%rbp)
jne 0xb346
cmpb $0x0, 0x3e(%rbx)
jne 0xb346
cmpb $0x0, 0x69(%rbx)
je 0xb652
cmpb $0x0, 0x2c(%r15)
jne 0xb36e
movq -0x38(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq -0x30(%rbp), %rcx
movq %r9, %r14
callq 0x676c
movq -0x80(%rbp), %rbx
movq %r14, %r9
movq %rax, -0x38(%rbp)
movq %r9, 0x8(%r15)
movq %r9, (%r15)
testq %r12, %r12
je 0xb3c9
cmpq %r9, (%r12)
jne 0xb3c9
movq %r12, %rdi
movq %rbx, %rsi
callq 0xb96b
movq 0x8(%r12), %rcx
movq -0x38(%rbp), %r12
testq %r12, %r12
movq %rcx, %r13
je 0xb5ea
movq 0x8(%r12), %rax
cmpq (%r15), %rax
jne 0xb5ea
movq %r12, %rdi
movq %r15, %rsi
movq -0x78(%rbp), %rdx
movq -0x30(%rbp), %r14
movq %r14, %rcx
callq 0xbb2a
jmp 0xb61c
testb %r13b, %r13b
je 0xb3db
movb $0x0, -0x58(%rbp)
movq %r9, -0x90(%rbp)
jmp 0xb411
movq %r9, -0x70(%rbp)
movb $0x1, -0x58(%rbp)
cmpb $0x0, 0x69(%rbx)
jne 0xb411
movq 0x50e8(%rbx), %rax
movzbl (%rax,%r9), %eax
movl %eax, -0x54(%rbp)
movq (%rbx), %rcx
shll $0x3, %eax
movq (%rcx,%rax), %rcx
movq 0x5118(%rbx), %rdx
addq (%rdx,%rax), %rcx
movq %rcx, -0x60(%rbp)
movl $0x0, -0x3c(%rbp)
cmpb $0x1, 0x69(%rbx)
jne 0xb46f
movq %r9, %r15
movq 0x50c8(%rbx), %rdi
callq 0xbdbe
movq 0x50a0(%rbx), %rcx
movslq %eax, %r14
movq (%rcx,%r14,8), %rcx
movl $0x1, %esi
movl $0x1, %edx
leaq -0x3c(%rbp), %rdi
callq 0x20a0
cmpl $0x1, %eax
je 0xb45f
leaq 0x4d82(%rip), %rdi      # 0x101dc
callq 0x3973
movq 0x5118(%rbx), %rax
incq (%rax,%r14,8)
movq %r15, %r9
jmp 0xb4a4
movq 0x50e8(%rbx), %rax
movzbl (%rax,%r9), %r14d
movq (%rbx), %rax
leal (,%r14,8), %ecx
movq (%rax,%rcx), %rax
movq 0x5118(%rbx), %rdx
movq (%rdx,%rcx), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, (%rdx,%rcx)
movzbl (%rax,%rsi), %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%rbp), %r15
movl %r15d, -0x50(%rbp)
movl %r14d, -0x4c(%rbp)
movb 0x69(%rbx), %al
testb %al, %al
jne 0xb4c1
cmpl -0x54(%rbp), %r14d
je 0xb4c1
movb $0x0, -0x58(%rbp)
testl %r15d, %r15d
je 0xb5c6
movq 0x5110(%rbx), %rcx
movq (%rcx,%r15,8), %r12
leaq 0x1(%r12), %rdx
movq %rdx, (%rcx,%r15,8)
testb %al, %al
je 0xb50b
movq %r15, %rdi
shlq $0x5, %rdi
addq 0x50d0(%rbx), %rdi
movzbl %r14b, %esi
movq %r9, %r14
callq 0x5ec7
movq %r14, %r9
movq -0xa0(%rbp), %rax
movb $0x1, (%rax)
jmp 0xb539
movq 0x50f0(%rbx), %rax
movb %r14b, (%rax,%r12)
cmpb $0x1, 0x3c(%rbx)
jne 0xb539
movq -0xa0(%rbp), %rax
cmpb $0x0, (%rax)
jne 0xb539
movq 0x50e8(%rbx), %rax
movzbl (%rax,%r12), %eax
cmpl %eax, %r14d
jne 0xb4ff
movq -0xa8(%rbp), %rax
movq -0x90(%rbp), %rcx
cmpq %rcx, (%rax,%r15,8)
je 0xb5c6
cmpb $0x0, 0x3d(%rbx)
je 0xb573
testb %r13b, %r13b
je 0xb5b4
movq 0x98(%rbx), %rax
cmpb $0x0, (%rax,%r12)
jne 0xb5b4
movq -0x88(%rbp), %rcx
movb %cl, (%rax,%r12)
jmp 0xb5b4
testb %r13b, %r13b
je 0xb5b4
movq 0x98(%rbx), %rax
movq %r12, %rdx
shrq $0x5, %rdx
movq (%rax,%rdx,8), %rsi
addl %r12d, %r12d
andl $0x3e, %r12d
movq %rsi, %rdi
movl %r12d, %ecx
shrq %cl, %rdi
testb $0x3, %dil
jne 0xb5b4
movq -0xb8(%rbp), %rdi
movl %r12d, %ecx
shlq %cl, %rdi
orq %rdi, %rsi
movq %rsi, (%rax,%rdx,8)
movq -0xa8(%rbp), %rax
movq -0x90(%rbp), %rcx
movq %rcx, (%rax,%r15,8)
incq %r9
movq -0xc8(%rbp), %r15
movq -0xb0(%rbp), %r10
movq -0x78(%rbp), %r12
cmpq 0x28(%rbx), %r9
jb 0xb209
jmp 0xb67f
cmpb $0x0, 0x2c(%r15)
movq -0x78(%rbp), %r12
jne 0xb607
movq %r15, %rdi
movq %r12, %rsi
movq -0x30(%rbp), %rdx
callq 0x689e
movq %r13, %rcx
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0xb624
movq -0x30(%rbp), %r14
movq %r14, %rsi
callq 0x6950
movq %r13, %rcx
movq %r14, %rdi
jmp 0xb628
movq -0x30(%rbp), %rdi
movq %r12, %r14
movq %rcx, 0x8(%r15)
movq %rcx, (%r15)
callq 0xb7e2
movq %r13, %r9
movq %rax, %r12
movq %r14, -0x38(%rbp)
movq %rax, 0x18(%r14)
movb $0x0, -0x58(%rbp)
movq -0xb0(%rbp), %r10
jmp 0xb5db
movq %r9, -0x68(%rbp)
leaq -0x70(%rbp), %rdi
movq %r15, %rsi
movq %r9, %r14
callq 0xb920
movq -0x80(%rbp), %rbx
movq %r14, %r9
jmp 0xb375
movq $0x0, -0x38(%rbp)
xorl %r10d, %r10d
xorl %r9d, %r9d
movb -0x58(%rbp), %al
cmpb $0x1, %al
jne 0xb6e0
leaq -0x1(%r9), %rcx
cmpq %rcx, -0x70(%rbp)
jne 0xb6ba
movq %r9, -0x68(%rbp)
movq %r9, 0x8(%r15)
movq 0x20(%r15), %rax
movslq -0x4c(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r15), %rcx
movslq -0x50(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r15)
jmp 0xb6e0
testb %al, %al
je 0xb6e0
cmpb $0x0, 0x3e(%rbx)
jne 0xb6e0
cmpb $0x0, 0x69(%rbx)
jne 0xb6e0
leaq -0x70(%rbp), %rdi
movq %r9, 0x8(%rdi)
movq %r15, %rsi
movq %r10, %r14
callq 0xb920
movq %r14, %r10
cmpb $0x0, 0x2c(%r15)
je 0xb6ed
movq -0x38(%rbp), %r15
jmp 0xb708
movq -0x38(%rbp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq -0x30(%rbp), %rcx
movq %r10, %r14
callq 0x676c
movq %r14, %r10
movq %rax, %r15
testq %r10, %r10
setne %al
andb 0x3e(%rbx), %al
cmpb $0x1, %al
jne 0xb723
incq %r10
movq %r10, %rdi
movq %rbx, %rsi
callq 0x3afe
testq %r15, %r15
movq -0x30(%rbp), %rsi
je 0xb732
cmpq $0x0, (%r15)
je 0xb737
xorl %r14d, %r14d
jmp 0xb743
movq 0x8(%r15), %rax
cmpq 0x28(%rbx), %rax
sete %r14b
testq %r15, %r15
je 0xb750
movq %r15, %rdi
callq 0x6950
cmpb $0x1, 0x69(%rbx)
jne 0xb76a
movq %rbx, %rdi
callq 0xbdf3
movl $0x50e0, %eax           # imm = 0x50E0
movl $0x50d8, %ecx           # imm = 0x50D8
jmp 0xb774
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