// File: /workspace/github_cpp_makefile_star2_O2/mouda[P]LTE_random_access-simulate/alglibinternal.cpp
// Function: alglib_impl::rankx(alglib_impl::ae_vector*, long, alglib_impl::apbuffers*, alglib_impl::ae_state*)
void rankx(/* Real    */ ae_vector* x,
     ae_int_t n,
     apbuffers* buf,
     ae_state *_state)
{
    ae_int_t i;
    ae_int_t j;
    ae_int_t k;
    ae_int_t t;
    double tmp;
    ae_int_t tmpi;


    
    /*
     * Prepare
     */
    if( n<1 )
    {
        return;
    }
    if( n==1 )
    {
        x->ptr.p_double[0] = 1;
        return;
    }
    if( buf->ra1.cnt<n )
    {
        ae_vector_set_length(&buf->ra1, n, _state);
    }
    if( buf->ia1.cnt<n )
    {
        ae_vector_set_length(&buf->ia1, n, _state);
    }
    for(i=0; i<=n-1; i++)
    {
        buf->ra1.ptr.p_double[i] = x->ptr.p_double[i];
        buf->ia1.ptr.p_int[i] = i;
    }
    
    /*
     * sort {R, C}
     */
    if( n!=1 )
    {
        i = 2;
        do
        {
            t = i;
            while(t!=1)
            {
                k = t/2;
                if( ae_fp_greater_eq(buf->ra1.ptr.p_double[k-1],buf->ra1.ptr.p_double[t-1]) )
                {
                    t = 1;
                }
                else
                {
                    tmp = buf->ra1.ptr.p_double[k-1];
                    buf->ra1.ptr.p_double[k-1] = buf->ra1.ptr.p_double[t-1];
                    buf->ra1.ptr.p_double[t-1] = tmp;
                    tmpi = buf->ia1.ptr.p_int[k-1];
                    buf->ia1.ptr.p_int[k-1] = buf->ia1.ptr.p_int[t-1];
                    buf->ia1.ptr.p_int[t-1] = tmpi;
                    t = k;
                }
            }
            i = i+1;
        }
        while(i<=n);
        i = n-1;
        do
        {
            tmp = buf->ra1.ptr.p_double[i];
            buf->ra1.ptr.p_double[i] = buf->ra1.ptr.p_double[0];
            buf->ra1.ptr.p_double[0] = tmp;
            tmpi = buf->ia1.ptr.p_int[i];
            buf->ia1.ptr.p_int[i] = buf->ia1.ptr.p_int[0];
            buf->ia1.ptr.p_int[0] = tmpi;
            t = 1;
            while(t!=0)
            {
                k = 2*t;
                if( k>i )
                {
                    t = 0;
                }
                else
                {
                    if( k<i )
                    {
                        if( ae_fp_greater(buf->ra1.ptr.p_double[k],buf->ra1.ptr.p_double[k-1]) )
                        {
                            k = k+1;
                        }
                    }
                    if( ae_fp_greater_eq(buf->ra1.ptr.p_double[t-1],buf->ra1.ptr.p_double[k-1]) )
                    {
                        t = 0;
                    }
                    else
                    {
                        tmp = buf->ra1.ptr.p_double[k-1];
                        buf->ra1.ptr.p_double[k-1] = buf->ra1.ptr.p_double[t-1];
                        buf->ra1.ptr.p_double[t-1] = tmp;
                        tmpi = buf->ia1.ptr.p_int[k-1];
                        buf->ia1.ptr.p_int[k-1] = buf->ia1.ptr.p_int[t-1];
                        buf->ia1.ptr.p_int[t-1] = tmpi;
                        t = k;
                    }
                }
            }
            i = i-1;
        }
        while(i>=1);
    }
    
    /*
     * compute tied ranks
     */
    i = 0;
    while(i<=n-1)
    {
        j = i+1;
        while(j<=n-1)
        {
            if( ae_fp_neq(buf->ra1.ptr.p_double[j],buf->ra1.ptr.p_double[i]) )
            {
                break;
            }
            j = j+1;
        }
        for(k=i; k<=j-1; k++)
        {
            buf->ra1.ptr.p_double[k] = 1+(double)(i+j-1)/(double)2;
        }
        i = j;
    }
    
    /*
     * back to x
     */
    for(i=0; i<=n-1; i++)
    {
        x->ptr.p_double[buf->ia1.ptr.p_int[i]] = buf->ra1.ptr.p_double[i];
    }
}

// Assembly:
testq %rsi, %rsi
jle 0x150a2
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdi, %r14
cmpq $0x1, %rsi
jne 0x150a3
movq 0x28(%r14), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %r12
movq %rdx, %rbx
cmpq %r15, 0xf0(%rdx)
jge 0x150c4
leaq 0xf0(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x23add
cmpq %r15, 0x30(%rbx)
jge 0x150d9
leaq 0x30(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x23add
xorl %eax, %eax
cmpq %rax, %r15
je 0x15102
movq 0x28(%r14), %rcx
movsd (%rcx,%rax,8), %xmm0
movq 0x118(%rbx), %rcx
movsd %xmm0, (%rcx,%rax,8)
movq 0x58(%rbx), %rcx
movq %rax, (%rcx,%rax,8)
incq %rax
jmp 0x150db
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
pushq $0x2
popq %r14
pushq $0x1
popq %rbp
movq %r14, %r15
movq %r15, %rcx
cmpq $0x1, %rcx
je 0x15198
movq %rcx, %r12
movq %rcx, %rax
cqto
idivq %r14
movq %rax, %r13
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%r13,8), %xmm0
movsd -0x8(%rax,%rcx,8), %xmm1
callq 0x24295
movq %rbp, %rcx
testb %al, %al
jne 0x15119
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%r13,8), %xmm0
movsd -0x8(%rax,%r12,8), %xmm1
movsd %xmm1, -0x8(%rax,%r13,8)
movq 0x118(%rbx), %rax
movsd %xmm0, -0x8(%rax,%r12,8)
movq 0x58(%rbx), %rax
movq -0x8(%rax,%r13,8), %rcx
movq -0x8(%rax,%r12,8), %rdx
movq %rdx, -0x8(%rax,%r13,8)
movq 0x58(%rbx), %rax
movq %rcx, -0x8(%rax,%r12,8)
movq %r13, %rcx
jmp 0x15119
leaq 0x1(%r15), %rax
cmpq 0x8(%rsp), %r15
movq %rax, %r15
jl 0x15116
movq 0x8(%rsp), %r15
leaq -0x1(%r15), %r12
movq %r12, %rbp
movq 0x118(%rbx), %rax
movsd (%rax,%rbp,8), %xmm0
movsd (%rax), %xmm1
movsd %xmm1, (%rax,%rbp,8)
movq 0x118(%rbx), %rax
movsd %xmm0, (%rax)
movq 0x58(%rbx), %rax
movq (%rax,%rbp,8), %rcx
movq (%rax), %rdx
movq %rdx, (%rax,%rbp,8)
movq 0x58(%rbx), %rax
movq %rcx, (%rax)
pushq $0x1
popq %rcx
testq %rcx, %rcx
je 0x1529b
movq %rcx, %r13
leaq (%rcx,%rcx), %r14
movl $0x0, %ecx
cmpq %rbp, %r14
jg 0x151ef
jge 0x1522a
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%r14,8), %xmm1
movsd (%rax,%r14,8), %xmm0
callq 0x24279
movzbl %al, %eax
orq %rax, %r14
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%r13,8), %xmm0
movsd -0x8(%rax,%r14,8), %xmm1
callq 0x24295
movl $0x0, %ecx
testb %al, %al
jne 0x151ef
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%r14,8), %xmm0
movsd -0x8(%rax,%r13,8), %xmm1
movsd %xmm1, -0x8(%rax,%r14,8)
movq 0x118(%rbx), %rax
movsd %xmm0, -0x8(%rax,%r13,8)
movq 0x58(%rbx), %rax
movq -0x8(%rax,%r14,8), %rcx
movq -0x8(%rax,%r13,8), %rdx
movq %rdx, -0x8(%rax,%r14,8)
movq 0x58(%rbx), %rax
movq %rcx, -0x8(%rax,%r13,8)
movq %r14, %rcx
jmp 0x151ef
leaq -0x1(%rbp), %rax
cmpq $0x1, %rbp
movq %rax, %rbp
jg 0x151b6
xorl %r13d, %r13d
cmpq %r15, %r13
jge 0x1532c
leaq (,%r13,2), %rcx
movq %r13, %r15
movq %r15, %r14
movq %rcx, %rbp
cmpq %r12, %r15
jge 0x152f1
leaq 0x1(%r14), %r15
movq 0x118(%rbx), %rax
movsd 0x8(%rax,%r14,8), %xmm0
movsd (%rax,%r13,8), %xmm1
callq 0x24217
leaq 0x1(%rbp), %rcx
testb %al, %al
je 0x152bf
jmp 0x152f6
movq 0x8(%rsp), %r15
cvtsi2sd %rbp, %xmm0
mulsd 0x1b185(%rip), %xmm0    # 0x30488
addsd 0x1aeed(%rip), %xmm0    # 0x301f8
cmpq %r14, %r13
jg 0x15322
movq 0x118(%rbx), %rax
movsd %xmm0, (%rax,%r13,8)
incq %r13
jmp 0x1530b
movq %r15, %r13
movq 0x8(%rsp), %r15
jmp 0x152af
xorl %eax, %eax
movq 0x10(%rsp), %rsi
cmpq %rax, %r15
je 0x15094
movq 0x58(%rbx), %rcx
movq 0x118(%rbx), %rdx
movsd (%rdx,%rax,8), %xmm0
movq 0x28(%rsi), %rdx
movq (%rcx,%rax,8), %rcx
movsd %xmm0, (%rdx,%rcx,8)
incq %rax
jmp 0x15333