// File: 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:
subq $0x128, %rsp            # imm = 0x128
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rax
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
movq %rdx, 0x110(%rsp)
movq %rcx, 0x108(%rsp)
movq %r8, 0x100(%rsp)
movq %r9, 0xf8(%rsp)
movq 0x120(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movq 0x138(%rsp), %rax
cmpq 0xe8(%rcx), %rax
jl 0x1a7ca
jmp 0x1ad7c
movq $0x0, 0xd8(%rsp)
leaq 0xd7(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0xed50
movq 0x68(%rsp), %rcx
leaq 0xe0(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
leaq 0xd8(%rsp), %rdx
callq 0x1fa00
jmp 0x1a809
leaq 0xd7(%rsp), %rdi
callq 0xf9c0
movq 0xf8(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
cmpq 0x130(%rsp), %rax
jg 0x1a903
movq 0x70(%rsp), %rax
movq %rax, %rdi
addq $0x50, %rdi
movq 0x110(%rsp), %rcx
movq 0xb8(%rsp), %rdx
movslq (%rcx,%rdx,4), %rsi
movq 0xe8(%rax), %rax
imulq %rax, %rsi
movq 0x138(%rsp), %rax
addq %rax, %rsi
callq 0x8830
movq %rax, 0x60(%rsp)
jmp 0x1a87e
movq 0x60(%rsp), %rax
movsbq (%rax), %rsi
leaq 0xe0(%rsp), %rdi
callq 0xccd0
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq 0xb8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb8(%rsp)
jmp 0x1a826
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xd7(%rsp), %rdi
callq 0xf9c0
jmp 0x1ad84
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xf0f0
jmp 0x1ad84
movq 0xf8(%rsp), %rcx
movq 0x100(%rsp), %rax
movq %rcx, (%rax)
movq $0x1, 0xb0(%rsp)
cmpq $0x100, 0xb0(%rsp) # imm = 0x100
jge 0x1a98e
movq 0xb0(%rsp), %rsi
subq $0x1, %rsi
leaq 0xe0(%rsp), %rdi
callq 0xccd0
movq (%rax), %rdx
movq 0x100(%rsp), %rax
movq 0xb0(%rsp), %rcx
subq $0x1, %rcx
addq (%rax,%rcx,8), %rdx
movq 0x100(%rsp), %rax
movq 0xb0(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x1a922
movb $0x0, 0xaf(%rsp)
movq 0xf8(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x58(%rsp)
movzbl 0xaf(%rsp), %eax
movl %eax, %esi
leaq 0xe0(%rsp), %rdi
callq 0xccd0
movq %rax, %rcx
movq 0x58(%rsp), %rax
addq (%rcx), %rax
movq %rax, 0xa0(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x98(%rsp)
jmp 0x1a9e3
movq 0x98(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jle 0x1aa64
movzbl 0xaf(%rsp), %eax
cmpl $0xff, %eax
jne 0x1aa09
jmp 0x1abe7
movb 0xaf(%rsp), %al
addb $0x1, %al
movb %al, 0xaf(%rsp)
movq 0x100(%rsp), %rax
movzbl 0xaf(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x98(%rsp)
movzbl 0xaf(%rsp), %eax
movl %eax, %esi
leaq 0xe0(%rsp), %rdi
callq 0xccd0
movq (%rax), %rax
addq 0xa0(%rsp), %rax
movq %rax, 0xa0(%rsp)
jmp 0x1abe2
movq 0x70(%rsp), %rax
movq 0x110(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movq 0x100(%rsp), %rdx
movq %rdx, 0x48(%rsp)
movq %rax, %rdi
addq $0x50, %rdi
movq 0x98(%rsp), %rdx
movslq (%rcx,%rdx,4), %rsi
movq 0xe8(%rax), %rax
imulq %rax, %rsi
movq 0x138(%rsp), %rax
addq %rax, %rsi
callq 0x8830
movq %rax, 0x50(%rsp)
jmp 0x1aab8
movq 0x70(%rsp), %rax
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rdx
movq 0x50(%rsp), %rsi
movzbl (%rsi), %esi
movq (%rdx,%rsi,8), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, 0x94(%rsp)
movq 0x110(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movq 0x98(%rsp), %rdx
movslq (%rcx,%rdx,4), %rsi
movl %esi, %ecx
movl %ecx, 0x2c(%rsp)
movq 0x100(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq %rax, %rdi
addq $0x50, %rdi
movq 0xe8(%rax), %rax
imulq %rax, %rsi
movq 0x138(%rsp), %rax
addq %rax, %rsi
callq 0x8830
movq %rax, 0x38(%rsp)
jmp 0x1ab32
movq 0x70(%rsp), %rax
movq 0x20(%rsp), %rcx
movl 0x2c(%rsp), %esi
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movzbl (%rdx), %edx
movl %edx, %r8d
movq (%rdi,%r8,8), %rdx
movq %rdx, %r9
incq %r9
movq %r9, (%rdi,%r8,8)
movl %esi, (%rcx,%rdx,4)
movl 0x94(%rsp), %esi
movq 0x110(%rsp), %rcx
movq 0x98(%rsp), %rdx
movl %esi, (%rcx,%rdx,4)
movq %rax, %rdi
addq $0x50, %rdi
movq 0x110(%rsp), %rcx
movq 0x98(%rsp), %rdx
movslq (%rcx,%rdx,4), %rsi
movq 0xe8(%rax), %rax
imulq %rax, %rsi
movq 0x138(%rsp), %rax
addq %rax, %rsi
callq 0x8830
movq %rax, 0x18(%rsp)
jmp 0x1abb8
movq 0x18(%rsp), %rax
movsbl (%rax), %eax
movzbl 0xaf(%rsp), %ecx
cmpl %ecx, %eax
jne 0x1abe0
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
jmp 0x1abe2
jmp 0x1a9e1
movq 0xf8(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0xf8(%rsp), %rax
subq $0x1, %rax
movq %rax, 0xa0(%rsp)
movq $0x1, 0x80(%rsp)
cmpq $0x100, 0x80(%rsp) # imm = 0x100
jge 0x1ad6f
movq 0x80(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0xccd0
movq (%rax), %rax
addq 0xa0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x88(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jg 0x1ad54
movq 0x70(%rsp), %rcx
movq 0x138(%rsp), %rax
movq 0xe8(%rcx), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x1aced
movq 0x88(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jg 0x1acd9
movq 0x108(%rsp), %rax
movq (%rax), %rax
movl %eax, %edx
movq 0x118(%rsp), %rax
movq 0x110(%rsp), %rcx
movq 0x78(%rsp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x1ac91
movq 0x108(%rsp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0x1ad40
movq 0x70(%rsp), %rdi
movq 0x118(%rsp), %rsi
movq 0x110(%rsp), %rdx
movq 0x108(%rsp), %rcx
movq 0x100(%rsp), %r8
movq 0x88(%rsp), %r9
movq 0xa0(%rsp), %r10
movq 0x138(%rsp), %r11
incq %r11
movq %rsp, %rax
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1a760
jmp 0x1ad3e
jmp 0x1ad40
movq 0xa0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x88(%rsp)
jmp 0x1ad56
movq 0x80(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x80(%rsp)
jmp 0x1ac17
leaq 0xe0(%rsp), %rdi
callq 0xf0f0
addq $0x128, %rsp            # imm = 0x128
retq
movq 0xc8(%rsp), %rdi
callq 0x8730
nopw %cs:(%rax,%rax)