// File: /workspace/github_cpp_makefile_star2_O2/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
xorpd %xmm0, %xmm0
movupd %xmm0, (%rdx)
testq %rsi, %rsi
jle 0x174bc
movq %rcx, %r12
movq %rsi, %r14
movq %rdi, %rbx
xorpd %xmm2, %xmm2
pushq $0x18
popq %r13
movq %rsi, %rbp
subq $0x1, %rbp
movsd %xmm2, (%rsp)
jb 0x17481
movq 0x28(%rbx), %rax
movsd -0x8(%rax,%r13), %xmm0
movsd (%rax,%r13), %xmm1
movq %r12, %rdi
callq 0x2589c
movsd (%rsp), %xmm1
movq %r12, %rdi
callq 0x244b6
movapd %xmm0, %xmm2
addq $0x10, %r13
jmp 0x17446
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorpd %xmm1, %xmm1
movapd %xmm2, %xmm0
callq 0x24217
testb %al, %al
je 0x174d5
movsd 0x18d54(%rip), %xmm1    # 0x301f8
movsd (%rsp), %xmm0
callq 0x24239
testb %al, %al
jne 0x174e5
movsd 0x191a6(%rip), %xmm0    # 0x30660
jmp 0x174ed
xorpd %xmm0, %xmm0
callq 0x25861
movsd %xmm0, (%r15)
movsd %xmm1, 0x8(%r15)
jmp 0x1785c
movsd 0x18d1b(%rip), %xmm0    # 0x301f8
movsd %xmm0, 0x48(%rsp)
jmp 0x1754a
movsd 0x19d83(%rip), %xmm0    # 0x31270
movq %r12, %rdi
callq 0x24424
movsd %xmm0, 0x48(%rsp)
movsd 0x18cf5(%rip), %xmm1    # 0x301f8
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x25861
movsd %xmm0, (%rsp)
movsd %xmm1, 0x30(%rsp)
movq 0x28(%rbx), %r13
addq $0x10, %r13
pushq $0x1
popq %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x26b4d
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movsd (%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
callq 0x26602
movq 0x28(%rbx), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x18(%rax), %xmm0
movaps %xmm0, 0x50(%rsp)
pushq $0x2
popq %r13
pushq $0x28
popq %rbp
cmpq %r14, %r13
jg 0x175a3
movq 0x28(%rbx), %rax
movsd -0x8(%rax,%rbp), %xmm0
movsd (%rax,%rbp), %xmm1
movq %r12, %rdi
callq 0x2589c
movsd 0x10(%rsp), %xmm1
movq %r12, %rdi
callq 0x244b6
movsd %xmm0, 0x10(%rsp)
incq %r13
addq $0x10, %rbp
jmp 0x1756a
xorpd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm0
callq 0x24217
xorpd %xmm0, %xmm0
testb %al, %al
je 0x17648
pushq $0x2
popq %r13
pushq $0x28
popq %rbp
cmpq %r14, %r13
jg 0x17632
movq 0x28(%rbx), %rax
movsd %xmm0, (%rsp)
movsd -0x8(%rax,%rbp), %xmm0
movsd (%rax,%rbp), %xmm1
movsd 0x10(%rsp), %xmm2
callq 0x25ae6
movsd %xmm0, 0x30(%rsp)
movsd %xmm1, 0x20(%rsp)
movq %r12, %rdi
callq 0x25873
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
movsd 0x30(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
callq 0x2596d
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsd (%rsp), %xmm0
incq %r13
addq $0x10, %rbp
jmp 0x175c5
movq %r12, %rdi
callq 0x24424
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm0
xorpd %xmm1, %xmm1
movsd %xmm0, 0x10(%rsp)
callq 0x241f5
testb %al, %al
je 0x17687
xorpd %xmm1, %xmm1
movapd 0x50(%rsp), %xmm0
callq 0x241f5
testb %al, %al
je 0x17687
xorpd %xmm0, %xmm0
callq 0x25861
movsd %xmm0, (%r15)
movsd %xmm1, 0x8(%r15)
jmp 0x17835
movsd 0x28(%rsp), %xmm0
movq %r12, %rdi
callq 0x2440c
movsd %xmm0, (%rsp)
movaps 0x50(%rsp), %xmm0
movq %r12, %rdi
callq 0x2440c
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
movq %r12, %rdi
callq 0x244b6
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
movq %r12, %rdi
callq 0x2440c
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
movq %r12, %rdi
callq 0x244b6
movapd %xmm0, %xmm1
movapd %xmm0, (%rsp)
xorpd 0x18ac5(%rip), %xmm0    # 0x301b0
movapd %xmm0, 0x30(%rsp)
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
movq %r12, %rdi
callq 0x2441f
movsd %xmm0, 0x20(%rsp)
movapd 0x50(%rsp), %xmm0
divsd (%rsp), %xmm0
movq %r12, %rdi
callq 0x2441f
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x10(%rsp), %xmm0
divsd (%rsp), %xmm0
movq %r12, %rdi
callq 0x2441f
addsd 0x20(%rsp), %xmm0
movq %r12, %rdi
callq 0x24424
mulsd 0x30(%rsp), %xmm0
movapd %xmm0, 0x10(%rsp)
xorpd %xmm1, %xmm1
movsd 0x28(%rsp), %xmm0
callq 0x24239
testb %al, %al
jne 0x17778
movapd 0x10(%rsp), %xmm2
movapd 0x18a3a(%rip), %xmm3    # 0x301b0
jmp 0x1778a
movapd 0x18a30(%rip), %xmm3    # 0x301b0
movapd 0x10(%rsp), %xmm2
xorpd %xmm3, %xmm2
movapd %xmm2, %xmm4
movsd 0x28(%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, 0x10(%rsp)
callq 0x25ad2
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
movsd 0x18a26(%rip), %xmm0    # 0x301f8
movapd %xmm2, %xmm1
movapd %xmm3, %xmm2
callq 0x25afb
cmpq $0x1, %r14
je 0x1781c
movq 0x28(%rbx), %r15
addq $0x20, %r15
pushq $0x2
popq %rdi
movq %r14, %rsi
movsd %xmm0, 0x30(%rsp)
movsd %xmm1, (%rsp)
callq 0x26b4d
pushq $0x1
popq %rsi
movq %r15, %rdi
movq %rax, %rdx
movsd 0x30(%rsp), %xmm0
movsd (%rsp), %xmm1
callq 0x26602
movapd 0x10(%rsp), %xmm0
callq 0x25861
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 0x48(%rsp), %xmm2
callq 0x25ac9
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