// File: /workspace/github_cpp_makefile_star2_O1/cjlin1[P]liblinear/newton.cpp
// Function: NEWTON::pcg(double*, double*, double*, double*)
int NEWTON::pcg(double *g, double *M, double *s, double *r)
{
	int i, inc = 1;
	int n = fun_obj->get_nr_variable();
	double one = 1;
	double *d = new double[n];
	double *Hd = new double[n];
	double zTr, znewTrnew, alpha, beta, cgtol, dHd;
	double *z = new double[n];
	double Q = 0, newQ, Qdiff;

	for (i=0; i<n; i++)
	{
		s[i] = 0;
		r[i] = -g[i];
		z[i] = r[i] / M[i];
		d[i] = z[i];
	}

	zTr = ddot_(&n, z, &inc, r, &inc);
	double gMinv_norm = sqrt(zTr);
	cgtol = min(eps_cg, sqrt(gMinv_norm));
	int cg_iter = 0;
	int max_cg_iter = max(n, 5);

	while (cg_iter < max_cg_iter)
	{
		cg_iter++;

		fun_obj->Hv(d, Hd);
		dHd = ddot_(&n, d, &inc, Hd, &inc);
		// avoid 0/0 in getting alpha
		if (dHd <= 1.0e-16)
			break;
		
		alpha = zTr/dHd;
		daxpy_(&n, &alpha, d, &inc, s, &inc);
		alpha = -alpha;
		daxpy_(&n, &alpha, Hd, &inc, r, &inc);

		// Using quadratic approximation as CG stopping criterion
		newQ = -0.5*(ddot_(&n, s, &inc, r, &inc) - ddot_(&n, s, &inc, g, &inc));
		Qdiff = newQ - Q;
		if (newQ <= 0 && Qdiff <= 0)
		{
			if (cg_iter * Qdiff >= cgtol * newQ)
				break;
		}
		else
		{
			info("WARNING: quadratic approximation > 0 or increasing in CG\n");
			break;
		}
		Q = newQ;

		for (i=0; i<n; i++)
			z[i] = r[i] / M[i];
		znewTrnew = ddot_(&n, z, &inc, r, &inc);
		beta = znewTrnew/zTr;
		dscal_(&n, &beta, d, &inc);
		daxpy_(&n, &one, z, &inc, d, &inc);
		zTr = znewTrnew;
	}

	if (cg_iter == max_cg_iter)
		info("WARNING: reaching maximal number of CG steps\n");

	delete[] d;
	delete[] Hd;
	delete[] z;

	return cg_iter;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, %r13
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, 0x60(%rsp)
movl $0x1, 0x1c(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x18(%rdi), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, 0xc(%rsp)
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x80(%rsp)
movl %eax, 0x10(%rsp)
movslq %eax, %rbp
leaq (,%rbp,8), %rax
testl %ebp, %ebp
movq $-0x1, %r15
cmovnsq %rax, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, %r14
movq %r15, %rdi
callq 0x2030
movq %rax, 0x58(%rsp)
movq %r15, %rdi
movq %r14, %r15
callq 0x2030
movq %r12, %rdx
movq %rax, %r12
testl %ebp, %ebp
jle 0x3c4d
movl 0x10(%rsp), %eax
xorl %ecx, %ecx
movapd 0xa03d(%rip), %xmm0     # 0xdc50
movq 0x60(%rsp), %rsi
movq $0x0, (%rdx,%rcx,8)
movsd (%rsi,%rcx,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, (%r13,%rcx,8)
movsd (%rbx,%rcx,8), %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, (%r12,%rcx,8)
movsd %xmm1, (%r15,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x3c18
movq %rbx, 0x68(%rsp)
movq %rdx, 0x70(%rsp)
leaq 0xc(%rsp), %rdi
leaq 0x1c(%rsp), %rdx
movq %r12, %rsi
movq %r13, %rcx
movq %rdx, %r8
callq 0xc524
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movsd %xmm0, 0x50(%rsp)
jb 0x3c85
sqrtsd %xmm0, %xmm0
jmp 0x3c8e
callq 0x20e0
xorpd %xmm2, %xmm2
movq 0x20(%rsp), %rax
movsd 0x8(%rax), %xmm1
movsd %xmm1, 0x38(%rsp)
ucomisd %xmm2, %xmm0
jb 0x3caa
sqrtsd %xmm0, %xmm0
jmp 0x3caf
callq 0x20e0
movsd 0x38(%rsp), %xmm1
minsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
leaq 0xc(%rsp), %rbp
movl (%rbp), %eax
cmpl $0x6, %eax
movl $0x5, %ecx
cmovgel %eax, %ecx
movl %ecx, 0x8(%rsp)
xorl %r14d, %r14d
leaq 0x1c(%rsp), %rbx
xorpd %xmm0, %xmm0
movq %r15, 0x30(%rsp)
movsd %xmm0, 0x40(%rsp)
incl %r14d
movq %r14, 0x48(%rsp)
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
movq %r15, %rsi
movq %r15, %r14
movq 0x58(%rsp), %r15
movq %r15, %rdx
callq *0x10(%rax)
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r15, %rcx
movq %rbx, %r8
callq 0xc524
movsd 0x9f02(%rip), %xmm1     # 0xdc30
ucomisd %xmm0, %xmm1
jae 0x3ed7
movsd 0x50(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
movq %rbp, %rdi
leaq 0x28(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq 0x70(%rsp), %r14
movq %r14, %r8
movq %rbx, %r9
callq 0xc400
movsd 0x28(%rsp), %xmm0
xorpd 0x9edc(%rip), %xmm0     # 0xdc50
movlpd %xmm0, 0x28(%rsp)
movq %rbp, %rdi
leaq 0x28(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %r13, %r8
movq %rbx, %r9
callq 0xc400
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, %rcx
movq %rbx, %r8
callq 0xc524
movsd %xmm0, 0x10(%rsp)
movq %rbp, %r15
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq 0x60(%rsp), %rcx
movq %rbx, %r8
callq 0xc524
movsd 0x10(%rsp), %xmm2
subsd %xmm0, %xmm2
mulsd 0x9e60(%rip), %xmm2     # 0xdc38
xorpd %xmm1, %xmm1
ucomisd %xmm2, %xmm1
jb 0x3edc
movapd %xmm2, %xmm0
subsd 0x40(%rsp), %xmm0
ucomisd %xmm0, %xmm1
jb 0x3edc
movq 0x48(%rsp), %r14
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
mulsd %xmm1, %xmm0
movsd 0x38(%rsp), %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jae 0x3efb
movsd %xmm2, 0x10(%rsp)
movslq 0xc(%rsp), %rax
testq %rax, %rax
movq 0x68(%rsp), %rdx
jle 0x3e50
xorl %ecx, %ecx
movsd (%r13,%rcx,8), %xmm0
divsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x3e36
movq %r15, %rbp
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r13, %rcx
movq %rbx, %r8
callq 0xc524
movsd %xmm0, 0x40(%rsp)
movapd %xmm0, %xmm1
divsd 0x50(%rsp), %xmm1
movsd %xmm1, 0x78(%rsp)
movq %r15, %rdi
leaq 0x78(%rsp), %rsi
movq 0x30(%rsp), %r15
movq %r15, %rdx
movq %rbx, %rcx
callq 0xc62c
movq %rbp, %rdi
leaq 0x80(%rsp), %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq %r15, %r8
movq %rbx, %r9
callq 0xc400
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x10(%rsp), %xmm0
cmpl 0x8(%rsp), %r14d
jne 0x3ce7
movl 0x8(%rsp), %eax
movl %eax, %r14d
jmp 0x3f00
movq %r14, %r15
jmp 0x3ef4
leaq 0x9e4b(%rip), %rsi      # 0xdd2e
movq 0x20(%rsp), %rdi
xorl %eax, %eax
callq 0x374c
movq 0x30(%rsp), %r15
movq 0x48(%rsp), %r14
jmp 0x3f00
movq 0x30(%rsp), %r15
cmpl 0x8(%rsp), %r14d
movq 0x58(%rsp), %rbx
jne 0x3f1f
leaq 0x9e55(%rip), %rsi      # 0xdd68
movq 0x20(%rsp), %rdi
xorl %eax, %eax
callq 0x374c
movq %r15, %rdi
callq 0x21e0
movq %rbx, %rdi
callq 0x21e0
movq %r12, %rdi
callq 0x21e0
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq