// File: /workspace/github_cpp_makefile_star2_O3/mouda[P]LTE_random_access-simulate/alglibinternal.cpp
// Function: alglib_impl::complexgeneratereflection(alglib_impl::ae_vector*, long, alglib_impl::ae_complex*, alglib_impl::ae_state*)
void complexgeneratereflection(/* Complex */ ae_vector* x,
     ae_int_t n,
     ae_complex* tau,
     ae_state *_state)
{
    ae_int_t j;
    ae_complex alpha;
    double alphi;
    double alphr;
    double beta;
    double xnorm;
    double mx;
    ae_complex t;
    double s;
    ae_complex v;

    tau->x = 0;
    tau->y = 0;

    if( n<=0 )
    {
        *tau = ae_complex_from_d(0);
        return;
    }
    
    /*
     * Scale if needed (to avoid overflow/underflow during intermediate
     * calculations).
     */
    mx = 0;
    for(j=1; j<=n; j++)
    {
        mx = ae_maxreal(ae_c_abs(x->ptr.p_complex[j], _state), mx, _state);
    }
    s = 1;
    if( ae_fp_neq(mx,0) )
    {
        if( ae_fp_less(mx,1) )
        {
            s = ae_sqrt(ae_minrealnumber, _state);
            v = ae_complex_from_d(1/s);
            ae_v_cmulc(&x->ptr.p_complex[1], 1, ae_v_len(1,n), v);
        }
        else
        {
            s = ae_sqrt(ae_maxrealnumber, _state);
            v = ae_complex_from_d(1/s);
            ae_v_cmulc(&x->ptr.p_complex[1], 1, ae_v_len(1,n), v);
        }
    }
    
    /*
     * calculate
     */
    alpha = x->ptr.p_complex[1];
    mx = 0;
    for(j=2; j<=n; j++)
    {
        mx = ae_maxreal(ae_c_abs(x->ptr.p_complex[j], _state), mx, _state);
    }
    xnorm = 0;
    if( ae_fp_neq(mx,0) )
    {
        for(j=2; j<=n; j++)
        {
            t = ae_c_div_d(x->ptr.p_complex[j],mx);
            xnorm = xnorm+ae_c_mul(t,ae_c_conj(t, _state)).x;
        }
        xnorm = ae_sqrt(xnorm, _state)*mx;
    }
    alphr = alpha.x;
    alphi = alpha.y;
    if( ae_fp_eq(xnorm,0)&&ae_fp_eq(alphi,0) )
    {
        *tau = ae_complex_from_d(0);
        x->ptr.p_complex[1] = ae_c_mul_d(x->ptr.p_complex[1],s);
        return;
    }
    mx = ae_maxreal(ae_fabs(alphr, _state), ae_fabs(alphi, _state), _state);
    mx = ae_maxreal(mx, ae_fabs(xnorm, _state), _state);
    beta = -mx*ae_sqrt(ae_sqr(alphr/mx, _state)+ae_sqr(alphi/mx, _state)+ae_sqr(xnorm/mx, _state), _state);
    if( ae_fp_less(alphr,0) )
    {
        beta = -beta;
    }
    tau->x = (beta-alphr)/beta;
    tau->y = -alphi/beta;
    alpha = ae_c_d_div(1,ae_c_sub_d(alpha,beta));
    if( n>1 )
    {
        ae_v_cmulc(&x->ptr.p_complex[2], 1, ae_v_len(2,n), alpha);
    }
    alpha = ae_complex_from_d(beta);
    x->ptr.p_complex[1] = alpha;
    
    /*
     * Scale back
     */
    x->ptr.p_complex[1] = ae_c_mul_d(x->ptr.p_complex[1],s);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
testq %rsi, %rsi
jle 0x185e6
movq %rcx, %r12
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
movl $0x18, %r13d
movq %rsi, %rbp
movsd %xmm0, (%rsp)
movq 0x28(%rbx), %rax
movsd -0x8(%rax,%r13), %xmm0
movsd (%rax,%r13), %xmm1
movq %r12, %rdi
callq 0x276fb
movsd (%rsp), %xmm1
movq %r12, %rdi
callq 0x25edc
addq $0x10, %r13
decq %rbp
jne 0x18580
xorpd %xmm1, %xmm1
movsd %xmm0, (%rsp)
callq 0x25b97
testb %al, %al
je 0x185fe
movsd 0x1ac12(%rip), %xmm1    # 0x331e0
movsd (%rsp), %xmm0
callq 0x25bb9
testb %al, %al
jne 0x1860e
movsd 0x1b07c(%rip), %xmm0    # 0x33660
jmp 0x18616
xorps %xmm0, %xmm0
callq 0x276c0
movsd %xmm0, (%r15)
movsd %xmm1, 0x8(%r15)
jmp 0x189ca
movsd 0x1abda(%rip), %xmm0    # 0x331e0
movsd %xmm0, 0x38(%rsp)
jmp 0x18674
movsd 0x1bc5a(%rip), %xmm0    # 0x34270
movq %r12, %rdi
callq 0x25e48
movsd %xmm0, 0x38(%rsp)
movsd 0x1abb4(%rip), %xmm1    # 0x331e0
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x276c0
movsd %xmm0, (%rsp)
movsd %xmm1, 0x10(%rsp)
movq 0x28(%rbx), %r13
addq $0x10, %r13
movl $0x1, %edi
movq %r14, %rsi
callq 0x2897c
movl $0x1, %esi
movq %r13, %rdi
movq %rax, %rdx
movsd (%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x28485
movq 0x28(%rbx), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x18(%rax), %xmm0
movapd %xmm0, 0x50(%rsp)
cmpq $0x1, %r14
jne 0x186bb
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorpd %xmm1, %xmm1
callq 0x25b97
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testb %al, %al
jne 0x187a0
jmp 0x187b1
cmpq $0x3, %r14
movl $0x2, %r13d
cmovgeq %r14, %r13
decq %r13
xorpd %xmm0, %xmm0
movl $0x28, %ebp
movsd %xmm0, (%rsp)
movq 0x28(%rbx), %rax
movsd -0x8(%rax,%rbp), %xmm0
movsd (%rax,%rbp), %xmm1
movq %r12, %rdi
callq 0x276fb
movsd (%rsp), %xmm1
movq %r12, %rdi
callq 0x25edc
addq $0x10, %rbp
decq %r13
jne 0x186d5
xorpd %xmm1, %xmm1
movsd %xmm0, (%rsp)
callq 0x25b97
xorpd %xmm0, %xmm0
testb %al, %al
je 0x187b1
xorpd %xmm1, %xmm1
cmpq $0x1, %r14
je 0x187a0
cmpq $0x3, %r14
movl $0x2, %r13d
cmovgeq %r14, %r13
decq %r13
movl $0x28, %ebp
movsd %xmm1, 0x10(%rsp)
movq 0x28(%rbx), %rax
movsd -0x8(%rax,%rbp), %xmm0
movsd (%rax,%rbp), %xmm1
movsd (%rsp), %xmm2
callq 0x27948
movsd %xmm0, 0x40(%rsp)
movsd %xmm1, 0x28(%rsp)
movq %r12, %rdi
callq 0x276d2
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
movsd 0x40(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
callq 0x277cf
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
addq $0x10, %rbp
decq %r13
jne 0x18741
movapd %xmm1, %xmm0
movq %r12, %rdi
callq 0x25e48
mulsd (%rsp), %xmm0
xorpd %xmm1, %xmm1
movsd %xmm0, (%rsp)
callq 0x25b75
testb %al, %al
je 0x187ef
xorpd %xmm1, %xmm1
movapd 0x50(%rsp), %xmm0
callq 0x25b75
testb %al, %al
je 0x187ef
xorpd %xmm0, %xmm0
callq 0x276c0
movsd %xmm0, (%r15)
movsd %xmm1, 0x8(%r15)
jmp 0x189a3
movsd 0x30(%rsp), %xmm0
movq %r12, %rdi
callq 0x25e30
movsd %xmm0, 0x10(%rsp)
movaps 0x50(%rsp), %xmm0
movq %r12, %rdi
callq 0x25e30
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
movq %r12, %rdi
callq 0x25edc
movsd %xmm0, 0x10(%rsp)
movsd (%rsp), %xmm0
movq %r12, %rdi
callq 0x25e30
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
movq %r12, %rdi
callq 0x25edc
movapd %xmm0, %xmm1
movapd %xmm0, 0x10(%rsp)
xorpd 0x1a959(%rip), %xmm0    # 0x331b0
movapd %xmm0, 0x40(%rsp)
movsd 0x30(%rsp), %xmm0
divsd %xmm1, %xmm0
movq %r12, %rdi
callq 0x25e43
movsd %xmm0, 0x28(%rsp)
movapd 0x50(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %r12, %rdi
callq 0x25e43
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd (%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %r12, %rdi
callq 0x25e43
addsd 0x28(%rsp), %xmm0
movq %r12, %rdi
callq 0x25e48
mulsd 0x40(%rsp), %xmm0
movapd %xmm0, (%rsp)
xorpd %xmm1, %xmm1
movsd 0x30(%rsp), %xmm0
callq 0x25bb9
testb %al, %al
jne 0x188e3
movapd (%rsp), %xmm2
movapd 0x1a8cf(%rip), %xmm3    # 0x331b0
jmp 0x188f4
movapd 0x1a8c5(%rip), %xmm3    # 0x331b0
movapd (%rsp), %xmm2
xorpd %xmm3, %xmm2
movapd %xmm2, %xmm4
movsd 0x30(%rsp), %xmm0
subsd %xmm0, %xmm4
movapd 0x50(%rsp), %xmm1
xorpd %xmm1, %xmm3
unpcklpd %xmm3, %xmm4            # xmm4 = xmm4[0],xmm3[0]
movapd %xmm2, %xmm3
unpcklpd %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0]
divpd %xmm3, %xmm4
movupd %xmm4, (%r15)
movapd %xmm2, (%rsp)
callq 0x27934
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
movsd 0x1a8a5(%rip), %xmm0    # 0x331e0
movapd %xmm2, %xmm1
movapd %xmm3, %xmm2
callq 0x2795d
cmpq $0x1, %r14
je 0x1898b
movq 0x28(%rbx), %r15
addq $0x20, %r15
movl $0x2, %edi
movq %r14, %rsi
movsd %xmm0, 0x40(%rsp)
movsd %xmm1, 0x10(%rsp)
callq 0x2897c
movl $0x1, %esi
movq %r15, %rdi
movq %rax, %rdx
movsd 0x40(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x28485
movapd (%rsp), %xmm0
callq 0x276c0
movq 0x28(%rbx), %rax
movsd %xmm0, 0x10(%rax)
movsd %xmm1, 0x18(%rax)
movq 0x28(%rbx), %rax
movsd 0x10(%rax), %xmm0
movsd 0x18(%rax), %xmm1
movsd 0x38(%rsp), %xmm2
callq 0x2792b
movq 0x28(%rbx), %rax
movsd %xmm0, 0x10(%rax)
movsd %xmm1, 0x18(%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq