// File: /workspace/github_cpp_makefile_star2_O3/cjlin1[P]liblinear/linear.cpp
// Function: solve_l2r_l1l2_svc(problem const*, parameter const*, double*, double, double, int)
static int solve_l2r_l1l2_svc(const problem *prob, const parameter *param, double *w, double Cp, double Cn, int max_iter=300)
{
	int l = prob->l;
	int w_size = prob->n;
	double eps = param->eps;
	int solver_type = param->solver_type;
	int i, s, iter = 0;
	double C, d, G;
	double *QD = new double[l];
	int *index = new int[l];
	double *alpha = new double[l];
	schar *y = new schar[l];
	int active_size = l;

	// PG: projected gradient, for shrinking and stopping
	double PG;
	double PGmax_old = INF;
	double PGmin_old = -INF;
	double PGmax_new, PGmin_new;

	// default solver_type: L2R_L2LOSS_SVC_DUAL
	double diag[3] = {0.5/Cn, 0, 0.5/Cp};
	double upper_bound[3] = {INF, 0, INF};
	if(solver_type == L2R_L1LOSS_SVC_DUAL)
	{
		diag[0] = 0;
		diag[2] = 0;
		upper_bound[0] = Cn;
		upper_bound[2] = Cp;
	}

	for(i=0; i<l; i++)
	{
		if(prob->y[i] > 0)
		{
			y[i] = +1;
		}
		else
		{
			y[i] = -1;
		}
	}

	// Initial alpha can be set here. Note that
	// 0 <= alpha[i] <= upper_bound[GETI(i)]
	for(i=0; i<l; i++)
		alpha[i] = 0;

	for(i=0; i<w_size; i++)
		w[i] = 0;
	for(i=0; i<l; i++)
	{
		QD[i] = diag[GETI(i)];

		feature_node * const xi = prob->x[i];
		QD[i] += sparse_operator::nrm2_sq(xi);
		sparse_operator::axpy(y[i]*alpha[i], xi, w);

		index[i] = i;
	}

	while (iter < max_iter)
	{
		PGmax_new = -INF;
		PGmin_new = INF;

		for (i=0; i<active_size; i++)
		{
			int j = i+rand()%(active_size-i);
			swap(index[i], index[j]);
		}

		for (s=0; s<active_size; s++)
		{
			i = index[s];
			const schar yi = y[i];
			feature_node * const xi = prob->x[i];

			G = yi*sparse_operator::dot(w, xi)-1;

			C = upper_bound[GETI(i)];
			G += alpha[i]*diag[GETI(i)];

			PG = 0;
			if (alpha[i] == 0)
			{
				if (G > PGmax_old)
				{
					active_size--;
					swap(index[s], index[active_size]);
					s--;
					continue;
				}
				else if (G < 0)
					PG = G;
			}
			else if (alpha[i] == C)
			{
				if (G < PGmin_old)
				{
					active_size--;
					swap(index[s], index[active_size]);
					s--;
					continue;
				}
				else if (G > 0)
					PG = G;
			}
			else
				PG = G;

			PGmax_new = max(PGmax_new, PG);
			PGmin_new = min(PGmin_new, PG);

			if(fabs(PG) > 1.0e-12)
			{
				double alpha_old = alpha[i];
				alpha[i] = min(max(alpha[i] - G/QD[i], 0.0), C);
				d = (alpha[i] - alpha_old)*yi;
				sparse_operator::axpy(d, xi, w);
			}
		}

		iter++;
		if(iter % 10 == 0)
			info(".");

		if(PGmax_new - PGmin_new <= eps &&
			fabs(PGmax_new) <= eps && fabs(PGmin_new) <= eps)
		{
			if(active_size == l)
				break;
			else
			{
				active_size = l;
				info("*");
				PGmax_old = INF;
				PGmin_old = -INF;
				continue;
			}
		}
		PGmax_old = PGmax_new;
		PGmin_old = PGmin_new;
		if (PGmax_old <= 0)
			PGmax_old = INF;
		if (PGmin_old >= 0)
			PGmin_old = -INF;
	}

	info("\noptimization finished, #iter = %d\n",iter);

	// calculate objective value

	double v = 0;
	int nSV = 0;
	for(i=0; i<w_size; i++)
		v += w[i]*w[i];
	for(i=0; i<l; i++)
	{
		v += alpha[i]*(alpha[i]*diag[GETI(i)] - 2);
		if(alpha[i] > 0)
			++nSV;
	}
	info("Objective value = %lf\n",v/2);
	info("nSV = %d\n",nSV);

	// Reconstruct w from the primal-dual relationship w=sum(\alpha_i y_i x_i)
	// This may reduce the weight density. Some zero weights become non-zeros
	// due to the numerical update w <- w + (alpha[i] - alpha_old) y_i x_i.
	if (param->w_recalc)
	{
		for(i=0; i<w_size; i++)
			w[i] = 0;
		for(i=0; i<l; i++)
		{
			feature_node * const xi = prob->x[i];
			if(alpha[i] > 0)
				sparse_operator::axpy(y[i]*alpha[i], xi, w);
		}
	}

	delete [] QD;
	delete [] alpha;
	delete [] y;
	delete [] index;

	return iter;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movsd %xmm1, 0x20(%rsp)
movsd %xmm0, (%rsp)
movq %rdx, %rbx
movslq (%rdi), %rbp
movq %rdi, 0x10(%rsp)
movl 0x4(%rdi), %eax
movq %rax, 0x30(%rsp)
movsd 0x8(%rsi), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rsi, 0x40(%rsp)
movl (%rsi), %r13d
leaq (,%rbp,8), %r15
testq %rbp, %rbp
movq $-0x1, %rax
movq %r15, %r14
cmovsq %rax, %r14
leaq (,%rbp,4), %r12
cmovsq %rax, %r12
movq %r14, %rdi
callq 0x2030
movq %rax, 0x38(%rsp)
movq %r12, %rdi
callq 0x2030
movq %rax, %r12
movq %r14, %rdi
callq 0x2030
movq %rax, 0x18(%rsp)
movq %rbp, %rdi
callq 0x2030
movsd 0x20(%rsp), %xmm2
movq %rax, %rsi
movsd 0x2e24(%rip), %xmm0     # 0xec30
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, 0x50(%rsp)
movsd (%rsp), %xmm1
movq $0x0, 0x58(%rsp)
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movapd 0x3116(%rip), %xmm0     # 0xef50
movapd %xmm0, 0x80(%rsp)
movabsq $0x7ff0000000000000, %rax # imm = 0x7FF0000000000000
movq %rax, 0x90(%rsp)
cmpl $0x3, %r13d
jne 0xbe79
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x60(%rsp)
movsd %xmm2, 0x80(%rsp)
movsd %xmm1, 0x90(%rsp)
testl %ebp, %ebp
movq 0x18(%rsp), %r14
movq %rsi, (%rsp)
jle 0xbec2
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movsd (%rax,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
setbe %dl
negb %dl
orb $0x1, %dl
movb %dl, (%rsi,%rcx)
incq %rcx
cmpq %rcx, %rbp
jne 0xbe95
testl %ebp, %ebp
jle 0xbec2
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20c0
movq 0x30(%rsp), %rax
testl %eax, %eax
jle 0xbedd
leaq (,%rax,8), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20c0
testl %ebp, %ebp
movq 0x38(%rsp), %r9
movsd 0x8(%rsp), %xmm6
jle 0xbf96
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movq (%rsp), %rdx
movsbq (%rdx,%rcx), %rdi
movsd 0x58(%rsp,%rdi,8), %xmm1
movq (%rax,%rcx,8), %rdx
movl (%rdx), %esi
cmpl $-0x1, %esi
je 0xbf7c
addq $0x10, %rdx
xorpd %xmm2, %xmm2
movq %rdx, %r8
movsd -0x8(%r8), %xmm3
mulsd %xmm3, %xmm3
addsd %xmm3, %xmm2
cmpl $-0x1, (%r8)
leaq 0x10(%r8), %r8
jne 0xbf24
addsd %xmm2, %xmm1
movsd %xmm1, (%r9,%rcx,8)
movsbl %dil, %edi
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
mulsd (%r14,%rcx,8), %xmm1
movslq %esi, %rsi
movsd -0x8(%rdx), %xmm2
mulsd %xmm1, %xmm2
addsd -0x8(%rbx,%rsi,8), %xmm2
movsd %xmm2, -0x8(%rbx,%rsi,8)
movl (%rdx), %esi
addq $0x10, %rdx
cmpl $-0x1, %esi
jne 0xbf57
jmp 0xbf86
addsd %xmm0, %xmm1
movsd %xmm1, (%r9,%rcx,8)
movl %ecx, (%r12,%rcx,4)
incq %rcx
cmpq %rbp, %rcx
jne 0xbeff
movsd 0x2092(%rip), %xmm1     # 0xe030
movsd 0x2e32(%rip), %xmm2     # 0xedd8
xorpd %xmm8, %xmm8
xorl %r15d, %r15d
movl %ebp, %r14d
movq %rbp, 0x48(%rsp)
movl %r15d, 0x20(%rsp)
movl %r14d, %r13d
testl %r14d, %r14d
jle 0xc1bc
movapd %xmm2, 0xa0(%rsp)
movapd %xmm1, 0x70(%rsp)
movl %r14d, %r15d
xorl %ebp, %ebp
callq 0x2090
cltd
idivl %r15d
movslq %edx, %rax
addq %rbp, %rax
movl (%r12,%rbp,4), %ecx
movl (%r12,%rax,4), %edx
movl %edx, (%r12,%rbp,4)
movl %ecx, (%r12,%rax,4)
incq %rbp
decl %r15d
cmpq %rbp, %r13
jne 0xbfdb
testl %r14d, %r14d
movq 0x38(%rsp), %r15
jle 0xc1d8
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
xorl %ecx, %ecx
movsd 0x2009(%rip), %xmm13    # 0xe030
movsd 0x2da8(%rip), %xmm14    # 0xedd8
movq 0x48(%rsp), %rbp
movq 0x18(%rsp), %r13
movsd 0x8(%rsp), %xmm6
movsd 0x1ff8(%rip), %xmm7     # 0xe040
xorpd %xmm8, %xmm8
movapd 0x2c1a(%rip), %xmm9     # 0xec70
movsd 0x2bf1(%rip), %xmm10    # 0xec50
movapd 0x70(%rsp), %xmm11
movapd 0xa0(%rsp), %xmm12
movslq %ecx, %r9
movl (%r12,%r9,4), %r8d
movslq %r8d, %rdi
movq (%rax,%rdi,8), %rdx
movl (%rdx), %esi
cmpl $-0x1, %esi
je 0xc0b3
leaq 0x10(%rdx), %r10
xorpd %xmm1, %xmm1
movl %esi, %r11d
movslq %r11d, %r11
movsd -0x8(%rbx,%r11,8), %xmm0
mulsd -0x8(%r10), %xmm0
addsd %xmm0, %xmm1
movl (%r10), %r11d
addq $0x10, %r10
cmpl $-0x1, %r11d
jne 0xc090
jmp 0xc0b7
xorpd %xmm1, %xmm1
movq (%rsp), %r10
movsbq (%r10,%rdi), %r10
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
mulsd %xmm0, %xmm1
addsd %xmm7, %xmm1
movsd 0x88(%rsp,%r10,8), %xmm3
movsd (%r13,%rdi,8), %xmm2
movsd 0x58(%rsp,%r10,8), %xmm4
mulsd %xmm2, %xmm4
addsd %xmm1, %xmm4
ucomisd %xmm8, %xmm2
jne 0xc10c
jp 0xc10c
ucomisd %xmm11, %xmm4
ja 0xc11f
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm1
ja 0xc13f
jmp 0xc143
ucomisd %xmm3, %xmm2
movapd %xmm4, %xmm1
jne 0xc143
jp 0xc143
ucomisd %xmm4, %xmm12
jbe 0xc135
movslq %r14d, %rdx
decl %r14d
movl -0x4(%r12,%rdx,4), %esi
movl %esi, (%r12,%r9,4)
movl %r8d, -0x4(%r12,%rdx,4)
jmp 0xc1b1
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm4
jbe 0xc143
movapd %xmm4, %xmm1
movapd %xmm1, %xmm5
andpd %xmm9, %xmm5
ucomisd %xmm10, %xmm5
jbe 0xc1a5
divsd (%r15,%rdi,8), %xmm4
movapd %xmm2, %xmm5
subsd %xmm4, %xmm5
maxsd %xmm8, %xmm5
minsd %xmm3, %xmm5
movsd %xmm5, (%r13,%rdi,8)
cmpl $-0x1, %esi
je 0xc1a5
subsd %xmm2, %xmm5
mulsd %xmm0, %xmm5
addq $0x10, %rdx
movslq %esi, %rsi
movsd -0x8(%rdx), %xmm0
mulsd %xmm5, %xmm0
addsd -0x8(%rbx,%rsi,8), %xmm0
movsd %xmm0, -0x8(%rbx,%rsi,8)
movl (%rdx), %esi
addq $0x10, %rdx
cmpl $-0x1, %esi
jne 0xc182
maxsd %xmm1, %xmm14
minsd %xmm1, %xmm13
incl %ecx
cmpl %r14d, %ecx
jl 0xc070
jmp 0xc202
movsd 0x2c13(%rip), %xmm14    # 0xedd8
movsd 0x1e62(%rip), %xmm13    # 0xe030
movl %r13d, %r14d
movq 0x18(%rsp), %r13
jmp 0xc202
movsd 0x2bf7(%rip), %xmm14    # 0xedd8
movsd 0x1e46(%rip), %xmm13    # 0xe030
movl %r13d, %r14d
movq 0x48(%rsp), %rbp
movq 0x18(%rsp), %r13
movsd 0x8(%rsp), %xmm6
xorpd %xmm8, %xmm8
movl 0x20(%rsp), %r15d
incl %r15d
imull $0xcccccccd, %r15d, %eax # imm = 0xCCCCCCCD
rorl %eax
cmpl $0x19999999, %eax       # imm = 0x19999999
ja 0xc24f
leaq 0x2d77(%rip), %rdi      # 0xef98
xorl %eax, %eax
movapd %xmm13, 0x20(%rsp)
movapd %xmm14, 0x70(%rsp)
callq 0x5d62
movapd 0x70(%rsp), %xmm14
movapd 0x20(%rsp), %xmm13
xorpd %xmm8, %xmm8
movsd 0x8(%rsp), %xmm6
movapd %xmm14, %xmm0
andpd 0x2a14(%rip), %xmm0     # 0xec70
ucomisd %xmm0, %xmm6
jb 0xc2b8
movapd %xmm14, %xmm0
subsd %xmm13, %xmm0
ucomisd %xmm0, %xmm6
jb 0xc2b8
movapd %xmm13, %xmm0
andpd 0x29f1(%rip), %xmm0     # 0xec70
ucomisd %xmm0, %xmm6
jb 0xc2b8
cmpl %ebp, %r14d
je 0xc306
leaq 0x2d09(%rip), %rdi      # 0xef9a
xorl %eax, %eax
callq 0x5d62
xorpd %xmm8, %xmm8
movsd 0x8(%rsp), %xmm6
movsd 0x1d85(%rip), %xmm1     # 0xe030
movsd 0x2b25(%rip), %xmm2     # 0xedd8
movl %ebp, %r14d
jmp 0xc2f9
movapd %xmm14, %xmm1
cmpnlesd %xmm8, %xmm1
andpd %xmm1, %xmm14
movsd 0x1d60(%rip), %xmm0     # 0xe030
andnpd %xmm0, %xmm1
orpd %xmm14, %xmm1
xorpd %xmm2, %xmm2
cmpnlesd %xmm13, %xmm2
andpd %xmm2, %xmm13
movsd 0x2ae8(%rip), %xmm0     # 0xedd8
andnpd %xmm0, %xmm2
orpd %xmm13, %xmm2
cmpl $0x12c, %r15d           # imm = 0x12C
jne 0xbfb6
leaq 0x2c8f(%rip), %rdi      # 0xef9c
movl %r15d, %esi
xorl %r15d, %r15d
movl %esi, 0x20(%rsp)
xorl %eax, %eax
callq 0x5d62
movq 0x30(%rsp), %rcx
xorpd %xmm0, %xmm0
testl %ecx, %ecx
jle 0xc34d
xorl %eax, %eax
movq (%rsp), %rdx
movq 0x40(%rsp), %r14
movsd (%rbx,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %rcx
jne 0xc336
jmp 0xc356
movq (%rsp), %rdx
movq 0x40(%rsp), %r14
testl %ebp, %ebp
jle 0xc3a1
xorl %eax, %eax
movsd 0x2aec(%rip), %xmm1     # 0xee50
xorpd %xmm2, %xmm2
xorl %r15d, %r15d
movsd (%r13,%rax,8), %xmm3
movsbq (%rdx,%rax), %rcx
movsd 0x58(%rsp,%rcx,8), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm1, %xmm4
mulsd %xmm3, %xmm4
xorl %ecx, %ecx
ucomisd %xmm2, %xmm3
seta %cl
addsd %xmm4, %xmm0
addl %ecx, %r15d
incq %rax
cmpq %rax, %rbp
jne 0xc36b
mulsd 0x2887(%rip), %xmm0     # 0xec30
leaq 0x2c3d(%rip), %rdi      # 0xefed
movb $0x1, %al
callq 0x5d62
leaq 0x2c46(%rip), %rdi      # 0xf004
movl %r15d, %esi
xorl %eax, %eax
callq 0x5d62
movq (%rsp), %r8
cmpb $0x1, 0x4c(%r14)
movq 0x38(%rsp), %r14
movl 0x20(%rsp), %r15d
jne 0xc466
movq 0x30(%rsp), %rdx
testl %edx, %edx
jle 0xc3fc
shlq $0x3, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20c0
movq (%rsp), %r8
testl %ebp, %ebp
jle 0xc466
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd (%r13,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0xc45e
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx,%rax,8), %rcx
movl (%rcx), %edx
cmpl $-0x1, %edx
je 0xc45e
movsbl (%r8,%rax), %esi
xorps %xmm2, %xmm2
cvtsi2sd %esi, %xmm2
mulsd %xmm2, %xmm1
addq $0x10, %rcx
movslq %edx, %rdx
movsd -0x8(%rcx), %xmm2
mulsd %xmm1, %xmm2
addsd -0x8(%rbx,%rdx,8), %xmm2
movsd %xmm2, -0x8(%rbx,%rdx,8)
movl (%rcx), %edx
addq $0x10, %rcx
cmpl $-0x1, %edx
jne 0xc43b
incq %rax
cmpq %rbp, %rax
jne 0xc406
movq %r14, %rdi
movq %r8, %rbx
callq 0x21e0
movq %r13, %rdi
callq 0x21e0
movq %rbx, %rdi
callq 0x21e0
movq %r12, %rdi
callq 0x21e0
movl %r15d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq