// File: yukimasano[P]cpp_matrix/Matrix.cpp
// Function: CG_pre(Matrix&, Vector&, int&)
Vector CG_pre(Matrix& B, Vector& b, int& count){
	assert(nrows(B) == ncols(B));
	int m =nrows(B);
	// we want a square system
  // do the preconditioning by multiplying with D^{-1}
  Matrix A = B;
  for (int k=1;k<=m;k++){
    double x = A(k,k);
    //A.set_col(k, A.get_col(k)/x);
    for (int j=1;j<=m;j++){
     A.set_val(k,j, A(k,j)/x);
    }
  }
  Vector v = b;
  for (int k=1;k<=m;k++){
    v.set_val(k, v(k)/B(k,k));
  }
	// INITIALISATION
	Vector x= v;
	double tol = 1.e-12;
	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 (r.norm() > tol){
    count+=1;
		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;

		x = x + alpha*p;
		r = v - A*x;
    if (count>100){
      r = r*0;
      count = -100000;
    }
  }
  //	std::cout<<count<<std::endl<<std::flush;
	return x;
}

// Assembly:
subq $0x268, %rsp            # imm = 0x268
movq %rdi, 0x78(%rsp)
movq %rdi, %rax
movq %rax, 0x80(%rsp)
movq %rdi, 0x260(%rsp)
movq %rsi, 0x258(%rsp)
movq %rdx, 0x250(%rsp)
movq %rcx, 0x248(%rsp)
movq 0x258(%rsp), %rdi
callq 0x3ac0
movl %eax, 0x8c(%rsp)
movq 0x258(%rsp), %rdi
callq 0x3ad0
movl %eax, %ecx
movl 0x8c(%rsp), %eax
cmpl %ecx, %eax
jne 0xbf27
jmp 0xbf46
leaq 0x351c(%rip), %rdi      # 0xf44a
leaq 0x3214(%rip), %rsi      # 0xf149
movl $0x37a, %edx            # imm = 0x37A
leaq 0x351e(%rip), %rcx      # 0xf45f
callq 0x2090
movq 0x258(%rsp), %rdi
callq 0x3ac0
movl %eax, 0x244(%rsp)
movq 0x258(%rsp), %rsi
leaq 0x230(%rsp), %rdi
callq 0x5010
movl $0x1, 0x22c(%rsp)
movl 0x22c(%rsp), %eax
cmpl 0x244(%rsp), %eax
jg 0xc06d
movl 0x22c(%rsp), %edx
leaq 0x230(%rsp), %rdi
movl %edx, %esi
callq 0x36e0
movq %rax, 0x70(%rsp)
jmp 0xbfab
movq 0x70(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x220(%rsp)
movl $0x1, 0x210(%rsp)
movl 0x210(%rsp), %eax
cmpl 0x244(%rsp), %eax
jg 0xc055
movl 0x22c(%rsp), %esi
movl %esi, 0x60(%rsp)
movl 0x210(%rsp), %edx
movl %edx, 0x64(%rsp)
leaq 0x230(%rsp), %rdi
callq 0x36e0
movq %rax, 0x68(%rsp)
jmp 0xc002
movl 0x64(%rsp), %edx
movl 0x60(%rsp), %esi
movq 0x68(%rsp), %rax
movsd (%rax), %xmm0
divsd 0x220(%rsp), %xmm0
leaq 0x230(%rsp), %rdi
callq 0x47c0
movl 0x210(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x210(%rsp)
jmp 0xbfc8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xcadd
jmp 0xc057
movl 0x22c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x22c(%rsp)
jmp 0xbf7a
movq 0x250(%rsp), %rsi
leaq 0x200(%rsp), %rdi
callq 0xd0d0
jmp 0xc084
movl $0x1, 0x1fc(%rsp)
movl 0x1fc(%rsp), %eax
cmpl 0x244(%rsp), %eax
jg 0xc145
movl 0x1fc(%rsp), %esi
movl %esi, 0x54(%rsp)
leaq 0x200(%rsp), %rdi
callq 0xdee0
movq %rax, 0x58(%rsp)
jmp 0xc0c2
movq 0x58(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x258(%rsp), %rdi
movl 0x1fc(%rsp), %edx
movl %edx, %esi
callq 0x36e0
movq %rax, 0x48(%rsp)
jmp 0xc0ee
movl 0x54(%rsp), %esi
movsd 0x40(%rsp), %xmm0
movq 0x48(%rsp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
leaq 0x200(%rsp), %rdi
callq 0xe480
jmp 0xc114
jmp 0xc116
movl 0x1fc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1fc(%rsp)
jmp 0xc08f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xcad0
movq 0x78(%rsp), %rdi
movb $0x0, 0x1fb(%rsp)
leaq 0x200(%rsp), %rsi
callq 0xd0d0
jmp 0xc161
movq 0x78(%rsp), %rdx
movabsq $0x3d719799812dea11, %rax # imm = 0x3D719799812DEA11
movq %rax, 0x1f0(%rsp)
leaq 0x1d0(%rsp), %rdi
leaq 0x230(%rsp), %rsi
callq 0x4800
jmp 0xc18f
leaq 0x1e0(%rsp), %rdi
leaq 0x200(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
callq 0xd470
jmp 0xc1ae
leaq 0x1d0(%rsp), %rdi
callq 0xd170
leaq 0x1c0(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0xd0d0
jmp 0xc1d2
movl 0x244(%rsp), %esi
leaq 0x1b0(%rsp), %rdi
callq 0xd040
jmp 0xc1e8
leaq 0x1a0(%rsp), %rdi
leaq 0x230(%rsp), %rsi
leaq 0x1e0(%rsp), %rdx
callq 0x4800
jmp 0xc207
leaq 0x1b0(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0xdc70
jmp 0xc21e
leaq 0x1a0(%rsp), %rdi
callq 0xd170
leaq 0x1e0(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0xd740
movsd %xmm0, 0x38(%rsp)
jmp 0xc248
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x198(%rsp)
leaq 0x1e0(%rsp), %rdi
movl $0x2, %esi
callq 0xe320
movsd %xmm0, 0x30(%rsp)
jmp 0xc271
leaq 0x1e0(%rsp), %rdi
movl $0x2, %esi
callq 0xe320
movsd %xmm0, 0x28(%rsp)
jmp 0xc28b
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd 0x198(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x198(%rsp)
leaq 0x178(%rsp), %rdi
leaq 0x198(%rsp), %rsi
leaq 0x1e0(%rsp), %rdx
callq 0xd910
jmp 0xc2d0
movq 0x78(%rsp), %rsi
leaq 0x188(%rsp), %rdi
leaq 0x178(%rsp), %rdx
callq 0xd1a0
jmp 0xc2ec
movq 0x78(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0xdc70
jmp 0xc300
leaq 0x188(%rsp), %rdi
callq 0xd170
leaq 0x178(%rsp), %rdi
callq 0xd170
movq 0x78(%rsp), %rdx
leaq 0x158(%rsp), %rdi
leaq 0x230(%rsp), %rsi
callq 0x4800
jmp 0xc336
leaq 0x168(%rsp), %rdi
leaq 0x200(%rsp), %rsi
leaq 0x158(%rsp), %rdx
callq 0xd470
jmp 0xc355
leaq 0x1e0(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0xdc70
jmp 0xc36c
leaq 0x168(%rsp), %rdi
callq 0xd170
leaq 0x158(%rsp), %rdi
callq 0xd170
movl 0x244(%rsp), %esi
leaq 0x138(%rsp), %rdi
callq 0xd040
jmp 0xc39c
jmp 0xc39e
leaq 0x1e0(%rsp), %rdi
movl $0x2, %esi
callq 0xe320
movsd %xmm0, 0x20(%rsp)
jmp 0xc3b8
movsd 0x20(%rsp), %xmm0
ucomisd 0x1f0(%rsp), %xmm0
jbe 0xca14
movq 0x248(%rsp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
leaq 0x128(%rsp), %rdi
leaq 0x230(%rsp), %rsi
leaq 0x1e0(%rsp), %rdx
callq 0x4800
jmp 0xc3fa
leaq 0x1b0(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xdc70
jmp 0xc411
leaq 0x128(%rsp), %rdi
callq 0xd170
leaq 0x118(%rsp), %rdi
leaq 0x230(%rsp), %rsi
leaq 0x1c0(%rsp), %rdx
callq 0x4800
jmp 0xc43d
leaq 0x138(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0xdc70
jmp 0xc454
leaq 0x118(%rsp), %rdi
callq 0xd170
leaq 0x1e0(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0xd740
movsd %xmm0, 0x18(%rsp)
jmp 0xc47e
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x150(%rsp)
leaq 0x1c0(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0xd740
movsd %xmm0, 0x10(%rsp)
jmp 0xc4aa
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x148(%rsp)
movsd 0x150(%rsp), %xmm0
movsd 0x148(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x150(%rsp)
leaq 0xf8(%rsp), %rdi
leaq 0x150(%rsp), %rsi
leaq 0x1c0(%rsp), %rdx
callq 0xd910
jmp 0xc4f7
leaq 0x108(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
leaq 0xf8(%rsp), %rdx
callq 0xd470
jmp 0xc516
leaq 0x1c0(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0xdc70
jmp 0xc52d
leaq 0x108(%rsp), %rdi
callq 0xd170
leaq 0xf8(%rsp), %rdi
callq 0xd170
leaq 0x1e0(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0xd740
movsd %xmm0, 0x8(%rsp)
jmp 0xc564
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x198(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0x230(%rsp), %rsi
leaq 0x1c0(%rsp), %rdx
callq 0x4800
jmp 0xc592
leaq 0x138(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0xdc70
jmp 0xc5a9
leaq 0xe8(%rsp), %rdi
callq 0xd170
leaq 0x1c0(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0xd740
movsd %xmm0, (%rsp)
jmp 0xc5d2
movsd (%rsp), %xmm0
movsd %xmm0, 0x148(%rsp)
movsd 0x198(%rsp), %xmm0
movsd 0x148(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x198(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0x198(%rsp), %rsi
leaq 0x1c0(%rsp), %rdx
callq 0xd910
jmp 0xc61e
movq 0x78(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0xd1a0
jmp 0xc63a
movq 0x78(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0xdc70
jmp 0xc64e
leaq 0xd8(%rsp), %rdi
callq 0xd170
leaq 0xc8(%rsp), %rdi
callq 0xd170
movq 0x78(%rsp), %rdx
leaq 0xa8(%rsp), %rdi
leaq 0x230(%rsp), %rsi
callq 0x4800
jmp 0xc684
leaq 0xb8(%rsp), %rdi
leaq 0x200(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
callq 0xd470
jmp 0xc6a3
leaq 0x1e0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0xdc70
jmp 0xc6ba
leaq 0xb8(%rsp), %rdi
callq 0xd170
leaq 0xa8(%rsp), %rdi
callq 0xd170
movq 0x248(%rsp), %rax
cmpl $0x64, (%rax)
jle 0xca0f
movq $0x0, 0x90(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0xd860
jmp 0xc710
leaq 0x1e0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0xdc70
jmp 0xc727
leaq 0x98(%rsp), %rdi
callq 0xd170
movq 0x248(%rsp), %rax
movl $0xfffe7960, (%rax)     # imm = 0xFFFE7960
jmp 0xca0f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xcac6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0xd170
jmp 0xcac6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xca83
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xca76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xca69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0x1a0(%rsp), %rdi
callq 0xd170
jmp 0xca69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xc82e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0x188(%rsp), %rdi
callq 0xd170
leaq 0x178(%rsp), %rdi
callq 0xd170
jmp 0xca69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xc877
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0x168(%rsp), %rdi
callq 0xd170
leaq 0x158(%rsp), %rdi
callq 0xd170
jmp 0xca69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xca5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0x128(%rsp), %rdi
callq 0xd170
jmp 0xca5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0x118(%rsp), %rdi
callq 0xd170
jmp 0xca5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xc925
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0x108(%rsp), %rdi
callq 0xd170
leaq 0xf8(%rsp), %rdi
callq 0xd170
jmp 0xca5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0xd170
jmp 0xca5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xc994
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0xd170
leaq 0xc8(%rsp), %rdi
callq 0xd170
jmp 0xca5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
jmp 0xc9dd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xd170
leaq 0xa8(%rsp), %rdi
callq 0xd170
jmp 0xca5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x218(%rsp)
movl %eax, 0x214(%rsp)
leaq 0x98(%rsp), %rdi
callq 0xd170
jmp 0xca5c
jmp 0xc39e
movb $0x1, 0x1fb(%rsp)
leaq 0x138(%rsp), %rdi
callq 0xd170
leaq 0x1b0(%rsp), %rdi
callq 0xd170
leaq 0x1c0(%rsp), %rdi
callq 0xd170
leaq 0x1e0(%rsp), %rdi
callq 0xd170
testb $0x1, 0x1fb(%rsp)
jne 0xca9c
jmp 0xca92
leaq 0x138(%rsp), %rdi
callq 0xd170
leaq 0x1b0(%rsp), %rdi
callq 0xd170
leaq 0x1c0(%rsp), %rdi
callq 0xd170
leaq 0x1e0(%rsp), %rdi
callq 0xd170
jmp 0xcac6
movq 0x78(%rsp), %rdi
callq 0xd170
leaq 0x200(%rsp), %rdi
callq 0xd170
leaq 0x230(%rsp), %rdi
callq 0x3660
movq 0x80(%rsp), %rax
addq $0x268, %rsp            # imm = 0x268
retq
movq 0x78(%rsp), %rdi
callq 0xd170
leaq 0x200(%rsp), %rdi
callq 0xd170
leaq 0x230(%rsp), %rdi
callq 0x3660
movq 0x218(%rsp), %rdi
callq 0x21e0
nopw (%rax,%rax)