// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/mergehm.c
// Function: addCharToPrefix_HM
static int addCharToPrefix_HM(customInt prefixLength, g_data *g) 
{
  assert(prefixLength<= MAX_LCP_SIZE);
  bool mergeChanged = false; // the Z vector has changed in this iteration
  bool nonMonotoneBlocks = false; // non monotone blocks found in this iteration
  // copy F array
  customInt position[g->sizeOfAlpha];
  array_copy(position, 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);
  // blockID: current block for each alphabet char. Used only if blockBeginsAt!=NULL
  customInt blockID[g->sizeOfAlpha];
  array_clear(blockID,g->sizeOfAlpha, g->mergeLen); // mergeLen is an invalid id 
  customInt id = 0;

  // main loop
  for (customInt k = 0; k < g->mergeLen; ++k) {
    if (g->verbose>2) {printf("k="CUSTOM_FORMAT" ",k);}
    int currentColor = g->mergeColor[k]; //  b in pseudocode
    int currentChar = g->bws[currentColor][g->inCnt[currentColor]++]; // c in pseudocode

    // if we are computing the LCP check if we are entering next block
    if (!g->bwtOnly && g->blockBeginsAt[k] && (g->blockBeginsAt[k] < prefixLength)) 
      id = k; 

    // write color in new Z array, except 0 chars
    if(currentChar!=0) {
      customInt positionToUpdate = position[currentChar]++;
      g->newMergeColor[positionToUpdate] = currentColor;
      if(g->mergeColor[positionToUpdate]!=currentColor)
        mergeChanged=true;
      if (!g->bwtOnly) {
        // create new block?
        if(blockID[currentChar] != id) {
            if(g->blockBeginsAt[positionToUpdate]==0) {// only 0 values in B should be overwritten
              g->blockBeginsAt[positionToUpdate] = prefixLength;
            }
          blockID[currentChar] = id;
        }
        // the previous block continues, check if it is non-monotone
        else { 
          assert(positionToUpdate>0);
          nonMonotoneBlocks = nonMonotoneBlocks||(g->newMergeColor[positionToUpdate-1]!=currentColor);
        }
      }
    }
    else // extra check for has0 chars, can be commented out 
      assert(currentChar==0);
  } // end main loop 
  
  // check we have seen all BWT positions 
  for(int i=0; i<g->numBwt; i++)
    assert(g->inCnt[i]==g->bwtLen[i]);

  // extra check on newMergeColor
  #ifndef NDEBUG
  bool stop=false;
  customInt cnt[MAX_NUMBER_OF_BWTS] = {0};
  for(customInt i=0;i<g->mergeLen;i++) 
    cnt[g->newMergeColor[i]]++;
  for(int i=0; i<g->numBwt; i++)
    if(cnt[i]!=g->bwtLen[i]) {
      printf("NEWMERGE %d cnt:"CUSTOM_FORMAT" len:"CUSTOM_FORMAT"\n",i,cnt[i],g->bwtLen[i]);
      stop=true;
    }
  assert(!stop);
  #endif

  // swap Merge and newMerge
  palette *tmp=g->mergeColor; g->mergeColor=g->newMergeColor; g->newMergeColor=tmp;
  if(g->bwtOnly && !mergeChanged)
    return 2; // no lcp and bwt not changed we can stop immediately
  if(!g->bwtOnly  && nonMonotoneBlocks==false)
    return 1; // al blocks are monothone, stop at next iteration 
  return  0; 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, -0x40(%rbp)
movslq 0x38(%rsi), %rdx
movl %edx, %eax
movq %rsp, %r15
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq 0x5108(%rsi), %rsi
movq %r15, %rdi
callq 0x35fb
movq 0x5118(%rbx), %rdi
movslq 0x20(%rbx), %rsi
xorl %r12d, %r12d
xorl %edx, %edx
callq 0x2fbc
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, -0x38(%rbp)
callq 0x2fbc
xorl %r13d, %r13d
xorl %r14d, %r14d
movq $0x0, -0x30(%rbp)
cmpq 0x28(%rbx), %r13
jae 0xba62
cmpl $0x3, 0x94(%rbx)
jl 0xb987
leaq 0x3991(%rip), %rdi      # 0xf30e
movq %r13, %rsi
xorl %eax, %eax
callq 0x2100
movq 0x50e8(%rbx), %rax
movzbl (%rax,%r13), %eax
movq (%rbx), %rcx
leal (,%rax,8), %edx
movq (%rcx,%rdx), %rcx
movq 0x5118(%rbx), %rsi
movq (%rsi,%rdx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%rsi,%rdx)
movzbl (%rcx,%rdi), %edx
cmpb $0x0, 0x3c(%rbx)
jne 0xb9dc
movq 0x98(%rbx), %rcx
movzbl (%rcx,%r13), %ecx
cmpq -0x40(%rbp), %rcx
movq %r14, %rsi
cmovbq %r13, %rsi
testq %rcx, %rcx
cmovneq %rsi, %r14
testq %rdx, %rdx
je 0xba21
movq (%r15,%rdx,8), %rsi
leaq 0x1(%rsi), %rcx
movq %rcx, (%r15,%rdx,8)
movq 0x50f0(%rbx), %rcx
movb %al, (%rcx,%rsi)
movq 0x50e8(%rbx), %rcx
cmpb %al, (%rcx,%rsi)
setne %cl
orb %r12b, %cl
cmpb $0x0, 0x3c(%rbx)
jne 0xba57
movq -0x38(%rbp), %rdi
cmpq %r14, (%rdi,%rdx,8)
jne 0xba26
testb $0x1, -0x30(%rbp)
je 0xba45
movb $0x1, %al
jmp 0xba53
movl %r12d, %ecx
jmp 0xba57
movq 0x98(%rbx), %rax
cmpb $0x0, (%rax,%rsi)
jne 0xba3b
movq -0x40(%rbp), %rdi
movb %dil, (%rax,%rsi)
movq -0x38(%rbp), %rax
movq %r14, (%rax,%rdx,8)
jmp 0xba57
movq 0x50f0(%rbx), %rdx
cmpb %al, -0x1(%rdx,%rsi)
setne %al
movq %rax, -0x30(%rbp)
incq %r13
movl %ecx, %r12d
jmp 0xb963
movdqu 0x50e8(%rbx), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqu %xmm0, 0x50e8(%rbx)
movb 0x3c(%rbx), %cl
movl %ecx, %eax
notb %al
testb $0x1, -0x30(%rbp)
sete %dl
andb %al, %dl
movzbl %dl, %edx
testb $0x1, %r12b
pushq $0x2
popq %rax
cmovnel %edx, %eax
testb %cl, %cl
cmovel %edx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq