// File: /workspace/github_cpp_makefile_star2_O2/mouda[P]LTE_random_access-simulate/alglibinternal.cpp
// Function: alglib_impl::tagsortbuf(alglib_impl::ae_vector*, long, alglib_impl::ae_vector*, alglib_impl::ae_vector*, alglib_impl::apbuffers*, alglib_impl::ae_state*)
void tagsortbuf(/* Real    */ ae_vector* a,
     ae_int_t n,
     /* Integer */ ae_vector* p1,
     /* Integer */ ae_vector* p2,
     apbuffers* buf,
     ae_state *_state)
{
    ae_int_t i;
    ae_int_t lv;
    ae_int_t lp;
    ae_int_t rv;
    ae_int_t rp;


    
    /*
     * Special cases
     */
    if( n<=0 )
    {
        return;
    }
    if( n==1 )
    {
        ivectorsetlengthatleast(p1, 1, _state);
        ivectorsetlengthatleast(p2, 1, _state);
        p1->ptr.p_int[0] = 0;
        p2->ptr.p_int[0] = 0;
        return;
    }
    
    /*
     * General case, N>1: prepare permutations table P1
     */
    ivectorsetlengthatleast(p1, n, _state);
    for(i=0; i<=n-1; i++)
    {
        p1->ptr.p_int[i] = i;
    }
    
    /*
     * General case, N>1: sort, update P1
     */
    rvectorsetlengthatleast(&buf->ra0, n, _state);
    ivectorsetlengthatleast(&buf->ia0, n, _state);
    tagsortfasti(a, p1, &buf->ra0, &buf->ia0, n, _state);
    
    /*
     * General case, N>1: fill permutations table P2
     *
     * To fill P2 we maintain two arrays:
     * * PV (Buf.IA0), Position(Value). PV[i] contains position of I-th key at the moment
     * * VP (Buf.IA1), Value(Position). VP[i] contains key which has position I at the moment
     *
     * At each step we making permutation of two items:
     *   Left, which is given by position/value pair LP/LV
     *   and Right, which is given by RP/RV
     * and updating PV[] and VP[] correspondingly.
     */
    ivectorsetlengthatleast(&buf->ia0, n, _state);
    ivectorsetlengthatleast(&buf->ia1, n, _state);
    ivectorsetlengthatleast(p2, n, _state);
    for(i=0; i<=n-1; i++)
    {
        buf->ia0.ptr.p_int[i] = i;
        buf->ia1.ptr.p_int[i] = i;
    }
    for(i=0; i<=n-1; i++)
    {
        
        /*
         * calculate LP, LV, RP, RV
         */
        lp = i;
        lv = buf->ia1.ptr.p_int[lp];
        rv = p1->ptr.p_int[i];
        rp = buf->ia0.ptr.p_int[rv];
        
        /*
         * Fill P2
         */
        p2->ptr.p_int[i] = rp;
        
        /*
         * update PV and VP
         */
        buf->ia1.ptr.p_int[lp] = rv;
        buf->ia1.ptr.p_int[rp] = lv;
        buf->ia0.ptr.p_int[lv] = rp;
        buf->ia0.ptr.p_int[rv] = lp;
    }
}

// Assembly:
testq %rsi, %rsi
jle 0x142fe
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r9, %r13
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r12
cmpq $0x1, %rsi
jne 0x142ff
pushq $0x1
popq %r15
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x12f05
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x12f05
movq 0x28(%r14), %rax
andq $0x0, (%rax)
movq 0x28(%rbx), %rax
andq $0x0, (%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r8, %r15
movq %rdi, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x12f05
xorl %eax, %eax
cmpq %rax, %r12
je 0x14328
movq 0x28(%r14), %rcx
movq %rax, (%rcx,%rax,8)
incq %rax
jmp 0x14316
leaq 0xc0(%r15), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x12f0f
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x12f05
movq (%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq %r12, %r8
movq %r13, %r9
callq 0x143fa
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x12f05
leaq 0x30(%r15), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x12f05
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x12f05
xorl %eax, %eax
cmpq %rax, %r12
je 0x143aa
movq 0x28(%r15), %rcx
movq %rax, (%rcx,%rax,8)
movq 0x58(%r15), %rcx
movq %rax, (%rcx,%rax,8)
incq %rax
jmp 0x14390
xorl %eax, %eax
cmpq %rax, %r12
je 0x142f0
movq 0x28(%r15), %rcx
movq 0x58(%r15), %rdx
movq (%rdx,%rax,8), %rdx
movq 0x28(%r14), %rsi
movq (%rsi,%rax,8), %rsi
movq (%rcx,%rsi,8), %rcx
movq 0x28(%rbx), %rdi
movq %rcx, (%rdi,%rax,8)
movq 0x58(%r15), %rdi
movq %rsi, (%rdi,%rax,8)
movq 0x58(%r15), %rdi
movq %rdx, (%rdi,%rcx,8)
movq 0x28(%r15), %rdi
movq %rcx, (%rdi,%rdx,8)
movq 0x28(%r15), %rcx
movq %rax, (%rcx,%rsi,8)
incq %rax
jmp 0x143ac