// File: yukimasano[P]cpp_matrix/Matrix.cpp
// Function: LUsolve(Matrix&, Vector&)
Vector LUsolve(Matrix& A, Vector& v){
	assert(nrows(A) == ncols(A));
	// we want a square system

	// INITIALISATION
	Matrix U=A;
	int m =nrows(A);
	Vector temp=ones(m);
	Matrix L = diag(temp);
	//LU FACTORISATION
	for (int k=1; k<=m-1; k++){
		for(int j = k+1; j<=m; j++){
			L.set_val(j,k,   (U(j,k)/U(k,k)));
			for (int l=k; l<=m; l++){
				U.set_val(j,l, (U(j,l) - U(k,l)*L(j,k)));
			}
		}
	}
	//std::cout<<L<<std::endl;
	Vector y(m);
	// FORWARD SOLVE
	for (int k=0; k<m; k++){
		y.set_val(k+1, v(k+1));
		for(int j = 0; j<k; j++){
			y.set_val(k+1, y(k+1)-(L(k+1,j+1))*y(j+1));
		}
		y.set_val(k+1, y(k+1)/L(k+1,k+1));
		}
	//std::cout<<y<<std::endl;
	Vector x(m);
	// BACKSUBSTITUTION
	for (int k=m-1; k>=0; k--){
		x.set_val(k+1, y(k+1));
		for (int j = k+1; j<m; j++){
			x.set_val(k+1, x(k+1)- U(k+1,j+1)*x(j+1));
		}
		x.set_val(k+1, x(k+1)/U(k+1,k+1));
	}
	//std::cout<<U<<std::endl;
	return x;
}

// Assembly:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, 0x110(%rsp)
movq %rdi, %rax
movq %rax, 0x118(%rsp)
movq %rdi, 0x1b0(%rsp)
movq %rsi, 0x1a8(%rsp)
movq %rdx, 0x1a0(%rsp)
movq 0x1a8(%rsp), %rdi
callq 0x3ac0
movl %eax, 0x120(%rsp)
movq 0x1a8(%rsp), %rdi
callq 0x3ad0
movl %eax, %ecx
movl 0x120(%rsp), %eax
cmpl %ecx, %eax
jne 0x6712
jmp 0x6731
leaq 0x8bf9(%rip), %rdi      # 0xf312
leaq 0x8a29(%rip), %rsi      # 0xf149
movl $0x1bf, %edx            # imm = 0x1BF
leaq 0x8c13(%rip), %rcx      # 0xf33f
callq 0x2090
movq 0x1a8(%rsp), %rsi
leaq 0x190(%rsp), %rdi
callq 0x5010
movq 0x1a8(%rsp), %rdi
callq 0x3ac0
movl %eax, 0x18c(%rsp)
movl 0x18c(%rsp), %esi
leaq 0x178(%rsp), %rdi
callq 0xe3d0
jmp 0x6770
leaq 0x158(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x4f30
jmp 0x6787
movl $0x1, 0x154(%rsp)
movl 0x154(%rsp), %eax
movl 0x18c(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jg 0x6a1a
movl 0x154(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x150(%rsp)
movl 0x150(%rsp), %eax
cmpl 0x18c(%rsp), %eax
jg 0x6a02
movl 0x150(%rsp), %esi
movl %esi, 0x100(%rsp)
movl 0x154(%rsp), %edx
movl %edx, 0x104(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x36e0
movq %rax, 0x108(%rsp)
jmp 0x6803
movq 0x108(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xf0(%rsp)
movl 0x154(%rsp), %edx
leaq 0x190(%rsp), %rdi
movl %edx, %esi
callq 0x36e0
movq %rax, 0xf8(%rsp)
jmp 0x6838
movl 0x104(%rsp), %edx
movl 0x100(%rsp), %esi
movq 0xf8(%rsp), %rax
movsd 0xf0(%rsp), %xmm0
divsd (%rax), %xmm0
leaq 0x158(%rsp), %rdi
callq 0x47c0
movl 0x154(%rsp), %eax
movl %eax, 0x14c(%rsp)
movl 0x14c(%rsp), %eax
cmpl 0x18c(%rsp), %eax
jg 0x69ea
movl 0x150(%rsp), %esi
movl %esi, 0xe0(%rsp)
movl 0x14c(%rsp), %edx
movl %edx, 0xe4(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x36e0
movq %rax, 0xe8(%rsp)
jmp 0x68bd
movq 0xe8(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xd0(%rsp)
movl 0x154(%rsp), %esi
movl 0x14c(%rsp), %edx
leaq 0x190(%rsp), %rdi
callq 0x36e0
movq %rax, 0xd8(%rsp)
jmp 0x68f7
movq 0xd8(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xc0(%rsp)
movl 0x150(%rsp), %esi
movl 0x154(%rsp), %edx
leaq 0x158(%rsp), %rdi
callq 0x36e0
movq %rax, 0xc8(%rsp)
jmp 0x6931
movl 0xe4(%rsp), %edx
movl 0xe0(%rsp), %esi
movsd 0xd0(%rsp), %xmm1
movsd 0xc0(%rsp), %xmm0
movq 0xc8(%rsp), %rax
movsd (%rax), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
leaq 0x190(%rsp), %rdi
callq 0x47c0
movl 0x14c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14c(%rsp)
jmp 0x6876
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0x6eef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0x6ee2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0x6ed5
jmp 0x69ec
movl 0x150(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x150(%rsp)
jmp 0x67bc
jmp 0x6a04
movl 0x154(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x154(%rsp)
jmp 0x6792
movl 0x18c(%rsp), %esi
leaq 0x138(%rsp), %rdi
callq 0xd040
jmp 0x6a30
movl $0x0, 0x134(%rsp)
movl 0x134(%rsp), %eax
cmpl 0x18c(%rsp), %eax
jge 0x6c4b
movl 0x134(%rsp), %esi
incl %esi
movl %esi, 0xb4(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0xdee0
movq %rax, 0xb8(%rsp)
jmp 0x6a76
movl 0xb4(%rsp), %esi
movq 0xb8(%rsp), %rax
movsd (%rax), %xmm0
leaq 0x138(%rsp), %rdi
callq 0xe480
jmp 0x6a98
movl $0x0, 0x130(%rsp)
movl 0x130(%rsp), %eax
cmpl 0x134(%rsp), %eax
jge 0x6bbe
movl 0x134(%rsp), %esi
incl %esi
movl %esi, 0xa4(%rsp)
leaq 0x138(%rsp), %rdi
callq 0xdee0
movq %rax, 0xa8(%rsp)
jmp 0x6ade
movq 0xa8(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x90(%rsp)
movl 0x134(%rsp), %esi
incl %esi
movl 0x130(%rsp), %edx
incl %edx
leaq 0x158(%rsp), %rdi
callq 0x36e0
movq %rax, 0x98(%rsp)
jmp 0x6b1c
movq 0x98(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movl 0x130(%rsp), %esi
incl %esi
leaq 0x138(%rsp), %rdi
callq 0xdee0
movq %rax, 0x88(%rsp)
jmp 0x6b51
movl 0xa4(%rsp), %esi
movsd 0x90(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movq 0x88(%rsp), %rax
movsd (%rax), %xmm2
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
leaq 0x138(%rsp), %rdi
callq 0xe480
jmp 0x6b8d
jmp 0x6b8f
movl 0x130(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x130(%rsp)
jmp 0x6aa3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0x6ec8
movl 0x134(%rsp), %esi
incl %esi
movl %esi, 0x74(%rsp)
leaq 0x138(%rsp), %rdi
callq 0xdee0
movq %rax, 0x78(%rsp)
jmp 0x6bdf
movq 0x78(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movl 0x134(%rsp), %edx
incl %edx
leaq 0x158(%rsp), %rdi
movl %edx, %esi
callq 0x36e0
movq %rax, 0x68(%rsp)
jmp 0x6c0d
movl 0x74(%rsp), %esi
movsd 0x60(%rsp), %xmm0
movq 0x68(%rsp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
leaq 0x138(%rsp), %rdi
callq 0xe480
jmp 0x6c33
jmp 0x6c35
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0x6a3b
movq 0x110(%rsp), %rdi
movb $0x0, 0x12f(%rsp)
movl 0x18c(%rsp), %esi
callq 0xd040
jmp 0x6c69
movl 0x18c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x128(%rsp)
cmpl $0x0, 0x128(%rsp)
jl 0x6e65
movl 0x128(%rsp), %esi
incl %esi
movl %esi, 0x54(%rsp)
leaq 0x138(%rsp), %rdi
callq 0xdee0
movq %rax, 0x58(%rsp)
jmp 0x6ca9
movl 0x54(%rsp), %esi
movq 0x110(%rsp), %rdi
movq 0x58(%rsp), %rax
movsd (%rax), %xmm0
callq 0xe480
jmp 0x6cc5
movl 0x128(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
movl 0x124(%rsp), %eax
cmpl 0x18c(%rsp), %eax
jge 0x6dda
movq 0x110(%rsp), %rdi
movl 0x128(%rsp), %esi
incl %esi
movl %esi, 0x44(%rsp)
callq 0xdee0
movq %rax, 0x48(%rsp)
jmp 0x6d0b
movq 0x48(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x128(%rsp), %esi
incl %esi
movl 0x124(%rsp), %edx
incl %edx
leaq 0x190(%rsp), %rdi
callq 0x36e0
movq %rax, 0x38(%rsp)
jmp 0x6d40
movq 0x110(%rsp), %rdi
movq 0x38(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0x124(%rsp), %esi
incl %esi
callq 0xdee0
movq %rax, 0x28(%rsp)
jmp 0x6d6c
movl 0x44(%rsp), %esi
movq 0x110(%rsp), %rdi
movsd 0x30(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movq 0x28(%rsp), %rax
movsd (%rax), %xmm2
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
callq 0xe480
jmp 0x6d9c
jmp 0x6d9e
movl 0x124(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0x6cd6
movq 0x110(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
callq 0xd170
jmp 0x6ec8
movq 0x110(%rsp), %rdi
movl 0x128(%rsp), %esi
incl %esi
movl %esi, 0x14(%rsp)
callq 0xdee0
movq %rax, 0x18(%rsp)
jmp 0x6dfb
movq 0x18(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movl 0x128(%rsp), %edx
incl %edx
leaq 0x190(%rsp), %rdi
movl %edx, %esi
callq 0x36e0
movq %rax, 0x8(%rsp)
jmp 0x6e28
movl 0x14(%rsp), %esi
movq 0x110(%rsp), %rdi
movsd (%rsp), %xmm0
movq 0x8(%rsp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
callq 0xe480
jmp 0x6e4d
jmp 0x6e4f
movl 0x128(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x128(%rsp)
jmp 0x6c7a
movb $0x1, 0x12f(%rsp)
testb $0x1, 0x12f(%rsp)
jne 0x6e84
movq 0x110(%rsp), %rdi
callq 0xd170
leaq 0x138(%rsp), %rdi
callq 0xd170
leaq 0x158(%rsp), %rdi
callq 0x3660
leaq 0x178(%rsp), %rdi
callq 0xd170
leaq 0x190(%rsp), %rdi
callq 0x3660
movq 0x118(%rsp), %rax
addq $0x1b8, %rsp            # imm = 0x1B8
retq
leaq 0x138(%rsp), %rdi
callq 0xd170
leaq 0x158(%rsp), %rdi
callq 0x3660
leaq 0x178(%rsp), %rdi
callq 0xd170
leaq 0x190(%rsp), %rdi
callq 0x3660
movq 0x170(%rsp), %rdi
callq 0x21e0
nopl (%rax)