// File: /workspace/github_c_makefile_star2_O1/felipelouza[P]egap/tools/src/gsacak.c
// Function: nameSubstr
uint_t nameSubstr(uint_t *SA, 
  int_t *s, uint_t *s1, uint_t n, 
  uint_t m, uint_t n1, int level, int cs) {
  uint_t i, j, cur_t, succ_t;

  // init the name array buffer
  for(i=n1; i<n; i++) SA[i]=EMPTY_k;

  // scan to compute the interim s1
  uint_t name=0, name_ctr=0;
  uint_t pre_pos=n-1, pre_len=0;
  for(i=0; i<n1; i++) {
    int diff=false;
    uint_t len, pos=SA[i];

    uint_t d;
    len=getLengthOfLMS(s, n, level, pos, cs);
    if(len!=pre_len) diff=true;
    else
      for(d=0; d<len; d++)
        if(pos+d==n-1 || pre_pos+d==n-1 ||
           chr(pos+d)!=chr(pre_pos+d)) {
          diff=true; break;
        }

    if(diff) {
      name=i; name_ctr++;
      SA[name]=1; // a new name.
      pre_pos=pos; pre_len=len;
    }
    else
      SA[name]++; // count this name.

    SA[n1+pos/2]=name;
  }

  // compact the interim s1 sparsely stored 
  //   in SA[n1, n-1] into SA[m-n1, m-1].
  for(i=n-1, j=m-1; i>=n1; i--)
    if(SA[i]!=EMPTY_k) SA[j--]=SA[i];

  // rename each S-type character of the
  //   interim s1 as the end of its bucket
  //   to produce the final s1.
  succ_t=1;
  for(i=n1-1; i>0; i--) {
    int_t ch=s1[i], ch1=s1[i-1];
    cur_t=(ch1< ch || (ch1==ch && succ_t==1))?1:0;
    if(cur_t==1) {
      s1[i-1]+=SA[s1[i-1]]-1;
    }
    succ_t=cur_t;
  }

  return name_ctr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %rbx
movq %rcx, %r9
movq %rdx, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
cmpq %rcx, %rbx
jae 0x86ca
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
movq %rbx, %rcx
movq %rax, (%rdi,%rcx,8)
incq %rcx
cmpq %rcx, %r9
jne 0x86be
movq %r8, 0x28(%rsp)
leaq -0x1(%r9), %rbp
testq %rbx, %rbx
je 0x87f4
movl 0x98(%rsp), %r12d
leaq (%rdi,%rbx,8), %rax
movq %rax, 0x40(%rsp)
leaq -0x1(%r9), %rax
movq %rax, 0x38(%rsp)
xorl %r13d, %r13d
xorl %ecx, %ecx
movq $0x0, 0x20(%rsp)
movq %rbp, 0x10(%rsp)
movq $0x0, (%rsp)
movq %rdi, 0x8(%rsp)
movq %r9, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
movq (%rdi,%r13,8), %r14
movq 0x18(%rsp), %rdi
movq %r9, %rsi
movq %r14, %rcx
movl %r12d, %r8d
callq 0x85cb
cmpq 0x20(%rsp), %rax
jne 0x87b6
testq %rax, %rax
je 0x87a6
movq 0x18(%rsp), %r8
movq 0x10(%rsp), %rdi
leaq (%r8,%rdi,8), %rcx
leaq (%r8,%rdi), %rdx
movq 0x38(%rsp), %r9
movq %r9, %rsi
subq %rdi, %rsi
leaq (%r8,%r14,8), %rdi
addq %r14, %r8
subq %r14, %r9
xorl %r10d, %r10d
cmpq %r10, %r9
je 0x87b6
cmpq %r10, %rsi
je 0x87b6
cmpl $0x8, %r12d
jne 0x8783
movq (%rdi,%r10,8), %r11
jmp 0x8788
movzbl (%r8,%r10), %r11d
cmpl $0x8, %r12d
jne 0x8794
movq (%rcx,%r10,8), %r15
jmp 0x8799
movzbl (%rdx,%r10), %r15d
cmpq %r15, %r11
jne 0x87b6
incq %r10
cmpq %r10, %rax
jne 0x876d
movq 0x8(%rsp), %rdi
movq 0x50(%rsp), %rcx
incq (%rdi,%rcx,8)
jmp 0x87d4
incq (%rsp)
movq 0x8(%rsp), %rdi
movq $0x1, (%rdi,%r13,8)
movq %r14, 0x10(%rsp)
movq %rax, 0x20(%rsp)
movq %r13, %rcx
andq $-0x2, %r14
movq 0x40(%rsp), %rax
movq %rcx, (%rax,%r14,4)
incq %r13
cmpq %rbx, %r13
movq 0x48(%rsp), %r9
jne 0x871b
jmp 0x87fc
movq $0x0, (%rsp)
cmpq %rbx, %rbp
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
jb 0x8830
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
decq %rdx
movq (%rdi,%rbp,8), %rcx
cmpq %rax, %rcx
je 0x8828
movq %rcx, (%rdi,%rdx,8)
decq %rdx
decq %rbp
cmpq %rbx, %rbp
jae 0x8818
cmpq $0x1, %rbx
je 0x886b
movb $0x1, %al
movq -0x10(%rsi,%rbx,8), %rdx
movb $0x1, %cl
cmpq -0x8(%rsi,%rbx,8), %rdx
jl 0x884d
sete %cl
andb %cl, %al
movl %eax, %ecx
testb %cl, %cl
je 0x8860
movq (%rdi,%rdx,8), %rax
addq %rdx, %rax
decq %rax
movq %rax, -0x10(%rsi,%rbx,8)
decq %rbx
movl %ecx, %eax
cmpq $0x1, %rbx
jne 0x8838
movq (%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq