// File: /workspace/github_cpp_makefile_star2_O1/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 0x15656
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
movq (%rdx), %rax
cmpq $0x1, %rsi
jne 0x1550d
testq %rax, %rax
jg 0x154e2
movl $0x1, %esi
movq %r14, %rdi
movq %r13, %rdx
callq 0x25a42
cmpq $0x0, (%rbx)
jg 0x154f8
movl $0x1, %esi
movq %rbx, %rdi
movq %r13, %rdx
callq 0x25a42
movq 0x28(%r14), %rax
xorl %ecx, %ecx
movq %rcx, (%rax)
movq 0x28(%rbx), %rax
movq %rcx, (%rax)
jmp 0x15648
movq %r8, %r15
movq %rdi, (%rsp)
cmpq %r12, %rax
jge 0x15527
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25a42
cmpq $0x2, %r12
movl $0x1, %eax
cmovgeq %r12, %rax
xorl %ecx, %ecx
movq 0x28(%r14), %rdx
movq %rcx, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x15536
leaq 0xc0(%r15), %rbp
cmpq %r12, 0xc0(%r15)
jge 0x15564
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25a42
cmpq %r12, (%r15)
jge 0x15577
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25a42
movq (%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq %r12, %r8
movq %r13, %r9
callq 0x15657
cmpq %r12, (%r15)
jge 0x155a2
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25a42
cmpq %r12, 0x30(%r15)
jge 0x155b7
leaq 0x30(%r15), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25a42
cmpq %r12, (%rbx)
jge 0x155ca
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25a42
cmpq $0x2, %r12
movl $0x1, %eax
cmovgeq %r12, %rax
xorl %ecx, %ecx
movq 0x28(%r15), %rdx
movq %rcx, (%rdx,%rcx,8)
movq 0x58(%r15), %rdx
movq %rcx, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x155d9
cmpq $0x2, %r12
movl $0x1, %eax
cmovgeq %r12, %rax
xorl %ecx, %ecx
movq 0x28(%r15), %rdx
movq 0x58(%r15), %rsi
movq (%rsi,%rcx,8), %rsi
movq 0x28(%r14), %rdi
movq (%rdi,%rcx,8), %rdi
movq (%rdx,%rdi,8), %rdx
movq 0x28(%rbx), %r8
movq %rdx, (%r8,%rcx,8)
movq 0x58(%r15), %r8
movq %rdi, (%r8,%rcx,8)
movq 0x58(%r15), %r8
movq %rsi, (%r8,%rdx,8)
movq 0x28(%r15), %r8
movq %rdx, (%r8,%rsi,8)
movq 0x28(%r15), %rdx
movq %rcx, (%rdx,%rdi,8)
incq %rcx
cmpq %rcx, %rax
jne 0x15600
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq