// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r12
movapd %xmm0, 0x40(%rsp)
movl $0x1, 0x4(%rsp)
movq %rdi, 0x28(%rsp)
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, 0x8(%rsp)
movl %eax, (%rsp)
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x60(%rsp)
movl %eax, %ebp
leaq (,%rbp,8), %rcx
testl %eax, %eax
movq $-0x1, %r13
cmovnsq %rcx, %r13
movq %r13, %rdi
callq 0x3030
movq %rax, %r15
movq %r13, %rdi
callq 0x3030
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x5607
xorl %eax, %eax
movapd 0x13b4e(%rip), %xmm0    # 0x19130
movq $0x0, (%r14,%rax,8)
movsd (%r12,%rax,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, (%rbx,%rax,8)
movlpd %xmm1, (%r15,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x55e2
movq %r14, 0x30(%rsp)
movq %rsp, %r13
leaq 0x4(%rsp), %rbp
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x17558
mulsd 0x13aee(%rip), %xmm0    # 0x19118
movsd %xmm0, 0x38(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movq %rbp, %r8
callq 0x17748
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x17558
xorl %r13d, %r13d
movsd 0x38(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x591a
movl $0x1, %eax
movq %rsp, %rbp
leaq 0x4(%rsp), %r12
movl %eax, %r14d
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq %r15, %rsi
movq 0x20(%rsp), %r13
movq %r13, %rdx
callq *0x10(%rax)
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x17748
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
movq 0x30(%rsp), %r13
movq %r13, %r8
movq %r12, %r9
callq 0x17624
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x17558
ucomisd 0x40(%rsp), %xmm0
ja 0x57ac
movsd 0x10(%rsp), %xmm0
xorpd 0x13a31(%rip), %xmm0    # 0x19130
movlpd %xmm0, 0x10(%rsp)
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r12, %rcx
movq %rbx, %r8
movq %r12, %r9
callq 0x17624
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq %r12, %r8
callq 0x17748
movsd %xmm0, 0x18(%rsp)
movapd %xmm0, %xmm1
divsd 0x8(%rsp), %xmm1
movsd %xmm1, 0x58(%rsp)
movq %rbp, %rdi
leaq 0x58(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x17850
movq %rbp, %rdi
leaq 0x60(%rsp), %rsi
movq %rbx, %rdx
movq %r12, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x17624
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x17558
leal 0x1(%r14), %eax
movsd 0x38(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %r13
jb 0x5678
jmp 0x591a
movq %r14, %r13
leaq 0x13abc(%rip), %rsi     # 0x19272
movq 0x28(%rsp), %rdi
xorl %eax, %eax
callq 0x4cdc
leaq 0x10(%rsp), %rsi
movsd (%rsi), %xmm0
xorpd 0x1395d(%rip), %xmm0    # 0x19130
movlpd %xmm0, (%rsi)
movq %rsp, %rbp
leaq 0x4(%rsp), %r12
movq %rbp, %rdi
movq %r15, %rdx
movq %r12, %rcx
movq 0x30(%rsp), %r14
movq %r14, %r8
movq %r12, %r9
callq 0x17624
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x17748
movsd %xmm0, 0x8(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %r12, %r8
callq 0x17748
movsd %xmm0, 0x18(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x17748
movapd %xmm0, %xmm2
movapd 0x40(%rsp), %xmm0
mulsd %xmm0, %xmm0
subsd 0x18(%rsp), %xmm0
movapd %xmm2, %xmm1
movapd %xmm0, 0x40(%rsp)
mulsd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm0
jb 0x5880
sqrtsd %xmm0, %xmm0
jmp 0x5895
movsd %xmm2, 0x18(%rsp)
callq 0x3190
movsd 0x18(%rsp), %xmm2
xorpd %xmm4, %xmm4
movsd 0x8(%rsp), %xmm3
movapd %xmm3, %xmm1
addsd %xmm0, %xmm1
subsd %xmm3, %xmm0
divsd %xmm2, %xmm0
movapd 0x40(%rsp), %xmm2
divsd %xmm1, %xmm2
cmpnlesd %xmm3, %xmm4
andpd %xmm4, %xmm0
andnpd %xmm2, %xmm4
orpd %xmm0, %xmm4
leaq 0x10(%rsp), %r12
movlpd %xmm4, (%r12)
movq %rsp, %rdi
leaq 0x4(%rsp), %rbp
movq %r12, %rsi
movq %r15, %rdx
movq %rbp, %rcx
movq %r14, %r8
movq %rbp, %r9
callq 0x17624
movsd (%r12), %xmm0
xorpd 0x13835(%rip), %xmm0    # 0x19130
movlpd %xmm0, (%r12)
movq %rsp, %rdi
movq %r12, %rsi
movq 0x20(%rsp), %rdx
movq %rbp, %rcx
movq %rbx, %r8
movq %rbp, %r9
callq 0x17624
movq %r15, %rdi
callq 0x32d0
movq 0x20(%rsp), %rdi
callq 0x32d0
movl %r13d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq