// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r13
movq %rdi, (%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, 0xc8(%rsp)
movl $0x1, 0x1c(%rsp)
movl %ebp, %r15d
shlq $0x3, %r15
testl %ebp, %ebp
movq $-0x1, %r12
cmovnsq %r15, %r12
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)
testl %ebp, %ebp
jle 0x4e5c
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3110
movq (%rsp), %rbx
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *(%rax)
movapd %xmm0, 0x80(%rsp)
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
movq %r13, 0x58(%rsp)
movq %r13, %rsi
movq 0x40(%rsp), %r14
movq %r14, %rdx
callq *0x8(%rax)
leaq 0xc(%rsp), %rdi
leaq 0x1c(%rsp), %rdx
movq %r14, %rsi
callq 0x17558
movsd (%rbx), %xmm1
movsd %xmm0, 0x48(%rsp)
mulsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq 0x20(%rsp), %rbp
movq 0x48(%rbp), %rdx
testq %rdx, %rdx
je 0x4eeb
leaq 0x14276(%rip), %rsi     # 0x19140
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3090
movq %r15, %rdi
movq 0x38(%rsp), %rsi
callq 0x14764
cmpl $0x0, 0x40(%rbp)
movq 0x30(%rsp), %r14
jle 0x4fe7
callq 0x3130
movq %rax, %r15
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq 0x58(%rsp), %rsi
callq *(%rax)
cmpl $0x0, 0x40(%rbp)
jle 0x4fd7
movsd %xmm0, 0x28(%rsp)
movq %r15, %rax
subq %r14, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x14196(%rip), %xmm0    # 0x190d0
leaq 0x1420a(%rip), %rsi     # 0x1914b
movq (%rsp), %rdi
xorl %edx, %edx
movb $0x1, %al
callq 0x4cdc
cmpl $0x3, 0x40(%rbp)
jl 0x4fc5
leaq 0x14208(%rip), %rsi     # 0x19163
movq (%rsp), %rdi
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x4cdc
cmpl $0x4, 0x40(%rbp)
jl 0x4fc5
cmpq $0x0, 0x30(%rbp)
je 0x5552
movq 0x38(%rbp), %rdi
movq 0x38(%rsp), %rbp
movq %rbp, %rsi
callq 0x16728
leaq 0x1421d(%rip), %rsi     # 0x191b2
movq (%rsp), %rbx
movq %rbx, %rdi
movb $0x1, %al
callq 0x4cdc
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rdi
movq %rbp, %rsi
callq 0x16728
leaq 0x14207(%rip), %rsi     # 0x191c2
movq %rbx, %rdi
movb $0x1, %al
callq 0x4cdc
leaq 0x1330b(%rip), %rsi     # 0x182d7
movq (%rsp), %rdi
xorl %eax, %eax
callq 0x4cdc
callq 0x3130
subq %r15, %r14
addq %rax, %r14
movq %r14, 0x30(%rsp)
movsd 0x10(%rsp), %xmm0
ucomisd 0x48(%rsp), %xmm0
movapd 0x80(%rsp), %xmm3
jae 0x550d
movl $0x1, %ebp
movsd 0x48(%rsp), %xmm0
movapd %xmm0, %xmm6
movsd %xmm0, 0x50(%rsp)
movq 0x90(%rsp), %r13
movq (%rsp), %rax
cmpl 0x8(%rax), %ebp
jg 0x550d
movapd %xmm3, 0x80(%rsp)
movq (%rsp), %rdi
movapd %xmm6, %xmm0
movl %ebp, 0x6c(%rsp)
movq 0x40(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rdx
movq 0x98(%rsp), %r15
movq %r15, %rcx
movsd %xmm6, 0x10(%rsp)
callq 0x5558
movl %eax, 0xa0(%rsp)
movslq 0xc(%rsp), %rdx
shlq $0x3, %rdx
movq 0x60(%rsp), %r14
movq %r14, %rdi
movq 0x58(%rsp), %rsi
callq 0x31b0
leaq 0xc(%rsp), %r12
movq %r12, %rdi
leaq 0xc8(%rsp), %rsi
movq %r13, %rdx
leaq 0x1c(%rsp), %rbx
movq %rbx, %rcx
movq %r14, %r8
movq %rbx, %r9
callq 0x17624
movq %r12, %rdi
movq %rbp, %rsi
movl 0x6c(%rsp), %ebp
movq %rbx, %rdx
movq %r13, %rcx
movq %rbx, %r8
callq 0x17748
movsd %xmm0, 0x70(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r15, %rcx
movq %rbx, %r8
callq 0x17748
movsd 0x70(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd 0x13fea(%rip), %xmm1    # 0x190d8
movapd %xmm1, 0xb0(%rsp)
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *(%rax)
movsd %xmm0, 0x28(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x17558
cmpl $0x1, %ebp
je 0x513d
movapd 0x80(%rsp), %xmm3
xorpd %xmm4, %xmm4
movsd 0x13fb3(%rip), %xmm5    # 0x190e8
movsd 0x10(%rsp), %xmm6
jmp 0x515c
movsd 0x10(%rsp), %xmm6
minsd %xmm0, %xmm6
movapd 0x80(%rsp), %xmm3
xorpd %xmm4, %xmm4
movsd 0x13f8c(%rip), %xmm5    # 0x190e8
movsd 0x28(%rsp), %xmm2
subsd %xmm3, %xmm2
movsd 0x70(%rsp), %xmm8
subsd %xmm8, %xmm2
ucomisd %xmm2, %xmm4
movsd 0x13f62(%rip), %xmm7    # 0x190e0
movapd 0xb0(%rsp), %xmm1
jae 0x51a0
divsd %xmm2, %xmm8
mulsd 0x13f41(%rip), %xmm8    # 0x190d8
movapd %xmm5, %xmm7
maxsd %xmm8, %xmm7
movapd %xmm3, %xmm8
subsd 0x28(%rsp), %xmm8
movapd %xmm1, %xmm9
mulsd 0x13f36(%rip), %xmm9    # 0x190f0
ucomisd %xmm8, %xmm9
jbe 0x51db
maxsd %xmm5, %xmm7
mulsd %xmm7, %xmm0
mulsd 0x13f2f(%rip), %xmm6    # 0x19100
minsd %xmm6, %xmm0
movapd %xmm0, %xmm6
jmp 0x5245
movapd %xmm1, %xmm2
mulsd %xmm5, %xmm2
ucomisd %xmm8, %xmm2
mulsd %xmm7, %xmm0
jbe 0x5200
movapd %xmm6, %xmm2
mulsd %xmm5, %xmm2
mulsd 0x13f02(%rip), %xmm6    # 0x19100
jmp 0x5223
movapd %xmm1, %xmm2
mulsd 0x13eec(%rip), %xmm2    # 0x190f8
ucomisd %xmm8, %xmm2
jbe 0x5231
movapd %xmm6, %xmm2
mulsd %xmm5, %xmm2
mulsd 0x13ebd(%rip), %xmm6    # 0x190e0
minsd %xmm6, %xmm0
maxsd %xmm0, %xmm2
movapd %xmm2, %xmm6
jmp 0x5245
movapd %xmm6, %xmm2
mulsd 0x13ea3(%rip), %xmm2    # 0x190e0
minsd %xmm2, %xmm0
maxsd %xmm0, %xmm6
movq 0x20(%rsp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x52c6
movq (%rsp), %rdi
leaq 0x13f76(%rip), %rsi     # 0x191d1
movl %ebp, %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 0x4cdc
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 0x5492
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 0x31b0
movq (%rsp), %rax
movq 0x10(%rax), %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 0x17558
movsd %xmm0, 0x50(%rsp)
movq 0x20(%rsp), %rax
movq 0x48(%rax), %rdx
testq %rdx, %rdx
je 0x5360
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x13df6(%rip), %rsi     # 0x19140
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3090
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x14764
movq 0x20(%rsp), %r12
cmpl $0x0, 0x40(%r12)
jle 0x5456
callq 0x3130
movq %rax, %r15
cmpl $0x0, 0x40(%r12)
jle 0x5441
movq %r15, %rax
subq 0x30(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x13d33(%rip), %xmm0    # 0x190d0
movq (%rsp), %rdi
leaq 0x13da3(%rip), %rsi     # 0x1914b
movl %ebp, %edx
movb $0x1, %al
callq 0x4cdc
movq 0x20(%rsp), %r12
cmpl $0x3, 0x40(%r12)
jl 0x542f
movq (%rsp), %rdi
leaq 0x13d9a(%rip), %rsi     # 0x19163
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x4cdc
cmpl $0x4, 0x40(%r12)
jl 0x542f
cmpq $0x0, 0x30(%r12)
je 0x554d
movq 0x38(%r12), %rdi
movq 0x38(%rsp), %r14
movq %r14, %rsi
callq 0x16728
movq (%rsp), %rbx
movq %rbx, %rdi
leaq 0x13da8(%rip), %rsi     # 0x191b2
movb $0x1, %al
callq 0x4cdc
movq 0x30(%r12), %rdi
movq %r14, %rsi
callq 0x16728
movq %rbx, %rdi
leaq 0x13d9a(%rip), %rsi     # 0x191c2
movb $0x1, %al
callq 0x4cdc
movq (%rsp), %rdi
leaq 0x12e9d(%rip), %rsi     # 0x182d7
xorl %eax, %eax
callq 0x4cdc
callq 0x3130
movq 0x30(%rsp), %rcx
subq %r15, %rcx
addq %rax, %rcx
movq %rcx, 0x30(%rsp)
movq (%rsp), %rax
movsd (%rax), %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 0x550d
incl %ebp
movsd 0x28(%rsp), %xmm3
movsd 0x13c6e(%rip), %xmm0    # 0x19108
ucomisd %xmm3, %xmm0
ja 0x54f2
ucomisd %xmm4, %xmm8
jne 0x54af
jp 0x54af
ucomisd %xmm1, %xmm4
jae 0x54fb
movapd 0x13c69(%rip), %xmm2    # 0x19120
andpd %xmm2, %xmm8
movapd %xmm3, %xmm0
andpd %xmm2, %xmm0
mulsd 0x13c44(%rip), %xmm0    # 0x19110
ucomisd %xmm8, %xmm0
jb 0x501f
andpd 0x13c41(%rip), %xmm1    # 0x19120
ucomisd %xmm1, %xmm0
jb 0x501f
leaq 0x13d5c(%rip), %rsi     # 0x1924c
jmp 0x5502
leaq 0x13d1b(%rip), %rsi     # 0x19214
jmp 0x5502
leaq 0x13d29(%rip), %rsi     # 0x1922b
movq (%rsp), %rdi
xorl %eax, %eax
callq 0x4cdc
movq 0x40(%rsp), %rdi
callq 0x32d0
movq 0x98(%rsp), %rdi
callq 0x32d0
movq 0x60(%rsp), %rdi
callq 0x32d0
movq 0x90(%rsp), %rdi
callq 0x32d0
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x361c
callq 0x35f7
nop