// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: quick_LIS(asg_arc_t_offset*, unsigned int, kvec_t_i32_warp*, kvec_t_i32_warp*)
void quick_LIS(asg_arc_t_offset* x, uint32_t n, kvec_t_i32_warp* tailIndex, kvec_t_i32_warp* prevIndex)
{
    tailIndex->a.n = prevIndex->a.n = 0;
    if(n == 0) return;

    kv_resize(int32_t, tailIndex->a, n);
    kv_resize(int32_t, prevIndex->a, n);

    long long len = 1, i, pos, m; ///the length of chain must be >=1
    tailIndex->a.a[0] = 0;
    prevIndex->a.a[0] = -1;

    ///x has already sorted by x_pos
    for(i = 1; i < (long long)n; i++) 
    { 
        if(Get_yOff(x[i].Off) < Get_yOff(x[tailIndex->a.a[0]].Off))  
        { 
            // new smallest value 
            tailIndex->a.a[0] = i; ///doesn't matter too much
        } 
        else if(Get_yOff(x[i].Off) > Get_yOff(x[tailIndex->a.a[len - 1]].Off)) 
        { 
            // arr[i] wants to extend largest subsequence 
            prevIndex->a.a[i] = tailIndex->a.a[len - 1]; 
            tailIndex->a.a[len++] = i; 
        } 
        else 
        { 
            // arr[i] wants to be a potential condidate of 
            // future subsequence 
            // It will replace ceil value in tailIndices 
            pos = GetCeilIndex(x, tailIndex, -1, len - 1, Get_yOff(x[i].Off)); 
            prevIndex->a.a[i] = pos > 0? tailIndex->a.a[pos - 1] : -1; 
            tailIndex->a.a[pos] = i; 
        } 
    }


    for (m = 0, i = tailIndex->a.a[len - 1]; m < len; i = prevIndex->a.a[i], m++)
    {
        tailIndex->a.a[len-m-1] = i;
    }

    tailIndex->a.n = len;
}

// Assembly:
xorl %eax, %eax
movq %rax, (%rcx)
movq %rax, (%rdx)
testl %esi, %esi
je 0x10cc5c
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rcx, %r15
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
movl %esi, %r12d
cmpq %r12, 0x8(%rdx)
jae 0x10cae0
leaq -0x1(%r12), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
cmpq %r12, 0x8(%r15)
jae 0x10cb3b
leaq -0x1(%r12), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r15)
movq 0x10(%r15), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movq %rax, 0x10(%r15)
jmp 0x10cb3f
movq 0x10(%r15), %rax
movq 0x10(%rbx), %rcx
movl $0x0, (%rcx)
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
cmpl $0x1, %ebp
jne 0x10cb62
addq $0x4, %rcx
movl $0x1, %edx
jmp 0x10cc33
cmpq $0x3, %r12
movl $0x2, %esi
cmovaeq %r12, %rsi
movl $0x1, %edx
movl $0x1, %edi
leaq (%rdi,%rdi,4), %r8
movl 0x18(%r14,%r8,8), %r8d
movslq (%rcx), %r9
leaq (%r9,%r9,4), %r9
movl 0x18(%r14,%r9,8), %r9d
cmpq %r9, %r8
jae 0x10cb9a
movl %edi, (%rcx)
jmp 0x10cc1e
movslq -0x4(%rcx,%rdx,4), %r9
leaq (%r9,%r9,4), %r10
cmpl 0x18(%r14,%r10,8), %r8d
jbe 0x10cbb6
movl %r9d, (%rax,%rdi,4)
movl %edi, (%rcx,%rdx,4)
incq %rdx
jmp 0x10cc1e
leaq -0x1(%rdx), %r9
testl %r9d, %r9d
jle 0x10cc0d
leal 0x1(%r9), %r10d
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
shrl %r10d
addl %r11d, %r10d
movslq %r10d, %r15
movslq (%rcx,%r15,4), %r15
leaq (%r15,%r15,4), %r15
movl 0x18(%r14,%r15,8), %r15d
cmpl %r8d, %r15d
jb 0x10cbe7
movl %r10d, %r9d
cmpq %r8, %r15
jb 0x10cbef
movl %r11d, %r10d
movl %r10d, %r11d
movl %r9d, %r10d
subl %r11d, %r10d
cmpl $0x1, %r10d
jg 0x10cbc9
movslq %r9d, %r8
testl %r9d, %r9d
jle 0x10cc10
movl -0x4(%rcx,%r8,4), %r9d
jmp 0x10cc16
movslq %r9d, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %r9d, (%rax,%rdi,4)
movl %edi, (%rcx,%r8,4)
incq %rdi
cmpq %rsi, %rdi
jne 0x10cb79
testq %rdx, %rdx
jle 0x10cc51
leaq (%rcx,%rdx,4), %rcx
addq $-0x4, %rcx
xorl %esi, %esi
movq %rcx, %rdi
movslq (%rdi), %rdi
movl %edi, (%rcx,%rsi,4)
leaq (%rax,%rdi,4), %rdi
decq %rsi
movq %rdx, %r8
addq %rsi, %r8
jne 0x10cc3c
movq %rdx, (%rbx)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq