// File: /workspace/github_c_makefile_star2_O3/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 %rdi, %r14
xorps %xmm0, %xmm0
movq %rsi, -0x40(%rbp)
movups %xmm0, (%rsi)
movq 0x5108(%r8), %rsi
movq 0x5110(%r8), %rdi
movslq 0x38(%r8), %rdx
callq 0x3a40
movq 0x5118(%rbx), %rdi
movslq 0x20(%rbx), %rsi
xorl %edx, %edx
callq 0x33a4
movq %rbx, %rdi
callq 0x5883
movq %r15, -0x70(%rbp)
movq %r15, %rdi
callq 0x6214
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, -0x90(%rbp)
callq 0x33a4
movq %r14, -0x50(%rbp)
movq %r14, %rdi
callq 0xb878
cmpq $0x0, 0x28(%rbx)
je 0x9c7e
movq %rax, -0x48(%rbp)
addl $-0x2, %r12d
movl %r12d, -0x84(%rbp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq $0x0, -0x60(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x2c(%rbp)
xorl %r15d, %r15d
movq $0x0, -0x68(%rbp)
movq %r12, -0x80(%rbp)
movq %r15, -0x78(%rbp)
movq 0x50c8(%rbx), %rdi
callq 0xbe6d
movl %eax, %r12d
movl %eax, %esi
andl $0x80, %esi
sete %r14b
shrl $0x7, %esi
movq -0x70(%rbp), %rdi
callq 0x621f
movl %eax, %r15d
orb %al, %r14b
testb $0x1, %r14b
jne 0x98b7
cmpb $0x1, 0x3e(%rbx)
jne 0x98b7
movq %r13, %rdi
movl -0x84(%rbp), %esi
movq %rbx, %rdx
callq 0x3b1c
incq -0x60(%rbp)
testb $0x1, %r15b
je 0x9a1b
leaq -0x1(%r13), %rax
cmpq %rax, -0x68(%rbp)
jne 0x98f3
movq -0x40(%rbp), %rsi
movq %r13, 0x8(%rsi)
movq 0x20(%rsi), %rax
movl -0x2c(%rbp), %ecx
incq (%rax,%rcx,8)
movslq 0x10(%rsi), %rcx
movslq -0x78(%rbp), %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%rsi)
jmp 0x991d
movq -0x40(%rbp), %rsi
cmpb $0x0, 0x2c(%rsi)
jne 0x9916
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x6839
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rsi
movq %r13, 0x8(%rsi)
movq %r13, (%rsi)
movq -0x48(%rbp), %r14
testq %r14, %r14
je 0x9a17
cmpq %r13, (%r14)
jne 0x9a17
movq 0x50c8(%rbx), %rdi
notq %r13
addq 0x8(%r14), %r13
movq %r13, %rsi
movl $0x1, %edx
callq 0x2260
movq 0x8(%r14), %rcx
subq (%r14), %rcx
movl 0x20(%rbx), %eax
cmpq $0x10000, %rcx          # imm = 0x10000
movq %r14, %r13
jae 0x9ac7
testl %eax, %eax
jle 0x99b4
xorl %r15d, %r15d
movq 0x10(%r13), %rcx
movzwl (%rcx,%r15,2), %ecx
testq %rcx, %rcx
je 0x99a9
movq 0x5118(%rbx), %rax
addq %rcx, (%rax,%r15,8)
movq 0x50a0(%rbx), %rax
movq (%rax,%r15,8), %rdi
movq 0x10(%r13), %rax
movzwl (%rax,%r15,2), %esi
movl $0x1, %edx
callq 0x2260
movl 0x20(%rbx), %eax
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0x996f
movl 0x38(%rbx), %eax
testl %eax, %eax
jle 0x9b6b
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x10(%r13), %rdx
movslq 0x20(%rbx), %rcx
addq %r12, %rcx
movzwl (%rdx,%rcx,2), %edx
testq %rdx, %rdx
je 0x9a03
movq 0x5110(%rbx), %rsi
addq %rdx, (%rsi,%r12,8)
testq %r12, %r12
je 0x9a03
movq 0x50d0(%rbx), %rdi
addq %r15, %rdi
movq 0x10(%r13), %rax
movzwl (%rax,%rcx,2), %esi
callq 0x5feb
movl 0x38(%rbx), %eax
incq %r12
movslq %eax, %rcx
addq $0x20, %r15
cmpq %rcx, %r12
jl 0x99c5
jmp 0x9b6b
movq %r13, -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 0x9a5d
leaq 0x6784(%rip), %rdi      # 0x101dc
callq 0x39fc
movq 0x5118(%rbx), %rax
incq (%rax,%r15,8)
testb %r12b, %r12b
movslq -0x54(%rbp), %r15
movq -0x80(%rbp), %r12
cmovsq %r13, %r12
testq %r15, %r15
je 0x9ab5
xorl %esi, %esi
movq -0x90(%rbp), %r14
cmpq %r12, (%r14,%r15,8)
setne %sil
movq %r15, %rdi
shlq $0x5, %rdi
addq 0x50d0(%rbx), %rdi
shll $0x7, %esi
addl -0x2c(%rbp), %esi
callq 0x5f89
movq 0x5110(%rbx), %rax
incq (%rax,%r15,8)
movq %r12, (%r14,%r15,8)
incq %r13
cmpq 0x28(%rbx), %r13
jb 0x9861
jmp 0x9c74
testl %eax, %eax
jle 0x9b11
xorl %r15d, %r15d
movq 0x10(%r13), %rcx
movq (%rcx,%r15,8), %rcx
testq %rcx, %rcx
je 0x9b06
movq 0x5118(%rbx), %rax
addq %rcx, (%rax,%r15,8)
movq 0x50a0(%rbx), %rax
movq (%rax,%r15,8), %rdi
movq 0x10(%r13), %rax
movq (%rax,%r15,8), %rsi
movl $0x1, %edx
callq 0x2260
movl 0x20(%rbx), %eax
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0x9ace
movl 0x38(%rbx), %eax
testl %eax, %eax
jle 0x9b6b
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x10(%r13), %rdx
movslq 0x20(%rbx), %rcx
addq %r12, %rcx
movq (%rdx,%rcx,8), %rdx
testq %rdx, %rdx
je 0x9b5c
movq 0x5110(%rbx), %rsi
addq %rdx, (%rsi,%r12,8)
testq %r12, %r12
je 0x9b5c
movq 0x50d0(%rbx), %rdi
addq %r15, %rdi
movq 0x10(%r13), %rax
movq (%rax,%rcx,8), %rsi
callq 0x5feb
movl 0x38(%rbx), %eax
incq %r12
movslq %eax, %rcx
addq $0x20, %r15
cmpq %rcx, %r12
jl 0x9b1e
movq -0x70(%rbp), %r14
movq %r14, %rdi
callq 0x634e
movq %rax, %r12
movq (%r13), %rsi
notq %rsi
addq 0x8(%r13), %rsi
movq %r14, %rdi
callq 0x62b1
movq %r14, %rdi
callq 0x634e
movq %r13, %rcx
movq %rax, %r13
cmpq 0x8(%rcx), %rax
je 0x9bcd
movq -0x70(%rbp), %rdi
callq 0x634e
movq -0x48(%rbp), %r14
movq (%r14), %rcx
movq 0x8(%r14), %r8
leaq 0x6673(%rip), %rdi      # 0x1022f
movq %r12, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2100
movq 0x8(%r14), %r13
movq -0x38(%rbp), %r12
testq %r12, %r12
movq -0x40(%rbp), %r15
je 0x9c0b
movq 0x8(%r12), %rax
cmpq (%r15), %rax
jne 0x9c0b
movq %r12, %rdi
movq %r15, %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %r14
movq %r14, %rcx
callq 0xbbcb
movq %r12, -0x48(%rbp)
movq %r15, %rdi
movq -0x78(%rbp), %r15
movq -0x80(%rbp), %r12
jmp 0x9c50
cmpb $0x0, 0x2c(%r15)
movq %r15, %rdi
jne 0x9c26
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x6967
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
testq %rax, %rax
movq -0x78(%rbp), %r15
movq -0x80(%rbp), %r12
je 0x9c4c
movq %rax, %rdi
movq -0x50(%rbp), %r14
movq %r14, %rsi
callq 0x6a18
movq -0x40(%rbp), %rdi
jmp 0x9c50
movq -0x50(%rbp), %r14
movq %r13, 0x8(%rdi)
movq %r13, (%rdi)
movq %r14, %rdi
callq 0xb878
movq -0x48(%rbp), %rcx
movq %rax, 0x18(%rcx)
movq %rcx, -0x38(%rbp)
movq %rax, -0x48(%rbp)
jmp 0x9ab8
movl -0x2c(%rbp), %r14d
movq -0x68(%rbp), %r12
jmp 0x9c9a
xorl %r12d, %r12d
xorl %r14d, %r14d
movq $0x0, -0x38(%rbp)
movq $0x0, -0x60(%rbp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq -0x70(%rbp), %rdi
callq 0x606f
leaq -0x1(%r13), %rax
cmpq %rax, %r12
jne 0x9cd4
movq -0x40(%rbp), %rsi
movq %r13, 0x8(%rsi)
movq 0x20(%rsi), %rax
incq (%rax,%r14,8)
movslq 0x10(%rsi), %rcx
movslq %r15d, %rdx
addq %rcx, %rdx
incq (%rax,%rdx,8)
movb $0x0, 0x2c(%rsi)
movq -0x60(%rbp), %r14
jmp 0x9cec
movq -0x40(%rbp), %rsi
testb $0x1, 0x2c(%rsi)
movq -0x60(%rbp), %r14
je 0x9cec
movq -0x50(%rbp), %r15
movq -0x38(%rbp), %r12
jmp 0x9d01
movq -0x38(%rbp), %rdi
xorl %edx, %edx
movq -0x50(%rbp), %r15
movq %r15, %rcx
callq 0x6839
movq %rax, %r12
testq %r14, %r14
setne %al
andb 0x3e(%rbx), %al
cmpb $0x1, %al
jne 0x9d1c
incq %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3b86
testq %r12, %r12
je 0x9d2d
cmpq $0x0, (%r12)
je 0x9d32
xorl %r14d, %r14d
jmp 0x9d3f
xorl %r14d, %r14d
jmp 0x9d4a
movq 0x8(%r12), %rax
cmpq 0x28(%rbx), %rax
sete %r14b
movq %r12, %rdi
movq %r15, %rsi
callq 0x6a18
movq %rbx, %rdi
callq 0xbea2
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