// File: /workspace/github_c_makefile_star2_O3/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 0x3a40
movq 0x5118(%rbx), %rdi
movslq 0x20(%rbx), %rsi
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x33a4
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 0x33a4
cmpq $0x0, 0x28(%rbx)
je 0xc455
movq $0x0, -0x30(%rbp)
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpl $0x3, 0x94(%rbx)
jl 0xc373
leaq 0x3fed(%rip), %rdi      # 0x10356
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), %ecx
cmpb $0x0, 0x3c(%rbx)
jne 0xc3c8
movq 0x98(%rbx), %rdx
movzbl (%rdx,%r13), %edx
cmpq -0x40(%rbp), %rdx
movq %r12, %rsi
cmovbq %r13, %rsi
testq %rdx, %rdx
cmovneq %rsi, %r12
testq %rcx, %rcx
je 0xc43f
movq (%r15,%rcx,8), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r15,%rcx,8)
movq 0x50f0(%rbx), %rsi
movb %al, (%rsi,%rdx)
movq 0x50e8(%rbx), %rsi
cmpb %al, (%rsi,%rdx)
setne %sil
orb %sil, %r14b
cmpb $0x0, 0x3c(%rbx)
jne 0xc43f
movq -0x38(%rbp), %rsi
cmpq %r12, (%rsi,%rcx,8)
jne 0xc40e
testb $0x1, -0x30(%rbp)
je 0xc42d
movb $0x1, %al
jmp 0xc43b
movq 0x98(%rbx), %rax
cmpb $0x0, (%rax,%rdx)
jne 0xc423
movq -0x40(%rbp), %rsi
movb %sil, (%rax,%rdx)
movq -0x38(%rbp), %rax
movq %r12, (%rax,%rcx,8)
jmp 0xc43f
movq 0x50f0(%rbx), %rcx
cmpb %al, -0x1(%rcx,%rdx)
setne %al
movq %rax, -0x30(%rbp)
incq %r13
cmpq 0x28(%rbx), %r13
jb 0xc359
testb $0x1, -0x30(%rbp)
sete %al
jmp 0xc457
movb $0x1, %al
movdqu 0x50e8(%rbx), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqu %xmm0, 0x50e8(%rbx)
movb 0x3c(%rbx), %cl
movl %ecx, %edx
xorb $0x1, %dl
andb %al, %dl
movzbl %dl, %edx
testb $0x1, %r14b
movl $0x2, %eax
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
nopl (%rax)