// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]lyndon-array/lib/bwt.c
// Function: bwt_lcp_inplace
int bwt_lcp_inplace(char *T, int n, int *LCP){

  int i, p, r=1, s;
  int p_a1, p_b1, l_a, l_b;
  LCP[n-1] = LCP[n-2] = 0;//base case

  for(s=n-3; s>=0; s--){
  
    /*steps 1 and 2*/
    p=r+1;
    for(i=s+1, r=0; T[i]!=0; i++)if(T[i]<=T[s])r++; //END_MARKER
    for(; i<n; i++) if(T[i]<T[s])r++;
    
    /*steps 2'*/
    p_a1=p+s-1;
    l_a=LCP[p_a1+1];
    while(T[p_a1]!=T[s])//RMQ function
      if(LCP[p_a1--]<l_a) l_a=LCP[p_a1+1];
    if(p_a1==s) l_a=0;
    else l_a++;
    
    /*steps 2''*/
    p_b1=p+s+1;
    l_b=LCP[p_b1];
    while(T[p_b1]!=T[s] && p_b1<n) //RMQ function
      if(LCP[++p_b1]<l_b) l_b=LCP[p_b1];
    if(p_b1==n) l_b=0;
    else l_b++;
    
    /*steps 3 and 4*/
    T[p+s]=T[s];
    for(i=s; i<s+r; i++){
      T[i]=T[i+1];
      LCP[i]=LCP[i+1];
    }
    T[s+r]=0; //END_MARKER;
    
    /*steps 4'*/
    LCP[s+r]=l_a;
    if(s+r+1<n)//If r+1 is not the last position
      LCP[s+r+1]=l_b;
    
    #if DEBUG
      printf("%c|%s\n", T[s], &T[s+1]);
      printf("lcp:\n");
      print_int(&LCP[s], n-s);
    #endif      
  }

return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movslq %esi, %rax
andq $0x0, -0x8(%rdx,%rax,4)
leal -0x3(%rax), %ecx
movq %rax, -0x8(%rsp)
addl $-0x2, %eax
leaq 0x1(%rdi), %r8
movq %r8, -0x30(%rsp)
leaq (%rax,%rdi), %r9
leaq 0x4(%rdx), %r8
movq %r8, -0x38(%rsp)
movslq %ecx, %r11
movl $0x1, %r12d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, -0x4c(%rsp)
movl %esi, %ecx
movq %rcx, -0x48(%rsp)
testl %r11d, %r11d
js 0x411e
movl %r11d, %r8d
movq -0x38(%rsp), %rcx
leaq (%rcx,%r8,4), %r13
movq -0x30(%rsp), %rcx
leaq (%rcx,%r8), %r14
movq -0x48(%rsp), %rcx
decl %ecx
movq %rcx, -0x48(%rsp)
xorl %r10d, %r10d
movl %eax, %ecx
movq %r9, -0x18(%rsp)
xorl %ebp, %ebp
movb (%r14,%r10), %bl
testb %bl, %bl
je 0x400a
xorl %r15d, %r15d
cmpb (%rdi,%r8), %bl
setle %r15b
addl %r15d, %ebp
incq %r10
incq %r9
incl %ecx
jmp 0x3fd7
movb (%r9), %r10b
xorl %ebx, %ebx
cmpb (%rdi,%r8), %r10b
setl %bl
addl %ebx, %ebp
incq %r9
incl %ecx
cmpl %esi, %ecx
jl 0x3ff7
movq %rax, -0x10(%rsp)
leal (%r8,%r12), %r9d
leal (%r8,%r12), %ecx
incl %ecx
movslq %ecx, %rax
movq %rax, -0x28(%rsp)
movl (%rdx,%rax,4), %r10d
movb (%rdi,%r8), %r15b
movl %r9d, -0x3c(%rsp)
movslq %r9d, %rcx
leaq (%rdi,%rcx), %rbx
leaq (%rdx,%rcx,4), %r9
xorl %ecx, %ecx
cmpb %r15b, (%rbx,%rcx)
je 0x4055
movl (%r9,%rcx,4), %eax
cmpl %r10d, %eax
cmovll %eax, %r10d
decq %rcx
jmp 0x403f
movl %r12d, %eax
negl %eax
movq %rax, -0x20(%rsp)
incl %r10d
movl -0x3c(%rsp), %eax
addl $0x2, %eax
cltq
movl (%rdx,%rax,4), %r9d
movq -0x48(%rsp), %rax
addl %r12d, %eax
movslq %eax, %rbx
addl -0x4c(%rsp), %r12d
cmpq -0x8(%rsp), %rbx
jge 0x409f
cmpb %r15b, (%rdi,%rbx)
je 0x409f
movl 0x4(%rdx,%rbx,4), %eax
incq %rbx
cmpl %r9d, %eax
cmovll %eax, %r9d
incl %r12d
jmp 0x407f
incl %r9d
movq -0x28(%rsp), %rax
movb %r15b, (%rdi,%rax)
movl %ebp, %r15d
addq %r8, %r15
xorl %r8d, %r8d
leal (%r11,%r8), %eax
cmpl %eax, %r15d
jle 0x40d5
movb (%r14,%r8), %al
movb %al, -0x1(%r14,%r8)
movl (%r13,%r8,4), %eax
movl %eax, -0x4(%r13,%r8,4)
incq %r8
jmp 0x40b4
cmpl %ecx, -0x20(%rsp)
movl $0x0, %eax
cmovel %eax, %r10d
movb $0x0, (%rdi,%r15)
movl %r10d, (%rdx,%r15,4)
leal 0x1(%r15), %ecx
cmpl %esi, %ecx
jge 0x4100
testl %r12d, %r12d
cmovel %r12d, %r9d
movl %ecx, %eax
movl %r9d, (%rdx,%rax,4)
decq %r11
movq -0x18(%rsp), %r9
decq %r9
movq -0x10(%rsp), %rax
decl %eax
decl -0x4c(%rsp)
movl %ebp, %r12d
jmp 0x3fa1
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq