// File: /workspace/github_cpp_makefile_star2_O3/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 0x16012
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 0x15d3b
movq 0x28(%r14), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
jmp 0x16004
movq %rcx, %r12
movq %rdx, %rbx
cmpq %r15, 0xf0(%rdx)
jge 0x15d5c
leaq 0xf0(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x25326
cmpq %r15, 0x30(%rbx)
jge 0x15d71
leaq 0x30(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x25326
xorl %eax, %eax
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
cmpq %rax, %r15
jne 0x15d73
movq %r14, 0x8(%rsp)
cmpq $0x3, %r15
movl $0x2, %r13d
movl $0x2, %r12d
cmovgeq %r15, %r12
movq %r13, %rbp
movq %r13, %r14
shrq $0x3f, %r14
addq %rbp, %r14
sarq %r14
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%r14,8), %xmm0
movsd -0x8(%rax,%rbp,8), %xmm1
callq 0x25c15
testb %al, %al
jne 0x15e2f
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%r14,8), %xmm0
movsd -0x8(%rax,%rbp,8), %xmm1
movsd %xmm1, -0x8(%rax,%r14,8)
movq 0x118(%rbx), %rax
movsd %xmm0, -0x8(%rax,%rbp,8)
movq 0x58(%rbx), %rax
movq -0x8(%rax,%r14,8), %rcx
movq -0x8(%rax,%rbp,8), %rdx
movq %rdx, -0x8(%rax,%r14,8)
movq 0x58(%rbx), %rax
movq %rcx, -0x8(%rax,%rbp,8)
andq $-0x2, %rbp
cmpq $0x2, %rbp
movq %r14, %rbp
jne 0x15db7
leaq 0x1(%r13), %rax
cmpq %r12, %r13
movq %rax, %r13
jne 0x15db1
movq %r15, (%rsp)
leaq -0x1(%r15), %r13
movq %r13, 0x10(%rsp)
movq 0x118(%rbx), %rax
movsd (%rax,%r13,8), %xmm0
movsd (%rax), %xmm1
movsd %xmm1, (%rax,%r13,8)
movq 0x118(%rbx), %rax
movsd %xmm0, (%rax)
movq 0x58(%rbx), %rax
movq (%rax,%r13,8), %rcx
movq (%rax), %rdx
movq %rdx, (%rax,%r13,8)
movq 0x58(%rbx), %rax
movq %rcx, (%rax)
cmpq $0x2, %r13
jl 0x15f34
movl $0x1, %ebp
movl $0x2, %r15d
cmpq %r13, %r15
jge 0x15ec0
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%r15,8), %xmm1
movsd (%rax,%r15,8), %xmm0
callq 0x25bf9
movzbl %al, %r14d
orq %r15, %r14
jmp 0x15ec3
movq %r15, %r14
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%rbp,8), %xmm0
movsd -0x8(%rax,%r14,8), %xmm1
callq 0x25c15
testb %al, %al
jne 0x15f34
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%r14,8), %xmm0
movsd -0x8(%rax,%rbp,8), %xmm1
movsd %xmm1, -0x8(%rax,%r14,8)
movq 0x118(%rbx), %rax
movsd %xmm0, -0x8(%rax,%rbp,8)
movq 0x58(%rbx), %rax
movq -0x8(%rax,%r14,8), %rcx
movq -0x8(%rax,%rbp,8), %rdx
movq %rdx, -0x8(%rax,%r14,8)
movq 0x58(%rbx), %rax
movq %rcx, -0x8(%rax,%rbp,8)
leaq (%r14,%r14), %r15
movq %r14, %rbp
cmpq %r13, %r15
jle 0x15e99
leaq -0x1(%r13), %rax
cmpq $0x1, %r13
movq %rax, %r13
jg 0x15e4c
xorl %r13d, %r13d
leaq (,%r13,2), %rcx
movl $0x1, %edx
movq %r13, %rbp
movq %rbp, %r12
movq %rcx, %r15
movq %rdx, %r14
cmpq 0x10(%rsp), %rbp
jge 0x15f92
leaq 0x1(%r12), %rbp
movq 0x118(%rbx), %rax
movsd 0x8(%rax,%r12,8), %xmm0
movsd (%rax,%r13,8), %xmm1
callq 0x25b97
leaq 0x1(%r15), %rcx
leaq 0x1(%r14), %rdx
testb %al, %al
je 0x15f58
cmpq %r12, %r13
jg 0x15fc4
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
mulsd 0x1d4e1(%rip), %xmm0    # 0x33488
addsd 0x1d231(%rip), %xmm0    # 0x331e0
movq 0x118(%rbx), %rax
movsd %xmm0, (%rax,%r13,8)
incq %r13
decq %r14
jne 0x15faf
incq %r12
movq %r12, %r13
cmpq (%rsp), %r12
jl 0x15f48
xorl %eax, %eax
movq 0x8(%rsp), %rsi
movq (%rsp), %rdi
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
cmpq %rax, %rdi
jne 0x15fdf
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq