// File: /workspace/github_c_makefile_star2_O0/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
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movb $0x0, -0x1a(%rbp)
movq -0x18(%rbp), %rax
movl 0x38(%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x80(%rbp)
movq %rdi, %rsp
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x5108(%rax), %rsi
movslq 0x38(%rax), %rdx
callq 0x3d70
movq -0x18(%rbp), %rax
movq 0x5118(%rax), %rdi
movslq 0x20(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x3310
movq -0x18(%rbp), %rax
movl 0x38(%rax), %eax
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x78(%rbp)
movq %rdi, %rsp
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movslq 0x38(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x3310
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jae 0x12218
movq -0x18(%rbp), %rax
cmpl $0x2, 0x94(%rax)
jle 0x1207c
movq -0x48(%rbp), %rsi
leaq 0x42c3(%rip), %rdi      # 0x16338
movb $0x0, %al
callq 0x20f0
movq -0x18(%rbp), %rax
movq 0x50e8(%rax), %rax
movq -0x48(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movq 0x5118(%rcx), %rdx
movslq -0x4c(%rbp), %rsi
movq (%rdx,%rsi,8), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rdx,%rsi,8)
movzbl (%rax,%rcx), %eax
movl %eax, -0x50(%rbp)
movq -0x18(%rbp), %rax
testb $0x1, 0x3c(%rax)
jne 0x12109
movq -0x18(%rbp), %rax
movq 0x98(%rax), %rax
movq -0x48(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x12109
movq -0x18(%rbp), %rax
movq 0x98(%rax), %rax
movq -0x48(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpq -0x10(%rbp), %rax
jae 0x12109
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x12203
movq -0x80(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movq (%rcx,%rdx,8), %rax
movq %rax, %rsi
addq $0x1, %rsi
movq %rsi, (%rcx,%rdx,8)
movq %rax, -0x58(%rbp)
movl -0x4c(%rbp), %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq 0x50f0(%rax), %rax
movq -0x58(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
movq 0x50e8(%rax), %rax
movq -0x58(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl -0x4c(%rbp), %eax
je 0x12161
movb $0x1, -0x19(%rbp)
movq -0x18(%rbp), %rax
testb $0x1, 0x3c(%rax)
jne 0x12201
movq -0x78(%rbp), %rax
movslq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x40(%rbp), %rax
je 0x121c3
movq -0x18(%rbp), %rax
movq 0x98(%rax), %rax
movq -0x58(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x121b1
movq -0x10(%rbp), %rax
movb %al, %dl
movq -0x18(%rbp), %rax
movq 0x98(%rax), %rax
movq -0x58(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x78(%rbp), %rax
movq -0x40(%rbp), %rdx
movslq -0x50(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x121ff
movb $0x1, %al
testb $0x1, -0x1a(%rbp)
movb %al, -0x81(%rbp)
jne 0x121f4
movq -0x18(%rbp), %rax
movq 0x50f0(%rax), %rax
movq -0x58(%rbp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
cmpl -0x4c(%rbp), %eax
setne %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
andb $0x1, %al
movb %al, -0x1a(%rbp)
jmp 0x12201
jmp 0x12205
jmp 0x12205
jmp 0x12207
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x1204b
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x12238
jmp 0x1222d
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1221f
movq -0x18(%rbp), %rax
movq 0x50e8(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movq 0x50f0(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50e8(%rax)
movq -0x68(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50f0(%rax)
movq -0x18(%rbp), %rax
testb $0x1, 0x3c(%rax)
je 0x1228c
testb $0x1, -0x19(%rbp)
jne 0x1228c
movl $0x2, -0x4(%rbp)
movl $0x1, -0x6c(%rbp)
jmp 0x122c1
movq -0x18(%rbp), %rax
testb $0x1, 0x3c(%rax)
jne 0x122b3
movb -0x1a(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x122b3
movl $0x1, -0x4(%rbp)
movl $0x1, -0x6c(%rbp)
jmp 0x122c1
movl $0x0, -0x4(%rbp)
movl $0x1, -0x6c(%rbp)
movq -0x28(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq