// File: /workspace/github_c_makefile_star2_O3/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, 0xc(%rsp)
movl %r8d, 0x8(%rsp)
movq %rcx, %r14
movq %rdx, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, 0x20(%rsp)
leaq (,%rcx,8), %rdi
callq 0x9fe6
xorpd %xmm0, %xmm0
testq %r14, %r14
movq %rax, 0x18(%rsp)
je 0x24ba
xorl %edx, %edx
movq (%rbx,%rdx,8), %rcx
movq %rdx, (%rax,%rcx,8)
incq %rdx
cmpq %rdx, %r14
jne 0x249a
cmpq $0x1, %r14
movl 0xc(%rsp), %r10d
movl 0x8(%rsp), %r11d
jne 0x24c1
xorl %ebx, %ebx
jmp 0x2612
movzbl %r10b, %ecx
movq %rcx, 0x30(%rsp)
movq 0x20(%rsp), %rcx
leaq 0x8(%rcx), %rbp
leaq 0x1(%rcx), %r13
pushq $0x1
popq %rcx
movq %rcx, %r12
xorl %esi, %esi
xorl %r9d, %r9d
movq %rbx, 0x28(%rsp)
movq (%rax,%r12,8), %r15
testq %r15, %r15
je 0x25db
movq %rsi, 0x50(%rsp)
movq -0x8(%rbx,%r15,8), %rdx
movq 0x20(%rsp), %rsi
leaq (%rsi,%rdx,8), %r8
addq %rdx, %rsi
xorl %ebx, %ebx
leaq (%rdx,%rbx), %rdi
cmpq %r14, %rdi
jae 0x254e
cmpl $0x8, %r11d
jne 0x252f
movq (%rbp,%rbx,8), %rdi
cmpq (%r8,%rbx,8), %rdi
jne 0x254e
cmpq 0x30(%rsp), %rdi
jne 0x253f
jmp 0x254e
movb (%r13,%rbx), %dil
cmpb (%rsi,%rbx), %dil
jne 0x254e
cmpb %r10b, %dil
je 0x254e
leaq (%r12,%rbx), %rdi
incq %rdi
incq %rbx
cmpq %r14, %rdi
jb 0x250c
movq 0x10(%rsp), %rsi
cmpq %rbx, (%rsi,%r15,8)
je 0x25b8
leaq 0x9521(%rip), %rdi      # 0xba81
movq %r15, %rsi
xorl %eax, %eax
movq %r9, 0x48(%rsp)
callq 0x20f0
movq 0xca52(%rip), %rax      # 0xefc8
movq (%rax), %rdi
movq 0x10(%rsp), %rax
movq (%rax,%r15,8), %rcx
movq 0x28(%rsp), %rax
movq (%rax,%r12,8), %r9
leaq 0x9489(%rip), %rsi      # 0xba1b
movq %r15, %rdx
movq %rbx, %r8
xorl %eax, %eax
callq 0x2190
movq 0x10(%rsp), %rsi
movl 0x8(%rsp), %r11d
movl 0xc(%rsp), %r10d
movq 0x48(%rsp), %r9
movq 0x18(%rsp), %rax
movq (%rsi,%r12,8), %rcx
movq 0x50(%rsp), %rsi
addq %rcx, %rsi
cmpq %rcx, %r9
cmovleq %rcx, %r9
cmpq $0x2, %rcx
pushq $0x1
popq %rdx
cmovlq %rdx, %rcx
movq 0x28(%rsp), %rbx
incq %r12
addq $0x8, %rbp
incq %r13
cmpq %r14, %r12
jne 0x24e7
movq %rsi, %xmm1
punpckldq 0x8a15(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x8a1d(%rip), %xmm1     # 0xb020
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %r9, %rbx
movq %r14, %xmm2
punpckldq 0x89f1(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x89f9(%rip), %xmm2     # 0xb020
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movapd %xmm0, 0x30(%rsp)
leaq 0x941c(%rip), %rdi      # 0xba60
movb $0x1, %al
callq 0x20f0
leaq 0x9420(%rip), %rdi      # 0xba72
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20f0
movq 0xc995(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x9421(%rip), %rsi      # 0xba8e
movaps 0x30(%rsp), %xmm0
movq %rbx, %rdx
movb $0x1, %al
callq 0x2190
movq 0x18(%rsp), %rdi
callq 0xa09d
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq