// File: bothaynahazem[P]AsuMathLabG13/Matrix2017.cpp
// Function: LUPDecompose(std::complex<double>**, int, double, int*)
int LUPDecompose(complex<double> **A, int N, double Tol, int *P) {

    int i, j, k, imax;
    complex<double> *ptr;
		double maxA,absA;
    for (i = 0; i <= N; i++)
        P[i] = i; //Unit permutation matrix, P[N] initialized with N

    for (i = 0; i < N; i++) {
        maxA = 0.0;
        imax = i;

        for (k = i; k < N; k++)
            if ((absA = std::abs(A[k][i])) > maxA) {
                maxA = absA;
                imax = k;
            }

        if (maxA < Tol) return 0; //failure, matrix is degenerate

        if (imax != i) {
            //pivoting P
            j = P[i];
            P[i] = P[imax];
            P[imax] = j;

            //pivoting rows of A
            ptr = A[i];
            A[i] = A[imax];
            A[imax] = ptr;

            //counting pivots starting from N (for determinant)
            P[N]++;
        }

        for (j = i + 1; j < N; j++) {
            A[j][i] /= A[i][i];

            for (k = i + 1; k < N; k++)
                A[j][k] -= A[j][i] * A[i][k];
        }
    }

    return 1;  //decomposition done
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movsd %xmm0, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x14b50
movl -0x2c(%rbp), %edx
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x14b2f
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x14d44
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x14bc4
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x2c(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
callq 0x19bf0
movsd %xmm0, -0x50(%rbp)
ucomisd -0x48(%rbp), %xmm0
jbe 0x14bb7
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x14bb9
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x14b77
movsd -0x48(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x14be0
movl $0x0, -0x4(%rbp)
jmp 0x14d4b
movl -0x38(%rbp), %eax
cmpl -0x2c(%rbp), %eax
je 0x14c63
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x30(%rbp), %edx
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x14d34
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movslq -0x2c(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x2c(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
callq 0x19c30
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x14d24
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x2c(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movslq -0x34(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x19a40
movsd %xmm0, -0x60(%rbp)
movsd %xmm1, -0x58(%rbp)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x34(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
leaq -0x60(%rbp), %rsi
callq 0x19970
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x14cb4
jmp 0x14d26
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x14c6c
jmp 0x14d36
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x14b57
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)