// File: /workspace/github_c_makefile_star2_O1/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:
movslq %esi, %rax
movq $0x0, -0x8(%rdx,%rax,4)
cmpl $0x3, %eax
jl 0x4e60
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leal -0x3(%rsi), %eax
leal -0x2(%rsi), %r12d
movl %esi, %r13d
leaq -0x1(%rdi), %rcx
movq %rcx, -0x28(%rsp)
leal -0x1(%rsi), %ecx
movl %ecx, -0x2c(%rsp)
leaq (%rdx,%rax,4), %r11
addq $0x4, %r11
leaq (%rax,%rdi), %rbx
incq %rbx
movl $0x1, %r8d
movq %r13, -0x20(%rsp)
movb 0x1(%rdi,%rax), %cl
testb %cl, %cl
je 0x4cec
movb (%rdi,%rax), %r10b
xorl %r15d, %r15d
movq %r12, %r9
xorl %ebp, %ebp
cmpb %r10b, %cl
setle %bpl
addl %ebp, %r15d
movb 0x1(%rdi,%r9), %cl
incq %r9
testb %cl, %cl
jne 0x4cd2
jmp 0x4cf3
leaq 0x1(%rax), %r9
xorl %r15d, %r15d
cmpl %esi, %r9d
jge 0x4d18
movb (%rdi,%rax), %r10b
movl %r9d, %r14d
addq %rdi, %r14
xorl %ecx, %ecx
cmpb %r10b, (%r14)
setl %cl
addl %ecx, %r15d
incq %r14
incl %r9d
cmpl %esi, %r9d
jl 0x4d02
leal (%r8,%rax), %r14d
leal (%r8,%rax), %ecx
incl %ecx
movslq %ecx, %rcx
movq %rcx, -0x8(%rsp)
movl (%rdx,%rcx,4), %ebp
movb (%rdi,%rax), %r9b
movslq %r14d, %rcx
cmpb %r9b, (%rdi,%rcx)
jne 0x4d3f
movl %r14d, %r10d
jmp 0x4d88
movq %r12, -0x10(%rsp)
movq %r8, -0x18(%rsp)
leal (%r8,%rax), %r10d
movslq %r10d, %rcx
leaq (%rdx,%rcx,4), %r13
addq -0x28(%rsp), %rcx
xorl %r8d, %r8d
movq %r8, %r12
movl (%r13,%r8,4), %r8d
cmpl %ebp, %r8d
cmovll %r8d, %ebp
leaq -0x1(%r12), %r8
cmpb %r9b, (%rcx,%r12)
jne 0x4d5c
addl %r8d, %r10d
movq -0x10(%rsp), %r12
movq -0x20(%rsp), %r13
movq -0x18(%rsp), %r8
movl %r10d, %ecx
incl %ebp
cmpq %rcx, %rax
movl $0x0, %ecx
cmovel %ecx, %ebp
addl $0x2, %r14d
movslq %r14d, %rcx
movl (%rdx,%rcx,4), %r10d
cmpl %esi, %ecx
jge 0x4ddb
cmpb %r9b, (%rdi,%rcx)
je 0x4ddb
addl -0x2c(%rsp), %r8d
movslq %r8d, %rcx
incq %rcx
movl %r8d, %r14d
movl (%rdx,%rcx,4), %r8d
cmpl %r10d, %r8d
cmovll %r8d, %r10d
incl %r14d
cmpq %r13, %rcx
jge 0x4ddb
leaq 0x1(%rcx), %r8
cmpb %r9b, (%rdi,%rcx)
movq %r8, %rcx
jne 0x4dbb
incl %r10d
cmpl %esi, %r14d
movl $0x0, %ecx
cmovel %ecx, %r10d
movq -0x8(%rsp), %rcx
movb %r9b, (%rdi,%rcx)
leal (%r15,%rax), %r9d
testl %r15d, %r15d
jle 0x4e1c
xorl %ecx, %ecx
movb (%rbx,%rcx), %r8b
movb %r8b, -0x1(%rbx,%rcx)
movl (%r11,%rcx,4), %r8d
movl %r8d, -0x4(%r11,%rcx,4)
incq %rcx
leal (%rcx,%rax), %r8d
cmpl %r8d, %r9d
jg 0x4dfe
movslq %r9d, %rcx
movb $0x0, (%rdi,%rcx)
movl %ebp, (%rdx,%rcx,4)
incl %r9d
cmpl %esi, %r9d
jge 0x4e35
movslq %r9d, %rcx
movl %r10d, (%rdx,%rcx,4)
leaq -0x1(%rax), %rcx
decq %r12
decl -0x2c(%rsp)
addq $-0x4, %r11
decq %rbx
movl %r15d, %r8d
testq %rax, %rax
movq %rcx, %rax
jg 0x4cc0
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
xorl %eax, %eax
retq