// File: /workspace/github_cpp_makefile_star2_O1/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 0x1660b
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r13
movq %rdi, %r14
cmpq $0x1, %rsi
jne 0x16323
movq 0x28(%r14), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
jmp 0x165fd
movq %rcx, %r12
movq %rdx, %rbx
cmpq %r13, 0xf0(%rdx)
jge 0x16344
leaq 0xf0(%rbx), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x25a42
cmpq %r13, 0x30(%rbx)
jge 0x16359
leaq 0x30(%rbx), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x25a42
cmpq $0x2, %r13
movl $0x1, %eax
cmovgeq %r13, %rax
xorl %ecx, %ecx
movq 0x28(%r14), %rdx
movsd (%rdx,%rcx,8), %xmm0
movq 0x118(%rbx), %rdx
movsd %xmm0, (%rdx,%rcx,8)
movq 0x58(%rbx), %rdx
movq %rcx, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x16368
cmpq $0x3, %r13
movl $0x2, %r15d
movl $0x2, %r12d
movq %r13, (%rsp)
cmovgeq %r13, %r12
movq %r15, %r13
movq %r13, %rbp
shrq $0x3f, %rbp
addq %r13, %rbp
sarq %rbp
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%rbp,8), %xmm0
movsd -0x8(%rax,%r13,8), %xmm1
callq 0x2635a
movl $0x1, %ecx
testb %al, %al
jne 0x1641e
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%rbp,8), %xmm0
movsd -0x8(%rax,%r13,8), %xmm1
movsd %xmm1, -0x8(%rax,%rbp,8)
movq 0x118(%rbx), %rax
movsd %xmm0, -0x8(%rax,%r13,8)
movq 0x58(%rbx), %rax
movq -0x8(%rax,%rbp,8), %rcx
movq -0x8(%rax,%r13,8), %rdx
movq %rdx, -0x8(%rax,%rbp,8)
movq 0x58(%rbx), %rax
movq %rcx, -0x8(%rax,%r13,8)
movq %rbp, %rcx
movq %rcx, %r13
cmpq $0x1, %rcx
jne 0x163a8
leaq 0x1(%r15), %rax
cmpq %r12, %r15
movq %rax, %r15
jne 0x163a5
movq (%rsp), %rax
leaq -0x1(%rax), %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)
movl $0x1, %r15d
leaq (%r15,%r15), %rbp
cmpq %r13, %rbp
jg 0x164c7
jge 0x164aa
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%rbp,8), %xmm1
movsd (%rax,%rbp,8), %xmm0
callq 0x2633e
movzbl %al, %eax
orq %rax, %rbp
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%r15,8), %xmm0
movsd -0x8(%rax,%rbp,8), %xmm1
callq 0x2635a
testb %al, %al
je 0x164cb
xorl %ebp, %ebp
jmp 0x1650f
movq 0x118(%rbx), %rax
movsd -0x8(%rax,%rbp,8), %xmm0
movsd -0x8(%rax,%r15,8), %xmm1
movsd %xmm1, -0x8(%rax,%rbp,8)
movq 0x118(%rbx), %rax
movsd %xmm0, -0x8(%rax,%r15,8)
movq 0x58(%rbx), %rax
movq -0x8(%rax,%rbp,8), %rcx
movq -0x8(%rax,%r15,8), %rdx
movq %rdx, -0x8(%rax,%rbp,8)
movq 0x58(%rbx), %rax
movq %rcx, -0x8(%rax,%r15,8)
movq %rbp, %r15
testq %rbp, %rbp
jne 0x16482
leaq -0x1(%r13), %rax
cmpq $0x1, %r13
movq %rax, %r13
jg 0x16444
movq %r14, 0x8(%rsp)
xorl %r13d, %r13d
leaq (,%r13,2), %rcx
movl $0x1, %edx
movq %r13, %r15
movq %r15, %r12
movq %rcx, %r14
movq %rdx, %rbp
cmpq 0x10(%rsp), %r15
jge 0x1657e
leaq 0x1(%r12), %r15
movq 0x118(%rbx), %rax
movsd 0x8(%rax,%r12,8), %xmm0
movsd (%rax,%r13,8), %xmm1
callq 0x262dc
leaq 0x1(%r14), %rcx
leaq 0x1(%rbp), %rdx
testb %al, %al
je 0x16544
cmpq %r12, %r13
jg 0x165b0
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
mulsd 0x1ded5(%rip), %xmm0    # 0x34468
addsd 0x1dc05(%rip), %xmm0    # 0x341a0
movq 0x118(%rbx), %rax
movsd %xmm0, (%rax,%r13,8)
incq %r13
decq %rbp
jne 0x1659b
incq %r12
movq %r12, %r13
cmpq (%rsp), %r12
jl 0x16534
movq (%rsp), %rcx
cmpq $0x2, %rcx
movl $0x1, %eax
cmovgeq %rcx, %rax
xorl %ecx, %ecx
movq 0x8(%rsp), %rdi
movq 0x58(%rbx), %rdx
movq 0x118(%rbx), %rsi
movsd (%rsi,%rcx,8), %xmm0
movq 0x28(%rdi), %rsi
movq (%rdx,%rcx,8), %rdx
movsd %xmm0, (%rsi,%rdx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x165d8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq