// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc8, %rsp
movq %rcx, 0x60(%rsp)
movq %rdx, 0xb8(%rsp)
movq %rdi, 0x20(%rsp)
movl 0x8(%rsi), %ebx
cmpl 0xc(%rsi), %ebx
jne 0x7cc8
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq %rsi, 0xc0(%rsp)
callq 0x3b80
movq (%r12), %r14
xorl %eax, %eax
testl %ebx, %ebx
cmovgl %ebx, %eax
movq %rbx, 0x90(%rsp)
incl %ebx
movq %rax, 0x58(%rsp)
leal 0x1(%rax), %r15d
pushq $0x1
popq %r13
cmpq %r15, %r13
je 0x7872
movq %r12, %rdi
movl %r13d, %esi
movl %r13d, %edx
callq 0x2e20
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
pushq $0x1
popq %rbp
cmpq %rbp, %rbx
je 0x786d
movq %r12, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x2e20
movsd (%rax), %xmm0
divsd 0x28(%rsp), %xmm0
movq -0x8(%r14,%r13,8), %rax
movsd %xmm0, -0x8(%rax,%rbp,8)
incq %rbp
jmp 0x7841
incq %r13
jmp 0x7821
leaq 0x80(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x80b8
movq 0x58(%rsp), %rax
negl %eax
movq %rax, 0x58(%rsp)
pushq $0x1
popq %r14
leaq 0x80(%rsp), %r12
movq 0x58(%rsp), %rax
addl %r14d, %eax
cmpl $0x1, %eax
je 0x78ee
movq %r12, %rdi
movl %r14d, %esi
callq 0x86de
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0xc0(%rsp), %rdi
movl %r14d, %esi
movl %r14d, %edx
callq 0x2e20
movsd 0x28(%rsp), %xmm0
divsd (%rax), %xmm0
movq %r12, %rdi
movl %r14d, %esi
callq 0x89d4
incl %r14d
jmp 0x789f
leaq 0x80(%rsp), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x80b8
leaq 0x70(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %rbx, %rdx
callq 0x372f
leaq 0x38(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x8238
leaq 0x70(%rsp), %rdi
callq 0x8104
leaq 0x70(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x80b8
leaq 0x98(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x8068
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x372f
leaq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x859c
leaq 0x10(%rsp), %rdi
callq 0x8104
leaq 0x38(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x835f
movsd %xmm0, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x891a
movsd %xmm0, 0x28(%rsp)
leaq 0x38(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x891a
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 0x8418
leaq 0x10(%rsp), %rdi
movq %rsp, %rdx
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x8111
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x859c
leaq 0x10(%rsp), %rdi
callq 0x8104
movq %rsp, %rdi
callq 0x8104
movq %rsp, %rdi
leaq 0x48(%rsp), %rsi
movq %rbx, %rdx
callq 0x372f
leaq 0x10(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq %rsp, %rdx
callq 0x8238
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x859c
leaq 0x10(%rsp), %rdi
callq 0x8104
movq %rsp, %rdi
callq 0x8104
leaq 0x10(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x8068
leaq 0x38(%rsp), %r15
movq %rsp, %r12
leaq 0x70(%rsp), %rbp
leaq 0x10(%rsp), %rbx
leaq 0xa8(%rsp), %r14
movq 0x20(%rsp), %r13
movq %r15, %rdi
pushq $0x2
popq %rsi
callq 0x891a
ucomisd 0x15f4(%rip), %xmm0     # 0x9098
jbe 0x7c6f
movq 0x60(%rsp), %rax
incl (%rax)
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
movq %r15, %rdx
callq 0x372f
leaq 0x98(%rsp), %rdi
movq %r12, %rsi
callq 0x859c
movq %r12, %rdi
callq 0x8104
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
movq %rbp, %rdx
callq 0x372f
movq %rbx, %rdi
movq %r12, %rsi
callq 0x859c
movq %r12, %rdi
callq 0x8104
movq %r15, %rdi
movq %rbx, %rsi
callq 0x835f
movsd %xmm0, 0x68(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x835f
movsd 0x68(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x68(%rsp)
movq %r14, %rdi
leaq 0x68(%rsp), %rsi
movq %rbp, %rdx
callq 0x8418
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x8238
movq %rbp, %rdi
movq %r12, %rsi
callq 0x859c
movq %r12, %rdi
callq 0x8104
movq %r14, %rdi
callq 0x8104
movq %r15, %rdi
movq %rbp, %rsi
callq 0x835f
movsd %xmm0, 0x30(%rsp)
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
movq %rbp, %rdx
callq 0x372f
movq %rbx, %rdi
movq %r12, %rsi
callq 0x859c
movq %r12, %rdi
callq 0x8104
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x835f
movsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
movq %rbp, %rdx
callq 0x8418
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x8111
movq %r13, %rdi
movq %r12, %rsi
callq 0x859c
movq %r12, %rdi
callq 0x8104
movq %r14, %rdi
callq 0x8104
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
movq %r13, %rdx
callq 0x372f
movq %r12, %rdi
leaq 0x80(%rsp), %rsi
movq %r14, %rdx
callq 0x8238
movq %r15, %rdi
movq %r12, %rsi
callq 0x859c
movq %r12, %rdi
callq 0x8104
movq %r14, %rdi
callq 0x8104
movq 0x60(%rsp), %rax
cmpl $0x65, (%rax)
jl 0x7a91
andq $0x0, 0xa8(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x83d5
movq %r15, %rdi
movq %r12, %rsi
callq 0x859c
movq %r12, %rdi
callq 0x8104
movq 0x60(%rsp), %rax
movl $0xfffe7960, (%rax)     # imm = 0xFFFE7960
jmp 0x7a91
leaq 0x10(%rsp), %rdi
callq 0x8104
leaq 0x98(%rsp), %rdi
callq 0x8104
leaq 0x70(%rsp), %rdi
callq 0x8104
leaq 0x38(%rsp), %rdi
callq 0x8104
leaq 0x80(%rsp), %rdi
callq 0x8104
leaq 0x48(%rsp), %rdi
callq 0x2dea
movq 0x20(%rsp), %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1783(%rip), %rdi      # 0x9452
leaq 0x147b(%rip), %rsi      # 0x9151
leaq 0x178a(%rip), %rcx      # 0x9467
movl $0x37a, %edx            # imm = 0x37A
callq 0x20b0
jmp 0x7d3b
jmp 0x7cef
jmp 0x7cfe
jmp 0x7d3b
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x8104
jmp 0x7d01
movq %rax, %r14
movq %rsp, %rdi
jmp 0x7d92
jmp 0x7d3b
jmp 0x7d8a
jmp 0x7d3b
movq %rax, %r14
jmp 0x7da4
movq %rax, %r14
jmp 0x7dae
movq %rax, %r14
leaq 0x70(%rsp), %rdi
jmp 0x7db3
movq %rax, %r14
jmp 0x7db8
jmp 0x7d85
jmp 0x7dd1
movq %rax, %r14
jmp 0x7d97
jmp 0x7d4e
jmp 0x7d8a
jmp 0x7d65
jmp 0x7d5c
jmp 0x7d8a
jmp 0x7d4e
jmp 0x7d4e
movq %rax, %r14
movq %rsp, %rdi
jmp 0x7d78
jmp 0x7d65
jmp 0x7d5c
jmp 0x7d8a
movq %rax, %r14
jmp 0x7d70
jmp 0x7d8a
jmp 0x7d8a
movq %rax, %r14
movq %rsp, %rdi
callq 0x8104
leaq 0xa8(%rsp), %rdi
callq 0x8104
jmp 0x7d8d
jmp 0x7d8a
jmp 0x7d8a
jmp 0x7dd1
movq %rax, %r14
jmp 0x7dc2
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x8104
leaq 0x98(%rsp), %rdi
callq 0x8104
leaq 0x70(%rsp), %rdi
callq 0x8104
leaq 0x38(%rsp), %rdi
callq 0x8104
movq 0x20(%rsp), %rdi
callq 0x8104
leaq 0x80(%rsp), %rdi
callq 0x8104
jmp 0x7dd4
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x2dea
movq %r14, %rdi
callq 0x21d0