// File: /workspace/github_cpp_makefile_star2_O1/mouda[P]LTE_random_access-simulate/alglibinternal.cpp
// Function: alglib_impl::rmatrixscaledtrsafesolve(alglib_impl::ae_matrix*, double, long, alglib_impl::ae_vector*, bool, long, bool, double, alglib_impl::ae_state*)
ae_bool rmatrixscaledtrsafesolve(/* Real    */ ae_matrix* a,
     double sa,
     ae_int_t n,
     /* Real    */ ae_vector* x,
     ae_bool isupper,
     ae_int_t trans,
     ae_bool isunit,
     double maxgrowth,
     ae_state *_state)
{
    ae_frame _frame_block;
    double lnmax;
    double nrmb;
    double nrmx;
    ae_int_t i;
    ae_complex alpha;
    ae_complex beta;
    double vr;
    ae_complex cx;
    ae_vector tmp;
    ae_bool result;

    ae_frame_make(_state, &_frame_block);
    ae_vector_init(&tmp, 0, DT_REAL, _state, ae_true);

    ae_assert(n>0, "RMatrixTRSafeSolve: incorrect N!", _state);
    ae_assert(trans==0||trans==1, "RMatrixTRSafeSolve: incorrect Trans!", _state);
    result = ae_true;
    lnmax = ae_log(ae_maxrealnumber, _state);
    
    /*
     * Quick return if possible
     */
    if( n<=0 )
    {
        ae_frame_leave(_state);
        return result;
    }
    
    /*
     * Load norms: right part and X
     */
    nrmb = 0;
    for(i=0; i<=n-1; i++)
    {
        nrmb = ae_maxreal(nrmb, ae_fabs(x->ptr.p_double[i], _state), _state);
    }
    nrmx = 0;
    
    /*
     * Solve
     */
    ae_vector_set_length(&tmp, n, _state);
    result = ae_true;
    if( isupper&&trans==0 )
    {
        
        /*
         * U*x = b
         */
        for(i=n-1; i>=0; i--)
        {
            
            /*
             * Task is reduced to alpha*x[i] = beta
             */
            if( isunit )
            {
                alpha = ae_complex_from_d(sa);
            }
            else
            {
                alpha = ae_complex_from_d(a->ptr.pp_double[i][i]*sa);
            }
            if( i<n-1 )
            {
                ae_v_moved(&tmp.ptr.p_double[i+1], 1, &a->ptr.pp_double[i][i+1], 1, ae_v_len(i+1,n-1), sa);
                vr = ae_v_dotproduct(&tmp.ptr.p_double[i+1], 1, &x->ptr.p_double[i+1], 1, ae_v_len(i+1,n-1));
                beta = ae_complex_from_d(x->ptr.p_double[i]-vr);
            }
            else
            {
                beta = ae_complex_from_d(x->ptr.p_double[i]);
            }
            
            /*
             * solve alpha*x[i] = beta
             */
            result = safesolve_cbasicsolveandupdate(alpha, beta, lnmax, nrmb, maxgrowth, &nrmx, &cx, _state);
            if( !result )
            {
                ae_frame_leave(_state);
                return result;
            }
            x->ptr.p_double[i] = cx.x;
        }
        ae_frame_leave(_state);
        return result;
    }
    if( !isupper&&trans==0 )
    {
        
        /*
         * L*x = b
         */
        for(i=0; i<=n-1; i++)
        {
            
            /*
             * Task is reduced to alpha*x[i] = beta
             */
            if( isunit )
            {
                alpha = ae_complex_from_d(sa);
            }
            else
            {
                alpha = ae_complex_from_d(a->ptr.pp_double[i][i]*sa);
            }
            if( i>0 )
            {
                ae_v_moved(&tmp.ptr.p_double[0], 1, &a->ptr.pp_double[i][0], 1, ae_v_len(0,i-1), sa);
                vr = ae_v_dotproduct(&tmp.ptr.p_double[0], 1, &x->ptr.p_double[0], 1, ae_v_len(0,i-1));
                beta = ae_complex_from_d(x->ptr.p_double[i]-vr);
            }
            else
            {
                beta = ae_complex_from_d(x->ptr.p_double[i]);
            }
            
            /*
             * solve alpha*x[i] = beta
             */
            result = safesolve_cbasicsolveandupdate(alpha, beta, lnmax, nrmb, maxgrowth, &nrmx, &cx, _state);
            if( !result )
            {
                ae_frame_leave(_state);
                return result;
            }
            x->ptr.p_double[i] = cx.x;
        }
        ae_frame_leave(_state);
        return result;
    }
    if( isupper&&trans==1 )
    {
        
        /*
         * U^T*x = b
         */
        for(i=0; i<=n-1; i++)
        {
            
            /*
             * Task is reduced to alpha*x[i] = beta
             */
            if( isunit )
            {
                alpha = ae_complex_from_d(sa);
            }
            else
            {
                alpha = ae_complex_from_d(a->ptr.pp_double[i][i]*sa);
            }
            beta = ae_complex_from_d(x->ptr.p_double[i]);
            
            /*
             * solve alpha*x[i] = beta
             */
            result = safesolve_cbasicsolveandupdate(alpha, beta, lnmax, nrmb, maxgrowth, &nrmx, &cx, _state);
            if( !result )
            {
                ae_frame_leave(_state);
                return result;
            }
            x->ptr.p_double[i] = cx.x;
            
            /*
             * update the rest of right part
             */
            if( i<n-1 )
            {
                vr = cx.x;
                ae_v_moved(&tmp.ptr.p_double[i+1], 1, &a->ptr.pp_double[i][i+1], 1, ae_v_len(i+1,n-1), sa);
                ae_v_subd(&x->ptr.p_double[i+1], 1, &tmp.ptr.p_double[i+1], 1, ae_v_len(i+1,n-1), vr);
            }
        }
        ae_frame_leave(_state);
        return result;
    }
    if( !isupper&&trans==1 )
    {
        
        /*
         * L^T*x = b
         */
        for(i=n-1; i>=0; i--)
        {
            
            /*
             * Task is reduced to alpha*x[i] = beta
             */
            if( isunit )
            {
                alpha = ae_complex_from_d(sa);
            }
            else
            {
                alpha = ae_complex_from_d(a->ptr.pp_double[i][i]*sa);
            }
            beta = ae_complex_from_d(x->ptr.p_double[i]);
            
            /*
             * solve alpha*x[i] = beta
             */
            result = safesolve_cbasicsolveandupdate(alpha, beta, lnmax, nrmb, maxgrowth, &nrmx, &cx, _state);
            if( !result )
            {
                ae_frame_leave(_state);
                return result;
            }
            x->ptr.p_double[i] = cx.x;
            
            /*
             * update the rest of right part
             */
            if( i>0 )
            {
                vr = cx.x;
                ae_v_moved(&tmp.ptr.p_double[0], 1, &a->ptr.pp_double[i][0], 1, ae_v_len(0,i-1), sa);
                ae_v_subd(&x->ptr.p_double[0], 1, &tmp.ptr.p_double[0], 1, ae_v_len(0,i-1), vr);
            }
        }
        ae_frame_leave(_state);
        return result;
    }
    result = ae_false;
    ae_frame_leave(_state);
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movsd %xmm1, 0x48(%rsp)
movl %r9d, 0x24(%rsp)
movq %r8, %rbp
movl %ecx, %r12d
movq %rdx, %r14
movq %rsi, %r15
movsd %xmm0, 0x10(%rsp)
movq %rdi, 0x28(%rsp)
movq 0xf0(%rsp), %r13
leaq 0xa0(%rsp), %rsi
movq %r13, %rdi
callq 0x2575d
leaq 0x70(%rsp), %rdi
movl $0x3, %edx
xorl %esi, %esi
movq %r13, %rcx
movl $0x1, %r8d
callq 0x25900
xorl %edi, %edi
testq %r15, %r15
setg %dil
leaq 0x184af(%rip), %rsi     # 0x36eea
movq %r13, %rdx
callq 0x2581c
xorl %edi, %edi
cmpq $0x2, %rbp
setb %dil
leaq 0x184b7(%rip), %rsi     # 0x36f0b
movq %r13, %rdx
callq 0x2581c
movsd 0x15bd4(%rip), %xmm0    # 0x34638
movq %r13, %rdi
callq 0x266d6
movsd %xmm0, 0x40(%rsp)
testq %r15, %r15
jle 0x1ec3a
xorps %xmm0, %xmm0
xorl %ebx, %ebx
movsd %xmm0, 0x30(%rsp)
movq 0x28(%r14), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r13, %rdi
callq 0x26575
movapd %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
movq %r13, %rdi
callq 0x26621
incq %rbx
cmpq %rbx, %r15
jne 0x1ea80
movsd %xmm0, 0x30(%rsp)
testq %rbp, %rbp
setne %bl
movq %r13, %rdx
leaq -0x1(%r15), %r13
movq $0x0, 0x38(%rsp)
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x25a42
movl %r12d, %eax
xorb $0x1, %al
orb %al, %bl
jne 0x1ec42
leaq (,%r15,8), %rbx
movsd 0x10(%rsp), %xmm0
cmpb $0x0, 0x24(%rsp)
jne 0x1eb16
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq -0x8(%rax,%rbx), %rax
movsd -0x8(%rax,%rbx), %xmm0
mulsd 0x10(%rsp), %xmm0
leaq -0x1(%r15), %r12
callq 0x27e6b
movsd %xmm0, 0x8(%rsp)
movsd %xmm1, 0x18(%rsp)
cmpq %r13, %r12
jge 0x1ebc2
movq 0x98(%rsp), %rbp
addq %rbx, %rbp
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq -0x8(%rax,%rbx), %r12
addq %rbx, %r12
movq %r15, %rdi
movq %r13, %rsi
callq 0x2923f
movl $0x1, %esi
movl $0x1, %ecx
movq %rbp, %rdi
movq %r12, %rdx
movq %rax, %r8
movsd 0x10(%rsp), %xmm0
callq 0x28fd1
movq 0x98(%rsp), %r12
addq %rbx, %r12
movq 0x28(%r14), %rbp
addq %rbx, %rbp
movq %r15, %rdi
movq %r13, %rsi
callq 0x2923f
movl $0x1, %esi
movl $0x1, %ecx
movq %r12, %rdi
movq %rbp, %rdx
movq %rax, %r8
callq 0x28ddc
movapd %xmm0, %xmm1
movq 0x28(%r14), %rax
movsd -0x8(%rax,%rbx), %xmm0
subsd %xmm1, %xmm0
jmp 0x1ebcc
movq 0x28(%r14), %rax
movsd -0x8(%rax,%rbx), %xmm0
callq 0x27e6b
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
movsd 0x8(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x40(%rsp), %xmm4
movsd 0x30(%rsp), %xmm5
movsd 0x48(%rsp), %xmm6
leaq 0x38(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq 0xf0(%rsp), %rdx
callq 0x1f0c7
testb %al, %al
je 0x1ef2d
movl %eax, %ebp
movsd 0x60(%rsp), %xmm0
movq 0x28(%r14), %rax
movsd %xmm0, -0x8(%rax,%rbx)
addq $-0x8, %rbx
decq %r15
jg 0x1eaef
jmp 0x1ef2f
movb $0x1, %bpl
jmp 0x1f0ab
testq %rbp, %rbp
setne %cl
orb %r12b, %cl
jne 0x1edae
negq %r15
movq $-0x1, %r12
leaq 0x38(%rsp), %rbx
movq 0xf0(%rsp), %r13
movsd 0x10(%rsp), %xmm0
cmpb $0x0, 0x24(%rsp)
jne 0x1ec90
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x8(%rax,%r12,8), %rax
movsd 0x8(%rax,%r12,8), %xmm0
mulsd 0x10(%rsp), %xmm0
callq 0x27e6b
movsd %xmm0, 0x8(%rsp)
movsd %xmm1, 0x18(%rsp)
cmpq $-0x1, %r12
je 0x1ed34
movq 0x98(%rsp), %rbp
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x8(%rax,%r12,8), %r13
xorl %edi, %edi
movq %r12, %rsi
callq 0x2923f
movl $0x1, %esi
movl $0x1, %ecx
movq %rbp, %rdi
movq %r13, %rdx
movq %rax, %r8
movsd 0x10(%rsp), %xmm0
callq 0x28fd1
movq 0x98(%rsp), %r13
movq 0x28(%r14), %rbp
xorl %edi, %edi
movq %r12, %rsi
callq 0x2923f
movl $0x1, %esi
movl $0x1, %ecx
movq %r13, %rdi
movq 0xf0(%rsp), %r13
movq %rbp, %rdx
movq %rax, %r8
callq 0x28ddc
movapd %xmm0, %xmm1
movq 0x28(%r14), %rax
movsd 0x8(%rax,%r12,8), %xmm0
subsd %xmm1, %xmm0
jmp 0x1ed3f
movq 0x28(%r14), %rax
movsd 0x8(%rax,%r12,8), %xmm0
callq 0x27e6b
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
movsd 0x8(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x40(%rsp), %xmm4
movsd 0x30(%rsp), %xmm5
movsd 0x48(%rsp), %xmm6
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
movq %r13, %rdx
callq 0x1f0c7
testb %al, %al
je 0x1f0a9
movl %eax, %ebp
movsd 0x60(%rsp), %xmm0
movq 0x28(%r14), %rax
movsd %xmm0, 0x8(%rax,%r12,8)
leaq (%r15,%r12), %rax
incq %rax
incq %r12
cmpq $-0x1, %rax
jne 0x1ec68
jmp 0x1f0ab
cmpq $0x1, %rbp
setne %cl
orb %al, %cl
jne 0x1ef3c
xorl %ebx, %ebx
xorl %r12d, %r12d
movsd 0x10(%rsp), %xmm0
cmpb $0x0, 0x24(%rsp)
jne 0x1ede7
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq (%rax,%rbx), %rax
movsd (%rax,%rbx), %xmm0
mulsd 0x10(%rsp), %xmm0
callq 0x27e6b
movsd %xmm0, 0x18(%rsp)
movsd %xmm1, 0x8(%rsp)
movq 0x28(%r14), %rax
movsd (%rax,%rbx), %xmm0
callq 0x27e6b
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
movsd 0x18(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x40(%rsp), %xmm4
movsd 0x30(%rsp), %xmm5
movsd 0x48(%rsp), %xmm6
leaq 0x38(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq 0xf0(%rsp), %rdx
callq 0x1f0c7
testb %al, %al
je 0x1ef2d
movl %eax, %ebp
movsd 0x60(%rsp), %xmm0
movq 0x28(%r14), %rax
movsd %xmm0, (%rax,%rbx)
cmpq %r13, %r12
jge 0x1ef1b
movq 0x98(%rsp), %rax
leaq 0x1(%r12), %rdi
movq %rdi, 0x8(%rsp)
addq %rbx, %rax
addq $0x8, %rax
movq %rax, 0x58(%rsp)
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq (%rax,%rbx), %rax
leaq 0x8(%rax,%rbx), %rax
movq %rax, 0x50(%rsp)
movq %r13, %rsi
movsd %xmm0, 0x18(%rsp)
callq 0x2923f
movl $0x1, %esi
movl $0x1, %ecx
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rdx
movq %rax, %r8
movsd 0x10(%rsp), %xmm0
callq 0x28fd1
movq 0x28(%r14), %rax
addq %rbx, %rax
addq $0x8, %rax
movq %rax, 0x58(%rsp)
movq 0x98(%rsp), %rax
leaq 0x8(%rax,%rbx), %rax
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2923f
movl $0x1, %esi
movl $0x1, %ecx
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rdx
movq %rax, %r8
movsd 0x18(%rsp), %xmm0
callq 0x291f0
incq %r12
addq $0x8, %rbx
cmpq %r12, %r15
jne 0x1edc2
jmp 0x1ef2f
xorl %ebp, %ebp
movq 0xf0(%rsp), %r13
jmp 0x1f0ab
cmpq $0x1, %rbp
setne %cl
xorb $0x1, %al
orb %cl, %al
movq 0xf0(%rsp), %r13
jne 0x1f0a9
leaq 0x38(%rsp), %rbx
leaq 0x60(%rsp), %r12
movsd 0x10(%rsp), %xmm0
cmpb $0x0, 0x24(%rsp)
jne 0x1ef87
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq -0x8(%rax,%r15,8), %rax
movsd -0x8(%rax,%r15,8), %xmm0
mulsd 0x10(%rsp), %xmm0
callq 0x27e6b
movsd %xmm0, 0x18(%rsp)
movsd %xmm1, 0x8(%rsp)
movq 0x28(%r14), %rax
movsd -0x8(%rax,%r15,8), %xmm0
callq 0x27e6b
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
movsd 0x18(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x40(%rsp), %xmm4
movsd 0x30(%rsp), %xmm5
movsd 0x48(%rsp), %xmm6
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1f0c7
testb %al, %al
je 0x1f0a9
movl %eax, %ebp
movsd 0x60(%rsp), %xmm0
movq 0x28(%r14), %rax
movsd %xmm0, -0x8(%rax,%r15,8)
cmpq $0x1, %r15
je 0x1f09e
movq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq -0x8(%rax,%r15,8), %r13
movq %r12, %rbx
leaq -0x2(%r15), %r12
xorl %edi, %edi
movq %r12, %rsi
movsd %xmm0, 0x18(%rsp)
callq 0x2923f
movl $0x1, %esi
movl $0x1, %ecx
movq 0x8(%rsp), %rdi
movq %r13, %rdx
movq %rax, %r8
movsd 0x10(%rsp), %xmm0
callq 0x28fd1
movq 0x28(%r14), %r13
movq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
xorl %edi, %edi
movq %r12, %rsi
movq %rbx, %r12
leaq 0x38(%rsp), %rbx
callq 0x2923f
movl $0x1, %esi
movl $0x1, %ecx
movq %r13, %rdi
movq 0xf0(%rsp), %r13
movq 0x8(%rsp), %rdx
movq %rax, %r8
movsd 0x18(%rsp), %xmm0
callq 0x291f0
decq %r15
jg 0x1ef5f
jmp 0x1f0ab
xorl %ebp, %ebp
movq %r13, %rdi
callq 0x256fa
movl %ebp, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq