// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl $0x1, -0x30(%rbp)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, -0x34(%rbp)
movsd 0x1dac1(%rip), %xmm0    # 0x24028
movsd %xmm0, -0x40(%rbp)
movslq -0x34(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x48(%rbp)
movslq -0x34(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x50(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x6633
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x65c5
movq -0x18(%rbp), %rsi
leaq -0x34(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x22df0
movaps %xmm0, %xmm1
movsd 0x1d9e9(%rip), %xmm0    # 0x24038
mulsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movq -0x28(%rbp), %rsi
movq -0x28(%rbp), %rcx
leaq -0x34(%rbp), %rdi
leaq -0x30(%rbp), %r8
movq %r8, %rdx
callq 0x23210
movsd %xmm0, -0x58(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x34(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x22df0
movaps %xmm0, %xmm1
movsd -0x78(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x66a0
jmp 0x6998
movq -0xb0(%rbp), %rax
movl -0x7c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x7c(%rbp)
movq 0x10(%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rcx
leaq -0x34(%rbp), %rdi
leaq -0x30(%rbp), %r8
movq %r8, %rdx
callq 0x23210
movaps %xmm0, %xmm1
movsd -0xb8(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x48(%rbp), %rdx
movq -0x20(%rbp), %r8
leaq -0x34(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x30(%rbp), %r9
movq %r9, %rcx
callq 0x22f90
movq -0x20(%rbp), %rsi
leaq -0x34(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x22df0
ucomisd -0x10(%rbp), %xmm0
jbe 0x68ef
movq -0xb0(%rbp), %rdi
leaq 0x1eb21(%rip), %rsi     # 0x25262
movb $0x0, %al
callq 0x5820
movsd -0x68(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x48(%rbp), %rdx
movq -0x20(%rbp), %r8
leaq -0x34(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x30(%rbp), %r9
movq %r9, %rcx
callq 0x22f90
movq -0x20(%rbp), %rsi
movq -0x48(%rbp), %rcx
leaq -0x34(%rbp), %rdi
leaq -0x30(%rbp), %r8
movq %r8, %rdx
callq 0x23210
movsd %xmm0, -0x88(%rbp)
movq -0x20(%rbp), %rsi
movq -0x20(%rbp), %rcx
leaq -0x34(%rbp), %rdi
leaq -0x30(%rbp), %r8
movq %r8, %rdx
callq 0x23210
movsd %xmm0, -0x90(%rbp)
movq -0x48(%rbp), %rsi
movq -0x48(%rbp), %rcx
leaq -0x34(%rbp), %rdi
leaq -0x30(%rbp), %r8
movq %r8, %rdx
callq 0x23210
movsd %xmm0, -0x98(%rbp)
movsd -0x10(%rbp), %xmm0
mulsd -0x10(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x88(%rbp), %xmm0
movsd -0x88(%rbp), %xmm2
movsd -0x98(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm3
subsd -0x90(%rbp), %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x3200
movsd %xmm0, -0xa8(%rbp)
movsd -0x88(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x6874
movsd -0xa0(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd -0x88(%rbp), %xmm1
addsd -0xa8(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
jmp 0x6891
movsd -0xa8(%rbp), %xmm0
subsd -0x88(%rbp), %xmm0
divsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x48(%rbp), %rdx
movq -0x20(%rbp), %r8
leaq -0x34(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x30(%rbp), %r9
movq %r9, %rcx
callq 0x22f90
movsd -0x68(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x50(%rbp), %rdx
movq -0x28(%rbp), %r8
leaq -0x34(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x30(%rbp), %r9
movq %r9, %rcx
callq 0x22f90
jmp 0x6998
movsd -0x68(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x50(%rbp), %rdx
movq -0x28(%rbp), %r8
leaq -0x34(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x30(%rbp), %r9
movq %r9, %rcx
callq 0x22f90
movq -0x28(%rbp), %rsi
movq -0x28(%rbp), %rcx
leaq -0x34(%rbp), %rdi
leaq -0x30(%rbp), %r8
movq %r8, %rdx
callq 0x23210
movsd %xmm0, -0x60(%rbp)
movsd -0x60(%rbp), %xmm0
divsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x48(%rbp), %rdx
leaq -0x34(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x30(%rbp), %rcx
callq 0x23460
movq -0x28(%rbp), %rdx
movq -0x48(%rbp), %r8
leaq -0x34(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x30(%rbp), %r9
movq %r9, %rcx
callq 0x22f90
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
jmp 0x667c
movq -0x48(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
je 0x69b5
movq -0xc0(%rbp), %rdi
callq 0x33b0
movq -0x50(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0x69d2
movq -0xc8(%rbp), %rdi
callq 0x33b0
movl -0x7c(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nop