// File: /workspace/github_cpp_makefile_star2_O2/qmmlib[P]QmmLibrary/tron.cpp
// Function: TRON::trcg(double, double*, double*, double*)
int TRON::trcg(double delta, double *g, 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 rTr, rnewTrnew, alpha, beta, cgtol;

	for (i=0; i<n; i++)
	{
		s[i] = 0;
		r[i] = -g[i];
		d[i] = r[i];
	}
	cgtol = 0.1*dnrm2_(&n, g, &inc);

	int cg_iter = 0;
	rTr = ddot_(&n, r, &inc, r, &inc);
	while (1)
	{
		if (dnrm2_(&n, r, &inc) <= cgtol)
			break;
		cg_iter++;
		fun_obj->Hv(d, Hd);

		alpha = rTr/ddot_(&n, d, &inc, Hd, &inc);
		daxpy_(&n, &alpha, d, &inc, s, &inc);
		if (dnrm2_(&n, s, &inc) > delta)
		{
			info("cg reaches trust region boundary\n");
			alpha = -alpha;
			daxpy_(&n, &alpha, d, &inc, s, &inc);

			double std = ddot_(&n, s, &inc, d, &inc);
			double sts = ddot_(&n, s, &inc, s, &inc);
			double dtd = ddot_(&n, d, &inc, d, &inc);
			double dsq = delta*delta;
			double rad = sqrt(std*std + dtd*(dsq-sts));
			if (std >= 0)
				alpha = (dsq - sts)/(std + rad);
			else
				alpha = (rad - std)/dtd;
			daxpy_(&n, &alpha, d, &inc, s, &inc);
			alpha = -alpha;
			daxpy_(&n, &alpha, Hd, &inc, r, &inc);
			break;
		}
		alpha = -alpha;
		daxpy_(&n, &alpha, Hd, &inc, r, &inc);
		rnewTrnew = ddot_(&n, r, &inc, r, &inc);
		beta = rnewTrnew/rTr;
		dscal_(&n, &beta, d, &inc);
		daxpy_(&n, &one, r, &inc, d, &inc);
		rTr = rnewTrnew;
	}

	delete[] d;
	delete[] Hd;

	return(cg_iter);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %rbx
movq %rdx, 0x28(%rsp)
movq %rsi, %r12
movapd %xmm0, 0x50(%rsp)
movl $0x1, 0xc(%rsp)
movq %rdi, 0x40(%rsp)
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, %r13d
movl %eax, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x70(%rsp)
movl %r13d, %eax
shlq $0x3, %rax
xorl %ebp, %ebp
testl %r13d, %r13d
pushq $-0x1
popq %r14
cmovnsq %rax, %r14
cmovlel %ebp, %r13d
movq %r14, %rdi
callq 0x3030
movq %rax, %r15
movq %r14, %rdi
callq 0x3030
movq 0x28(%rsp), %rcx
movq %rax, 0x38(%rsp)
cmpq %rbp, %r13
je 0x5049
andq $0x0, (%rcx,%rbp,8)
movsd (%r12,%rbp,8), %xmm0
xorpd 0x110f7(%rip), %xmm0    # 0x16130
movlpd %xmm0, (%rbx,%rbp,8)
movlpd %xmm0, (%r15,%rbp,8)
incq %rbp
jmp 0x5021
movq %r15, 0x48(%rsp)
leaq 0x8(%rsp), %r13
leaq 0xc(%rsp), %rbp
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x143d4
mulsd 0x110aa(%rip), %xmm0    # 0x16118
movsd %xmm0, 0x60(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movq %rbp, %r8
callq 0x145b0
movsd %xmm0, 0x10(%rsp)
movl $0x0, 0x4(%rsp)
movq 0x38(%rsp), %r15
movq %rbx, 0x30(%rsp)
movq 0x48(%rsp), %r12
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x143d4
movsd 0x60(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x51d2
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq %r12, %rsi
movq %r15, %rdx
callq *0x10(%rax)
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq %rbp, %r8
callq 0x145b0
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movq %r13, %rdi
leaq 0x20(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
movq %rbp, %rcx
movq 0x28(%rsp), %rbx
movq %rbx, %r8
movq %rbp, %r9
callq 0x14498
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x143d4
ucomisd 0x50(%rsp), %xmm0
ja 0x51e0
movsd 0x20(%rsp), %xmm0
xorpd 0x10feb(%rip), %xmm0    # 0x16130
movlpd %xmm0, 0x20(%rsp)
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rbp, %rcx
movq 0x30(%rsp), %rbx
movq %rbx, %r8
movq %rbp, %r9
callq 0x14498
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movq %rbp, %r8
callq 0x145b0
movsd %xmm0, 0x18(%rsp)
movapd %xmm0, %xmm1
divsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x68(%rsp)
movq %r13, %rdi
leaq 0x68(%rsp), %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x146a4
movq %r13, %rdi
leaq 0x70(%rsp), %rsi
movq %rbx, %rdx
movq %rbp, %rcx
movq %r12, %r8
movq %rbp, %r9
callq 0x14498
incl 0x4(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x50a5
movl 0x4(%rsp), %ebx
movq 0x48(%rsp), %r14
jmp 0x5365
leaq 0x1108b(%rip), %rsi     # 0x16272
movq 0x40(%rsp), %rdi
xorl %eax, %eax
callq 0x4718
leaq 0x20(%rsp), %rsi
movsd (%rsi), %xmm0
xorpd 0x10f2c(%rip), %xmm0    # 0x16130
movlpd %xmm0, (%rsi)
leaq 0x8(%rsp), %r13
leaq 0xc(%rsp), %r14
movq %r13, %rdi
movq %r12, %rdx
movq %r14, %rcx
movq 0x28(%rsp), %rbx
movq %rbx, %r8
movq %r14, %r9
callq 0x14498
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r12, %rcx
movq %r14, %r8
callq 0x145b0
movsd %xmm0, 0x10(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %r14, %r8
callq 0x145b0
movsd %xmm0, 0x18(%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r12, %rcx
movq %r14, %r8
callq 0x145b0
movapd %xmm0, %xmm2
movapd 0x50(%rsp), %xmm0
mulsd %xmm0, %xmm0
subsd 0x18(%rsp), %xmm0
movapd %xmm2, %xmm1
movapd %xmm0, 0x50(%rsp)
mulsd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm0
movq %r12, %r14
jb 0x52b6
sqrtsd %xmm0, %xmm0
jmp 0x52cb
movsd %xmm2, 0x18(%rsp)
callq 0x31a0
movsd 0x18(%rsp), %xmm2
xorpd %xmm4, %xmm4
movq 0x30(%rsp), %rbx
movsd 0x10(%rsp), %xmm3
movapd %xmm3, %xmm1
addsd %xmm0, %xmm1
subsd %xmm3, %xmm0
divsd %xmm2, %xmm0
movapd 0x50(%rsp), %xmm2
divsd %xmm1, %xmm2
cmpnlesd %xmm3, %xmm4
andpd %xmm4, %xmm0
andnpd %xmm2, %xmm4
orpd %xmm0, %xmm4
leaq 0x20(%rsp), %r12
movlpd %xmm4, (%r12)
leaq 0x8(%rsp), %r13
leaq 0xc(%rsp), %rbp
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbp, %rcx
movq 0x28(%rsp), %r8
movq %rbp, %r9
callq 0x14498
movsd (%r12), %xmm0
xorpd 0x10df3(%rip), %xmm0    # 0x16130
movlpd %xmm0, (%r12)
movq %r13, %rdi
movq %r12, %rsi
movq 0x38(%rsp), %r15
movq %r15, %rdx
movq %rbp, %rcx
movq %rbx, %r8
movq %rbp, %r9
callq 0x14498
movl 0x4(%rsp), %ebx
incl %ebx
movq %r14, %rdi
callq 0x32f0
movq %r15, %rdi
callq 0x32f0
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq