// File: /workspace/github_c_makefile_star2_O3/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 %r8, 0x28(%rsp)
movq %rcx, %r9
movq %rdx, 0x30(%rsp)
movq %rsi, 0x20(%rsp)
movq %rcx, %rdx
subq %rbx, %rdx
jbe 0x852f
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
leaq 0x1(%rdx), %rcx
andq $-0x2, %rcx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq (%rdi,%rbx,8), %rdx
addq $0x8, %rdx
xorl %esi, %esi
movdqa 0x35f1(%rip), %xmm1     # 0xbab0
movdqa 0x35f9(%rip), %xmm2     # 0xbac0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movq %rsi, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
por %xmm1, %xmm4
pxor %xmm2, %xmm4
movdqa %xmm4, %xmm5
pcmpgtd %xmm0, %xmm5
pcmpeqd %xmm0, %xmm4
pshufd $0xf5, %xmm4, %xmm6     # xmm6 = xmm4[1,1,3,3]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm4     # xmm4 = xmm5[1,1,3,3]
por %xmm6, %xmm4
movd %xmm4, %r8d
notl %r8d
testb $0x1, %r8b
je 0x8512
movq %rax, -0x8(%rdx,%rsi,8)
pxor %xmm3, %xmm4
pextrw $0x4, %xmm4, %r8d
testb $0x1, %r8b
je 0x8526
movq %rax, (%rdx,%rsi,8)
addq $0x2, %rsi
cmpq %rsi, %rcx
jne 0x84cf
leaq -0x1(%r9), %r14
testq %rbx, %rbx
je 0x8644
leaq (%rdi,%rbx,8), %rax
movq %rax, 0x40(%rsp)
leaq -0x1(%r9), %rax
movq %rax, 0x38(%rsp)
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %r15d, %r15d
movq %r14, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movq %r9, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
movq (%rdi,%rbp,8), %r12
movq 0x20(%rsp), %rdi
movq %r9, %rsi
movq %r12, %rcx
movl 0x98(%rsp), %r8d
callq 0x83b4
cmpq %r15, %rax
jne 0x8607
testq %r15, %r15
je 0x85f7
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %rdi
leaq (%r8,%rdi,8), %rcx
leaq (%r8,%rdi), %rdx
movq 0x38(%rsp), %r9
movq %r9, %rsi
subq %rdi, %rsi
leaq (%r8,%r12,8), %rdi
addq %r12, %r8
subq %r12, %r9
xorl %r10d, %r10d
cmpq %r10, %r9
je 0x8607
cmpq %r10, %rsi
je 0x8607
cmpl $0x8, 0x98(%rsp)
jne 0x85e0
movq (%rdi,%r10,8), %r11
movq (%rcx,%r10,8), %r13
jmp 0x85ea
movzbl (%r8,%r10), %r11d
movzbl (%rdx,%r10), %r13d
cmpq %r13, %r11
jne 0x8607
incq %r10
cmpq %r10, %r15
jne 0x85c2
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %rcx
incq (%rdi,%rcx,8)
jmp 0x8624
incq 0x8(%rsp)
movq 0x10(%rsp), %rdi
movq $0x1, (%rdi,%rbp,8)
movq %r12, 0x18(%rsp)
movq %rax, %r15
movq %rbp, %rcx
andq $-0x2, %r12
movq 0x40(%rsp), %rax
movq %rcx, (%rax,%r12,4)
incq %rbp
cmpq %rbx, %rbp
movq 0x48(%rsp), %r9
jne 0x856d
jmp 0x864d
movq $0x0, 0x8(%rsp)
cmpq %rbx, %r14
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %rdx
jb 0x8681
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
decq %rdx
movq (%rdi,%r14,8), %rcx
cmpq %rax, %rcx
je 0x8679
movq %rcx, (%rdi,%rdx,8)
decq %rdx
decq %r14
cmpq %rbx, %r14
jae 0x8669
cmpq $0x1, %rbx
je 0x86bb
movb $0x1, %al
movq -0x10(%r8,%rbx,8), %rcx
cmpq -0x8(%r8,%rbx,8), %rcx
setl %dl
sete %sil
andb %sil, %al
orb %dl, %al
cmpb $0x1, %al
jne 0x86b2
movq (%rdi,%rcx,8), %rdx
addq %rdx, %rcx
decq %rcx
movq %rcx, -0x10(%r8,%rbx,8)
decq %rbx
cmpq $0x1, %rbx
jne 0x8689
movq 0x8(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq