// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq $0x0, -0x8(%rdx,%r12,4)
cmpl $0x3, %r12d
jl 0x4e6c
leal -0x3(%rsi), %ecx
leal -0x2(%rsi), %r14d
leaq -0x1(%rdi), %rax
movq %rax, -0x28(%rsp)
leal -0x1(%rsi), %eax
movl %eax, -0x2c(%rsp)
leaq (%rdx,%rcx,4), %r11
addq $0x4, %r11
leaq (%rcx,%rdi), %rbx
incq %rbx
movl $0x1, %r13d
movq %r12, -0x20(%rsp)
movb 0x1(%rdi,%rcx), %r8b
testb %r8b, %r8b
je 0x4cfd
movb (%rdi,%rcx), %r10b
xorl %r15d, %r15d
movq %r14, %r9
xorl %ebp, %ebp
cmpb %r10b, %r8b
setle %bpl
addl %ebp, %r15d
movb 0x1(%rdi,%r9), %r8b
incq %r9
testb %r8b, %r8b
jne 0x4ce2
jmp 0x4d04
leaq 0x1(%rcx), %r9
xorl %r15d, %r15d
movb (%rdi,%rcx), %bpl
cmpl %esi, %r9d
movq %r14, -0x8(%rsp)
jge 0x4d30
movl %r9d, %r10d
addq %rdi, %r10
xorl %r8d, %r8d
cmpb %bpl, (%r10)
setl %r8b
addl %r8d, %r15d
incq %r10
incl %r9d
cmpl %esi, %r9d
jl 0x4d18
leal (%rcx,%r13), %r14d
leal (%rcx,%r13), %r8d
incl %r8d
movslq %r8d, %r9
movq %r13, %rax
movl (%rdx,%r9,4), %r13d
movslq %r14d, %r8
cmpb %bpl, (%rdi,%r8)
movq %rax, -0x10(%rsp)
jne 0x4d58
movl %r14d, %r10d
jmp 0x4d95
movq %r9, -0x18(%rsp)
leal (%rax,%rcx), %r10d
movslq %r10d, %r8
leaq (%rdx,%r8,4), %r9
addq -0x28(%rsp), %r8
xorl %eax, %eax
movq %rax, %r12
movl (%r9,%rax,4), %eax
cmpl %r13d, %eax
cmovll %eax, %r13d
leaq -0x1(%r12), %rax
cmpb %bpl, (%r8,%r12)
jne 0x4d6f
addl %eax, %r10d
movq -0x20(%rsp), %r12
movq -0x18(%rsp), %r9
movl %r10d, %eax
incl %r13d
cmpq %rax, %rcx
movl $0x0, %eax
cmovel %eax, %r13d
addl $0x2, %r14d
movslq %r14d, %r8
movl (%rdx,%r8,4), %r10d
cmpl %esi, %r8d
jge 0x4def
cmpb %bpl, (%rdi,%r8)
je 0x4def
movq -0x10(%rsp), %rax
addl -0x2c(%rsp), %eax
movslq %eax, %r8
incq %r8
movl %eax, %r14d
movl (%rdx,%r8,4), %eax
cmpl %r10d, %eax
cmovll %eax, %r10d
incl %r14d
cmpq %r12, %r8
jge 0x4def
leaq 0x1(%r8), %rax
cmpb %bpl, (%rdi,%r8)
movq %rax, %r8
jne 0x4dcf
incl %r10d
cmpl %esi, %r14d
movl $0x0, %eax
cmovel %eax, %r10d
movb %bpl, (%rdi,%r9)
leal (%r15,%rcx), %r9d
testl %r15d, %r15d
jle 0x4e2c
xorl %r8d, %r8d
movb (%rbx,%r8), %al
movb %al, -0x1(%rbx,%r8)
movl (%r11,%r8,4), %eax
movl %eax, -0x4(%r11,%r8,4)
incq %r8
leal (%r8,%rcx), %eax
cmpl %eax, %r9d
jg 0x4e0e
movslq %r9d, %rax
movb $0x0, (%rdi,%rax)
movl %r13d, (%rdx,%rax,4)
incl %r9d
cmpl %esi, %r9d
movq -0x8(%rsp), %r14
jge 0x4e4b
movslq %r9d, %rax
movl %r10d, (%rdx,%rax,4)
leaq -0x1(%rcx), %rax
decq %r14
decl -0x2c(%rsp)
addq $-0x4, %r11
decq %rbx
movl %r15d, %r13d
testq %rcx, %rcx
movq %rax, %rcx
jg 0x4cce
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq