// File: /workspace/github_c_makefile_star2_O1/mohuangrui[P]ArtraCFD/linear_system.c
// Function: LUFactorization
static int LUFactorization(const int n, Real A[restrict][n], int permute[restrict])
{
    const Real epsilon = 1.0e-15; /* a small number for singularity check */
    const Real zero = 0.0;
    const Real one = 1.0;
    Real temp = 0.0; /* auxiliary variable */
    Real maximum = 0.0; /* store the maximum value in column */
    Real scale[n]; /* stores the implicit scaling of each row with variable length array */
    int rowMax = 0; /* record the row number of current pivot element */
    int sign = 1; /* used for determine the sign of determinant after pivoting */
    /*
     * Loop over rows to get the implicit scaling information.
     */
    for (int row = 0; row < n; ++row) {
        maximum = zero;
        for (int col = 0; col < n; ++col) {
            temp = fabs(A[row][col]);
            if (temp > maximum) {
                maximum = temp;
            }
        }
        if (zero == maximum) {
            ShowError("singular matrix in LU factorization...");
        }
        scale[row] = one / maximum; /* save the scaling */
    }
    /*
     * Do LU factorization with partial pivoting.
     */
    for (int loop = 0; loop < n; ++loop) {
        maximum = zero; /* initialize for the search of largest pivot element */
        rowMax = loop; /* initialize the pivot position to current row */
        for (int row = loop; row < n; ++row) { /* search pivot element for current loop */
            temp = scale[row] * fabs(A[row][loop]);
            if (temp > maximum) {
                maximum = temp;
                rowMax = row;
            }
        }
        if (loop != rowMax) { /* interchange rows if required */
            for (int col = 0; col < n; ++col) {
                temp = A[rowMax][col];
                A[rowMax][col] = A[loop][col];
                A[loop][col] = temp;
            }
            sign = -sign; /* change the parity of sign */
            scale[rowMax] = scale[loop]; /* replace the scale factor */
        }
        permute[loop] = rowMax; /* record the permutation */
        if (zero == A[loop][loop]) { /* substitute zero pivot element with epsilon */
            A[loop][loop] = epsilon;
        }
        for (int row = loop + 1; row < n; ++row) {
            A[row][loop] = A[row][loop] / A[loop][loop]; /* divide column element with pivot element */
            for (int col = loop + 1; col < n; ++col) { /* reduce remaining submatrix */
                A[row][col] = A[row][col] - A[row][loop] * A[loop][col];
            }
        }
    }
    return sign;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, -0x58(%rbp)
movq %rsi, -0x30(%rbp)
movl %edi, %r13d
movq %rsp, %r15
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movl %edi, -0x38(%rbp)
testl %edi, %edi
jle 0x13ce1
leaq (,%r13,8), %rbx
movapd 0x63cf(%rip), %xmm1     # 0x1a040
movq -0x30(%rbp), %r14
xorl %r12d, %r12d
xorl %eax, %eax
xorpd %xmm2, %xmm2
movapd %xmm2, %xmm0
movsd (%r14,%rax,8), %xmm2
andpd %xmm1, %xmm2
maxsd %xmm0, %xmm2
incq %rax
cmpq %rax, %r13
jne 0x13c7e
ucomisd 0x6370(%rip), %xmm2     # 0x1a010
jne 0x13cc4
jp 0x13cc4
leaq 0xb63e(%rip), %rdi      # 0x1f2e9
xorl %eax, %eax
movapd %xmm2, -0x50(%rbp)
callq 0x8fba
movapd -0x50(%rbp), %xmm2
movapd 0x637c(%rip), %xmm1     # 0x1a040
movsd 0x633c(%rip), %xmm0     # 0x1a008
divsd %xmm2, %xmm0
movsd %xmm0, (%r15,%r12,8)
incq %r12
addq %rbx, %r14
cmpq %r13, %r12
jne 0x13c78
cmpl $0x0, -0x38(%rbp)
jle 0x13e61
leaq (,%r13,8), %rax
leaq 0x8(,%r13,8), %rcx
movq %rcx, -0x38(%rbp)
movq -0x30(%rbp), %r11
leaq (%r11,%r13,8), %rcx
movq %rcx, -0x50(%rbp)
movl $0x1, -0x3c(%rbp)
movl $0x1, %esi
xorl %r8d, %r8d
movapd 0x631e(%rip), %xmm0     # 0x1a040
xorpd %xmm1, %xmm1
movq %r13, %r9
movq %r11, %r10
movq %r8, %r12
movq %r11, %rdi
movq %r8, %rbx
xorpd %xmm2, %xmm2
movsd (%rdi), %xmm3
andpd %xmm0, %xmm3
mulsd (%r15,%rbx,8), %xmm3
ucomisd %xmm2, %xmm3
maxsd %xmm2, %xmm3
cmoval %ebx, %r8d
incq %rbx
addq %rax, %rdi
movapd %xmm3, %xmm2
cmpq %rbx, %r13
jne 0x13d39
movl %r8d, %ecx
cmpq %rcx, %r12
je 0x13dab
movslq %r8d, %rdi
movq %rdi, %rcx
imulq %r13, %rcx
movq -0x30(%rbp), %rdx
leaq (%rdx,%rcx,8), %rcx
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm2
movsd (%r10,%rdx,8), %xmm3
movsd %xmm3, (%rcx,%rdx,8)
movsd %xmm2, (%r10,%rdx,8)
incq %rdx
cmpq %rdx, %r13
jne 0x13d7e
negl -0x3c(%rbp)
movsd (%r15,%r12,8), %xmm2
movsd %xmm2, (%r15,%rdi,8)
movq -0x58(%rbp), %rcx
movl %r8d, (%rcx,%r12,4)
movq %r12, %rcx
imulq %r13, %rcx
movq -0x30(%rbp), %rdx
leaq (%rdx,%rcx,8), %rbx
movsd (%rbx,%r12,8), %xmm2
ucomisd %xmm1, %xmm2
jne 0x13dde
jp 0x13dde
movabsq $0x3cd203af9ee75616, %rcx # imm = 0x3CD203AF9EE75616
movq %rcx, (%rbx,%r12,8)
leaq 0x1(%r12), %r8
cmpq %r13, %r8
jae 0x13e44
movq -0x50(%rbp), %r14
movq %rsi, %rdi
movq %rdi, %rcx
imulq %r13, %rcx
movq -0x30(%rbp), %rdx
leaq (%rdx,%rcx,8), %rcx
movsd (%rcx,%r12,8), %xmm2
divsd (%rbx,%r12,8), %xmm2
movsd %xmm2, (%rcx,%r12,8)
movl $0x1, %edx
movsd (%r14,%rdx,8), %xmm2
movsd (%rcx,%r12,8), %xmm3
mulsd (%r11,%rdx,8), %xmm3
subsd %xmm3, %xmm2
movsd %xmm2, (%r14,%rdx,8)
incq %rdx
cmpq %rdx, %r9
jne 0x13e15
incq %rdi
addq %rax, %r14
cmpq %r13, %rdi
jne 0x13def
incq %rsi
movq -0x38(%rbp), %rcx
addq %rcx, %r11
addq %rax, %r10
addq %rcx, -0x50(%rbp)
decq %r9
cmpq %r13, %r8
jne 0x13d2c
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq