// File: /workspace/github_c_makefile_star2_O3/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
testl %edi, %edi
jle 0x13a97
movl %edi, -0x38(%rbp)
leaq (,%r13,8), %rbx
movapd 0x5788(%rip), %xmm3     # 0x19040
movq -0x30(%rbp), %r14
xorl %r12d, %r12d
xorl %eax, %eax
xorpd %xmm1, %xmm1
movapd %xmm1, %xmm0
movsd (%r14,%rax,8), %xmm1
andpd %xmm3, %xmm1
maxsd %xmm0, %xmm1
incq %rax
cmpq %rax, %r13
jne 0x138c5
ucomisd 0x5729(%rip), %xmm1     # 0x19010
jne 0x1390b
jp 0x1390b
leaq 0xaa87(%rip), %rdi      # 0x1e379
xorl %eax, %eax
movapd %xmm1, -0x50(%rbp)
callq 0x8d7e
movapd -0x50(%rbp), %xmm1
movapd 0x5735(%rip), %xmm3     # 0x19040
movsd 0x56f5(%rip), %xmm0     # 0x19008
divsd %xmm1, %xmm0
movsd %xmm0, (%r15,%r12,8)
incq %r12
addq %rbx, %r14
cmpq %r13, %r12
jne 0x138bf
cmpl $0x0, -0x38(%rbp)
jle 0x13a97
leaq 0x8(,%r13,8), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %r10
leaq (%r10,%r13,8), %rax
movq %rax, -0x50(%rbp)
movl $0x1, -0x3c(%rbp)
movl $0x1, %edx
xorl %r12d, %r12d
xorpd %xmm0, %xmm0
movq %r13, %r8
movq %r10, %r9
movq %r12, %r11
movq %r10, %rsi
movq %r12, %rdi
xorpd %xmm1, %xmm1
movsd (%rsi), %xmm2
andpd %xmm3, %xmm2
mulsd (%r15,%rdi,8), %xmm2
ucomisd %xmm1, %xmm2
maxsd %xmm1, %xmm2
cmoval %edi, %r12d
incq %rdi
addq %rbx, %rsi
movapd %xmm2, %xmm1
cmpq %rdi, %r13
jne 0x13970
movl %r12d, %eax
cmpq %rax, %r11
je 0x139e2
movslq %r12d, %rsi
movq %rsi, %rax
imulq %r13, %rax
movq -0x30(%rbp), %rcx
leaq (%rcx,%rax,8), %rax
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm1
movsd (%r9,%rcx,8), %xmm2
movsd %xmm2, (%rax,%rcx,8)
movsd %xmm1, (%r9,%rcx,8)
incq %rcx
cmpq %rcx, %r13
jne 0x139b5
negl -0x3c(%rbp)
movsd (%r15,%r11,8), %xmm1
movsd %xmm1, (%r15,%rsi,8)
movq -0x58(%rbp), %rax
movl %r12d, (%rax,%r11,4)
movq %r11, %rax
imulq %r13, %rax
movq -0x30(%rbp), %rcx
leaq (%rcx,%rax,8), %rdi
movsd (%rdi,%r11,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x13a15
jp 0x13a15
movabsq $0x3cd203af9ee75616, %rax # imm = 0x3CD203AF9EE75616
movq %rax, (%rdi,%r11,8)
leaq 0x1(%r11), %r12
cmpq %r13, %r12
jae 0x13a7a
movq -0x50(%rbp), %r14
movq %rdx, %rsi
movq %rsi, %rax
imulq %r13, %rax
movq -0x30(%rbp), %rcx
leaq (%rcx,%rax,8), %rax
movsd (%rax,%r11,8), %xmm1
divsd (%rdi,%r11,8), %xmm1
movsd %xmm1, (%rax,%r11,8)
movl $0x1, %ecx
movsd (%r14,%rcx,8), %xmm1
movsd (%rax,%r11,8), %xmm2
mulsd (%r10,%rcx,8), %xmm2
subsd %xmm2, %xmm1
movsd %xmm1, (%r14,%rcx,8)
incq %rcx
cmpq %rcx, %r8
jne 0x13a4b
incq %rsi
addq %rbx, %r14
cmpq %r13, %rsi
jne 0x13a25
incq %rdx
movq -0x38(%rbp), %rax
addq %rax, %r10
addq %rbx, %r9
addq %rax, -0x50(%rbp)
decq %r8
cmpq %r13, %r12
jne 0x13963
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop