// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %r8, %rbx
movq %rcx, %r15
movl %edx, %r12d
movq %rsi, %r13
movq %rdi, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rsi)
movq 0x5108(%r8), %rsi
movq 0x5110(%r8), %rdi
movslq 0x38(%r8), %rdx
callq 0x39b7
movq 0x5118(%rbx), %rdi
movslq 0x20(%rbx), %rsi
xorl %edx, %edx
callq 0x3304
movq %rbx, %rdi
callq 0x57d9
movq %r15, -0x78(%rbp)
movq %r15, %rdi
callq 0x614c
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, -0x90(%rbp)
callq 0x3304
movq %r14, -0x48(%rbp)
movq %r14, %rdi
callq 0xb7e2
cmpq $0x0, 0x28(%rbx)
je 0x9bc3
addl $-0x2, %r12d
movq $0x0, -0x88(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x68(%rbp)
movl %r12d, -0x7c(%rbp)
movq %rax, -0x38(%rbp)
movq %r13, %r14
movq 0x50c8(%rbx), %rdi
callq 0xbdbe
movl %eax, %r12d
movl %eax, %esi
andl $0x80, %esi
sete %r13b
shrl $0x7, %esi
movq -0x78(%rbp), %rdi
callq 0x6157
movl %eax, %r15d
orb %al, %r13b
testb $0x1, %r13b
jne 0x9800
cmpb $0x1, 0x3e(%rbx)
jne 0x9800
movq -0x40(%rbp), %rdi
movl -0x7c(%rbp), %esi
movq %rbx, %rdx
callq 0x3a94
incq -0x60(%rbp)
movq %r14, %r13
testb $0x1, %r15b
movq -0x38(%rbp), %r14
je 0x995c
movq -0x40(%rbp), %r15
leaq -0x1(%r15), %rax
cmpq %rax, -0x68(%rbp)
jne 0x9844
movq %r15, 0x8(%r13)
movq 0x20(%r13), %rax
movl -0x2c(%rbp), %ecx
incq (%rax,%rcx,8)
movslq 0x10(%r13), %rcx
movslq -0x70(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r13)
jmp 0x986a
cmpb $0x0, 0x2c(%r13)
jne 0x9862
movq -0x50(%rbp), %rdi
movq %r13, %rsi
movq %r14, %rdx
movq -0x48(%rbp), %rcx
callq 0x676c
movq %rax, -0x50(%rbp)
movq %r15, 0x8(%r13)
movq %r15, (%r13)
testq %r14, %r14
je 0x9958
movq (%r14), %rsi
cmpq %r15, %rsi
jne 0x9958
movq 0x50c8(%rbx), %rdi
notq %rsi
addq 0x8(%r14), %rsi
movl $0x1, %edx
callq 0x2260
movq 0x8(%r14), %rcx
subq (%r14), %rcx
movl 0x20(%rbx), %eax
cmpq $0x10000, %rcx          # imm = 0x10000
jae 0x9a26
testl %eax, %eax
jle 0x98f8
xorl %r15d, %r15d
movq 0x10(%r14), %rax
movzwl (%rax,%r15,2), %eax
testq %rax, %rax
je 0x98ec
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 $0x1, %edx
callq 0x2260
incq %r15
movslq 0x20(%rbx), %rax
cmpq %rax, %r15
jl 0x98b5
cmpl $0x0, 0x38(%rbx)
jle 0x9ac5
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x10(%r14), %rcx
movslq 0x20(%rbx), %rax
addq %r12, %rax
movzwl (%rcx,%rax,2), %ecx
testq %rcx, %rcx
je 0x9943
movq 0x5110(%rbx), %rdx
addq %rcx, (%rdx,%r12,8)
testq %r12, %r12
je 0x9943
movq 0x50d0(%rbx), %rdi
addq %r15, %rdi
movq 0x10(%r14), %rcx
movzwl (%rcx,%rax,2), %esi
callq 0x5f27
incq %r12
movslq 0x38(%rbx), %rax
addq $0x20, %r15
cmpq %rax, %r12
jl 0x9908
jmp 0x9ac5
movq %r15, -0x68(%rbp)
movl %r12d, %ecx
andl $0x7f, %ecx
movl $0x0, -0x54(%rbp)
movq 0x50a0(%rbx), %rax
movl %ecx, -0x2c(%rbp)
movl %ecx, %r15d
movq (%rax,%r15,8), %rcx
movl $0x1, %esi
movl $0x1, %edx
leaq -0x54(%rbp), %rdi
callq 0x20a0
cmpl $0x1, %eax
je 0x999e
leaq 0x6843(%rip), %rdi      # 0x101dc
callq 0x3973
movq 0x5118(%rbx), %rax
incq (%rax,%r15,8)
testb %r12b, %r12b
movslq -0x54(%rbp), %r14
movq -0x88(%rbp), %r12
movq -0x40(%rbp), %r15
cmovsq %r15, %r12
testq %r14, %r14
je 0x9a01
xorl %esi, %esi
movq -0x90(%rbp), %r15
cmpq %r12, (%r15,%r14,8)
setne %sil
movq %r14, %rdi
shlq $0x5, %rdi
addq 0x50d0(%rbx), %rdi
shll $0x7, %esi
addl -0x2c(%rbp), %esi
callq 0x5ec7
movq 0x5110(%rbx), %rax
incq (%rax,%r14,8)
movq %r12, (%r15,%r14,8)
movq -0x40(%rbp), %r15
movq %r12, -0x88(%rbp)
movq %r14, -0x70(%rbp)
incq %r15
movq -0x38(%rbp), %rax
movq %r15, -0x40(%rbp)
cmpq 0x28(%rbx), %r15
jb 0x97ad
jmp 0x9bae
testl %eax, %eax
jle 0x9a6e
xorl %r15d, %r15d
movq 0x10(%r14), %rax
movq (%rax,%r15,8), %rax
testq %rax, %rax
je 0x9a62
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 $0x1, %edx
callq 0x2260
incq %r15
movslq 0x20(%rbx), %rax
cmpq %rax, %r15
jl 0x9a2d
cmpl $0x0, 0x38(%rbx)
jle 0x9ac5
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x10(%r14), %rcx
movslq 0x20(%rbx), %rax
addq %r12, %rax
movq (%rcx,%rax,8), %rcx
testq %rcx, %rcx
je 0x9ab5
movq 0x5110(%rbx), %rdx
addq %rcx, (%rdx,%r12,8)
testq %r12, %r12
je 0x9ab5
movq 0x50d0(%rbx), %rdi
addq %r15, %rdi
movq 0x10(%r14), %rcx
movq (%rcx,%rax,8), %rsi
callq 0x5f27
incq %r12
movslq 0x38(%rbx), %rax
addq $0x20, %r15
cmpq %rax, %r12
jl 0x9a7a
movq -0x78(%rbp), %r14
movq %r14, %rdi
callq 0x6286
movq %rax, %r12
movq -0x38(%rbp), %rax
movq (%rax), %rsi
notq %rsi
movq -0x38(%rbp), %rax
addq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x61e9
movq %r14, %rdi
movq -0x38(%rbp), %r14
callq 0x6286
cmpq 0x8(%r14), %rax
je 0x9b24
movq -0x78(%rbp), %rdi
callq 0x6286
movq (%r14), %rcx
movq 0x8(%r14), %r8
leaq 0x6718(%rip), %rdi      # 0x1022f
movq %r12, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2100
movq 0x8(%r14), %rax
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %r15
testq %r15, %r15
je 0x9b5c
movq 0x8(%r15), %rax
cmpq (%r13), %rax
jne 0x9b5c
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq -0x48(%rbp), %r14
movq %r14, %rcx
callq 0xbb2a
movq %r14, %rdi
movq %r15, %r14
jmp 0x9b90
cmpb $0x0, 0x2c(%r13)
jne 0x9b72
movq %r13, %rdi
movq %r14, %rsi
movq -0x48(%rbp), %rdx
callq 0x689e
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x9b8c
movq -0x48(%rbp), %r15
movq %r15, %rsi
callq 0x6950
movq %r15, %rdi
jmp 0x9b90
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %r15
movq %r15, 0x8(%r13)
movq %r15, (%r13)
callq 0xb7e2
movq %rax, 0x18(%r14)
movq %r14, -0x50(%rbp)
jmp 0x9a13
movl -0x2c(%rbp), %eax
movq %rax, -0x38(%rbp)
movq -0x50(%rbp), %r12
movq -0x68(%rbp), %r14
movq -0x40(%rbp), %r15
jmp 0x9be4
movq $0x0, -0x70(%rbp)
xorl %r14d, %r14d
movq $0x0, -0x38(%rbp)
xorl %r12d, %r12d
movq $0x0, -0x60(%rbp)
xorl %r15d, %r15d
movq -0x78(%rbp), %rdi
callq 0x5fab
leaq -0x1(%r15), %rax
cmpq %rax, %r14
jne 0x9c1a
movq %r15, 0x8(%r13)
movq 0x20(%r13), %rax
movq -0x38(%rbp), %rcx
incq (%rax,%rcx,8)
movslq 0x10(%r13), %rcx
movslq -0x70(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%r13)
cmpb $0x0, 0x2c(%r13)
jne 0x9c35
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq -0x48(%rbp), %rcx
callq 0x676c
movq %rax, %r12
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
setne %al
andb 0x3e(%rbx), %al
cmpb $0x1, %al
jne 0x9c51
incq %rdi
movq %rbx, %rsi
callq 0x3afe
testq %r12, %r12
movq -0x48(%rbp), %rsi
je 0x9c61
cmpq $0x0, (%r12)
je 0x9c66
xorl %r14d, %r14d
jmp 0x9c73
movq 0x8(%r12), %rax
cmpq 0x28(%rbx), %rax
sete %r14b
testq %r12, %r12
je 0x9c80
movq %r12, %rdi
callq 0x6950
movq %rbx, %rdi
callq 0xbdf3
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