// File: yukimasano[P]cpp_matrix/Matrix.cpp
// Function: CG(Matrix&, Vector&, int&)
Vector CG(Matrix& A, Vector& v, int& count){
	assert(nrows(A) == ncols(A));
	int m =nrows(A);
	// we want a square system

	// INITIALISATION
  Vector x= v;
	double tol = 1.e-14*v.norm();
	Vector r = v - A*x;
	Vector p = r;
	Vector temp(m);
	temp = A*r;
	double alpha;
	alpha = r*temp;
	alpha = ((r.norm())*(r.norm()))/alpha;
	x = x +alpha *r;
	r = v - A*x;
	double beta;
	double anorm;
	Vector temp2(m);
	//std::cout<<" before the while loop "<<(A*x -c).norm() << std::endl<<std::flush;
	while ((A*x -v).norm() > tol){
		temp = A*r;
		temp2 = A*p;
		beta = r*temp2;
		anorm = p*temp2;
		beta = beta /anorm;
		p = r-beta*p;
		alpha = r*p;
    temp2 = A*p;
    anorm = p*temp2;
		alpha = alpha/anorm;
		count+=1;
		x = x + alpha*p;
		r = v - A*x;
    if (count>1000000){
      count = -1;
      return x;
    }
	}
  //	std::cout<<count<<std::endl<<std::flush;
	return x;
}

// Assembly:
subq $0x228, %rsp            # imm = 0x228
movq %rdi, 0x68(%rsp)
movq %rdi, %rax
movq %rax, 0x70(%rsp)
movq %rdi, 0x220(%rsp)
movq %rsi, 0x218(%rsp)
movq %rdx, 0x210(%rsp)
movq %rcx, 0x208(%rsp)
movq 0x218(%rsp), %rdi
callq 0x3ac0
movl %eax, 0x78(%rsp)
movq 0x218(%rsp), %rdi
callq 0x3ad0
movl %eax, %ecx
movl 0x78(%rsp), %eax
cmpl %ecx, %eax
jne 0xb46e
jmp 0xb48d
leaq 0x3e9d(%rip), %rdi      # 0xf312
leaq 0x3ccd(%rip), %rsi      # 0xf149
movl $0x34d, %edx            # imm = 0x34D
leaq 0x3f9d(%rip), %rcx      # 0xf425
callq 0x2090
movq 0x218(%rsp), %rdi
callq 0x3ac0
movq 0x68(%rsp), %rdi
movl %eax, 0x204(%rsp)
movb $0x0, 0x203(%rsp)
movq 0x210(%rsp), %rsi
callq 0xd0d0
movq 0x210(%rsp), %rdi
movl $0x2, %esi
callq 0xe320
movsd %xmm0, 0x60(%rsp)
jmp 0xb4d5
movq 0x68(%rsp), %rdx
movsd 0x60(%rsp), %xmm0
movsd 0x3ba8(%rip), %xmm1     # 0xf090
mulsd %xmm1, %xmm0
movsd %xmm0, 0x1f8(%rsp)
movq 0x210(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x218(%rsp), %rsi
leaq 0x1c8(%rsp), %rdi
callq 0x4800
jmp 0xb519
movq 0x58(%rsp), %rsi
leaq 0x1d8(%rsp), %rdi
leaq 0x1c8(%rsp), %rdx
callq 0xd470
jmp 0xb535
leaq 0x1c8(%rsp), %rdi
callq 0xd170
leaq 0x1b8(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0xd0d0
jmp 0xb559
movl 0x204(%rsp), %esi
leaq 0x1a8(%rsp), %rdi
callq 0xd040
jmp 0xb56f
movq 0x218(%rsp), %rsi
leaq 0x198(%rsp), %rdi
leaq 0x1d8(%rsp), %rdx
callq 0x4800
jmp 0xb58e
leaq 0x1a8(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0xdc70
jmp 0xb5a5
leaq 0x198(%rsp), %rdi
callq 0xd170
leaq 0x1d8(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0xd740
movsd %xmm0, 0x50(%rsp)
jmp 0xb5cf
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x190(%rsp)
leaq 0x1d8(%rsp), %rdi
movl $0x2, %esi
callq 0xe320
movsd %xmm0, 0x48(%rsp)
jmp 0xb5f8
leaq 0x1d8(%rsp), %rdi
movl $0x2, %esi
callq 0xe320
movsd %xmm0, 0x40(%rsp)
jmp 0xb612
movsd 0x40(%rsp), %xmm1
movsd 0x48(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd 0x190(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x190(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0x190(%rsp), %rsi
leaq 0x1d8(%rsp), %rdx
callq 0xd910
jmp 0xb657
movq 0x68(%rsp), %rsi
leaq 0x180(%rsp), %rdi
leaq 0x170(%rsp), %rdx
callq 0xd1a0
jmp 0xb673
movq 0x68(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0xdc70
jmp 0xb687
leaq 0x180(%rsp), %rdi
callq 0xd170
leaq 0x170(%rsp), %rdi
callq 0xd170
movq 0x68(%rsp), %rdx
movq 0x210(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x218(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0x4800
jmp 0xb6ca
movq 0x38(%rsp), %rsi
leaq 0x160(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0xd470
jmp 0xb6e6
leaq 0x1d8(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0xdc70
jmp 0xb6fd
leaq 0x160(%rsp), %rdi
callq 0xd170
leaq 0x150(%rsp), %rdi
callq 0xd170
movl 0x204(%rsp), %esi
leaq 0x130(%rsp), %rdi
callq 0xd040
jmp 0xb72d
jmp 0xb72f
movq 0x68(%rsp), %rdx
movq 0x218(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x4800
jmp 0xb74b
movq 0x210(%rsp), %rdx
leaq 0x120(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0xd470
jmp 0xb76a
leaq 0x120(%rsp), %rdi
movl $0x2, %esi
callq 0xe320
movsd %xmm0, 0x30(%rsp)
jmp 0xb784
movsd 0x1f8(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x120(%rsp), %rdi
callq 0xd170
leaq 0x110(%rsp), %rdi
callq 0xd170
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xbdfe
movq 0x218(%rsp), %rsi
leaq 0x100(%rsp), %rdi
leaq 0x1d8(%rsp), %rdx
callq 0x4800
jmp 0xb7e2
leaq 0x1a8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0xdc70
jmp 0xb7f9
leaq 0x100(%rsp), %rdi
callq 0xd170
movq 0x218(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
leaq 0x1b8(%rsp), %rdx
callq 0x4800
jmp 0xb825
leaq 0x130(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0xdc70
jmp 0xb83c
leaq 0xf0(%rsp), %rdi
callq 0xd170
leaq 0x1d8(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0xd740
movsd %xmm0, 0x20(%rsp)
jmp 0xb866
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x148(%rsp)
leaq 0x1b8(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0xd740
movsd %xmm0, 0x18(%rsp)
jmp 0xb892
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
movsd 0x148(%rsp), %xmm0
movsd 0x140(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x148(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0x148(%rsp), %rsi
leaq 0x1b8(%rsp), %rdx
callq 0xd910
jmp 0xb8df
leaq 0xe0(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
callq 0xd470
jmp 0xb8fe
leaq 0x1b8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0xdc70
jmp 0xb915
leaq 0xe0(%rsp), %rdi
callq 0xd170
leaq 0xd0(%rsp), %rdi
callq 0xd170
leaq 0x1d8(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0xd740
movsd %xmm0, 0x10(%rsp)
jmp 0xb94c
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x190(%rsp)
movq 0x218(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
leaq 0x1b8(%rsp), %rdx
callq 0x4800
jmp 0xb97a
leaq 0x130(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0xdc70
jmp 0xb991
leaq 0xc0(%rsp), %rdi
callq 0xd170
leaq 0x1b8(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0xd740
movsd %xmm0, 0x8(%rsp)
jmp 0xb9bb
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
movsd 0x190(%rsp), %xmm0
movsd 0x140(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x190(%rsp)
movq 0x208(%rsp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
leaq 0xa0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
leaq 0x1b8(%rsp), %rdx
callq 0xd910
jmp 0xba16
movq 0x68(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
callq 0xd1a0
jmp 0xba32
movq 0x68(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xdc70
jmp 0xba46
leaq 0xb0(%rsp), %rdi
callq 0xd170
leaq 0xa0(%rsp), %rdi
callq 0xd170
movq 0x68(%rsp), %rdx
movq 0x210(%rsp), %rax
movq %rax, (%rsp)
movq 0x218(%rsp), %rsi
leaq 0x80(%rsp), %rdi
callq 0x4800
jmp 0xba88
movq (%rsp), %rsi
leaq 0x90(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0xd470
jmp 0xbaa3
leaq 0x1d8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xdc70
jmp 0xbaba
leaq 0x90(%rsp), %rdi
callq 0xd170
leaq 0x80(%rsp), %rdi
callq 0xd170
movq 0x208(%rsp), %rax
cmpl $0xf4240, (%rax)        # imm = 0xF4240
jle 0xbdf9
movq 0x208(%rsp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movb $0x1, 0x203(%rsp)
movl $0x1, 0x7c(%rsp)
jmp 0xbe0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbe9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0xd170
jmp 0xbe9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbe75
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbe68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbe5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x198(%rsp), %rdi
callq 0xd170
jmp 0xbe5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbbf2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x180(%rsp), %rdi
callq 0xd170
leaq 0x170(%rsp), %rdi
callq 0xd170
jmp 0xbe5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbc3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x160(%rsp), %rdi
callq 0xd170
leaq 0x150(%rsp), %rdi
callq 0xd170
jmp 0xbe5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbe4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbc9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x120(%rsp), %rdi
callq 0xd170
leaq 0x110(%rsp), %rdi
callq 0xd170
jmp 0xbe4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xd170
jmp 0xbe4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0xd170
jmp 0xbe4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbd32
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xd170
leaq 0xd0(%rsp), %rdi
callq 0xd170
jmp 0xbe4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0xd170
jmp 0xbe4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbda1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0xd170
leaq 0xa0(%rsp), %rdi
callq 0xd170
jmp 0xbe4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0xbdea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x90(%rsp), %rdi
callq 0xd170
leaq 0x80(%rsp), %rdi
callq 0xd170
jmp 0xbe4e
jmp 0xb72f
movb $0x1, 0x203(%rsp)
movl $0x1, 0x7c(%rsp)
leaq 0x130(%rsp), %rdi
callq 0xd170
leaq 0x1a8(%rsp), %rdi
callq 0xd170
leaq 0x1b8(%rsp), %rdi
callq 0xd170
leaq 0x1d8(%rsp), %rdi
callq 0xd170
testb $0x1, 0x203(%rsp)
jne 0xbe8e
jmp 0xbe84
leaq 0x130(%rsp), %rdi
callq 0xd170
leaq 0x1a8(%rsp), %rdi
callq 0xd170
leaq 0x1b8(%rsp), %rdi
callq 0xd170
leaq 0x1d8(%rsp), %rdi
callq 0xd170
jmp 0xbe9b
movq 0x68(%rsp), %rdi
callq 0xd170
movq 0x70(%rsp), %rax
addq $0x228, %rsp            # imm = 0x228
retq
movq 0x68(%rsp), %rdi
callq 0xd170
movq 0x1f0(%rsp), %rdi
callq 0x21e0
nopw %cs:(%rax,%rax)