// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, 0x58(%rsp)
movq %rdx, 0xb0(%rsp)
movq %rdi, 0x20(%rsp)
movl 0x8(%rsi), %r14d
cmpl 0xc(%rsi), %r14d
jne 0x7d97
movq %rsi, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3d86
movq %r14, 0x88(%rsp)
testl %r14d, %r14d
jle 0x7952
leaq 0x48(%rsp), %r12
movq (%r12), %r15
movq 0x88(%rsp), %rax
leaq 0x1(%rax), %r14
movl $0x1, %r13d
movq %r12, %rdi
movl %r13d, %esi
movl %r13d, %edx
callq 0x2f5c
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movl $0x1, %ebp
movq %r12, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x2f5c
movsd (%rax), %xmm0
divsd 0x28(%rsp), %xmm0
movq -0x8(%r15,%r13,8), %rax
movsd %xmm0, -0x8(%rax,%rbp,8)
incq %rbp
cmpq %rbp, %r14
jne 0x7920
incq %r13
cmpq %r14, %r13
jne 0x7903
leaq 0x78(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x822a
movq 0x88(%rsp), %r13
testl %r13d, %r13d
jle 0x79cd
movl %r13d, %r15d
negl %r15d
movl $0x1, %r12d
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0x8abc
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq %rbx, %rdi
movl %r12d, %esi
movl %r12d, %edx
callq 0x2f5c
movsd 0x28(%rsp), %xmm0
divsd (%rax), %xmm0
movq %r14, %rdi
movl %r12d, %esi
callq 0x8e60
leal (%r15,%r12), %eax
incl %eax
movl %r12d, %ecx
incl %ecx
movl %ecx, %r12d
cmpl $0x1, %eax
jne 0x7982
leaq 0x78(%rsp), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x822a
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %rbx, %rdx
callq 0x3915
leaq 0x38(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x8428
leaq 0x68(%rsp), %rdi
callq 0x827e
leaq 0x68(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x822a
leaq 0x90(%rsp), %rdi
movl %r13d, %esi
callq 0x81e8
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x3915
leaq 0x90(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x893a
leaq 0x10(%rsp), %rdi
callq 0x827e
leaq 0x38(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x85c5
movsd %xmm0, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
movl $0x2, %esi
callq 0x8d68
movsd %xmm0, 0x28(%rsp)
leaq 0x38(%rsp), %rdi
movl $0x2, %esi
callq 0x8d68
movsd 0x28(%rsp), %xmm1
mulsd %xmm0, %xmm1
leaq 0x30(%rsp), %rsi
divsd (%rsi), %xmm1
movsd %xmm1, (%rsi)
movq %rsp, %rdi
leaq 0x38(%rsp), %rdx
callq 0x86d1
leaq 0x10(%rsp), %rdi
movq %rsp, %rdx
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x828b
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x893a
leaq 0x10(%rsp), %rdi
callq 0x827e
movq %rsp, %rdi
callq 0x827e
movq %rsp, %rdi
leaq 0x48(%rsp), %rsi
movq %rbx, %rdx
callq 0x3915
leaq 0x10(%rsp), %rdi
leaq 0x78(%rsp), %rsi
movq %rsp, %rdx
callq 0x8428
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x893a
leaq 0x10(%rsp), %rdi
callq 0x827e
movq %rsp, %rdi
callq 0x827e
leaq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0x81e8
leaq 0x38(%rsp), %r15
movq %rsp, %r12
leaq 0x68(%rsp), %rbp
leaq 0x10(%rsp), %rbx
leaq 0xa0(%rsp), %r14
movq 0x20(%rsp), %r13
movq %r15, %rdi
movl $0x2, %esi
callq 0x8d68
ucomisd 0x1522(%rip), %xmm0     # 0x9098
jbe 0x7d41
movq 0x58(%rsp), %rax
incl (%rax)
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
movq %r15, %rdx
callq 0x3915
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
callq 0x893a
movq %r12, %rdi
callq 0x827e
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
movq %rbp, %rdx
callq 0x3915
movq %rbx, %rdi
movq %r12, %rsi
callq 0x893a
movq %r12, %rdi
callq 0x827e
movq %r15, %rdi
movq %rbx, %rsi
callq 0x85c5
movsd %xmm0, 0x60(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x85c5
movsd 0x60(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x60(%rsp)
movq %r14, %rdi
leaq 0x60(%rsp), %rsi
movq %rbp, %rdx
callq 0x86d1
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x8428
movq %rbp, %rdi
movq %r12, %rsi
callq 0x893a
movq %r12, %rdi
callq 0x827e
movq %r14, %rdi
callq 0x827e
movq %r15, %rdi
movq %rbp, %rsi
callq 0x85c5
movsd %xmm0, 0x30(%rsp)
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
movq %rbp, %rdx
callq 0x3915
movq %rbx, %rdi
movq %r12, %rsi
callq 0x893a
movq %r12, %rdi
callq 0x827e
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x85c5
movsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
movq %rbp, %rdx
callq 0x86d1
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x828b
movq %r13, %rdi
movq %r12, %rsi
callq 0x893a
movq %r12, %rdi
callq 0x827e
movq %r14, %rdi
callq 0x827e
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
movq %r13, %rdx
callq 0x3915
movq %r12, %rdi
leaq 0x78(%rsp), %rsi
movq %r14, %rdx
callq 0x8428
movq %r15, %rdi
movq %r12, %rsi
callq 0x893a
movq %r12, %rdi
callq 0x827e
movq %r14, %rdi
callq 0x827e
movq 0x58(%rsp), %rax
cmpl $0x65, (%rax)
jl 0x7b61
movq $0x0, 0xa0(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x864a
movq %r15, %rdi
movq %r12, %rsi
callq 0x893a
movq %r12, %rdi
callq 0x827e
movq 0x58(%rsp), %rax
movl $0xfffe7960, (%rax)     # imm = 0xFFFE7960
jmp 0x7b61
leaq 0x10(%rsp), %rdi
callq 0x827e
leaq 0x90(%rsp), %rdi
callq 0x827e
leaq 0x68(%rsp), %rdi
callq 0x827e
leaq 0x38(%rsp), %rdi
callq 0x827e
leaq 0x78(%rsp), %rdi
callq 0x827e
leaq 0x48(%rsp), %rdi
callq 0x2f1c
movq 0x20(%rsp), %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16b4(%rip), %rdi      # 0x9452
leaq 0x13ac(%rip), %rsi      # 0x9151
leaq 0x16bb(%rip), %rcx      # 0x9467
movl $0x37a, %edx            # imm = 0x37A
callq 0x20b0
jmp 0x7e0d
jmp 0x7dbe
jmp 0x7dcd
jmp 0x7e0d
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x827e
jmp 0x7dd0
movq %rax, %r14
movq %rsp, %rdi
jmp 0x7e67
jmp 0x7e0d
jmp 0x7e5f
jmp 0x7e0d
movq %rax, %r14
jmp 0x7e79
movq %rax, %r14
jmp 0x7e83
movq %rax, %r14
leaq 0x68(%rsp), %rdi
jmp 0x7e88
movq %rax, %r14
jmp 0x7e8d
jmp 0x7e5a
jmp 0x7ea3
movq %rax, %r14
jmp 0x7e6c
jmp 0x7e25
jmp 0x7e5f
jmp 0x7ea3
jmp 0x7e3c
jmp 0x7e31
jmp 0x7e5f
jmp 0x7e25
jmp 0x7e25
movq %rax, %r14
movq %rsp, %rdi
jmp 0x7e4f
jmp 0x7e31
jmp 0x7e5f
movq %rax, %r14
jmp 0x7e47
jmp 0x7e5f
jmp 0x7e3c
jmp 0x7e5f
movq %rax, %r14
movq %rsp, %rdi
callq 0x827e
leaq 0xa0(%rsp), %rdi
callq 0x827e
jmp 0x7e62
jmp 0x7e5f
jmp 0x7e5f
movq %rax, %r14
jmp 0x7e97
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x827e
leaq 0x90(%rsp), %rdi
callq 0x827e
leaq 0x68(%rsp), %rdi
callq 0x827e
leaq 0x38(%rsp), %rdi
callq 0x827e
movq 0x20(%rsp), %rdi
callq 0x827e
leaq 0x78(%rsp), %rdi
callq 0x827e
jmp 0x7ea6
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x2f1c
movq %r14, %rdi
callq 0x21f0