// File: /workspace/github_cpp_makefile_star2_O1/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, (%rsp)
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x60(%rsp)
movl %eax, 0x8(%rsp)
movslq %eax, %rbp
leaq (,%rbp,8), %rax
testl %ebp, %ebp
movq $-0x1, %r13
cmovnsq %rax, %r13
movq %r13, %rdi
callq 0x3030
movq %rax, %r15
movq %r13, %rdi
callq 0x3030
movq %rax, 0x20(%rsp)
testl %ebp, %ebp
jle 0x567d
movl 0x8(%rsp), %eax
xorl %ecx, %ecx
movapd 0x11aa8(%rip), %xmm0    # 0x17100
movq $0x0, (%r14,%rcx,8)
movsd (%r12,%rcx,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, (%rbx,%rcx,8)
movlpd %xmm1, (%r15,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5658
movq %r14, 0x30(%rsp)
movq %rsp, %r13
leaq 0x4(%rsp), %rbp
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x15830
mulsd 0x11a48(%rip), %xmm0    # 0x170e8
movsd %xmm0, 0x38(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movq %rbp, %r8
callq 0x15a24
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x15830
xorl %r13d, %r13d
movsd 0x38(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x5990
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 0x15a24
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 0x15900
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x15830
ucomisd 0x40(%rsp), %xmm0
ja 0x5822
movsd 0x10(%rsp), %xmm0
xorpd 0x1198b(%rip), %xmm0    # 0x17100
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 0x15900
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq %r12, %r8
callq 0x15a24
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 0x15b2c
movq %rbp, %rdi
leaq 0x60(%rsp), %rsi
movq %rbx, %rdx
movq %r12, %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x15900
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x15830
leal 0x1(%r14), %eax
movsd 0x38(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %r13
jb 0x56ee
jmp 0x5990
movq %r14, %r13
leaq 0x11a16(%rip), %rsi     # 0x17242
movq 0x28(%rsp), %rdi
xorl %eax, %eax
callq 0x4d50
leaq 0x10(%rsp), %rsi
movsd (%rsi), %xmm0
xorpd 0x118b7(%rip), %xmm0    # 0x17100
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 0x15900
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x15a24
movsd %xmm0, 0x8(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %r12, %r8
callq 0x15a24
movsd %xmm0, 0x18(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x15a24
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 0x58f6
sqrtsd %xmm0, %xmm0
jmp 0x590b
movsd %xmm2, 0x18(%rsp)
callq 0x3180
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 0x15900
movsd (%r12), %xmm0
xorpd 0x1178f(%rip), %xmm0    # 0x17100
movlpd %xmm0, (%r12)
movq %rsp, %rdi
movq %r12, %rsi
movq 0x20(%rsp), %rdx
movq %rbp, %rcx
movq %rbx, %r8
movq %rbp, %r9
callq 0x15900
movq %r15, %rdi
callq 0x32c0
movq 0x20(%rsp), %rdi
callq 0x32c0
movl %r13d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq