// File: /workspace/github_cpp_makefile_star2_O3/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 0x15074
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 0x14f4b
testq %rax, %rax
jg 0x14f20
movl $0x1, %esi
movq %r14, %rdi
movq %r13, %rdx
callq 0x25326
cmpq $0x0, (%rbx)
jg 0x14f36
movl $0x1, %esi
movq %rbx, %rdi
movq %r13, %rdx
callq 0x25326
movq 0x28(%r14), %rax
xorl %ecx, %ecx
movq %rcx, (%rax)
movq 0x28(%rbx), %rax
movq %rcx, (%rax)
jmp 0x15066
movq %r8, %r15
movq %rdi, %rbp
cmpq %r12, %rax
jge 0x14f64
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25326
xorl %eax, %eax
movq 0x28(%r14), %rcx
movq %rax, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x14f66
leaq 0xc0(%r15), %rdx
cmpq %r12, 0xc0(%r15)
movq %rdx, (%rsp)
jge 0x14f9c
movq %rdx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25326
movq (%rsp), %rdx
cmpq %r12, (%r15)
jge 0x14fb3
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25326
movq (%rsp), %rdx
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rcx
movq %r12, %r8
movq %r13, %r9
callq 0x15075
cmpq %r12, (%r15)
jge 0x14fda
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25326
cmpq %r12, 0x30(%r15)
jge 0x14fef
leaq 0x30(%r15), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25326
cmpq %r12, (%rbx)
jge 0x15002
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25326
xorl %eax, %eax
movq 0x28(%r15), %rcx
movq %rax, (%rcx,%rax,8)
movq 0x58(%r15), %rcx
movq %rax, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x15004
xorl %eax, %eax
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
cmpq %rax, %r12
jne 0x1501e
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq