// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/tools/lib/lcp_array.c
// Function: lcp_array_check_phi
int lcp_array_check_phi(unsigned char *T, int_t *SA, int_t *LCP, uint_t n, int cs, unsigned char separator){
  
  uint_t i;
  uint64_t sum=0;
  int_t maximum=0;
  int_t previous_lcp = 1;

  uint_t *ISA = (uint_t* ) malloc(n * sizeof(uint_t));
  find_inverse(SA, n, ISA);

  for(i=1;i<n;i++) {

    int_t k=ISA[i]; 
    if(k==0) continue;

    int_t j=SA[ISA[i]-1];
    int_t h=previous_lcp-1;

    for(h=0;i+h<n && j+h<n;h++) if(chr(i+h)!=chr(j+h) || (chr(i+h)==separator && chr(j+h)==separator)) break;

    if(LCP[k]!=h) {
    printf("%" PRIdN " %" PRIdN "\t%" PRIdN "\n",k,j, previous_lcp);
      fprintf(stdout,"isNotLCP! Incorrect LCP value: LCP[%" PRIdN "]=%" PRIdN "!=%" PRIdN "\t(%" PRIdN ")\n", k, LCP[k],h, SA[i]);
      //return 0;
    }
    sum+=LCP[i];
    maximum=max(maximum, LCP[i]);

    previous_lcp = max(1,LCP[i]);
  }
  
  printf("LCP_mean = %.2lf\n", (double)sum/(double)n);
  printf("LCP_max = %" PRIdN "\n", maximum);

  fprintf(stderr, "%.2lf\t %" PRIdN "\n", (double)sum/(double)n, maximum);

  free(ISA);
  
return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, %ebx
movl %r8d, %r12d
movq %rcx, %r15
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, %r13
leaq (,%rcx,8), %rdi
callq 0x92c8
movq %r14, 0x20(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %rax, 0x28(%rsp)
movq %rax, %rdx
callq 0x5258
movl %ebx, %r9d
movl %ebx, %r10d
leaq 0x8(%r13), %r11
movq %r13, 0x48(%rsp)
leaq 0x1(%r13), %r14
pushq $0x1
popq %rbp
movq %rbp, %rcx
xorl %r8d, %r8d
xorl %edx, %edx
movl %ebx, 0xc(%rsp)
movq %r10, 0x30(%rsp)
cmpq %r15, %rbp
jae 0x57a4
movq 0x28(%rsp), %rax
movq (%rax,%rbp,8), %rbx
testq %rbx, %rbx
je 0x5795
movq %rdx, 0x50(%rsp)
movq 0x20(%rsp), %rax
movq -0x8(%rax,%rbx,8), %rdx
movq 0x48(%rsp), %rsi
leaq (%rsi,%rdx,8), %rax
addq %rdx, %rsi
xorl %r13d, %r13d
movq %rbp, %rdi
addq %r13, %rdi
cmpq %r15, %rdi
jae 0x5708
leaq (%rdx,%r13), %rdi
cmpq %r15, %rdi
jae 0x5708
cmpl $0x8, %r12d
jne 0x56f4
movq (%r11,%r13,8), %rdi
cmpq (%rax,%r13,8), %rdi
jne 0x5708
cmpq %r10, %rdi
jne 0x5703
jmp 0x5708
movb (%r14,%r13), %dil
cmpb (%rsi,%r13), %dil
jne 0x5708
cmpb %r9b, %dil
je 0x5708
incq %r13
jmp 0x56c9
movq 0x10(%rsp), %rax
cmpq %r13, (%rax,%rbx,8)
je 0x5777
leaq 0x535f(%rip), %rdi      # 0xaa79
movq %rbx, %rsi
xorl %eax, %eax
movq %r8, 0x40(%rsp)
movq %r11, 0x38(%rsp)
callq 0x20f0
movq 0x8893(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
movq 0x10(%rsp), %rax
movq (%rax,%rbx,8), %rcx
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %r9
leaq 0x52c2(%rip), %rsi      # 0xaa13
movq %rbx, %rdx
movq %r13, %r8
xorl %eax, %eax
callq 0x2190
movq 0x38(%rsp), %r11
movq 0x30(%rsp), %r10
movq 0x10(%rsp), %rax
movl 0xc(%rsp), %r9d
movq 0x40(%rsp), %r8
movq (%rax,%rbp,8), %rcx
movq 0x50(%rsp), %rdx
addq %rcx, %rdx
cmpq %rcx, %r8
cmovleq %rcx, %r8
cmpq $0x2, %rcx
pushq $0x1
popq %rax
cmovlq %rax, %rcx
incq %rbp
addq $0x8, %r11
incq %r14
jmp 0x5690
movq %rdx, %xmm0
movq 0x485f(%rip), %xmm1     # 0xa010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x4863(%rip), %xmm2     # 0xa020
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movq %r15, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movapd %xmm3, 0x10(%rsp)
leaq 0x5261(%rip), %rdi      # 0xaa58
movapd %xmm3, %xmm0
movb $0x1, %al
movq %r8, %rbx
callq 0x20f0
leaq 0x525e(%rip), %rdi      # 0xaa6a
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20f0
movq 0x87db(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x525f(%rip), %rsi      # 0xaa86
movaps 0x10(%rsp), %xmm0
movq %rbx, %rdx
movb $0x1, %al
callq 0x2190
movq 0x28(%rsp), %rdi
callq 0x939e
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq