// File: /workspace/github_cpp_makefile_star2_O2/IlanPrice[P]Linear-Algebra-Library-in-Cplusplus/Matrix.cpp
// Function: GMRES(Matrix const&, Vector const&)
Vector GMRES(const Matrix& A, const Vector& b)
{
	std::ofstream GMRESres;
	GMRESres.open("GMRESres.txt");
	int n = length(b);
	Vector x(n);
	Vector temp1 = A*x;
	Vector r = b - temp1;
	double norm_r = norm(r);
	GMRESres << std::setprecision(15) << norm_r << "\n";
	double norm_r0 = norm_r;
	Vector v = r/norm_r;
	Matrix V(length(v),n);
	Matrix H(n+1,n);
	Vector r_e1(n+1);
	r_e1(1) = norm_r0;
	Matrix G = eye(n+1);
	V.setElements(1,n,1,1,v);

	double Tol = 1e-14;
	int k=0;
	while (norm_r > Tol)
		{
			k = k+1;
			Vector v_k = V.getElements(1,n,k,k,"Vector");
			Vector w = A*v_k;
			for (int j = 1; j<=k; j++)
			{
				Vector v_j = V.getElements(1,n,j,j,"Vector");
				double h = v_j*w;
				H(j,k) = h;
				w = w - h*v_j;
			}
			double hplus = norm(w);
			H(k+1,k) = hplus;
			if (hplus<1e-13)
			{break;}
			else
			{
				Vector v_kplus1 = w/hplus;
				V.setElements(1,n,k+1,k+1,v_kplus1);
				Matrix Hhat = H.getElements(1,(k+1),1,k);
				Hhat = G.getElements(1,k+1,1,k+1)*Hhat;
				std::pair<double,double> g = givens(Hhat(k,k),Hhat(k+1,k));
				double gcos = g.first; double gsin = g.second;
				G.applygivens(k,k+1,gcos,gsin);
				norm_r = std::abs(norm_r*gsin);
				GMRESres << std::setprecision(15) << norm_r << "\n";
			}

		}

		r_e1.setValues(1,k+1,G.getElements(1,k+1,1,k+1)*r_e1.getValues(1,k+1));
		H.setElements(1,k+1,1,k, G.getElements(1,k+1,1,k+1)*H.getElements(1,(k+1),1,k));
		Vector y = backsubst(H.getElements(1,k,1,k),r_e1.getValues(1,k));
		Vector xfinal = temp1 + V.getElements(1,n,1,k)*y;
		GMRESres.close();
		return xfinal;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3b8, %rsp            # imm = 0x3B8
movq %rdx, %rbx
movq %rsi, 0xb8(%rsp)
movq %rdi, 0x78(%rsp)
leaq 0x1b8(%rsp), %r14
movq %r14, %rdi
callq 0x1140
leaq 0x3ed5(%rip), %rsi      # 0x95b1
pushq $0x10
popq %rdx
movq %r14, %rdi
callq 0x1040
movq %rbx, %rdi
callq 0x2141
leaq 0x148(%rsp), %rdi
movl %eax, 0xc(%rsp)
movl %eax, %esi
callq 0x186c
leaq 0xf8(%rsp), %rdi
leaq 0x148(%rsp), %rdx
movq 0xb8(%rsp), %rsi
callq 0x38bd
leaq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rdx
movq %rbx, %rsi
callq 0x1a3c
leaq 0xe8(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x20c8
movsd %xmm0, 0x60(%rsp)
leaq 0x1b8(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0xf, 0x1c0(%rsp,%rax)
callq 0x1130
leaq 0x3b56(%rip), %rsi      # 0x92ca
movq %rax, %rdi
callq 0x10e0
leaq 0x60(%rsp), %rdx
movsd (%rdx), %xmm0
movsd %xmm0, 0x50(%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x1c60
leaq 0xd8(%rsp), %rdi
callq 0x2141
movl 0xc(%rsp), %ebp
leaq 0x88(%rsp), %rdi
movl %eax, %esi
movl %ebp, %edx
callq 0x285c
movl %ebp, %ebx
incl %ebx
leaq 0x68(%rsp), %rdi
movl %ebx, %esi
movl %ebp, %edx
callq 0x285c
leaq 0xa8(%rsp), %rdi
movl %ebx, %esi
callq 0x186c
leaq 0xa8(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x2030
movsd 0x50(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x50c7
leaq 0x138(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x18bc
leaq 0x88(%rsp), %rdi
pushq $0x1
popq %r12
leaq 0x138(%rsp), %r9
movl %r12d, %esi
movl 0xc(%rsp), %edx
movl %r12d, %ecx
movl %r12d, %r8d
callq 0x32d6
leaq 0x138(%rsp), %rdi
callq 0x1908
movq $0x0, 0x80(%rsp)
leaq 0x10(%rsp), %r14
leaq 0x40(%rsp), %rbp
leaq 0x158(%rsp), %rbx
leaq 0x168(%rsp), %r13
movq $0x0, 0x20(%rsp)
movsd 0x60(%rsp), %xmm0
ucomisd 0x3a4f(%rip), %xmm0     # 0x92e0
jbe 0x5c0b
leaq 0x198(%rsp), %rdi
leaq 0x3d18(%rip), %rsi      # 0x95be
movq %r14, %rdx
callq 0x1060
movq 0x20(%rsp), %rax
leal 0x1(%rax), %r8d
leaq 0x28(%rsp), %r15
movq %r15, %rdi
leaq 0x88(%rsp), %rsi
movl %r12d, %edx
movl 0xc(%rsp), %ecx
movq %r8, 0x38(%rsp)
movl %r8d, %r9d
callq 0x2d9e
leaq 0x198(%rsp), %rdi
callq 0x1070
movq %r14, %rdi
movq 0xb8(%rsp), %rsi
movq %r15, %rdx
callq 0x38bd
pushq $0x1
popq %rax
movl %eax, %r15d
movq 0x80(%rsp), %rax
addl %r15d, %eax
cmpl $0x2, %eax
je 0x59d8
leaq 0x178(%rsp), %rdi
leaq 0x3c9a(%rip), %rsi      # 0x95be
movq %rbx, %rdx
callq 0x1060
movq %rbp, %rdi
leaq 0x88(%rsp), %rsi
pushq $0x1
popq %rdx
movl 0xc(%rsp), %ecx
movl %r15d, %r8d
movl %r15d, %r9d
callq 0x2d9e
leaq 0x178(%rsp), %rdi
callq 0x1070
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1b63
movsd %xmm0, 0xc8(%rsp)
leaq 0x68(%rsp), %rdi
movl %r15d, %esi
movq 0x38(%rsp), %rdx
movsd %xmm0, 0x50(%rsp)
callq 0x3a1a
movsd 0x50(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r13, %rdi
leaq 0xc8(%rsp), %rsi
movq %rbp, %rdx
callq 0x1c1c
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1a3c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1da0
movq %rbx, %rdi
callq 0x1908
movq %r13, %rdi
callq 0x1908
movq %rbp, %rdi
callq 0x1908
incl %r15d
jmp 0x5901
movq %r14, %rdi
pushq $0x2
popq %rsi
callq 0x20c8
movsd %xmm0, 0xc0(%rsp)
movq 0x20(%rsp), %rsi
addl $0x2, %esi
leaq 0x68(%rsp), %rdi
movq %rsi, 0x20(%rsp)
movq 0x38(%rsp), %rdx
movsd %xmm0, 0x50(%rsp)
callq 0x3a1a
movsd 0x50(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x38c8(%rip), %xmm0     # 0x92e8
ucomisd 0xc0(%rsp), %xmm0
ja 0x5c12
movq %rbp, %rdi
movq %r14, %rsi
leaq 0xc0(%rsp), %rdx
callq 0x1c60
leaq 0x128(%rsp), %rdi
movq %rbp, %rsi
callq 0x18bc
leaq 0x88(%rsp), %rdi
pushq $0x1
popq %r15
movl %r15d, %esi
movl 0xc(%rsp), %edx
movq 0x20(%rsp), %r12
movl %r12d, %ecx
movl %r12d, %r8d
leaq 0x128(%rsp), %r9
callq 0x32d6
leaq 0x128(%rsp), %rdi
callq 0x1908
movq %rbx, %rdi
leaq 0x68(%rsp), %rsi
movl %r15d, %edx
movl %r12d, %ecx
movl %r15d, %r8d
movq 0x38(%rsp), %r9
callq 0x2b96
leaq 0xc8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
movl %r15d, %edx
movl %r12d, %ecx
movl %r15d, %r8d
movl %r12d, %r9d
callq 0x2b96
movq %r13, %rdi
leaq 0xc8(%rsp), %r15
movq %r15, %rsi
movq %rbx, %rdx
callq 0x376b
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4c1c
movq %r13, %rdi
callq 0x2a78
movq %r15, %rdi
callq 0x2a78
movq %rbx, %rdi
movq 0x38(%rsp), %r15
movl %r15d, %esi
movl %r15d, %edx
callq 0x3a1a
movq 0x20(%rsp), %r12
movsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movq %rbx, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq 0x3a1a
movsd (%rax), %xmm3
movapd %xmm3, %xmm2
mulsd %xmm3, %xmm2
movsd 0x50(%rsp), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
sqrtsd %xmm1, %xmm1
divsd %xmm1, %xmm0
xorpd 0x34eb(%rip), %xmm3     # 0x9040
divsd %xmm1, %xmm3
movapd %xmm3, 0x50(%rsp)
leaq 0x98(%rsp), %rdi
movl %r15d, %esi
movl %r12d, %edx
movapd %xmm3, %xmm1
callq 0x610e
movapd 0x50(%rsp), %xmm0
mulsd 0x60(%rsp), %xmm0
andpd 0x34c6(%rip), %xmm0     # 0x9050
movlpd %xmm0, 0x60(%rsp)
movq 0x1b8(%rsp), %rax
movq -0x18(%rax), %rax
movq $0xf, 0x1c0(%rsp,%rax)
leaq 0x1b8(%rsp), %rdi
callq 0x1130
movq %rax, %rdi
leaq 0x370b(%rip), %rsi      # 0x92ca
callq 0x10e0
movq %rbx, %rdi
callq 0x2a78
movq %rbp, %rdi
callq 0x1908
movq %r14, %rdi
callq 0x1908
leaq 0x28(%rsp), %rdi
callq 0x1908
movq 0x80(%rsp), %rax
decl %eax
movq %rax, 0x80(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x20(%rsp)
pushq $0x1
popq %r12
jmp 0x5883
movq 0x20(%rsp), %r12
jmp 0x5c2b
leaq 0x10(%rsp), %rdi
callq 0x1908
leaq 0x28(%rsp), %rdi
callq 0x1908
movq 0x38(%rsp), %r12
movq %r12, %r14
leal 0x1(%r12), %ebx
leaq 0x28(%rsp), %rdi
leaq 0x98(%rsp), %rsi
pushq $0x1
popq %rdx
movl %ebx, %ecx
movl %edx, %r8d
movl %ebx, %r9d
callq 0x2b96
leaq 0x10(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
pushq $0x1
popq %rdx
movl %ebx, %ecx
callq 0x2446
leaq 0x118(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x38bd
leaq 0xa8(%rsp), %rdi
pushq $0x1
popq %rsi
leaq 0x118(%rsp), %rcx
movl %ebx, %edx
callq 0x2488
leaq 0x118(%rsp), %rdi
callq 0x1908
leaq 0x10(%rsp), %rdi
callq 0x1908
leaq 0x28(%rsp), %rdi
callq 0x2a78
leaq 0x28(%rsp), %rdi
leaq 0x98(%rsp), %rsi
pushq $0x1
popq %rdx
movl %ebx, %ecx
movl %edx, %r8d
movl %ebx, %r9d
callq 0x2b96
leaq 0x10(%rsp), %rdi
leaq 0x68(%rsp), %rsi
pushq $0x1
popq %rdx
movl %ebx, %ecx
movl %edx, %r8d
movl %r14d, %r9d
callq 0x2b96
leaq 0x108(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x376b
leaq 0x68(%rsp), %rdi
pushq $0x1
popq %rcx
leaq 0x108(%rsp), %r9
movl %ecx, %esi
movl %ebx, %edx
movq %r14, %rbx
movl %ebx, %r8d
callq 0x30c2
leaq 0x108(%rsp), %rdi
callq 0x2a78
leaq 0x10(%rsp), %rdi
callq 0x2a78
leaq 0x28(%rsp), %rdi
callq 0x2a78
leaq 0x10(%rsp), %rdi
leaq 0x68(%rsp), %rsi
pushq $0x1
popq %rdx
movl %ebx, %ecx
movl %edx, %r8d
movl %ebx, %r9d
callq 0x2b96
leaq 0x40(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
pushq $0x1
popq %rdx
movl %ebx, %ecx
callq 0x2446
leaq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x40(%rsp), %rdx
xorl %ecx, %ecx
callq 0x46e5
leaq 0x40(%rsp), %rdi
callq 0x1908
leaq 0x10(%rsp), %rdi
callq 0x2a78
leaq 0x40(%rsp), %rdi
leaq 0x88(%rsp), %rsi
pushq $0x1
popq %rdx
movl 0xc(%rsp), %ecx
movl %edx, %r8d
movl %r14d, %r9d
callq 0x2b96
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x38bd
leaq 0xf8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq 0x78(%rsp), %rdi
callq 0x1915
leaq 0x10(%rsp), %rdi
callq 0x1908
leaq 0x40(%rsp), %rdi
callq 0x2a78
leaq 0x1b8(%rsp), %rdi
callq 0x1100
leaq 0x28(%rsp), %rdi
callq 0x1908
leaq 0x98(%rsp), %rdi
callq 0x2a78
leaq 0xa8(%rsp), %rdi
callq 0x1908
leaq 0x68(%rsp), %rdi
callq 0x2a78
leaq 0x88(%rsp), %rdi
callq 0x2a78
leaq 0xd8(%rsp), %rdi
callq 0x1908
leaq 0xe8(%rsp), %rdi
callq 0x1908
leaq 0xf8(%rsp), %rdi
callq 0x1908
leaq 0x148(%rsp), %rdi
callq 0x1908
leaq 0x1b8(%rsp), %rdi
callq 0x1110
movq 0x78(%rsp), %rax
addq $0x3b8, %rsp            # imm = 0x3B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq 0x78(%rsp), %rdi
jmp 0x605c
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x1908
jmp 0x5ebf
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x2a78
jmp 0x6061
jmp 0x5ff2
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x1908
jmp 0x5ee5
movq %rax, %r14
leaq 0x10(%rsp), %rdi
jmp 0x5f42
jmp 0x5fd8
movq %rax, %r14
leaq 0x108(%rsp), %rdi
callq 0x2a78
jmp 0x5f06
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x2a78
jmp 0x5f3d
jmp 0x5f3a
jmp 0x5fd8
movq %rax, %r14
leaq 0x118(%rsp), %rdi
callq 0x1908
jmp 0x5f2e
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x1908
jmp 0x5f3d
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x2a78
jmp 0x606b
jmp 0x5fd8
movq %rax, %r14
leaq 0x138(%rsp), %rdi
jmp 0x6066
jmp 0x5fd8
jmp 0x5f65
movq %rax, %r14
jmp 0x6078
movq %rax, %r14
jmp 0x6085
movq %rax, %r14
jmp 0x608f
jmp 0x5fa3
movq %rax, %r14
jmp 0x60b6
movq %rax, %r14
jmp 0x60c3
jmp 0x5f93
jmp 0x5f93
movq %rax, %r14
jmp 0x60d0
movq %rax, %r14
jmp 0x609c
movq %rax, %r14
jmp 0x60a9
jmp 0x5ff9
jmp 0x6033
movq %rax, %r14
leaq 0xc8(%rsp), %rdi
callq 0x2a78
jmp 0x5ffc
jmp 0x604a
movq %rax, %r14
leaq 0x128(%rsp), %rdi
jmp 0x602c
jmp 0x604a
movq %rax, %r14
jmp 0x606b
movq %rax, %r14
leaq 0x198(%rsp), %rdi
callq 0x1070
jmp 0x606b
movq %rax, %r14
jmp 0x6061
jmp 0x5ff9
movq %rax, %r14
leaq 0x158(%rsp), %rdi
callq 0x2a78
jmp 0x604d
jmp 0x6033
jmp 0x604a
movq %rax, %r14
jmp 0x6024
movq %rax, %r14
leaq 0x158(%rsp), %rdi
callq 0x1908
leaq 0x168(%rsp), %rdi
callq 0x1908
jmp 0x604d
movq %rax, %r14
jmp 0x6057
movq %rax, %r14
leaq 0x178(%rsp), %rdi
callq 0x1070
jmp 0x6057
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x1908
leaq 0x10(%rsp), %rdi
callq 0x1908
leaq 0x28(%rsp), %rdi
callq 0x1908
leaq 0x98(%rsp), %rdi
callq 0x2a78
leaq 0xa8(%rsp), %rdi
callq 0x1908
leaq 0x68(%rsp), %rdi
callq 0x2a78
leaq 0x88(%rsp), %rdi
callq 0x2a78
leaq 0xd8(%rsp), %rdi
callq 0x1908
leaq 0xe8(%rsp), %rdi
callq 0x1908
leaq 0xf8(%rsp), %rdi
callq 0x1908
leaq 0x148(%rsp), %rdi
callq 0x1908
leaq 0x1b8(%rsp), %rdi
callq 0x1110
movq %r14, %rdi
callq 0x1180