// File: /workspace/github_c_makefile_star2_O1/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
movl %ecx, %r10d
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %r14
movl %r9d, 0x8(%rsp)
cmpl %ecx, %r9d
jge 0x739f
movslq 0x8(%rsp), %rax
movslq %r10d, %rcx
movl $0x80000000, (%r14,%rax,4) # imm = 0x80000000
incq %rax
cmpq %rax, %rcx
jne 0x738f
movl %r8d, 0x18(%rsp)
movl 0x8(%rsp), %eax
movl %eax, %ebx
testl %eax, %eax
jle 0x74ce
movl 0x88(%rsp), %r15d
leal -0x1(%r10), %eax
movl %eax, 0x10(%rsp)
cltq
movq %rax, 0x38(%rsp)
xorl %r13d, %r13d
xorl %ecx, %ecx
movl $0x0, 0x14(%rsp)
movl $0x0, 0xc(%rsp)
movq %r10, 0x20(%rsp)
movl %ecx, 0x1c(%rsp)
movl (%r14,%r13,4), %ebp
movq 0x28(%rsp), %rdi
movl %r10d, %esi
movl %ebp, %ecx
movl %r15d, %r8d
callq 0x7288
cmpl 0x14(%rsp), %eax
jne 0x7491
testl %eax, %eax
jle 0x7484
movslq %ebp, %rcx
movslq 0x10(%rsp), %rdx
movl %edx, %esi
notl %esi
addl 0x20(%rsp), %esi
movl %eax, %edi
movq 0x28(%rsp), %r11
leaq (%r11,%rdx,4), %r8
movq %r8, 0x40(%rsp)
addq %r11, %rdx
movq 0x38(%rsp), %r9
subq %rcx, %r9
leaq (%r11,%rcx,4), %r10
addq %r11, %rcx
xorl %r11d, %r11d
cmpq %r11, %r9
je 0x7491
cmpq %r11, %rsi
je 0x7491
cmpl $0x4, %r15d
jne 0x7456
movl (%r10,%r11,4), %r12d
jmp 0x745b
movzbl (%rcx,%r11), %r12d
movl %r15d, %r8d
cmpl $0x4, %r15d
jne 0x746f
movq 0x40(%rsp), %r15
movl (%r15,%r11,4), %r15d
jmp 0x7474
movzbl (%rdx,%r11), %r15d
cmpl %r15d, %r12d
movl %r8d, %r15d
jne 0x7491
incq %r11
cmpq %r11, %rdi
jne 0x7440
movl 0x1c(%rsp), %ecx
movslq %ecx, %rax
incl (%r14,%rax,4)
jmp 0x74a8
incl 0xc(%rsp)
movl $0x1, (%r14,%r13,4)
movl %ebp, 0x10(%rsp)
movl %eax, 0x14(%rsp)
movl %r13d, %ecx
movl %ebp, %eax
shrl $0x1f, %eax
addl %ebp, %eax
sarl %eax
addl 0x8(%rsp), %eax
cltq
movl %ecx, (%r14,%rax,4)
incq %r13
cmpq %rbx, %r13
movq 0x20(%rsp), %r10
jne 0x73e3
jmp 0x74d6
movl $0x0, 0xc(%rsp)
cmpl 0x8(%rsp), %r10d
movq 0x30(%rsp), %rdi
movl 0x18(%rsp), %r8d
jle 0x750f
decl %r8d
movslq %r10d, %rax
movslq 0x8(%rsp), %rcx
movl -0x4(%r14,%rax,4), %edx
movl %edx, %esi
negl %esi
jo 0x7507
movslq %r8d, %rsi
decl %r8d
movl %edx, (%r14,%rsi,4)
decq %rax
cmpq %rcx, %rax
jg 0x74f2
cmpl $0x2, 0x8(%rsp)
jl 0x754e
incq %rbx
movb $0x1, %al
movl -0x8(%rdi,%rbx,4), %esi
movslq -0xc(%rdi,%rbx,4), %rcx
movb $0x1, %dl
cmpl %esi, %ecx
jl 0x7533
cmpl %esi, %ecx
sete %dl
andb %dl, %al
movl %eax, %edx
testb %dl, %dl
je 0x7543
movl (%r14,%rcx,4), %eax
addl %ecx, %eax
decl %eax
movl %eax, -0xc(%rdi,%rbx,4)
decq %rbx
movl %edx, %eax
cmpq $0x2, %rbx
jg 0x751b
movl 0xc(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq