// File: /workspace/github_cpp_makefile_star2_O1/mouda[P]LTE_random_access-simulate/alglibinternal.cpp
// Function: alglib_impl::tsort_tagsortfastrrec(alglib_impl::ae_vector*, alglib_impl::ae_vector*, alglib_impl::ae_vector*, alglib_impl::ae_vector*, long, long, alglib_impl::ae_state*)
static void tsort_tagsortfastrrec(/* Real    */ ae_vector* a,
     /* Real    */ ae_vector* b,
     /* Real    */ ae_vector* bufa,
     /* Real    */ ae_vector* bufb,
     ae_int_t i1,
     ae_int_t i2,
     ae_state *_state)
{
    ae_int_t i;
    ae_int_t j;
    ae_int_t k;
    double tmpr;
    double tmpr2;
    ae_int_t tmpi;
    ae_int_t cntless;
    ae_int_t cnteq;
    ae_int_t cntgreater;
    double v0;
    double v1;
    double v2;
    double vp;


    
    /*
     * Fast exit
     */
    if( i2<=i1 )
    {
        return;
    }
    
    /*
     * Non-recursive sort for small arrays
     */
    if( i2-i1<=16 )
    {
        for(j=i1+1; j<=i2; j++)
        {
            
            /*
             * Search elements [I1..J-1] for place to insert Jth element.
             *
             * This code stops immediatly if we can leave A[J] at J-th position
             * (all elements have same value of A[J] larger than any of them)
             */
            tmpr = a->ptr.p_double[j];
            tmpi = j;
            for(k=j-1; k>=i1; k--)
            {
                if( a->ptr.p_double[k]<=tmpr )
                {
                    break;
                }
                tmpi = k;
            }
            k = tmpi;
            
            /*
             * Insert Jth element into Kth position
             */
            if( k!=j )
            {
                tmpr = a->ptr.p_double[j];
                tmpr2 = b->ptr.p_double[j];
                for(i=j-1; i>=k; i--)
                {
                    a->ptr.p_double[i+1] = a->ptr.p_double[i];
                    b->ptr.p_double[i+1] = b->ptr.p_double[i];
                }
                a->ptr.p_double[k] = tmpr;
                b->ptr.p_double[k] = tmpr2;
            }
        }
        return;
    }
    
    /*
     * Quicksort: choose pivot
     * Here we assume that I2-I1>=16
     */
    v0 = a->ptr.p_double[i1];
    v1 = a->ptr.p_double[i1+(i2-i1)/2];
    v2 = a->ptr.p_double[i2];
    if( v0>v1 )
    {
        tmpr = v1;
        v1 = v0;
        v0 = tmpr;
    }
    if( v1>v2 )
    {
        tmpr = v2;
        v2 = v1;
        v1 = tmpr;
    }
    if( v0>v1 )
    {
        tmpr = v1;
        v1 = v0;
        v0 = tmpr;
    }
    vp = v1;
    
    /*
     * now pass through A/B and:
     * * move elements that are LESS than VP to the left of A/B
     * * move elements that are EQUAL to VP to the right of BufA/BufB (in the reverse order)
     * * move elements that are GREATER than VP to the left of BufA/BufB (in the normal order
     * * move elements from the tail of BufA/BufB to the middle of A/B (restoring normal order)
     * * move elements from the left of BufA/BufB to the end of A/B
     */
    cntless = 0;
    cnteq = 0;
    cntgreater = 0;
    for(i=i1; i<=i2; i++)
    {
        v0 = a->ptr.p_double[i];
        if( v0<vp )
        {
            
            /*
             * LESS
             */
            k = i1+cntless;
            if( i!=k )
            {
                a->ptr.p_double[k] = v0;
                b->ptr.p_double[k] = b->ptr.p_double[i];
            }
            cntless = cntless+1;
            continue;
        }
        if( v0==vp )
        {
            
            /*
             * EQUAL
             */
            k = i2-cnteq;
            bufa->ptr.p_double[k] = v0;
            bufb->ptr.p_double[k] = b->ptr.p_double[i];
            cnteq = cnteq+1;
            continue;
        }
        
        /*
         * GREATER
         */
        k = i1+cntgreater;
        bufa->ptr.p_double[k] = v0;
        bufb->ptr.p_double[k] = b->ptr.p_double[i];
        cntgreater = cntgreater+1;
    }
    for(i=0; i<=cnteq-1; i++)
    {
        j = i1+cntless+cnteq-1-i;
        k = i2+i-(cnteq-1);
        a->ptr.p_double[j] = bufa->ptr.p_double[k];
        b->ptr.p_double[j] = bufb->ptr.p_double[k];
    }
    for(i=0; i<=cntgreater-1; i++)
    {
        j = i1+cntless+cnteq+i;
        k = i1+i;
        a->ptr.p_double[j] = bufa->ptr.p_double[k];
        b->ptr.p_double[j] = bufb->ptr.p_double[k];
    }
    
    /*
     * Sort left and right parts of the array (ignoring middle part)
     */
    tsort_tagsortfastrrec(a, b, bufa, bufb, i1, i1+cntless-1, _state);
    tsort_tagsortfastrrec(a, b, bufa, bufb, i1+cntless+cnteq, i2, _state);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r15
leaq 0x1(%r9), %r10
movq %r9, 0x10(%rsp)
movq %r10, 0x8(%rsp)
movq %r9, %rax
subq %r8, %rax
jle 0x15da3
cmpq $0x10, %rax
jle 0x15cf6
cmpq %r8, %r9
jge 0x15b6a
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %eax, %eax
jmp 0x15c30
movq 0x28(%r15), %rcx
leaq (%rcx,%r8,8), %rdx
movsd (%rdx), %xmm1
andq $-0x2, %rax
movsd (%rdx,%rax,4), %xmm2
movsd (%rcx,%r9,8), %xmm3
movapd %xmm2, %xmm0
minsd %xmm1, %xmm0
maxsd %xmm2, %xmm1
minsd %xmm1, %xmm3
maxsd %xmm3, %xmm0
movq %r8, %rcx
xorl %eax, %eax
xorl %ebp, %ebp
xorl %ebx, %ebx
movq 0x28(%r15), %rdx
movsd (%rdx,%rcx,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x15bd2
leaq (%rbx,%r8), %rsi
cmpq %rsi, %rcx
je 0x15bcd
movsd %xmm1, (%rdx,%rsi,8)
movq 0x28(%r14), %rdx
movsd (%rdx,%rcx,8), %xmm1
movsd %xmm1, (%rdx,%rsi,8)
incq %rbx
jmp 0x15c24
ucomisd %xmm0, %xmm1
jne 0x15c01
jp 0x15c01
movq %r9, %rdx
subq %rbp, %rdx
movq 0x28(%r13), %rsi
movsd %xmm1, (%rsi,%rdx,8)
movq 0x28(%r14), %rsi
movsd (%rsi,%rcx,8), %xmm1
movq 0x28(%r12), %rsi
movsd %xmm1, (%rsi,%rdx,8)
incq %rbp
jmp 0x15c24
leaq (%rax,%r8), %rdx
movq 0x28(%r13), %rsi
movsd %xmm1, (%rsi,%rdx,8)
movq 0x28(%r14), %rsi
movsd (%rsi,%rcx,8), %xmm1
movq 0x28(%r12), %rsi
movsd %xmm1, (%rsi,%rdx,8)
incq %rax
incq %rcx
cmpq %rcx, %r10
jne 0x15ba2
testq %rbp, %rbp
jle 0x15c7f
leaq (,%rbx,8), %rcx
leaq (%rcx,%r8,8), %rcx
movq %r10, %rdx
subq %rbp, %rdx
movq %rbp, %rsi
movq 0x28(%r13), %rdi
movsd (%rdi,%rdx,8), %xmm0
movq 0x28(%r15), %rdi
addq %rcx, %rdi
movsd %xmm0, -0x8(%rdi,%rsi,8)
movq 0x28(%r12), %rdi
movsd (%rdi,%rdx,8), %xmm0
movq 0x28(%r14), %rdi
addq %rcx, %rdi
movsd %xmm0, -0x8(%rdi,%rsi,8)
incq %rdx
decq %rsi
jne 0x15c4a
testq %rax, %rax
jle 0x15cc6
leaq (,%rbp,8), %rcx
leaq (%rcx,%rbx,8), %rcx
movq %r8, %rdx
movq 0x28(%r13), %rsi
movsd (%rsi,%rdx,8), %xmm0
movq 0x28(%r15), %rsi
addq %rcx, %rsi
movsd %xmm0, (%rsi,%rdx,8)
movq 0x28(%r12), %rsi
movsd (%rsi,%rdx,8), %xmm0
movq 0x28(%r14), %rsi
addq %rcx, %rsi
movsd %xmm0, (%rsi,%rdx,8)
incq %rdx
decq %rax
jne 0x15c93
leaq (%rbx,%r8), %r9
decq %r9
addq %r8, %rbx
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x15b1c
addq %rbp, %rbx
movq %rbx, %r8
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r10
jmp 0x15b44
cmpq %r8, %r9
jle 0x15da3
leaq 0x1(%r8), %rax
movq %r8, %rsi
leaq 0x1(%rsi), %rcx
movq 0x28(%r15), %rdi
movsd 0x8(%rdi,%rsi,8), %xmm0
movq %rcx, %rdx
cmpq %r8, %rsi
jl 0x15d46
ucomisd (%rdi,%rsi,8), %xmm0
movq %rcx, %rdx
jae 0x15d46
movq %rsi, %r10
cmpq %r8, %r10
jle 0x15d43
leaq -0x1(%r10), %rdx
ucomisd -0x8(%rdi,%r10,8), %xmm0
movq %rdx, %r10
jb 0x15d29
incq %rdx
jmp 0x15d46
movq %r8, %rdx
cmpq %rcx, %rdx
je 0x15d94
movq 0x28(%r14), %rdi
movsd (%rdi,%rcx,8), %xmm1
cmpq %rdx, %rsi
jl 0x15d82
movq %rax, %rsi
movq 0x28(%r15), %rdi
movsd -0x8(%rdi,%rsi,8), %xmm2
movsd %xmm2, (%rdi,%rsi,8)
movq 0x28(%r14), %rdi
movsd -0x8(%rdi,%rsi,8), %xmm2
movsd %xmm2, (%rdi,%rsi,8)
decq %rsi
cmpq %rdx, %rsi
jg 0x15d5c
movq 0x28(%r15), %rsi
movsd %xmm0, (%rsi,%rdx,8)
movq 0x28(%r14), %rsi
movsd %xmm1, (%rsi,%rdx,8)
incq %rax
movq %rcx, %rsi
cmpq %r9, %rcx
jne 0x15d06
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq