// File: /workspace/github_cpp_makefile_star2_O1/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 0x108316
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 0x1081b0
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 0x108209
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)
movq 0x10(%rbx), %rax
movl $0x0, (%rax)
movq 0x10(%r15), %rcx
movl $0xffffffff, (%rcx)     # imm = 0xFFFFFFFF
movl $0x1, %edx
cmpl $0x1, %ebp
je 0x1082eb
cmpq $0x3, %r12
movl $0x2, %esi
cmovaeq %r12, %rsi
movl $0x1, %edi
leaq (%rdi,%rdi,4), %r8
movl 0x18(%r14,%r8,8), %r8d
movslq (%rax), %r9
leaq (%r9,%r9,4), %r9
movl 0x18(%r14,%r9,8), %r9d
cmpq %r9, %r8
jae 0x10825e
movl %edi, (%rax)
jmp 0x1082df
movslq -0x4(%rax,%rdx,4), %r9
leaq (%r9,%r9,4), %r10
cmpl 0x18(%r14,%r10,8), %r8d
jbe 0x10827a
movl %r9d, (%rcx,%rdi,4)
movl %edi, (%rax,%rdx,4)
incq %rdx
jmp 0x1082df
leaq -0x1(%rdx), %r9
testl %r9d, %r9d
jle 0x1082c2
leal 0x1(%r9), %r10d
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
shrl %r10d
addl %r11d, %r10d
movslq %r10d, %r15
movslq (%rax,%r15,4), %r15
leaq (%r15,%r15,4), %r15
movl 0x18(%r14,%r15,8), %r15d
cmpl %r8d, %r15d
jb 0x1082ab
movl %r10d, %r9d
cmpq %r8, %r15
jb 0x1082b3
movl %r11d, %r10d
movl %r10d, %r11d
movl %r9d, %r10d
subl %r11d, %r10d
cmpl $0x1, %r10d
jg 0x10828d
movslq %r9d, %r8
testl %r8d, %r8d
jle 0x1082d1
movl -0x4(%rax,%r8,4), %r9d
jmp 0x1082d7
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %r9d, (%rcx,%rdi,4)
movl %edi, (%rax,%r8,4)
incq %rdi
cmpq %rsi, %rdi
jne 0x10823d
testq %rdx, %rdx
jle 0x10830b
leaq (%rax,%rdx,4), %rdi
addq $-0x4, %rdi
movq %rdx, %rsi
movslq (%rdi), %rdi
movl %edi, -0x4(%rax,%rsi,4)
leaq (%rcx,%rdi,4), %rdi
decq %rsi
jne 0x1082fb
movq %rdx, (%rbx)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq