// File: /workspace/github_cpp_makefile_star2_O2/cjlin1[P]liblinear/linear.cpp
// Function: l2r_erm_fun::linesearch_and_update(double*, double*, double*, double*, double)
double l2r_erm_fun::linesearch_and_update(double *w, double *s, double *f, double *g, double alpha)
{
	int i;
	int l = prob->l;
	double sTs = 0;
	double wTs = 0;
	double gTs = 0;
	double eta = 0.01;
	int w_size = get_nr_variable();
	int max_num_linesearch = 20;
	double fold = *f;
	Xv(s, tmp);

	for (i=0;i<w_size;i++)
	{
		sTs += s[i] * s[i];
		wTs += s[i] * w[i];
		gTs += s[i] * g[i];
	}
	if(regularize_bias == 0)
	{
		// bias not used in calculating (w + \alpha s)^T (w + \alpha s)
		sTs -= s[w_size-1] * s[w_size-1];
		wTs -= s[w_size-1] * w[w_size-1];
	}

	int num_linesearch = 0;
	for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++)
	{
		double loss = 0;
		for(i=0;i<l;i++)
		{
			double inner_product = tmp[i] * alpha + wx[i];
			loss += C_times_loss(i, inner_product);
		}
		*f = loss + (alpha * alpha * sTs + wTw) / 2.0 + alpha * wTs;
		if (*f - fold <= eta * alpha * gTs)
		{
			for (i=0;i<l;i++)
				wx[i] += alpha * tmp[i];
			break;
		}
		else
			alpha *= 0.5;
	}

	if (num_linesearch >= max_num_linesearch)
	{
		*f = fold;
		return 0;
	}
	else
		for (i=0;i<w_size;i++)
			w[i] += alpha * s[i];

	wTw += alpha * alpha * sTs + 2* alpha * wTs;
	return alpha;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm0, (%rsp)
movq %r8, %r13
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %rax
movq 0x10(%rdi), %rcx
movl (%rcx), %r12d
callq *0x18(%rax)
movl %eax, 0x8(%rsp)
movq %rbp, 0x18(%rsp)
movsd (%rbp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x20(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3a56
movl 0x8(%rsp), %ecx
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %ebp
cmovgl %ecx, %ebp
xorpd %xmm3, %xmm3
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
cmpq %rax, %rbp
je 0x3b59
movsd (%r14,%rax,8), %xmm0
movapd %xmm0, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movsd (%r15,%rax,8), %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd %xmm1, %xmm2
addpd %xmm2, %xmm3
mulsd (%r13,%rax,8), %xmm0
movapd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
incq %rax
jmp 0x3b18
cmpl $0x0, 0x30(%rbx)
movq %r14, 0x38(%rsp)
jne 0x3b85
movslq %ecx, %rax
movsd -0x8(%r14,%rax,8), %xmm0
movsd -0x8(%r15,%rax,8), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm1, %xmm0
subpd %xmm0, %xmm3
movapd %xmm3, %xmm0
movsd (%rsp), %xmm3
xorl %r14d, %r14d
testl %r12d, %r12d
cmovlel %r14d, %r12d
movapd %xmm0, 0x40(%rsp)
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm0, 0x50(%rsp)
cmpl $0x14, %r14d
je 0x3c69
xorpd %xmm2, %xmm2
xorl %r13d, %r13d
movsd %xmm3, (%rsp)
cmpq %r13, %r12
je 0x3c01
movq 0x20(%rbx), %rax
movq (%rbx), %rcx
movq 0x18(%rbx), %rdx
movsd (%rax,%r13,8), %xmm0
mulsd %xmm3, %xmm0
addsd (%rdx,%r13,8), %xmm0
movq %rbx, %rdi
movl %r13d, %esi
movsd %xmm2, 0x8(%rsp)
callq *0x40(%rcx)
movsd 0x8(%rsp), %xmm2
movsd (%rsp), %xmm3
addsd %xmm0, %xmm2
incq %r13
jmp 0x3bbe
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
mulsd 0x50(%rsp), %xmm0
movsd 0x28(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd 0x9010(%rip), %xmm4     # 0xcc30
mulsd %xmm4, %xmm1
addsd %xmm1, %xmm2
movapd %xmm3, %xmm1
mulsd 0x40(%rsp), %xmm1
addsd %xmm2, %xmm1
movq 0x18(%rsp), %rax
movsd %xmm1, (%rax)
subsd 0x10(%rsp), %xmm1
movapd %xmm3, %xmm2
mulsd 0x8fd7(%rip), %xmm2     # 0xcc28
mulsd 0x20(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jae 0x3c7e
mulsd %xmm4, %xmm3
incl %r14d
jmp 0x3ba8
movq 0x18(%rsp), %rax
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
xorpd %xmm3, %xmm3
jmp 0x3ce7
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
xorl %edx, %edx
movq 0x38(%rsp), %rsi
cmpq %rdx, %r12
je 0x3caa
movsd (%rcx,%rdx,8), %xmm1
mulsd %xmm3, %xmm1
addsd (%rax,%rdx,8), %xmm1
movsd %xmm1, (%rax,%rdx,8)
incq %rdx
jmp 0x3c8d
xorl %eax, %eax
cmpq %rax, %rbp
je 0x3ccb
movsd (%rsi,%rax,8), %xmm1
mulsd %xmm3, %xmm1
addsd (%r15,%rax,8), %xmm1
movsd %xmm1, (%r15,%rax,8)
incq %rax
jmp 0x3cac
movapd %xmm3, %xmm1
addsd %xmm3, %xmm1
mulsd 0x40(%rsp), %xmm1
addsd %xmm1, %xmm0
addsd 0x28(%rbx), %xmm0
movsd %xmm0, 0x28(%rbx)
movapd %xmm3, %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq