// File: 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:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
cmpl $0x0, -0xc(%rbp)
jne 0x1732e8
jmp 0x1736bf
jmp 0x1732ea
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0xc(%rbp), %ecx
cmpq %rcx, %rax
jae 0x1733c6
movl -0xc(%rbp), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq -0x18(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq -0x18(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq -0x18(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq -0x18(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq -0x18(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x1733c8
jmp 0x1733ca
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movl -0xc(%rbp), %ecx
cmpq %rcx, %rax
jae 0x1734a6
movl -0xc(%rbp), %eax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq -0x20(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq -0x20(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq -0x20(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq -0x20(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq -0x20(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x1734a8
movq $0x1, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl $0x0, (%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq $0x1, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl -0xc(%rbp), %ecx
cmpq %rcx, %rax
jge 0x17364b
movq -0x8(%rbp), %rax
imulq $0x28, -0x30(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl %eax, %eax
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rdx
movq 0x10(%rdx), %rdx
movslq (%rdx), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq 0x18(%rcx), %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jge 0x17352c
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl %ecx, (%rax)
jmp 0x173638
movq -0x8(%rbp), %rax
imulq $0x28, -0x30(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl %eax, %eax
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rdx
movq 0x10(%rdx), %rdx
movq -0x28(%rbp), %rsi
subq $0x1, %rsi
movslq (%rdx,%rsi,4), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq 0x18(%rcx), %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jle 0x1735af
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
subq $0x1, %rcx
movl (%rax,%rcx,4), %edx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x28(%rbp)
movl %edx, (%rax,%rcx,4)
jmp 0x173636
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movq 0x18(%rax), %rax
movl %eax, %eax
movl %eax, %r8d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x185c10
cltq
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jle 0x173605
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x44(%rbp)
jmp 0x17360f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x44(%rbp)
jmp 0x17360f
movl -0x44(%rbp), %edx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x173638
jmp 0x17363a
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x1734d4
movq $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
subq $0x1, %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x1736b4
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
subq -0x40(%rbp), %rcx
subq $0x1, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x17366b
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)