// File: /workspace/github_cpp_makefile_star2_O2/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 $0x4d8, %rsp            # imm = 0x4D8
movq %r8, 0x20(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, %r15d
movl %eax, 0xc(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xc8(%rsp)
movl $0x1, 0x1c(%rsp)
movl %r15d, %eax
shlq $0x3, %rax
xorl %r13d, %r13d
testl %r15d, %r15d
pushq $-0x1
popq %r12
cmovnsq %rax, %r12
cmovlel %r13d, %r15d
movq %r12, %rdi
callq 0x3030
movq %rax, 0x90(%rsp)
movq %r12, %rdi
callq 0x3030
movq %rax, 0x98(%rsp)
movq %r12, %rdi
callq 0x3030
movq %rax, 0x60(%rsp)
movq %r12, %rdi
callq 0x3030
movq %rax, 0x40(%rsp)
cmpq %r13, %r15
je 0x489b
andq $0x0, (%r14,%r13,8)
incq %r13
jmp 0x488c
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *(%rax)
movapd %xmm0, 0x80(%rsp)
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
movq %r14, 0x58(%rsp)
movq %r14, %rsi
movq 0x40(%rsp), %r14
movq %r14, %rdx
callq *0x8(%rax)
leaq 0xc(%rsp), %rdi
leaq 0x1c(%rsp), %rdx
movq %r14, %rsi
callq 0x143d4
movsd (%rbx), %xmm1
movsd %xmm0, 0x48(%rsp)
mulsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq 0x30(%rsp), %r13
movq 0x48(%r13), %rdx
testq %rdx, %rdx
je 0x4926
leaq 0x1183b(%rip), %rsi     # 0x16140
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3090
movq %r15, %rdi
movq 0x38(%rsp), %rsi
callq 0x1195c
cmpl $0x0, 0x40(%r13)
jle 0x4a15
callq 0x3140
movq %rax, %r15
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
movq 0x58(%rsp), %rsi
callq *(%rax)
cmpl $0x0, 0x40(%r13)
jle 0x4a00
movsd %xmm0, 0x28(%rsp)
movq %r15, %rax
subq 0x20(%rsp), %rax
cvtsi2sd %rax, %xmm0
divsd 0x11763(%rip), %xmm0    # 0x160d0
leaq 0x117d7(%rip), %rsi     # 0x1614b
movq %rbx, %rdi
xorl %edx, %edx
movb $0x1, %al
callq 0x4718
cmpl $0x3, 0x40(%r13)
jl 0x49ef
leaq 0x117d5(%rip), %rsi     # 0x16163
movq %rbx, %rdi
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x4718
cmpl $0x4, 0x40(%r13)
jl 0x49ef
cmpq $0x0, 0x30(%r13)
je 0x4f7b
movq 0x38(%r13), %rdi
movq 0x38(%rsp), %r12
movq %r12, %rsi
callq 0x13850
leaq 0x117ea(%rip), %rsi     # 0x161b2
movq %rbx, %rdi
movb $0x1, %al
callq 0x4718
movq 0x30(%r13), %rdi
movq %r12, %rsi
callq 0x13850
leaq 0x117dd(%rip), %rsi     # 0x161c2
movq %rbx, %rdi
movb $0x1, %al
callq 0x4718
leaq 0x108e1(%rip), %rsi     # 0x152d7
movq %rbx, %rdi
xorl %eax, %eax
callq 0x4718
callq 0x3140
movq 0x20(%rsp), %rcx
subq %r15, %rcx
addq %rax, %rcx
movq %rcx, 0x20(%rsp)
movsd 0x10(%rsp), %xmm0
ucomisd 0x48(%rsp), %xmm0
movapd 0x80(%rsp), %xmm3
jae 0x4f3b
pushq $0x1
popq %r15
movsd 0x48(%rsp), %xmm0
movapd %xmm0, %xmm6
movsd %xmm0, 0x50(%rsp)
movq %rbx, 0xc0(%rsp)
cmpl 0x8(%rbx), %r15d
jg 0x4f3b
movapd %xmm3, 0x80(%rsp)
movq %rbx, %rdi
movapd %xmm6, %xmm0
movq 0x40(%rsp), %r13
movq %r13, %rsi
movl %r15d, 0x6c(%rsp)
movq 0x90(%rsp), %r15
movq %r15, %rdx
movq 0x98(%rsp), %r12
movq %r12, %rcx
movsd %xmm6, 0x10(%rsp)
callq 0x4fa0
movl %eax, 0xa0(%rsp)
movslq 0xc(%rsp), %rdx
shlq $0x3, %rdx
movq 0x60(%rsp), %r14
movq %r14, %rdi
movq 0x58(%rsp), %rsi
callq 0x31c0
leaq 0xc(%rsp), %rbx
movq %rbx, %rdi
leaq 0xc8(%rsp), %rsi
movq %r15, %rdx
leaq 0x1c(%rsp), %rbp
movq %rbp, %rcx
movq %r14, %r8
movq %rbp, %r9
callq 0x14498
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq %rbp, %r8
callq 0x145b0
movsd %xmm0, 0x70(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %rbp, %r8
callq 0x145b0
movsd 0x70(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd 0x115bc(%rip), %xmm1    # 0x160d8
movapd %xmm1, 0xb0(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, %r12
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *(%rax)
movsd %xmm0, 0x28(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movl 0x6c(%rsp), %r15d
movq %rbp, %rdx
callq 0x143d4
cmpl $0x1, %r15d
je 0x4b78
movapd 0x80(%rsp), %xmm3
xorpd %xmm4, %xmm4
movsd 0x11578(%rip), %xmm5    # 0x160e8
movsd 0x10(%rsp), %xmm6
jmp 0x4b97
movsd 0x10(%rsp), %xmm6
minsd %xmm0, %xmm6
movapd 0x80(%rsp), %xmm3
xorpd %xmm4, %xmm4
movsd 0x11551(%rip), %xmm5    # 0x160e8
movsd 0x28(%rsp), %xmm2
subsd %xmm3, %xmm2
movsd 0x70(%rsp), %xmm8
subsd %xmm8, %xmm2
ucomisd %xmm2, %xmm4
movsd 0x11527(%rip), %xmm7    # 0x160e0
movq %r12, %rbx
movapd 0xb0(%rsp), %xmm1
jae 0x4bde
divsd %xmm2, %xmm8
mulsd 0x11503(%rip), %xmm8    # 0x160d8
movapd %xmm5, %xmm7
maxsd %xmm8, %xmm7
movapd %xmm3, %xmm8
subsd 0x28(%rsp), %xmm8
movapd %xmm1, %xmm9
mulsd 0x114f8(%rip), %xmm9    # 0x160f0
ucomisd %xmm8, %xmm9
jbe 0x4c19
maxsd %xmm5, %xmm7
mulsd %xmm7, %xmm0
mulsd 0x114f1(%rip), %xmm6    # 0x16100
minsd %xmm6, %xmm0
movapd %xmm0, %xmm6
jmp 0x4c83
movapd %xmm1, %xmm2
mulsd %xmm5, %xmm2
ucomisd %xmm8, %xmm2
mulsd %xmm7, %xmm0
jbe 0x4c3e
movapd %xmm6, %xmm2
mulsd %xmm5, %xmm2
mulsd 0x114c4(%rip), %xmm6    # 0x16100
jmp 0x4c61
movapd %xmm1, %xmm2
mulsd 0x114ae(%rip), %xmm2    # 0x160f8
ucomisd %xmm8, %xmm2
jbe 0x4c6f
movapd %xmm6, %xmm2
mulsd %xmm5, %xmm2
mulsd 0x1147f(%rip), %xmm6    # 0x160e0
minsd %xmm6, %xmm0
maxsd %xmm0, %xmm2
movapd %xmm2, %xmm6
jmp 0x4c83
movapd %xmm6, %xmm2
mulsd 0x11465(%rip), %xmm2    # 0x160e0
minsd %xmm2, %xmm0
maxsd %xmm0, %xmm6
movq 0x30(%rsp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x4d04
movq %rbx, %rdi
leaq 0x11539(%rip), %rsi     # 0x161d1
movl %r15d, %edx
movapd %xmm8, %xmm0
movapd %xmm6, %xmm2
movaps 0x80(%rsp), %xmm3
movsd 0x50(%rsp), %xmm4
movl 0xa0(%rsp), %ecx
movb $0x5, %al
movsd %xmm6, 0x10(%rsp)
movapd %xmm8, 0x70(%rsp)
movapd %xmm9, 0xa0(%rsp)
callq 0x4718
movapd 0xa0(%rsp), %xmm9
movapd 0x70(%rsp), %xmm8
movapd 0xb0(%rsp), %xmm1
movsd 0x10(%rsp), %xmm6
xorpd %xmm4, %xmm4
movapd 0x80(%rsp), %xmm3
ucomisd %xmm9, %xmm8
jbe 0x4ec1
movapd %xmm8, 0x70(%rsp)
movsd %xmm6, 0x10(%rsp)
movslq 0xc(%rsp), %rdx
shlq $0x3, %rdx
movq 0x58(%rsp), %r14
movq %r14, %rdi
movq 0x60(%rsp), %rsi
callq 0x31c0
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq 0x40(%rsp), %r14
movq %r14, %rdx
callq *0x8(%rax)
leaq 0xc(%rsp), %rdi
movq %r14, %rsi
leaq 0x1c(%rsp), %rdx
callq 0x143d4
movsd %xmm0, 0x50(%rsp)
movq 0x30(%rsp), %rax
movq 0x48(%rax), %rdx
testq %rdx, %rdx
je 0x4d9b
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
leaq 0x113bc(%rip), %rsi     # 0x16140
movl %r15d, %ecx
xorl %eax, %eax
callq 0x3090
movq %r14, %rdi
movq 0x38(%rsp), %rsi
callq 0x1195c
movq 0x30(%rsp), %r12
cmpl $0x0, 0x40(%r12)
jle 0x4e88
callq 0x3140
movq %rax, %r14
cmpl $0x0, 0x40(%r12)
jle 0x4e73
movq %r14, %rax
subq 0x20(%rsp), %rax
cvtsi2sd %rax, %xmm0
divsd 0x112fb(%rip), %xmm0    # 0x160d0
movq %rbx, %rdi
leaq 0x1136c(%rip), %rsi     # 0x1614b
movl %r15d, %edx
movb $0x1, %al
callq 0x4718
movq 0x30(%rsp), %r12
cmpl $0x3, 0x40(%r12)
jl 0x4e62
movq %rbx, %rdi
leaq 0x11363(%rip), %rsi     # 0x16163
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x4718
cmpl $0x4, 0x40(%r12)
jl 0x4e62
cmpq $0x0, 0x30(%r12)
je 0x4f7b
movq 0x38(%r12), %rdi
movq 0x38(%rsp), %r13
movq %r13, %rsi
callq 0x13850
movq %rbx, %rdi
leaq 0x11375(%rip), %rsi     # 0x161b2
movb $0x1, %al
callq 0x4718
movq 0x30(%r12), %rdi
movq %r13, %rsi
callq 0x13850
movq %rbx, %rdi
leaq 0x11367(%rip), %rsi     # 0x161c2
movb $0x1, %al
callq 0x4718
movq %rbx, %rdi
leaq 0x1046b(%rip), %rsi     # 0x152d7
xorl %eax, %eax
callq 0x4718
callq 0x3140
movq 0x20(%rsp), %rcx
subq %r14, %rcx
addq %rax, %rcx
movq %rcx, 0x20(%rsp)
movsd (%rbx), %xmm0
mulsd 0x48(%rsp), %xmm0
ucomisd 0x50(%rsp), %xmm0
xorpd %xmm4, %xmm4
movsd 0x10(%rsp), %xmm6
movapd 0xb0(%rsp), %xmm1
movapd 0x70(%rsp), %xmm8
jae 0x4f3b
incl %r15d
movsd 0x28(%rsp), %xmm3
movsd 0x1123f(%rip), %xmm0    # 0x16108
ucomisd %xmm3, %xmm0
ja 0x4f21
ucomisd %xmm4, %xmm8
jne 0x4ede
jp 0x4ede
ucomisd %xmm1, %xmm4
jae 0x4f2a
movapd 0x1123a(%rip), %xmm2    # 0x16120
andpd %xmm2, %xmm8
movapd %xmm3, %xmm0
andpd %xmm2, %xmm0
mulsd 0x11215(%rip), %xmm0    # 0x16110
ucomisd %xmm8, %xmm0
jb 0x4a4c
andpd 0x11212(%rip), %xmm1    # 0x16120
ucomisd %xmm1, %xmm0
jb 0x4a4c
leaq 0x1132d(%rip), %rsi     # 0x1624c
jmp 0x4f31
leaq 0x112ec(%rip), %rsi     # 0x16214
jmp 0x4f31
leaq 0x112fa(%rip), %rsi     # 0x1622b
movq %rbx, %rdi
xorl %eax, %eax
callq 0x4718
movq 0x40(%rsp), %rdi
callq 0x32f0
movq 0x98(%rsp), %rdi
callq 0x32f0
movq 0x60(%rsp), %rdi
callq 0x32f0
movq 0x90(%rsp), %rdi
callq 0x32f0
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x15056(%rip), %rax     # 0x19fd8
movq (%rax), %rcx
leaq 0x111ec(%rip), %rdi     # 0x16178
pushq $0x39
popq %rsi
pushq $0x1
popq %rdx
callq 0x34a0
pushq $0x1
popq %rdi
callq 0x32c0
nop