// File: cjlin1[P]liblinear/newton.cpp
// Function: function::linesearch_and_update(double*, double*, double*, double*, double)
double function::linesearch_and_update(double *w, double *s, double *f, double *g, double alpha)
{
	double gTs = 0;
	double eta = 0.01;
	int n = get_nr_variable();
	int max_num_linesearch = 20;
	double *w_new = new double[n];
	double fold = *f;

	for (int i=0;i<n;i++)
		gTs += s[i] * g[i];

	int num_linesearch = 0;
	for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++)
	{
		for (int i=0;i<n;i++)
			w_new[i] = w[i] + alpha*s[i];
		*f = fun(w_new);
		if (*f - fold <= eta * alpha * gTs)
			break;
		else
			alpha *= 0.5;
	}

	if (num_linesearch >= max_num_linesearch)
	{
		*f = fold;
		return 0;
	}
	else
		memcpy(w, w_new, sizeof(double)*n);

	delete [] w_new;
	return alpha;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x78(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd 0xeb94(%rip), %xmm0     # 0x12030
movsd %xmm0, -0x48(%rbp)
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, -0x4c(%rbp)
movl $0x14, -0x50(%rbp)
movslq -0x4c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x352d
movq -0x20(%rbp), %rax
movslq -0x64(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movslq -0x64(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x40(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x34ee
movl $0x0, -0x68(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x35f0
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x3595
movq -0x18(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0x38(%rbp), %xmm0
movq -0x20(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x58(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x354e
movq -0x78(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq (%rdi), %rax
callq *(%rax)
movq -0x28(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
movsd (%rax), %xmm1
subsd -0x60(%rbp), %xmm1
movsd -0x48(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
mulsd -0x40(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x35ce
jmp 0x35f0
movsd 0xea42(%rip), %xmm0     # 0x12018
mulsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x35e2
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x353b
movl -0x68(%rbp), %eax
cmpl -0x50(%rbp), %eax
jl 0x360f
movsd -0x60(%rbp), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, (%rax)
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x3645
movq -0x18(%rbp), %rdi
movq -0x58(%rbp), %rsi
movslq -0x4c(%rbp), %rdx
shlq $0x3, %rdx
callq 0x20f0
movq -0x58(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x363b
movq -0x80(%rbp), %rdi
callq 0x21b0
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)