// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %r8, %r13
movq %rcx, %r14
movq %rdx, %rbx
movq %rsi, 0x58(%rsp)
movl $0x1, 0x24(%rsp)
movq %rdi, 0x28(%rsp)
movq 0x18(%rdi), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, %r15d
movl %eax, 0x4(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x70(%rsp)
movl %r15d, %eax
shlq $0x3, %rax
xorl %ebp, %ebp
testl %r15d, %r15d
pushq $-0x1
popq %r12
cmovnsq %rax, %r12
cmovlel %ebp, %r15d
movq %r12, %rdi
callq 0x2030
movq %rax, 0x18(%rsp)
movq %r12, %rdi
callq 0x2030
movq %rax, 0x48(%rsp)
movq %r12, %rdi
callq 0x2030
movq 0x58(%rsp), %rdx
movq %rax, %r12
movq 0x18(%rsp), %rax
cmpq %rbp, %r15
je 0x35fd
andq $0x0, (%r14,%rbp,8)
movsd (%rdx,%rbp,8), %xmm0
xorpd 0x96a3(%rip), %xmm0     # 0xcc80
movlpd %xmm0, (%r13,%rbp,8)
movsd (%rbx,%rbp,8), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, (%r12,%rbp,8)
movsd %xmm0, (%rax,%rbp,8)
incq %rbp
jmp 0x35c6
movq %r14, 0x60(%rsp)
leaq 0x4(%rsp), %rdi
leaq 0x24(%rsp), %rdx
movq %r12, %rsi
movq %r13, %rcx
movq %rdx, %r8
callq 0xb6f4
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movsd %xmm0, 0x10(%rsp)
jb 0x3630
sqrtsd %xmm0, %xmm0
jmp 0x3639
callq 0x20d0
xorpd %xmm2, %xmm2
movq 0x28(%rsp), %rax
movsd 0x8(%rax), %xmm1
movsd %xmm1, 0x38(%rsp)
ucomisd %xmm2, %xmm0
jb 0x3655
sqrtsd %xmm0, %xmm0
jmp 0x365a
callq 0x20d0
movsd 0x10(%rsp), %xmm2
movsd 0x38(%rsp), %xmm1
minsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
leaq 0x4(%rsp), %rax
movl (%rax), %eax
cmpl $0x6, %eax
pushq $0x5
popq %rcx
cmovgel %eax, %ecx
movl %ecx, 0x8(%rsp)
leaq 0x24(%rsp), %rbp
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpl 0x8(%rsp), %eax
je 0x387f
movsd %xmm0, 0x40(%rsp)
movsd %xmm2, 0x10(%rsp)
incl %eax
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
movq 0x18(%rsp), %r15
movq %r15, %rsi
movq 0x48(%rsp), %r14
movq %r14, %rdx
callq *0x10(%rax)
leaq 0x4(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r14, %rcx
movq %rbp, %r8
callq 0xb6f4
movsd 0x9570(%rip), %xmm1     # 0xcc58
ucomisd %xmm0, %xmm1
jae 0x3898
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
leaq 0x4(%rsp), %r14
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rbp, %rcx
movq 0x60(%rsp), %r15
movq %r15, %r8
movq %rbp, %r9
callq 0xb5dc
movsd 0x30(%rsp), %xmm0
xorpd 0x954b(%rip), %xmm0     # 0xcc80
movlpd %xmm0, 0x30(%rsp)
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %rbp, %rcx
movq %r13, %r8
movq %rbp, %r9
callq 0xb5dc
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0xb6f4
movsd %xmm0, 0x50(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq 0x58(%rsp), %rcx
movq %rbp, %r8
callq 0xb6f4
movsd 0x50(%rsp), %xmm2
subsd %xmm0, %xmm2
mulsd 0x94c8(%rip), %xmm2     # 0xcc60
xorpd %xmm1, %xmm1
ucomisd %xmm2, %xmm1
jb 0x3885
movapd %xmm2, %xmm0
subsd 0x40(%rsp), %xmm0
ucomisd %xmm0, %xmm1
jb 0x3885
cvtsi2sdl 0xc(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x38(%rsp), %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jae 0x3898
movl 0x4(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x3804
movsd (%r13,%rcx,8), %xmm0
divsd (%rbx,%rcx,8), %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
jmp 0x37e8
leaq 0x4(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r13, %rcx
movq %rbp, %r8
movsd %xmm2, 0x50(%rsp)
callq 0xb6f4
movsd %xmm0, 0x40(%rsp)
movapd %xmm0, %xmm1
divsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x68(%rsp)
movq %r14, %rdi
leaq 0x68(%rsp), %rsi
movq 0x18(%rsp), %r15
movq %r15, %rdx
movq %rbp, %rcx
callq 0xb7e8
movq %r14, %rdi
leaq 0x70(%rsp), %rsi
movq %r12, %rdx
movq %rbp, %rcx
movq %r15, %r8
movq %rbp, %r9
callq 0xb5dc
movsd 0x50(%rsp), %xmm0
movsd 0x40(%rsp), %xmm2
movl 0xc(%rsp), %eax
jmp 0x368f
movl 0x8(%rsp), %ebx
jmp 0x38a2
leaq 0x94d2(%rip), %rsi      # 0xcd5e
movq 0x28(%rsp), %rdi
xorl %eax, %eax
callq 0x313e
movl 0xc(%rsp), %ebx
cmpl 0x8(%rsp), %ebx
jne 0x38b5
leaq 0x94ef(%rip), %rsi      # 0xcd98
movq 0x28(%rsp), %rdi
xorl %eax, %eax
callq 0x313e
movq 0x18(%rsp), %rdi
callq 0x21e0
movq 0x48(%rsp), %rdi
callq 0x21e0
movq %r12, %rdi
callq 0x21e0
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq