// File: /workspace/github_cpp_makefile_star2_O1/qmmlib[P]QmmLibrary/tron.cpp
// Function: TRON::tron(double*, parameter const*, model const*, long)
void TRON::tron(double *w, const parameter *param, const struct model* model_, clock_t cpu_begin)
{
	// Parameters for updating the iterates.
	double eta0 = 1e-4, eta1 = 0.25, eta2 = 0.75;

	// Parameters for updating the trust region size delta.
	double sigma1 = 0.25, sigma2 = 0.5, sigma3 = 4;

	int n = fun_obj->get_nr_variable();
	int i, cg_iter;
	double delta, snorm, one=1.0;
	double alpha, f, fnew, prered, actred, gs;
	int search = 1, iter = 1, inc = 1;
	double *s = new double[n];
	double *r = new double[n];
	double *w_new = new double[n];
	double *g = new double[n];

	clock_t cpu_now;
	double cpu_time_elapsed;

	for (i=0; i<n; i++)
		w[i] = 0;

        f = fun_obj->fun(w);
	fun_obj->grad(w, g);
	delta = dnrm2_(&n, g, &inc);
	double gnorm1 = delta;
	double gnorm = gnorm1;

	if (gnorm <= eps*gnorm1)
		search = 0;

	if(param->save_each_iter!=0) {char filenametosave[1024]; sprintf(filenametosave,"%s%d.model",param->save_each_iter,0); save_model(filenametosave,model_);}
	if (param->chat_level > 0)
	{
		cpu_now = clock();
		cpu_time_elapsed = ((double) (cpu_now - cpu_begin)) / ((double) CLOCKS_PER_SEC);																					
		iter = 0;
		double objective_value = fun_obj->fun(w);
		__QMMCHAT__
		cpu_begin += (clock() - cpu_now);
	}
	
	iter = 1;

	while (iter <= max_iter && search)
	{
		cg_iter = trcg(delta, g, s, r);

		memcpy(w_new, w, sizeof(double)*n);
		daxpy_(&n, &one, s, &inc, w_new, &inc);

		gs = ddot_(&n, g, &inc, s, &inc);
		prered = -0.5*(gs-ddot_(&n, s, &inc, r, &inc));
                fnew = fun_obj->fun(w_new);

		// Compute the actual reduction.
	        actred = f - fnew;

		// On the first iteration, adjust the initial step bound.
		snorm = dnrm2_(&n, s, &inc);
		if (iter == 1)
			delta = min(delta, snorm);

		// Compute prediction alpha*snorm of the step.
		if (fnew - f - gs <= 0)
			alpha = sigma3;
		else
			alpha = max(sigma1, -0.5*(gs/(fnew - f - gs)));

		// Update the trust region bound according to the ratio of actual to predicted reduction.
		if (actred < eta0*prered)
			delta = min(max(alpha, sigma1)*snorm, sigma2*delta);
		else if (actred < eta1*prered)
			delta = max(sigma1*delta, min(alpha*snorm, sigma2*delta));
		else if (actred < eta2*prered)
			delta = max(sigma1*delta, min(alpha*snorm, sigma3*delta));
		else
			delta = max(delta, min(alpha*snorm, sigma3*delta));

		if (param->chat_level == 0) {
			info("iter %2d act %5.3e pre %5.3e delta %5.3e f %5.3e |g| %5.3e CG %3d\n", iter, actred, prered, delta, f, gnorm, cg_iter);
		}
				
		if (actred > eta0*prered)
		{
			iter++;
			memcpy(w, w_new, sizeof(double)*n);
			f = fnew;
		        fun_obj->grad(w, g);

			gnorm = dnrm2_(&n, g, &inc);

			if(param->save_each_iter!=0) {char filenametosave[1024]; sprintf(filenametosave,"%s%d.model",param->save_each_iter,iter-1); save_model(filenametosave,model_);}
			if (param->chat_level > 0) {
				cpu_now = clock();
				cpu_time_elapsed = ((double) (cpu_now - cpu_begin)) / ((double) CLOCKS_PER_SEC);
				double objective_value = f;
				iter--;
				__QMMCHAT__
				iter++;
				cpu_begin += (clock() - cpu_now);
			}

			if (gnorm <= eps*gnorm1)
				break;
		}
		if (f < -1.0e+32)
		{
			info("WARNING: f < -1.0e+32\n");
			break;
		}
		if (fabs(actred) <= 0 && prered <= 0)
		{
			info("WARNING: actred and prered <= 0\n");
			break;
		}
		if (fabs(actred) <= 1.0e-12*fabs(f) &&
		    fabs(prered) <= 1.0e-12*fabs(f))
		{
			info("WARNING: actred and prered too small\n");
			break;
		}
	}

	delete[] g;
	delete[] r;
	delete[] w_new;
	delete[] s;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e8, %rsp            # imm = 0x4E8
movq %r8, 0x20(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, %r12
movq %rdi, 0x48(%rsp)
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, %ebp
movl %eax, 0xc(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xd8(%rsp)
movl $0x1, 0x1c(%rsp)
movslq %ebp, %r13
leaq (,%r13,8), %rax
testl %r13d, %r13d
movq $-0x1, %r15
cmovnsq %rax, %r15
movq %r15, %rdi
callq 0x3030
movq %rax, 0x98(%rsp)
movq %r15, %rdi
callq 0x3030
movq %rax, 0xa0(%rsp)
movq %r15, %rdi
callq 0x3030
movq %rax, 0x60(%rsp)
movq %r15, %rdi
callq 0x3030
movq %rax, 0x40(%rsp)
testl %r13d, %r13d
jle 0x4eda
movl %ebp, %edx
shlq $0x3, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x3110
movq 0x48(%rsp), %r13
movq 0x10(%r13), %rdi
movq (%rdi), %rax
movq %r12, %rsi
callq *(%rax)
movapd %xmm0, 0x80(%rsp)
movq 0x10(%r13), %rdi
movq (%rdi), %rax
movq %r12, %rsi
movq 0x40(%rsp), %r14
movq %r14, %rdx
callq *0x8(%rax)
leaq 0xc(%rsp), %rdi
leaq 0x1c(%rsp), %rdx
movq %r14, %rsi
callq 0x15830
movsd (%r13), %xmm1
movsd %xmm0, 0x50(%rsp)
mulsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq 0x30(%rsp), %rbp
movq 0x48(%rbp), %rdx
testq %rdx, %rdx
je 0x4f67
leaq 0x121ca(%rip), %rsi     # 0x17110
leaq 0xe0(%rsp), %r15
movq %r15, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3090
movq %r15, %rdi
movq 0x38(%rsp), %rsi
callq 0x12b67
cmpl $0x0, 0x40(%rbp)
movq %r12, 0xa8(%rsp)
jle 0x5063
callq 0x3120
movq %rax, %r15
movq 0x10(%r13), %rdi
movq (%rdi), %rax
movq %r12, %rsi
callq *(%rax)
cmpl $0x0, 0x40(%rbp)
jle 0x504e
movsd %xmm0, 0x28(%rsp)
movq %r15, %rax
subq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x120eb(%rip), %xmm0    # 0x170a0
leaq 0x1215f(%rip), %rsi     # 0x1711b
movq %r13, %rdi
xorl %edx, %edx
movb $0x1, %al
callq 0x4d50
cmpl $0x3, 0x40(%rbp)
jl 0x503d
leaq 0x1215e(%rip), %rsi     # 0x17133
movq %r13, %rdi
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x4d50
cmpl $0x4, 0x40(%rbp)
jl 0x503d
cmpq $0x0, 0x30(%rbp)
je 0x55c7
movq 0x38(%rbp), %rdi
movq 0x38(%rsp), %r13
movq %r13, %rsi
callq 0x14a3c
leaq 0x12174(%rip), %rsi     # 0x17182
movq 0x48(%rsp), %rbx
movq %rbx, %rdi
movb $0x1, %al
callq 0x4d50
movq 0x30(%rbp), %rdi
movq %r13, %rsi
movq %rbx, %r13
callq 0x14a3c
leaq 0x1215f(%rip), %rsi     # 0x17192
movq %rbx, %rdi
movb $0x1, %al
callq 0x4d50
leaq 0x11265(%rip), %rsi     # 0x162a9
movq %r13, %rdi
xorl %eax, %eax
callq 0x4d50
callq 0x3120
movq 0x20(%rsp), %rcx
subq %r15, %rcx
addq %rax, %rcx
movq %rcx, 0x20(%rsp)
movsd 0x10(%rsp), %xmm0
ucomisd 0x50(%rsp), %xmm0
movapd 0x80(%rsp), %xmm3
jae 0x5582
movl $0x1, %ebp
movsd 0x50(%rsp), %xmm0
movapd %xmm0, %xmm6
movsd %xmm0, 0x58(%rsp)
cmpl 0x8(%r13), %ebp
jg 0x5582
movapd %xmm3, 0x80(%rsp)
movq %r13, %rdi
movapd %xmm6, %xmm0
movl %ebp, 0x6c(%rsp)
movq 0x40(%rsp), %rbp
movq %rbp, %rsi
movq 0x98(%rsp), %r13
movq %r13, %rdx
movq 0xa0(%rsp), %r15
movq %r15, %rcx
movsd %xmm6, 0x10(%rsp)
callq 0x55cc
movl %eax, 0xb0(%rsp)
movslq 0xc(%rsp), %rdx
shlq $0x3, %rdx
movq 0x60(%rsp), %r14
movq %r14, %rdi
movq 0xa8(%rsp), %rsi
callq 0x31a0
leaq 0xc(%rsp), %rbx
movq %rbx, %rdi
leaq 0xd8(%rsp), %rsi
movq %r13, %rdx
leaq 0x1c(%rsp), %r12
movq %r12, %rcx
movq %r14, %r8
movq %r12, %r9
callq 0x15900
movq %rbx, %rdi
movq %rbp, %rsi
movl 0x6c(%rsp), %ebp
movq %r12, %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x15a24
movsd %xmm0, 0x70(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x15a24
movsd 0x70(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd 0x11f3f(%rip), %xmm1    # 0x170a8
movapd %xmm1, 0xc0(%rsp)
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *(%rax)
movsd %xmm0, 0x28(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x15830
cmpl $0x1, %ebp
je 0x51b9
movapd 0x80(%rsp), %xmm3
xorpd %xmm4, %xmm4
movsd 0x11f07(%rip), %xmm5    # 0x170b8
movsd 0x10(%rsp), %xmm6
jmp 0x51d8
movsd 0x10(%rsp), %xmm6
minsd %xmm0, %xmm6
movapd 0x80(%rsp), %xmm3
xorpd %xmm4, %xmm4
movsd 0x11ee0(%rip), %xmm5    # 0x170b8
movsd 0x28(%rsp), %xmm2
subsd %xmm3, %xmm2
movsd 0x70(%rsp), %xmm8
subsd %xmm8, %xmm2
ucomisd %xmm2, %xmm4
movsd 0x11eb6(%rip), %xmm7    # 0x170b0
movq 0xa8(%rsp), %rbx
movq 0x48(%rsp), %r13
movapd 0xc0(%rsp), %xmm1
jae 0x5229
divsd %xmm2, %xmm8
mulsd 0x11e88(%rip), %xmm8    # 0x170a8
movapd %xmm5, %xmm7
maxsd %xmm8, %xmm7
movapd %xmm3, %xmm8
subsd 0x28(%rsp), %xmm8
movapd %xmm1, %xmm9
mulsd 0x11e7d(%rip), %xmm9    # 0x170c0
ucomisd %xmm8, %xmm9
jbe 0x5264
maxsd %xmm5, %xmm7
mulsd %xmm7, %xmm0
mulsd 0x11e76(%rip), %xmm6    # 0x170d0
minsd %xmm6, %xmm0
movapd %xmm0, %xmm6
jmp 0x52ce
movapd %xmm1, %xmm2
mulsd %xmm5, %xmm2
ucomisd %xmm8, %xmm2
mulsd %xmm7, %xmm0
jbe 0x5289
movapd %xmm6, %xmm2
mulsd %xmm5, %xmm2
mulsd 0x11e49(%rip), %xmm6    # 0x170d0
jmp 0x52ac
movapd %xmm1, %xmm2
mulsd 0x11e33(%rip), %xmm2    # 0x170c8
ucomisd %xmm8, %xmm2
jbe 0x52ba
movapd %xmm6, %xmm2
mulsd %xmm5, %xmm2
mulsd 0x11e04(%rip), %xmm6    # 0x170b0
minsd %xmm6, %xmm0
maxsd %xmm0, %xmm2
movapd %xmm2, %xmm6
jmp 0x52ce
movapd %xmm6, %xmm2
mulsd 0x11dea(%rip), %xmm2    # 0x170b0
minsd %xmm2, %xmm0
maxsd %xmm0, %xmm6
movq 0x30(%rsp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x534e
movq %r13, %rdi
leaq 0x11ebe(%rip), %rsi     # 0x171a1
movl %ebp, %edx
movapd %xmm8, %xmm0
movapd %xmm6, %xmm2
movaps 0x80(%rsp), %xmm3
movsd 0x58(%rsp), %xmm4
movl 0xb0(%rsp), %ecx
movb $0x5, %al
movsd %xmm6, 0x10(%rsp)
movapd %xmm8, 0x70(%rsp)
movapd %xmm9, 0xb0(%rsp)
callq 0x4d50
movapd 0xb0(%rsp), %xmm9
movapd 0x70(%rsp), %xmm8
movapd 0xc0(%rsp), %xmm1
movsd 0x10(%rsp), %xmm6
xorpd %xmm4, %xmm4
movapd 0x80(%rsp), %xmm3
ucomisd %xmm9, %xmm8
jbe 0x5508
movapd %xmm8, 0x70(%rsp)
movsd %xmm6, 0x10(%rsp)
movslq 0xc(%rsp), %rdx
shlq $0x3, %rdx
movq %rbx, %rdi
movq 0x60(%rsp), %rsi
callq 0x31a0
movq 0x10(%r13), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq 0x40(%rsp), %r14
movq %r14, %rdx
callq *0x8(%rax)
leaq 0xc(%rsp), %rdi
movq %r14, %rsi
leaq 0x1c(%rsp), %rdx
callq 0x15830
movsd %xmm0, 0x58(%rsp)
movq 0x30(%rsp), %rax
movq 0x48(%rax), %rdx
testq %rdx, %rdx
je 0x53df
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x11d47(%rip), %rsi     # 0x17110
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3090
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x12b67
movq 0x30(%rsp), %r12
cmpl $0x0, 0x40(%r12)
jle 0x54ce
callq 0x3120
movq %rax, %r15
cmpl $0x0, 0x40(%r12)
jle 0x54b9
movq %r15, %rax
subq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x11c84(%rip), %xmm0    # 0x170a0
movq %r13, %rdi
leaq 0x11cf5(%rip), %rsi     # 0x1711b
movl %ebp, %edx
movb $0x1, %al
callq 0x4d50
movq 0x30(%rsp), %r12
cmpl $0x3, 0x40(%r12)
jl 0x54a8
movq %r13, %rdi
leaq 0x11ced(%rip), %rsi     # 0x17133
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x4d50
cmpl $0x4, 0x40(%r12)
jl 0x54a8
cmpq $0x0, 0x30(%r12)
je 0x55c2
movq 0x38(%r12), %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
callq 0x14a3c
movq %r13, %rdi
leaq 0x11cff(%rip), %rsi     # 0x17182
movb $0x1, %al
callq 0x4d50
movq 0x30(%r12), %rdi
movq %rbx, %rsi
callq 0x14a3c
movq %r13, %rdi
leaq 0x11cf1(%rip), %rsi     # 0x17192
movb $0x1, %al
callq 0x4d50
movq %r13, %rdi
leaq 0x10df7(%rip), %rsi     # 0x162a9
xorl %eax, %eax
callq 0x4d50
callq 0x3120
movq 0x20(%rsp), %rcx
subq %r15, %rcx
addq %rax, %rcx
movq %rcx, 0x20(%rsp)
movsd (%r13), %xmm0
mulsd 0x50(%rsp), %xmm0
ucomisd 0x58(%rsp), %xmm0
xorpd %xmm4, %xmm4
movsd 0x10(%rsp), %xmm6
movapd 0xc0(%rsp), %xmm1
movapd 0x70(%rsp), %xmm8
jae 0x5582
incl %ebp
movsd 0x28(%rsp), %xmm3
movsd 0x11bc8(%rip), %xmm0    # 0x170d8
ucomisd %xmm3, %xmm0
ja 0x5568
ucomisd %xmm4, %xmm8
jne 0x5525
jp 0x5525
ucomisd %xmm1, %xmm4
jae 0x5571
movapd 0x11bc3(%rip), %xmm2    # 0x170f0
andpd %xmm2, %xmm8
movapd %xmm3, %xmm0
andpd %xmm2, %xmm0
mulsd 0x11b9e(%rip), %xmm0    # 0x170e0
ucomisd %xmm8, %xmm0
jb 0x5093
andpd 0x11b9b(%rip), %xmm1    # 0x170f0
ucomisd %xmm1, %xmm0
jb 0x5093
leaq 0x11cb6(%rip), %rsi     # 0x1721c
jmp 0x5578
leaq 0x11c75(%rip), %rsi     # 0x171e4
jmp 0x5578
leaq 0x11c83(%rip), %rsi     # 0x171fb
movq %r13, %rdi
xorl %eax, %eax
callq 0x4d50
movq 0x40(%rsp), %rdi
callq 0x32c0
movq 0xa0(%rsp), %rdi
callq 0x32c0
movq 0x60(%rsp), %rdi
callq 0x32c0
movq 0x98(%rsp), %rdi
callq 0x32c0
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x360c
callq 0x35e7