// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movsd %xmm0, 0x8(%rsp)
movq %r8, %r13
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %rax
movq 0x10(%rdi), %rcx
movl (%rcx), %ebp
callq *0x18(%rax)
movl %eax, 0x4(%rsp)
movq %r12, 0x18(%rsp)
movsd (%r12), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x20(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4014
movl 0x4(%rsp), %edx
testl %edx, %edx
jle 0x4114
movl %edx, %eax
xorpd %xmm4, %xmm4
xorpd %xmm5, %xmm5
xorl %ecx, %ecx
movsd (%r14,%rcx,8), %xmm0
movapd %xmm0, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movsd (%r15,%rcx,8), %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd %xmm1, %xmm2
mulsd (%r13,%rcx,8), %xmm0
addpd %xmm2, %xmm4
addsd %xmm0, %xmm5
incq %rcx
cmpq %rcx, %rax
jne 0x40df
jmp 0x411c
xorpd %xmm4, %xmm4
xorpd %xmm5, %xmm5
cmpl $0x0, 0x30(%rbx)
movsd 0x8(%rsp), %xmm3
jne 0x4149
movslq %edx, %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, %xmm4
movapd %xmm4, %xmm0
unpckhpd %xmm4, %xmm0            # xmm0 = xmm0[1],xmm4[1]
movapd %xmm0, 0x30(%rsp)
xorl %r12d, %r12d
movapd %xmm4, 0x40(%rsp)
movsd %xmm5, 0x20(%rsp)
xorpd %xmm2, %xmm2
testl %ebp, %ebp
jle 0x41b9
xorl %r13d, %r13d
movsd %xmm3, 0x8(%rsp)
movsd %xmm2, 0x28(%rsp)
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
callq *0x40(%rcx)
movsd 0x28(%rsp), %xmm2
movsd 0x8(%rsp), %xmm3
addsd %xmm0, %xmm2
incq %r13
cmpq %r13, %rbp
jne 0x4177
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
mulsd 0x30(%rsp), %xmm0
movsd 0x28(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd 0xaa58(%rip), %xmm6     # 0xec30
mulsd %xmm6, %xmm1
addsd %xmm2, %xmm1
movapd %xmm3, %xmm2
movapd 0x40(%rsp), %xmm4
mulsd %xmm4, %xmm2
addsd %xmm1, %xmm2
movq 0x18(%rsp), %rax
movsd %xmm2, (%rax)
subsd 0x10(%rsp), %xmm2
movapd %xmm3, %xmm1
mulsd 0xaa1b(%rip), %xmm1     # 0xec28
movsd 0x20(%rsp), %xmm5
mulsd %xmm5, %xmm1
ucomisd %xmm2, %xmm1
jae 0x4243
mulsd %xmm6, %xmm3
incl %r12d
cmpl $0x14, %r12d
jne 0x4166
movq 0x18(%rsp), %rax
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
xorpd %xmm3, %xmm3
jmp 0x42b0
testl %ebp, %ebp
jle 0x426c
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm1
mulsd %xmm3, %xmm1
addsd (%rax,%rdx,8), %xmm1
movsd %xmm1, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %rbp
jne 0x4251
movl 0x4(%rsp), %eax
testl %eax, %eax
jle 0x4296
movl %eax, %eax
xorl %ecx, %ecx
movsd (%r14,%rcx,8), %xmm1
mulsd %xmm3, %xmm1
addsd (%r15,%rcx,8), %xmm1
movsd %xmm1, (%r15,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x4278
movapd %xmm3, %xmm1
addsd %xmm3, %xmm1
mulsd %xmm4, %xmm1
addsd %xmm1, %xmm0
addsd 0x28(%rbx), %xmm0
movsd %xmm0, 0x28(%rbx)
movapd %xmm3, %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop