// File: /workspace/github_c_makefile_star2_O0/felipelouza[P]egap/./merge128ext.h
// Function: addCharToPrefix128ext
static bool addCharToPrefix128ext(solidBlockFile *solidHead, liquidBlock *liquid, uint32_t prefixLength, bitfile *b, 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);
  rewind_bw_files(g);  // set file pointers at the beginning of each BWT 
  bitfile_rewind(b);    
  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; 
  uint64_t lcpWritten =0;

  protoBlock cblock = {.beginsAt = 0};
  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
    // read newblock & color
    int currentColor = fread_color(g->fmergeColor);// read color and new block bit 
    bool new_block = ((currentColor & 0x80)!=0);   // extract new block bit, it is set if a block starts here
    currentColor &= 0x7F;                          // delete new block bit from color
    // read the old block bit: it is set if the block is at least 2 iterations old
    assert(bitfile_tell(b)==k);
    bool old_block = bitfile_read_or_write(b,new_block);// read bit and simultaneouly set it if new_block==true
    if(new_block && !old_block && g->lcpCompute) // the block was created exactly during the previous iteration 
      {writeLcp(k,prefixLength-2,g); lcpWritten++;} // save lcp value found in previous iteration
    if (old_block) {
      // if the block we just left is a singleton we add it to liquid that remains active
      if(cblock.beginsAt==k-1) {
        cblock.endsAt = k;           // solidifiable singleton block just ended
        add_singleton2liquid(&cblock, liquid);
      }
      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 solid 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
        skip128ext(next, b, g);             // skip block
        k = next->endsAt;                   // update k (note after this there is a continue)
        // 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);
        continue;                   // resume from the end of the block 
      }  // end skip solid block
      // we are entering a relevant block: make it a candidate for solidification  
      cblock.beginsAt = k; 
    }   // end if(new_block || old_block)
    // processing a char in a relevant block
    assert(ftell(g->fmergeColor)==(k+1)*sizeof(palette));   // we already have currentColor but we check the file pointer is at the right position  
    assert(ftell(g->bwf[currentColor])==(g->inCnt[currentColor]+(g->bws[currentColor]-g->bws[0])+g->symb_offset)*sizeof(symbol)); 
    int currentChar=0;
    int e = fread(&currentChar,sizeof(symbol),1,g->bwf[currentColor]);
    if(e!=1) die("mergegap:addCharToPrefix:bwt[color]Read"); 
    g->inCnt[currentColor]++;
    // 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

    // write color and newblock bit in newMerge array, except 0 chars
    if(new_block) id = k;
    if(currentChar!=0) {
      int bit = (blockID[currentChar] != id) ? 1 : 0;
      cwriter_put(&g->fnewMergeColor[currentChar],currentColor|(bit<<7));
      g->F[currentChar]++; 
      assert(cwriter_tell(&g->fnewMergeColor[currentChar])==g->F[currentChar]*sizeof(palette));
      blockID[currentChar] = id; // update block id, always!
    }
    k++;
  } // end main loop
  assert(bitfile_tell(b)==g->mergeLen);
  bitfile_flush(b);    // save to file last bits 
  assert(k==g->mergeLen);
  assert(next==NULL); 
  if(cblock.beginsAt==k-1) {
    cblock.endsAt = k;           // solidifiable singleton block just ended
    add_singleton2liquid(&cblock, liquid);
    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
  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;
  return everything_irrelevant;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0xd565
jmp 0xd55a
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xd54c
movq -0x28(%rbp), %rax
movq 0x5108(%rax), %rsi
movq 0x5110(%rax), %rdi
movslq 0x38(%rax), %rdx
callq 0x3d70
movq -0x28(%rbp), %rax
movq 0x5118(%rax), %rdi
movslq 0x20(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x3310
movq -0x28(%rbp), %rdi
callq 0x6e70
movq -0x20(%rbp), %rdi
callq 0x7e40
movq -0x28(%rbp), %rdi
callq 0x11450
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x38(%rbp)
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0xc0(%rbp)
movq %rdi, %rsp
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movslq 0x38(%rax), %rsi
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x3310
movq $0x0, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x2140
movq -0x8(%rbp), %rdi
callq 0x10650
movq %rax, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jae 0xd999
movq -0x28(%rbp), %rax
movq 0x50c8(%rax), %rdi
callq 0x11580
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x95(%rbp)
movl -0x94(%rbp), %eax
andl $0x7f, %eax
movl %eax, -0x94(%rbp)
movq -0x20(%rbp), %rdi
movb -0x95(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x7e70
andb $0x1, %al
movb %al, -0x96(%rbp)
testb $0x1, -0x95(%rbp)
je 0xd6e0
testb $0x1, -0x96(%rbp)
jne 0xd6e0
movq -0x28(%rbp), %rax
testb $0x1, 0x3e(%rax)
je 0xd6e0
movq -0x50(%rbp), %rdi
movl -0x14(%rbp), %esi
subl $0x2, %esi
movq -0x28(%rbp), %rdx
callq 0x4000
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
testb $0x1, -0x96(%rbp)
je 0xd85c
movq -0x80(%rbp), %rax
movq -0x50(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0xd715
movq -0x50(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x10950
jmp 0xd754
movq -0x10(%rbp), %rax
testb $0x1, 0x2c(%rax)
jne 0xd741
movq -0x90(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq -0x8(%rbp), %rcx
callq 0x8a10
movq %rax, -0x90(%rbp)
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, -0x88(%rbp)
je 0xd854
movq -0x88(%rbp), %rax
movq (%rax), %rax
cmpq -0x50(%rbp), %rax
jne 0xd854
movq -0x88(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x115d0
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x90(%rbp)
je 0xd7b7
movq -0x90(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
je 0xd7ff
movq -0x10(%rbp), %rax
testb $0x1, 0x2c(%rax)
jne 0xd7d5
movq -0x10(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0x8af0
cmpq $0x0, -0x90(%rbp)
je 0xd7ef
movq -0x90(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x8db0
movq -0x88(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0xd81a
movq -0x90(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq -0x8(%rbp), %rcx
callq 0x10e30
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
callq 0x10650
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0xd639
movq -0x50(%rbp), %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x9c(%rbp)
movq -0x28(%rbp), %rax
movq 0x50a0(%rax), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
leaq -0x9c(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2090
movl %eax, -0xa0(%rbp)
cmpl $0x1, -0xa0(%rbp)
je 0xd8ab
leaq 0x8982(%rip), %rdi      # 0x16228
callq 0x3ce0
movq -0x28(%rbp), %rax
movq 0x5118(%rax), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x9c(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x5c(%rbp)
testb $0x1, -0x95(%rbp)
je 0xd8ec
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x9c(%rbp)
je 0xd988
movq -0xc0(%rbp), %rax
movslq -0x9c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x48(%rbp), %rsi
xorl %eax, %eax
movl $0x1, %ecx
cmpq %rsi, %rdx
cmovnel %ecx, %eax
movl %eax, -0xa4(%rbp)
movq -0x28(%rbp), %rax
movq 0x50d0(%rax), %rdi
movslq -0x9c(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
movl -0x94(%rbp), %eax
movl -0xa4(%rbp), %ecx
shll $0x7, %ecx
orl %ecx, %eax
movzbl %al, %esi
callq 0x7960
movq -0xc0(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x5110(%rcx), %rcx
movslq -0x9c(%rbp), %rdx
movq (%rcx,%rdx,8), %rsi
addq $0x1, %rsi
movq %rsi, (%rcx,%rdx,8)
movq -0x48(%rbp), %rdx
movslq -0x9c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0xd639
movq -0x20(%rbp), %rdi
callq 0x7b30
movq -0x80(%rbp), %rax
movq -0x50(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0xd9c8
movq -0x50(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x10950
movq -0x10(%rbp), %rax
testb $0x1, 0x2c(%rax)
jne 0xd9f1
movq -0x90(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
callq 0x8a10
movq %rax, -0x90(%rbp)
movq -0x28(%rbp), %rax
testb $0x1, 0x3e(%rax)
je 0xda17
cmpq $0x0, -0x58(%rbp)
jbe 0xda17
movq -0x58(%rbp), %rdi
addq $0x1, %rdi
movq %rdi, -0x58(%rbp)
movq -0x28(%rbp), %rsi
callq 0x40c0
movb $0x0, -0xa5(%rbp)
cmpq $0x0, -0x90(%rbp)
je 0xda51
movq -0x90(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xda51
movq -0x90(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jne 0xda51
movb $0x1, -0xa5(%rbp)
cmpq $0x0, -0x90(%rbp)
je 0xda6b
movq -0x90(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x8db0
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0xda97
jmp 0xda86
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xda75
movq -0x28(%rbp), %rdi
callq 0x11990
movq -0x28(%rbp), %rax
movq 0x50d8(%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rax
movq 0x50e0(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x50d8(%rax)
movq -0xb8(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x50e0(%rax)
movb -0xa5(%rbp), %al
movq -0x38(%rbp), %rcx
movq %rcx, %rsp
andb $0x1, %al
movq %rbp, %rsp
popq %rbp
retq
nop