// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]lyndon-array/external/sacak-lyndon.c
// Function: nameSubstr
int_t nameSubstr(int_t *SA, 
  int_t *s, int_t *s1, int_t n, 
  int_t m, int_t n1, int level, int cs) {
  int_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
  int_t name=0, name_ctr=0;
  int_t pre_pos=n-1, pre_len=0;
  for(i=0; i<n1; i++) {
    int diff=false;
    int_t len, pos=SA[i];

    int_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 $0x48, %rsp
movq %rsi, 0x28(%rsp)
movq %rdi, %r14
movl %r9d, 0x8(%rsp)
movslq %r9d, %rsi
movq %rcx, 0x20(%rsp)
movslq %ecx, %rax
movq %rsi, %rcx
cmpq %rax, %rcx
jge 0x6bab
movl $0x80000000, (%r14,%rcx,4) # imm = 0x80000000
incq %rcx
jmp 0x6b99
movq %rsi, 0x30(%rsp)
movl %r8d, 0x18(%rsp)
movq %rdx, 0x38(%rsp)
movq 0x20(%rsp), %rax
leal -0x1(%rax), %ecx
xorl %ebp, %ebp
movl 0x8(%rsp), %eax
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
movq %rdx, 0x40(%rsp)
movl $0x0, 0x10(%rsp)
movl %ecx, 0xc(%rsp)
movl %ecx, 0x14(%rsp)
xorl %ebx, %ebx
xorl %esi, %esi
cmpq 0x40(%rsp), %rbp
je 0x6cc4
movl %esi, 0x1c(%rsp)
movl (%r14,%rbp,4), %r12d
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl %r12d, %ecx
movl 0x88(%rsp), %r8d
callq 0x6adb
cmpl %ebx, %eax
jne 0x6c87
movslq %r12d, %rcx
movslq 0x14(%rsp), %r11
testl %ebx, %ebx
movl $0x0, %edx
cmovgl %ebx, %edx
movq 0x28(%rsp), %r9
leaq (%r9,%rcx,4), %rsi
addq %r9, %rcx
movl 0xc(%rsp), %r10d
movl %r10d, %edi
subl %r12d, %edi
leaq (%r9,%r11,4), %r8
addq %r11, %r9
subl %r11d, %r10d
xorl %r11d, %r11d
cmpq %r11, %rdx
je 0x6cb7
cmpl %r11d, %edi
je 0x6c87
cmpl %r11d, %r10d
je 0x6c87
cmpl $0x4, 0x88(%rsp)
jne 0x6c75
movl (%rsi,%r11,4), %r13d
movl (%r8,%r11,4), %r15d
jmp 0x6c7f
movzbl (%rcx,%r11), %r13d
movzbl (%r9,%r11), %r15d
incq %r11
cmpl %r15d, %r13d
je 0x6c52
incl 0x10(%rsp)
movl $0x1, (%r14,%rbp,4)
movl %r12d, 0x14(%rsp)
movl %eax, %ebx
movl %ebp, %esi
movl %r12d, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
addl 0x8(%rsp), %eax
cltq
movl %esi, (%r14,%rax,4)
incq %rbp
jmp 0x6beb
movl 0x1c(%rsp), %esi
movslq %esi, %rax
incl (%r14,%rax,4)
jmp 0x6c9c
movl 0x18(%rsp), %esi
decl %esi
movslq 0xc(%rsp), %rax
movq 0x38(%rsp), %r8
movq 0x30(%rsp), %rdi
cmpq %rdi, %rax
jl 0x6cf6
movl (%r14,%rax,4), %ecx
movl %ecx, %edx
negl %edx
jo 0x6cf1
movslq %esi, %rdx
decl %esi
movl %ecx, (%r14,%rdx,4)
decq %rax
jmp 0x6cd9
movl 0x8(%rsp), %eax
movb $0x1, %cl
cmpl $0x2, %eax
jl 0x6d2d
movslq -0x8(%r8,%rax,4), %rdx
cmpl -0x4(%r8,%rax,4), %edx
setl %sil
sete %dil
andb %dil, %cl
orb %sil, %cl
je 0x6d28
movl (%r14,%rdx,4), %esi
addl %esi, %edx
decl %edx
movl %edx, -0x8(%r8,%rax,4)
decq %rax
jmp 0x6cfc
movl 0x10(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq