// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/sparseSA.cpp
// Function: sparseSA::radixStep(int*, int*, long&, long*, long, long, long)
void sparseSA::radixStep(int *t_new, int *SA, long &bucketNr, long *BucketBegin, long l, long r, long h) {
  if(h >= K) return;
  // first pass: count
  vector<long> Sigma(256, 0); // Sigma counts occurring characters in bucket
  for (long i = l; i <= r; i++) Sigma[ S[ SA[i]*K + h ] ]++; // count characters
  BucketBegin[0] = l; for (long i = 1; i < 256; i++) { BucketBegin[i] = Sigma[i-1] + BucketBegin[i-1]; } // accumulate

  // second pass: move (this variant does *not* need an additional array!)
  unsigned char currentKey = 0;    // character of current bucket
  long end = l-1+Sigma[currentKey]; // end of current bucket
  long pos = l;                     // 'pos' is current position in bucket
  while (1) {
    if (pos > end) { // Reached the end of the bucket.
      if (currentKey == 255) break; // Last character?
      currentKey++; // Advance to next characer.
      pos = BucketBegin[currentKey]; // Next bucket start.
      end += Sigma[currentKey]; // Next bucket end.
    }
    else {
      // American flag sort of McIlroy et al. 1993. BucketBegin keeps
      // track of current position where to add to bucket set.
      int tmp = SA[ BucketBegin[ (unsigned char) S[ SA[pos]*K + h ] ] ];
      SA[ BucketBegin[ (unsigned char) S[ SA[pos]*K + h] ]++ ] = SA[pos];  // Move bucket beginning to the right, and replace
      SA[ pos ] = tmp; // Save value at bucket beginning.
      if (S[ SA[pos]*K + h ] == currentKey) pos++; // Advance to next position if the right character.
    }
  }
  // recursively refine buckets and calculate new text:
  long beg = l; end = l-1;
  for (long i = 1; i < 256; i++) { // step through Sigma to find bucket borders
    end += Sigma[i];
    if (beg <= end) {
      if(h == K-1) {
	for (long j = beg; j <= end; j++) {
	  t_new[ SA[j] ] = bucketNr; // set new text
	}
	bucketNr++;
      }
      else {
	radixStep(t_new, SA, bucketNr, BucketBegin, beg, end, h+1); // recursive refinement
      }
      beg = end + 1; // advance to next bucket
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x88(%rsp), %rax
cmpq %rax, 0xe8(%rdi)
jle 0xf406
movq %r9, %rbp
movq %r8, %rbx
movq %rdx, %r15
movq %rdi, %r13
movq %rsi, 0x10(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, %r12
movq 0x80(%rsp), %r14
leaq 0x40(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x28(%rsp), %rdi
leaq 0xf(%rsp), %rcx
movl $0x100, %esi            # imm = 0x100
callq 0x111e4
movq 0x50(%r13), %rcx
addq %r12, %rcx
leaq 0x28(%rsp), %rax
movq (%rax), %rax
movq %rbp, %rdx
cmpq %r14, %rdx
jg 0xf2b4
movslq (%r15,%rdx,4), %rsi
imulq 0xe8(%r13), %rsi
movsbq (%rcx,%rsi), %rsi
incq (%rax,%rsi,8)
incq %rdx
jmp 0xf295
movq %rbp, (%rbx)
pushq $0x1
popq %rcx
movq %rbp, %rdx
cmpq $0x100, %rcx            # imm = 0x100
je 0xf2d4
addq -0x8(%rax,%rcx,8), %rdx
movq %rdx, (%rbx,%rcx,8)
incq %rcx
jmp 0xf2bd
leaq -0x1(%rbp), %r14
movq %rax, 0x18(%rsp)
movq (%rax), %rcx
addq %r14, %rcx
movq 0x50(%r13), %rdx
addq 0x88(%rsp), %rdx
xorl %esi, %esi
movq %rbp, %rdi
movzbl %sil, %r8d
cmpq %rcx, %rdi
jg 0xf345
movslq (%r15,%rdi,4), %r9
movq %r9, %r10
imulq 0xe8(%r13), %r10
movzbl (%rdx,%r10), %r10d
movq (%rbx,%r10,8), %r11
movslq (%r15,%r11,4), %r12
leaq 0x1(%r11), %rax
movq %rax, (%rbx,%r10,8)
movl %r9d, (%r15,%r11,4)
movl %r12d, (%r15,%rdi,4)
imulq 0xe8(%r13), %r12
movsbl (%rdx,%r12), %eax
xorl %r9d, %r9d
cmpl %r8d, %eax
sete %r9b
addq %r9, %rdi
jmp 0xf2f8
cmpl $0xff, %r8d
je 0xf368
incb %sil
movzbl %sil, %r8d
shll $0x3, %r8d
movq (%rbx,%r8), %rdi
movq 0x18(%rsp), %rax
addq (%rax,%r8), %rcx
jmp 0xf2f4
movq 0x88(%rsp), %rax
incq %rax
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r12
movq 0x20(%rsp), %rcx
movq 0x10(%rsp), %rsi
cmpq $0x100, %r12            # imm = 0x100
je 0xf3fc
movq 0x28(%rsp), %rax
addq (%rax,%r12,8), %r14
cmpq %r14, %rbp
jg 0xf3f7
movq 0xe8(%r13), %rax
decq %rax
cmpq 0x88(%rsp), %rax
jne 0xf3c4
movl (%rcx), %eax
cmpq %r14, %rbp
jg 0xf3f0
movslq (%r15,%rbp,4), %rdx
movl %eax, (%rsi,%rdx,4)
incq %rbp
jmp 0xf3b3
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %r8
movq %rbp, %r9
pushq 0x18(%rsp)
pushq %r14
callq 0xf222
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rcx
addq $0x10, %rsp
jmp 0xf3f3
incq (%rcx)
leaq 0x1(%r14), %rbp
incq %r12
jmp 0xf386
leaq 0x28(%rsp), %rdi
callq 0xa3d4
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0xa3d4
movq %rbx, %rdi
callq 0x75e0