// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %rdx, -0x60(%rbp)
movq %rsi, %r14
movl %edi, %r15d
movl %edi, %r13d
movq %rsp, %rcx
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
testl %edi, %edi
movl $0x0, %eax
cmovgl %edi, %eax
movq %rax, -0x50(%rbp)
leaq (,%r13,8), %r12
movapd 0x5713(%rip), %xmm3     # 0x18040
movq %rsi, %rbx
xorl %ecx, %ecx
cmpq -0x50(%rbp), %rcx
je 0x129ab
xorpd %xmm1, %xmm1
xorl %eax, %eax
cmpq %rax, %r13
je 0x1295a
movsd (%r14,%rax,8), %xmm0
andpd %xmm3, %xmm0
maxsd %xmm1, %xmm0
incq %rax
movapd %xmm0, %xmm1
jmp 0x1293e
ucomisd 0x56ae(%rip), %xmm1     # 0x18010
jne 0x1298e
jp 0x1298e
leaq 0xa864(%rip), %rdi      # 0x1d1d1
xorl %eax, %eax
movq %rcx, -0x38(%rbp)
movsd %xmm1, -0x30(%rbp)
callq 0x856a
movsd -0x30(%rbp), %xmm1
movq -0x38(%rbp), %rcx
movapd 0x56b2(%rip), %xmm3     # 0x18040
movsd 0x5672(%rip), %xmm0     # 0x18008
divsd %xmm1, %xmm0
movq -0x48(%rbp), %rax
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
addq %r12, %r14
jmp 0x12932
leaq 0x8(,%r13,8), %rax
movq %rax, -0x58(%rbp)
leaq (%rbx,%r13,8), %rax
movq %rax, -0x38(%rbp)
pushq $0x1
popq %rax
movl %eax, -0x3c(%rbp)
pushq $0x1
popq %rax
xorl %edi, %edi
xorpd %xmm0, %xmm0
movq %rbx, %r14
movq %rbx, %r10
movq %rbx, %rdx
movq %rax, -0x30(%rbp)
cmpq -0x50(%rbp), %rdi
movq -0x48(%rbp), %r11
je 0x12b2d
xorpd %xmm1, %xmm1
movq %rdx, -0x68(%rbp)
movq %rdx, %rcx
movq %rdi, %r8
movl %edi, %edx
cmpq %r8, %r13
je 0x12a24
movsd (%rcx), %xmm2
andpd %xmm3, %xmm2
mulsd (%r11,%r8,8), %xmm2
ucomisd %xmm1, %xmm2
cmoval %r8d, %edx
maxsd %xmm1, %xmm2
incq %r8
addq %r12, %rcx
movapd %xmm2, %xmm1
jmp 0x129f9
movl %edx, %ecx
cmpq %rcx, %rdi
jne 0x12a34
movq %rdi, %rcx
imulq %r13, %rcx
jmp 0x12a7d
movslq %edx, %r8
movq %r8, %rcx
imulq %r13, %rcx
leaq (%rbx,%rcx,8), %rsi
movq %rdi, %rcx
imulq %r13, %rcx
xorl %r9d, %r9d
cmpq %r9, %r13
je 0x12a6e
movsd (%rsi,%r9,8), %xmm1
movsd (%r10,%r9,8), %xmm2
movsd %xmm2, (%rsi,%r9,8)
movsd %xmm1, (%r10,%r9,8)
incq %r9
jmp 0x12a4c
negl -0x3c(%rbp)
movsd (%r11,%rdi,8), %xmm1
movsd %xmm1, (%r11,%r8,8)
movq -0x60(%rbp), %rsi
movl %edx, (%rsi,%rdi,4)
leaq (%rbx,%rcx,8), %rdx
movsd (%rdx,%rdi,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x12aa3
jp 0x12aa3
movabsq $0x3cd203af9ee75616, %rsi # imm = 0x3CD203AF9EE75616
movq %rsi, (%rdx,%rdi,8)
movq %r14, -0x70(%rbp)
leaq (%r14,%rcx,8), %r14
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %r8
cmpl %r13d, %r8d
jge 0x12b07
movq %r8, %rsi
imulq %r13, %rsi
leaq (%rbx,%rsi,8), %r9
movsd (%r9,%rdi,8), %xmm1
divsd (%rdx,%rdi,8), %xmm1
movsd %xmm1, (%r9,%rdi,8)
movq %rax, %rsi
leal (%rdi,%rsi), %r11d
cmpl %r15d, %r11d
jge 0x12aff
movsd (%r9,%rdi,8), %xmm1
mulsd (%r14,%rsi,8), %xmm1
movsd (%rcx,%rsi,8), %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, (%rcx,%rsi,8)
incq %rsi
jmp 0x12ad7
incq %r8
addq %r12, %rcx
jmp 0x12ab3
incq %rdi
incq -0x30(%rbp)
movq -0x58(%rbp), %rcx
movq -0x68(%rbp), %rdx
addq %rcx, %rdx
addq %r12, %r10
movq -0x70(%rbp), %r14
addq $0x8, %r14
addq %rcx, -0x38(%rbp)
jmp 0x129db
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq