// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, %rbx
movq %rcx, %r15
movl %edx, %r12d
movq %rdi, %r14
xorps %xmm0, %xmm0
movq %rsi, -0x38(%rbp)
movups %xmm0, (%rsi)
movq 0x5108(%r8), %rsi
movq 0x5110(%r8), %rdi
movslq 0x38(%r8), %rdx
callq 0x35fb
movq 0x5118(%rbx), %rdi
movslq 0x20(%rbx), %rsi
xorl %edx, %edx
callq 0x2fbc
movq %rbx, %rdi
callq 0x5389
movq %r15, -0x58(%rbp)
movq %r15, %rdi
callq 0x5c94
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, -0x78(%rbp)
callq 0x2fbc
leaq -0xa0(%rbp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
andq $0x0, 0x20(%rax)
movq %r14, -0x40(%rbp)
movq %r14, %rdi
callq 0xaea5
movq %rax, -0x30(%rbp)
addl $-0x2, %r12d
movl %r12d, -0x64(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x60(%rbp)
xorl %r12d, %r12d
xorl %eax, %eax
cmpq 0x28(%rbx), %r12
jae 0x9408
movq %rax, -0x70(%rbp)
movq 0x50c8(%rbx), %rdi
callq 0xb4bd
movl %eax, %r15d
movl %eax, %esi
andl $0x80, %esi
sete %r14b
shrl $0x7, %esi
movq -0x58(%rbp), %rdi
callq 0x5c9f
movl %eax, %r13d
orb %al, %r14b
testb $0x1, %r14b
jne 0x9086
cmpb $0x1, 0x3e(%rbx)
jne 0x9086
movq %r12, %rdi
movl -0x64(%rbp), %esi
movq %rbx, %rdx
callq 0x36c8
incq -0x60(%rbp)
testb $0x1, %r13b
je 0x90f6
leaq -0x1(%r12), %rax
cmpq -0xa0(%rbp), %rax
jne 0x90b3
movq %r12, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0xafd6
jmp 0x90dd
movq -0x38(%rbp), %rsi
cmpb $0x0, 0x2c(%rsi)
jne 0x90d6
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0x628f
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rsi
movq %r12, 0x8(%rsi)
movq %r12, (%rsi)
movq -0x30(%rbp), %r14
testq %r14, %r14
je 0x90ef
cmpq (%r14), %r12
je 0x919a
movq %r12, -0xa0(%rbp)
movl %r15d, %r13d
andl $0x0, -0x4c(%rbp)
andl $0x7f, %r13d
movq 0x50a0(%rbx), %rax
movl %r13d, %r14d
movq (%rax,%r14,8), %rcx
leaq -0x4c(%rbp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
callq 0x20a0
cmpl $0x1, %eax
je 0x912f
leaq 0x606a(%rip), %rdi      # 0xf194
callq 0x35b7
movq 0x5118(%rbx), %rax
incq (%rax,%r14,8)
testb %r15b, %r15b
movq -0x70(%rbp), %rax
cmovsq %r12, %rax
movslq -0x4c(%rbp), %r15
movl %r15d, -0x80(%rbp)
movl %r13d, -0x7c(%rbp)
testq %r15, %r15
je 0x9192
xorl %esi, %esi
movq -0x78(%rbp), %r14
cmpq %rax, (%r14,%r15,8)
setne %sil
movq %r15, %rdi
shlq $0x5, %rdi
addq 0x50d0(%rbx), %rdi
shll $0x7, %esi
orl %r13d, %esi
movq %rax, %r13
callq 0x5a1c
movq 0x5110(%rbx), %rax
incq (%rax,%r15,8)
movq %r13, %rax
movq %r13, (%r14,%r15,8)
incq %r12
jmp 0x902d
movq 0x50c8(%rbx), %rdi
notq %r12
addq 0x8(%r14), %r12
movq %r12, %rsi
pushq $0x1
popq %r12
movl %r12d, %edx
callq 0x2260
movq 0x8(%r14), %rax
subq (%r14), %rax
xorl %r15d, %r15d
cmpq $0x10000, %rax          # imm = 0x10000
jae 0x920c
movslq 0x20(%rbx), %rax
cmpq %rax, %r15
jge 0x9251
movq 0x10(%r14), %rax
movzwl (%rax,%r15,2), %eax
testq %rax, %rax
je 0x9207
movq 0x5118(%rbx), %rcx
addq %rax, (%rcx,%r15,8)
movq 0x50a0(%rbx), %rax
movq (%rax,%r15,8), %rdi
movq 0x10(%r14), %rax
movzwl (%rax,%r15,2), %esi
movl %r12d, %edx
callq 0x2260
incq %r15
jmp 0x91c9
movslq 0x20(%rbx), %rax
cmpq %rax, %r15
jge 0x92b0
movq 0x10(%r14), %rax
movq (%rax,%r15,8), %rax
testq %rax, %rax
je 0x924c
movq 0x5118(%rbx), %rcx
addq %rax, (%rcx,%r15,8)
movq 0x50a0(%rbx), %rax
movq (%rax,%r15,8), %rdi
movq 0x10(%r14), %rax
movq (%rax,%r15,8), %rsi
movl %r12d, %edx
callq 0x2260
incq %r15
jmp 0x920c
xorl %r15d, %r15d
xorl %r12d, %r12d
movslq 0x38(%rbx), %rax
cmpq %rax, %r12
jge 0x930b
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movslq 0x20(%rbx), %rax
addq %r12, %rax
movzwl (%rcx,%rax,2), %ecx
testq %rcx, %rcx
je 0x92a7
movq 0x5110(%rbx), %rdx
addq %rcx, (%rdx,%r12,8)
testq %r15, %r15
je 0x92a7
movq 0x50d0(%rbx), %rdi
addq %r15, %rdi
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movzwl (%rcx,%rax,2), %esi
callq 0x5a7b
incq %r12
addq $0x20, %r15
jmp 0x9257
xorl %r15d, %r15d
xorl %r12d, %r12d
movslq 0x38(%rbx), %rax
cmpq %rax, %r12
jge 0x930b
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movslq 0x20(%rbx), %rax
addq %r12, %rax
movq (%rcx,%rax,8), %rcx
testq %rcx, %rcx
je 0x9302
movq 0x5110(%rbx), %rdx
addq %rcx, (%rdx,%r12,8)
testq %r15, %r15
je 0x9302
movq 0x50d0(%rbx), %rdi
addq %r15, %rdi
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx,%rax,8), %rsi
callq 0x5a7b
incq %r12
addq $0x20, %r15
jmp 0x92b6
movq -0x58(%rbp), %r14
movq %r14, %rdi
callq 0x5dc9
movq %rax, %r15
movq -0x30(%rbp), %r13
movq (%r13), %rsi
notq %rsi
addq 0x8(%r13), %rsi
movq %r14, %rdi
callq 0x5d2c
movq %r14, %rdi
callq 0x5dc9
movq %rax, %r12
cmpq 0x8(%r13), %rax
je 0x936e
movq -0x58(%rbp), %rdi
callq 0x5dc9
movq -0x30(%rbp), %r14
movq (%r14), %rcx
movq 0x8(%r14), %r8
leaq 0x5e8a(%rip), %rdi      # 0xf1e7
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2100
movq 0x8(%r14), %r12
movq -0x48(%rbp), %rcx
testq %rcx, %rcx
movq -0x38(%rbp), %r15
je 0x93a3
movq 0x8(%rcx), %rax
cmpq (%r15), %rax
jne 0x93a3
movq %rcx, %rdi
movq %rcx, %r13
movq %r15, %rsi
movq -0x30(%rbp), %rdx
movq -0x40(%rbp), %r14
movq %r14, %rcx
callq 0xb1e2
movq %r13, -0x30(%rbp)
jmp 0x93e0
cmpb $0x0, 0x2c(%r15)
jne 0x93c2
movq %r15, %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x63c9
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %r15
testq %rcx, %rcx
je 0x93dc
movq %rcx, %rdi
movq -0x40(%rbp), %r14
movq %r14, %rsi
callq 0x648d
movq -0x38(%rbp), %r15
jmp 0x93e0
movq -0x40(%rbp), %r14
movq %r12, 0x8(%r15)
movq %r12, (%r15)
movq %r14, %rdi
callq 0xaea5
movq -0x30(%rbp), %rcx
movq %rax, 0x18(%rcx)
movq %rcx, -0x48(%rbp)
movq %rax, -0x30(%rbp)
movq -0x70(%rbp), %rax
jmp 0x902d
movq -0x58(%rbp), %rdi
callq 0x5afc
movq -0xa0(%rbp), %rax
incq %rax
cmpq %r12, %rax
jne 0x9434
leaq -0xa0(%rbp), %rdi
movq %r12, 0x8(%rdi)
movq -0x38(%rbp), %rsi
callq 0xafd6
movq -0x38(%rbp), %rsi
cmpb $0x0, 0x2c(%rsi)
je 0x944c
movq -0x40(%rbp), %r15
movq -0x60(%rbp), %rdi
movq -0x48(%rbp), %rcx
jmp 0x9465
movq -0x48(%rbp), %rdi
xorl %edx, %edx
movq -0x40(%rbp), %r15
movq %r15, %rcx
callq 0x628f
movq %rax, %rcx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
setne %al
andb 0x3e(%rbx), %al
cmpb $0x1, %al
jne 0x9483
incq %rdi
movq %rcx, %r14
movq %rbx, %rsi
callq 0x3748
movq %r14, %rcx
testq %rcx, %rcx
je 0x9493
cmpq $0x0, (%rcx)
je 0x9498
xorl %r14d, %r14d
jmp 0x94a4
xorl %r14d, %r14d
jmp 0x94af
movq 0x8(%rcx), %rax
cmpq 0x28(%rbx), %rax
sete %r14b
movq %rcx, %rdi
movq %r15, %rsi
callq 0x648d
movq %rbx, %rdi
callq 0xb4eb
movdqu 0x50d8(%rbx), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqu %xmm0, 0x50d8(%rbx)
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq