// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movl %r8d, 0x10(%rsp)
movl %ecx, %r10d
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movl %r9d, (%rsp)
cmpl %ecx, %r9d
jge 0x7614
movslq (%rsp), %rcx
movslq %r10d, %rdx
subq %rcx, %rdx
leaq 0x3(%rdx), %rax
andq $-0x4, %rax
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq (%rdi,%rcx,4), %rcx
addq $0xc, %rcx
xorl %edx, %edx
movdqa 0x2272(%rip), %xmm1     # 0x97b0
movdqa 0x227a(%rip), %xmm2     # 0x97c0
movdqa 0x2282(%rip), %xmm3     # 0x97d0
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movq %rdx, %xmm5
pshufd $0x44, %xmm5, %xmm5     # xmm5 = xmm5[0,1,0,1]
movdqa %xmm5, %xmm6
por %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %esi
notl %esi
testb $0x1, %sil
je 0x75a1
movl $0x80000000, -0xc(%rcx,%rdx,4) # imm = 0x80000000
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0x75b8
movl $0x80000000, -0x8(%rcx,%rdx,4) # imm = 0x80000000
por %xmm1, %xmm5
pxor %xmm3, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
pxor %xmm4, %xmm5
pextrw $0x0, %xmm5, %esi
testb $0x1, %sil
je 0x75f5
movl $0x80000000, -0x4(%rcx,%rdx,4) # imm = 0x80000000
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x7607
movl $0x80000000, (%rcx,%rdx,4) # imm = 0x80000000
addq $0x4, %rdx
cmpq %rdx, %rax
jne 0x7556
movl (%rsp), %eax
movl %eax, %ebx
testl %eax, %eax
jle 0x772a
leal -0x1(%r10), %eax
movl %eax, 0xc(%rsp)
cltq
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
xorl %ecx, %ecx
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movq %r10, 0x30(%rsp)
movl %ecx, 0x14(%rsp)
movq %rdi, %rbp
movl (%rdi,%r15,4), %r12d
movq 0x18(%rsp), %rdi
movl %r10d, %esi
movl %r12d, %ecx
movl 0x78(%rsp), %r8d
callq 0x7421
cmpl 0x4(%rsp), %eax
jne 0x76e8
cmpl $0x0, 0x4(%rsp)
jle 0x76d8
movslq %r12d, %rcx
movslq 0xc(%rsp), %rdx
movq 0x28(%rsp), %r9
movl %r9d, %esi
subl %edx, %esi
movl 0x4(%rsp), %edi
movq 0x18(%rsp), %r11
leaq (%r11,%rdx,4), %r8
addq %r11, %rdx
subq %rcx, %r9
leaq (%r11,%rcx,4), %r10
addq %r11, %rcx
xorl %r11d, %r11d
cmpq %r11, %r9
je 0x76e8
cmpq %r11, %rsi
je 0x76e8
cmpl $0x4, 0x78(%rsp)
jne 0x76c1
movl (%r10,%r11,4), %r14d
movl (%r8,%r11,4), %r13d
jmp 0x76cb
movzbl (%rcx,%r11), %r14d
movzbl (%rdx,%r11), %r13d
cmpl %r13d, %r14d
jne 0x76e8
incq %r11
cmpq %r11, %rdi
jne 0x76a6
movl 0x14(%rsp), %ecx
movslq %ecx, %rax
movq %rbp, %rdi
incl (%rbp,%rax,4)
jmp 0x7704
incl 0x8(%rsp)
movq %rbp, %rdi
movl $0x1, (%rbp,%r15,4)
movl %r12d, 0xc(%rsp)
movl %eax, 0x4(%rsp)
movl %r15d, %ecx
movl %r12d, %eax
shrl $0x1f, %eax
addl %r12d, %eax
sarl %eax
addl (%rsp), %eax
cltq
movl %ecx, (%rdi,%rax,4)
incq %r15
cmpq %rbx, %r15
movq 0x30(%rsp), %r10
jne 0x764a
jmp 0x7732
movl $0x0, 0x8(%rsp)
cmpl %r10d, (%rsp)
movq 0x20(%rsp), %r8
movl 0x10(%rsp), %r9d
jge 0x7767
decl %r9d
movslq %r10d, %rax
movslq (%rsp), %rcx
movl -0x4(%rdi,%rax,4), %edx
movl %edx, %esi
negl %esi
jo 0x775f
movslq %r9d, %rsi
decl %r9d
movl %edx, (%rdi,%rsi,4)
decq %rax
cmpq %rcx, %rax
jg 0x774c
cmpl $0x2, (%rsp)
jl 0x77a1
incq %rbx
movb $0x1, %al
movslq -0xc(%r8,%rbx,4), %rcx
cmpl -0x8(%r8,%rbx,4), %ecx
setl %dl
sete %sil
andb %sil, %al
orb %dl, %al
cmpb $0x1, %al
jne 0x7798
movl (%rdi,%rcx,4), %edx
addl %edx, %ecx
decl %ecx
movl %ecx, -0xc(%r8,%rbx,4)
decq %rbx
cmpq $0x2, %rbx
ja 0x7772
movl 0x8(%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq