// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %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)
movq %r12, 0x28(%rsp)
movsd (%r12), %xmm0
movsd %xmm0, 0x20(%rsp)
movl %eax, %r12d
movq 0x20(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x40be
testl %r12d, %r12d
jle 0x41bf
movl %r12d, %eax
xorpd %xmm4, %xmm4
xorl %ecx, %ecx
xorpd %xmm5, %xmm5
xorpd %xmm6, %xmm6
movsd (%r14,%rcx,8), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm1, %xmm4
movsd (%r15,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
mulsd (%r13,%rcx,8), %xmm0
addsd %xmm1, %xmm5
addsd %xmm0, %xmm6
incq %rcx
cmpq %rcx, %rax
jne 0x418a
jmp 0x41cb
xorpd %xmm6, %xmm6
xorpd %xmm5, %xmm5
xorpd %xmm4, %xmm4
cmpl $0x0, 0x30(%rbx)
movsd 0x8(%rsp), %xmm3
movl %r12d, 0x1c(%rsp)
jne 0x41fd
movslq %r12d, %rax
movsd -0x8(%r14,%rax,8), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
subsd %xmm1, %xmm4
mulsd -0x8(%r15,%rax,8), %xmm0
subsd %xmm0, %xmm5
xorl %r12d, %r12d
movsd %xmm4, 0x40(%rsp)
movsd %xmm5, 0x38(%rsp)
movsd %xmm6, 0x30(%rsp)
xorpd %xmm0, %xmm0
testl %ebp, %ebp
jle 0x4271
xorl %r13d, %r13d
movsd %xmm3, 0x8(%rsp)
movsd %xmm0, 0x10(%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 0x8(%rsp), %xmm3
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
incq %r13
cmpq %r13, %rbp
jne 0x4223
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
movsd 0x40(%rsp), %xmm4
mulsd %xmm4, %xmm1
addsd 0x28(%rbx), %xmm1
movsd 0x9978(%rip), %xmm2     # 0xdc08
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movapd %xmm3, %xmm0
movsd 0x38(%rsp), %xmm5
mulsd %xmm5, %xmm0
addsd %xmm1, %xmm0
movq 0x28(%rsp), %rax
movsd %xmm0, (%rax)
subsd 0x20(%rsp), %xmm0
movapd %xmm3, %xmm1
mulsd 0x993b(%rip), %xmm1     # 0xdc00
movsd 0x30(%rsp), %xmm6
mulsd %xmm6, %xmm1
ucomisd %xmm0, %xmm1
jae 0x42db
mulsd %xmm2, %xmm3
jmp 0x4304
testl %ebp, %ebp
jle 0x4304
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm2
mulsd %xmm3, %xmm2
addsd (%rax,%rdx,8), %xmm2
movsd %xmm2, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %rbp
jne 0x42e9
ucomisd %xmm0, %xmm1
jae 0x431d
incl %r12d
cmpl $0x14, %r12d
jne 0x4212
movl $0x14, %r12d
cmpl $0x13, %r12d
jbe 0x4338
movq 0x28(%rsp), %rax
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
xorpd %xmm3, %xmm3
jmp 0x4388
movl 0x1c(%rsp), %eax
testl %eax, %eax
jle 0x4362
movl %eax, %eax
xorl %ecx, %ecx
movsd (%r14,%rcx,8), %xmm0
mulsd %xmm3, %xmm0
addsd (%r15,%rcx,8), %xmm0
movsd %xmm0, (%r15,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x4344
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
movapd %xmm3, %xmm1
addsd %xmm3, %xmm1
mulsd %xmm1, %xmm5
mulsd %xmm4, %xmm0
addsd %xmm5, %xmm0
addsd 0x28(%rbx), %xmm0
movsd %xmm0, 0x28(%rbx)
movapd %xmm3, %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop