// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %r9, %rbx
movq %rcx, %r9
movq %rsi, 0x18(%rsp)
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
movq %rbx, %rax
cmpq %r9, %rax
jae 0x7c74
movq %rcx, (%rdi,%rax,8)
incq %rax
jmp 0x7c66
movq %r8, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
leaq -0x1(%r9), %rbp
leaq (%rdi,%rbx,8), %rax
movq %rax, 0x30(%rsp)
movq $0x0, (%rsp)
movq %rbp, 0x10(%rsp)
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %r12d, %r12d
movq %rdi, 0x8(%rsp)
movq %r9, 0x38(%rsp)
cmpq %rbx, %r12
je 0x7d7c
movq %rcx, 0x40(%rsp)
movq (%rdi,%r12,8), %r14
movq 0x18(%rsp), %rdi
movq %r9, %rsi
movq %r14, %rcx
movl 0x88(%rsp), %r8d
callq 0x7b86
cmpq %r13, %rax
jne 0x7d36
movq 0x18(%rsp), %r8
leaq (%r8,%r14,8), %rcx
leaq (%r8,%r14), %rdx
movq %rbp, %rsi
subq %r14, %rsi
movq 0x10(%rsp), %r10
leaq (%r8,%r10,8), %rdi
addq %r10, %r8
movq %rbp, %r9
subq %r10, %r9
xorl %r10d, %r10d
cmpq %r10, %r13
je 0x7d6c
cmpq %r10, %rsi
je 0x7d36
cmpq %r10, %r9
je 0x7d36
cmpl $0x8, 0x88(%rsp)
jne 0x7d24
movq (%rcx,%r10,8), %r11
movq (%rdi,%r10,8), %r15
jmp 0x7d2e
movzbl (%rdx,%r10), %r11d
movzbl (%r8,%r10), %r15d
incq %r10
cmpq %r15, %r11
je 0x7d01
incq (%rsp)
movq 0x8(%rsp), %rdi
movq $0x1, (%rdi,%r12,8)
movq %r14, 0x10(%rsp)
movq %rax, %r13
movq %r12, %rcx
andq $-0x2, %r14
movq 0x30(%rsp), %rax
movq %rcx, (%rax,%r14,4)
incq %r12
movq 0x38(%rsp), %r9
jmp 0x7caa
movq 0x8(%rsp), %rdi
movq 0x40(%rsp), %rcx
incq (%rdi,%rcx,8)
jmp 0x7d52
movq 0x20(%rsp), %rcx
decq %rcx
movq 0x28(%rsp), %r8
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
cmpq %rbx, %rbp
jb 0x7dad
movq (%rdi,%rbp,8), %rax
cmpq %rdx, %rax
je 0x7da8
movq %rax, (%rdi,%rcx,8)
decq %rcx
decq %rbp
jmp 0x7d93
movb $0x1, %al
cmpq $0x1, %rbx
je 0x7de1
movq -0x10(%r8,%rbx,8), %rcx
cmpq -0x8(%r8,%rbx,8), %rcx
setl %dl
sete %sil
andb %sil, %al
orb %dl, %al
je 0x7ddc
movq (%rdi,%rcx,8), %rdx
addq %rdx, %rcx
decq %rcx
movq %rcx, -0x10(%r8,%rbx,8)
decq %rbx
jmp 0x7daf
movq (%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq