// File: /workspace/github_cpp_makefile_star2_O1/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 0x18c7f
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 0x27ea6
movsd (%rsp), %xmm1
movq %r12, %rdi
callq 0x26621
addq $0x10, %r13
decq %rbp
jne 0x18c19
xorpd %xmm1, %xmm1
movsd %xmm0, (%rsp)
callq 0x262dc
testb %al, %al
je 0x18c97
movsd 0x1b539(%rip), %xmm1    # 0x341a0
movsd (%rsp), %xmm0
callq 0x262fe
testb %al, %al
jne 0x18ca7
movsd 0x1b9bb(%rip), %xmm0    # 0x34638
jmp 0x18caf
xorps %xmm0, %xmm0
callq 0x27e6b
movsd %xmm0, (%r15)
movsd %xmm1, 0x8(%r15)
jmp 0x1904a
movsd 0x1b501(%rip), %xmm0    # 0x341a0
movsd %xmm0, 0x38(%rsp)
jmp 0x18d0d
movsd 0x1c7c9(%rip), %xmm0    # 0x35478
movq %r12, %rdi
callq 0x2658d
movsd %xmm0, 0x38(%rsp)
movsd 0x1b4db(%rip), %xmm1    # 0x341a0
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x27e6b
movsd %xmm0, (%rsp)
movsd %xmm1, 0x10(%rsp)
movq 0x28(%rbx), %r13
addq $0x10, %r13
movl $0x1, %edi
movq %r14, %rsi
callq 0x2923f
movl $0x1, %esi
movq %r13, %rdi
movq %rax, %rdx
movsd (%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x28d48
movq 0x28(%rbx), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x18(%rax), %xmm0
movapd %xmm0, 0x50(%rsp)
cmpq $0x1, %r14
xorpd %xmm0, %xmm0
je 0x18d79
cmpq $0x3, %r14
movl $0x2, %r13d
cmovgeq %r14, %r13
decq %r13
movl $0x28, %ebp
movsd %xmm0, (%rsp)
movq 0x28(%rbx), %rax
movsd -0x8(%rax,%rbp), %xmm0
movsd (%rax,%rbp), %xmm1
movq %r12, %rdi
callq 0x27ea6
movsd (%rsp), %xmm1
movq %r12, %rdi
callq 0x26621
addq $0x10, %rbp
decq %r13
jne 0x18d47
xorpd %xmm1, %xmm1
movsd %xmm0, (%rsp)
callq 0x262dc
xorpd %xmm0, %xmm0
testb %al, %al
je 0x18e33
cmpq $0x1, %r14
je 0x18e1e
cmpq $0x3, %r14
movl $0x2, %r13d
cmovgeq %r14, %r13
decq %r13
movl $0x28, %ebp
movsd %xmm0, 0x10(%rsp)
movq 0x28(%rbx), %rax
movsd -0x8(%rax,%rbp), %xmm0
movsd (%rax,%rbp), %xmm1
movsd (%rsp), %xmm2
callq 0x280fa
movsd %xmm0, 0x40(%rsp)
movsd %xmm1, 0x28(%rsp)
movq %r12, %rdi
callq 0x27e7d
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
movsd 0x40(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
callq 0x27f95
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
addq $0x10, %rbp
decq %r13
jne 0x18db3
movq %r12, %rdi
callq 0x2658d
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm0
xorpd %xmm1, %xmm1
movsd %xmm0, (%rsp)
callq 0x262ba
testb %al, %al
je 0x18e71
xorpd %xmm1, %xmm1
movapd 0x50(%rsp), %xmm0
callq 0x262ba
testb %al, %al
je 0x18e71
xorpd %xmm0, %xmm0
callq 0x27e6b
movsd %xmm0, (%r15)
movsd %xmm1, 0x8(%r15)
jmp 0x19023
movsd 0x30(%rsp), %xmm0
movq %r12, %rdi
callq 0x26575
movsd %xmm0, 0x10(%rsp)
movaps 0x50(%rsp), %xmm0
movq %r12, %rdi
callq 0x26575
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
movq %r12, %rdi
callq 0x26621
movsd %xmm0, 0x10(%rsp)
movsd (%rsp), %xmm0
movq %r12, %rdi
callq 0x26575
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
movq %r12, %rdi
callq 0x26621
movapd %xmm0, %xmm1
movapd %xmm0, 0x10(%rsp)
xorpd 0x1b307(%rip), %xmm0    # 0x341e0
movapd %xmm0, 0x40(%rsp)
movsd 0x30(%rsp), %xmm0
divsd %xmm1, %xmm0
movq %r12, %rdi
callq 0x26588
movsd %xmm0, 0x28(%rsp)
movapd 0x50(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %r12, %rdi
callq 0x26588
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd (%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %r12, %rdi
callq 0x26588
addsd 0x28(%rsp), %xmm0
movq %r12, %rdi
callq 0x2658d
mulsd 0x40(%rsp), %xmm0
movapd %xmm0, (%rsp)
xorpd %xmm1, %xmm1
movsd 0x30(%rsp), %xmm0
callq 0x262fe
testb %al, %al
jne 0x18f65
movapd (%rsp), %xmm2
movapd 0x1b27d(%rip), %xmm3    # 0x341e0
jmp 0x18f76
movapd 0x1b273(%rip), %xmm3    # 0x341e0
movapd (%rsp), %xmm2
xorpd %xmm3, %xmm2
movapd %xmm2, %xmm1
movsd 0x30(%rsp), %xmm0
subsd %xmm0, %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, (%r15)
movapd 0x50(%rsp), %xmm1
xorpd %xmm1, %xmm3
divsd %xmm2, %xmm3
movsd %xmm3, 0x8(%r15)
movapd %xmm2, (%rsp)
callq 0x280e6
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
movsd 0x1b1e5(%rip), %xmm0    # 0x341a0
movapd %xmm2, %xmm1
movapd %xmm3, %xmm2
callq 0x28103
cmpq $0x1, %r14
je 0x1900b
movq 0x28(%rbx), %r15
addq $0x20, %r15
movl $0x2, %edi
movq %r14, %rsi
movsd %xmm0, 0x40(%rsp)
movsd %xmm1, 0x10(%rsp)
callq 0x2923f
movl $0x1, %esi
movq %r15, %rdi
movq %rax, %rdx
movsd 0x40(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x28d48
movapd (%rsp), %xmm0
callq 0x27e6b
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 0x280dd
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