// File: cjlin1[P]liblinear/newton.cpp
// Function: NEWTON::newton(double*)
void NEWTON::newton(double *w)
{
	int n = fun_obj->get_nr_variable();
	int i, cg_iter;
	double step_size;
	double f, fold, actred;
	double init_step_size = 1;
	int search = 1, iter = 1, inc = 1;
	double *s = new double[n];
	double *r = new double[n];
	double *g = new double[n];

	const double alpha_pcg = 0.01;
	double *M = new double[n];

	// calculate gradient norm at w=0 for stopping condition.
	double *w0 = new double[n];
	for (i=0; i<n; i++)
		w0[i] = 0;
	fun_obj->fun(w0);
	fun_obj->grad(w0, g);
	double gnorm0 = dnrm2_(&n, g, &inc);
	delete [] w0;

	f = fun_obj->fun(w);
	fun_obj->grad(w, g);
	double gnorm = dnrm2_(&n, g, &inc);
	info("init f %5.3e |g| %5.3e\n", f, gnorm);

	if (gnorm <= eps*gnorm0)
		search = 0;

	while (iter <= max_iter && search)
	{
		fun_obj->get_diag_preconditioner(M);
		for(i=0; i<n; i++)
			M[i] = (1-alpha_pcg) + alpha_pcg*M[i];
		cg_iter = pcg(g, M, s, r);

		fold = f;
		step_size = fun_obj->linesearch_and_update(w, s, &f, g, init_step_size);

		if (step_size == 0)
		{
			info("WARNING: line search fails\n");
			break;
		}

		fun_obj->grad(w, g);
		gnorm = dnrm2_(&n, g, &inc);

		info("iter %2d f %5.3e |g| %5.3e CG %3d step_size %4.2e \n", iter, f, gnorm, cg_iter, step_size);
		
		if (gnorm <= eps*gnorm0)
			break;
		if (f < -1.0e+32)
		{
			info("WARNING: f < -1.0e+32\n");
			break;
		}
		actred = fold - f;
		if (fabs(actred) <= 1.0e-12*fabs(f))
		{
			info("WARNING: actred too small\n");
			break;
		}

		iter++;
	}

	if(iter >= max_iter)
		info("\nWARNING: reaching max number of Newton iterations\n");

	delete[] g;
	delete[] r;
	delete[] s;
	delete[] M;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl %eax, -0x14(%rbp)
movsd 0xe815(%rip), %xmm0     # 0x12028
movsd %xmm0, -0x48(%rbp)
movl $0x1, -0x4c(%rbp)
movl $0x1, -0x50(%rbp)
movl $0x1, -0x54(%rbp)
movslq -0x14(%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, -0x60(%rbp)
movslq -0x14(%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, -0x68(%rbp)
movslq -0x14(%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, -0x70(%rbp)
movsd 0xe780(%rip), %xmm0     # 0x12030
movsd %xmm0, -0x78(%rbp)
movslq -0x14(%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, -0x80(%rbp)
movslq -0x14(%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, -0x88(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x3937
movq -0x88(%rbp), %rax
movslq -0x18(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x3911
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x88(%rbp), %rsi
movq (%rdi), %rax
callq *(%rax)
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x88(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x70(%rbp), %rsi
leaq -0x14(%rbp), %rdi
leaq -0x54(%rbp), %rdx
callq 0x11790
movsd %xmm0, -0x90(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x39a3
movq -0xa8(%rbp), %rdi
callq 0x21b0
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rsi
movq (%rdi), %rax
callq *(%rax)
movq -0xa0(%rbp), %rax
movsd %xmm0, -0x30(%rbp)
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x70(%rbp), %rsi
leaq -0x14(%rbp), %rdi
leaq -0x54(%rbp), %rdx
callq 0x11790
movq -0xa0(%rbp), %rdi
movsd %xmm0, -0x98(%rbp)
movsd -0x30(%rbp), %xmm0
movsd -0x98(%rbp), %xmm1
leaq 0xf2a7(%rip), %rsi      # 0x12cb0
movb $0x2, %al
callq 0x3660
movq -0xa0(%rbp), %rax
movsd -0x98(%rbp), %xmm1
movsd (%rax), %xmm0
mulsd -0x90(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x3a38
movl $0x0, -0x4c(%rbp)
jmp 0x3a3a
movq -0xa0(%rbp), %rdx
movl -0x50(%rbp), %ecx
xorl %eax, %eax
cmpl 0x10(%rdx), %ecx
movb %al, -0xa9(%rbp)
jg 0x3a5e
cmpl $0x0, -0x4c(%rbp)
setne %al
movb %al, -0xa9(%rbp)
movb -0xa9(%rbp), %al
testb $0x1, %al
jne 0x3a6d
jmp 0x3c5f
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x80(%rbp), %rsi
movq (%rdi), %rax
callq *0x20(%rax)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x3ace
movq -0x80(%rbp), %rax
movslq -0x18(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0xe58a(%rip), %xmm1     # 0x12030
mulsd %xmm1, %xmm0
movsd 0xf1d6(%rip), %xmm1     # 0x12c88
addsd %xmm1, %xmm0
movq -0x80(%rbp), %rax
movslq -0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x3a89
movq -0xa0(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x80(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq -0x68(%rbp), %r8
callq 0x3d00
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, -0x1c(%rbp)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x70(%rbp), %r8
movsd -0x48(%rbp), %xmm0
movq (%rdi), %rax
leaq -0x30(%rbp), %rcx
callq *0x38(%rax)
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3b4e
jp 0x3b4e
movq -0xa0(%rbp), %rdi
leaq 0xf186(%rip), %rsi      # 0x12cc8
movb $0x0, %al
callq 0x3660
jmp 0x3c5f
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x70(%rbp), %rsi
leaq -0x14(%rbp), %rdi
leaq -0x54(%rbp), %rdx
callq 0x11790
movq -0xa0(%rbp), %rdi
movsd %xmm0, -0x98(%rbp)
movl -0x50(%rbp), %edx
movsd -0x30(%rbp), %xmm0
movsd -0x98(%rbp), %xmm1
movl -0x1c(%rbp), %ecx
movsd -0x28(%rbp), %xmm2
leaq 0xf13e(%rip), %rsi      # 0x12ce4
movb $0x3, %al
callq 0x3660
movq -0xa0(%rbp), %rax
movsd -0x98(%rbp), %xmm1
movsd (%rax), %xmm0
mulsd -0x90(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x3bd3
jmp 0x3c5f
movsd 0xf09d(%rip), %xmm0     # 0x12c78
ucomisd -0x30(%rbp), %xmm0
jbe 0x3bf9
movq -0xa0(%rbp), %rdi
leaq 0xf128(%rip), %rsi      # 0x12d18
movb $0x0, %al
callq 0x3660
jmp 0x3c5f
movsd -0x38(%rbp), %xmm0
subsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm1
movaps 0xf08c(%rip), %xmm0     # 0x12ca0
pand %xmm0, %xmm1
movsd -0x30(%rbp), %xmm2
movaps 0xf07c(%rip), %xmm0     # 0x12ca0
pand %xmm0, %xmm2
movsd 0xf050(%rip), %xmm0     # 0x12c80
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x3c51
movq -0xa0(%rbp), %rdi
leaq 0xf0e7(%rip), %rsi      # 0x12d2f
movb $0x0, %al
callq 0x3660
jmp 0x3c5f
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3a3a
movq -0xa0(%rbp), %rcx
movl -0x50(%rbp), %eax
cmpl 0x10(%rcx), %eax
jl 0x3c83
movq -0xa0(%rbp), %rdi
leaq 0xf0ce(%rip), %rsi      # 0x12d4a
movb $0x0, %al
callq 0x3660
movq -0x70(%rbp), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
je 0x3ca0
movq -0xb8(%rbp), %rdi
callq 0x21b0
movq -0x68(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
je 0x3cbd
movq -0xc0(%rbp), %rdi
callq 0x21b0
movq -0x60(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0x3cda
movq -0xc8(%rbp), %rdi
callq 0x21b0
movq -0x80(%rbp), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, %rax
je 0x3cf7
movq -0xd0(%rbp), %rdi
callq 0x21b0
addq $0xd0, %rsp
popq %rbp
retq