// File: /workspace/github_cpp_makefile_star2_O2/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:
andq $0x0, (%rcx)
andq $0x0, (%rdx)
testl %esi, %esi
je 0xc9db1
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r14
movq %rdx, %r9
movq %rdi, %r15
movl %esi, %r12d
cmpq %r12, 0x8(%rdx)
movq %rdx, 0x8(%rsp)
jae 0xc9c72
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(%r9)
movq 0x10(%r9), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movq 0x8(%rsp), %r9
movq %rax, 0x10(%r9)
cmpq %r12, 0x8(%r14)
movq %r14, 0x10(%rsp)
jae 0xc9cd7
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(%r14)
movq 0x10(%r14), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movq 0x8(%rsp), %r9
movq %rax, 0x10(%r14)
jmp 0xc9cdb
movq 0x10(%r14), %rax
movq 0x10(%r9), %rcx
andl $0x0, (%rcx)
orl $-0x1, (%rax)
leaq 0x40(%r15), %r14
pushq $0x1
popq %r13
movq %r13, %rbp
cmpq %r12, %r13
jae 0xc9d70
movq (%r14), %r8
movl %r8d, %edx
movslq (%rcx), %rsi
imulq $0x28, %rsi, %rsi
movl 0x18(%r15,%rsi), %esi
cmpq %rsi, %rdx
jae 0xc9d11
movl %r13d, (%rcx)
jmp 0xc9d67
movslq -0x4(%rcx,%rbp,4), %rsi
imulq $0x28, %rsi, %rdi
cmpl 0x18(%r15,%rdi), %edx
jbe 0xc9d2e
movl %esi, (%rax,%r13,4)
movl %r13d, (%rcx,%rbp,4)
incq %rbp
jmp 0xc9d67
leaq -0x1(%rbp), %rcx
movq %r15, %rdi
movq %r9, %rsi
pushq $-0x1
popq %rbx
movl %ebx, %edx
callq 0xd3ea9
movq 0x8(%rsp), %r9
movslq %eax, %rdx
movq 0x10(%r9), %rcx
testl %edx, %edx
jle 0xc9d56
movl -0x4(%rcx,%rdx,4), %ebx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl %ebx, (%rax,%r13,4)
movl %r13d, (%rcx,%rdx,4)
incq %r13
addq $0x28, %r14
jmp 0xc9cf0
leaq (%rcx,%rbp,4), %rcx
addq $-0x4, %rcx
xorl %edx, %edx
testq %rbp, %rbp
movl $0x0, %esi
cmovgq %rbp, %rsi
negq %rsi
movq %rcx, %rdi
cmpq %rdx, %rsi
je 0xc9da0
movslq (%rdi), %rdi
movl %edi, (%rcx,%rdx,4)
leaq (%rax,%rdi,4), %rdi
decq %rdx
jmp 0xc9d8c
movq %rbp, (%r9)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq