// File: /workspace/github_cpp_makefile_star2_O1/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, 0x40(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %rdx, %rbx
movslq (%rdi), %r13
movq %rdi, 0x8(%rsp)
movl 0x4(%rdi), %eax
movq %rax, 0x30(%rsp)
movsd 0x8(%rsi), %xmm0
movsd %xmm0, 0x38(%rsp)
movq %rsi, 0x98(%rsp)
movl (%rsi), %r15d
leaq (,%r13,8), %r14
testq %r13, %r13
movq $-0x1, %rax
movq %r14, 0x80(%rsp)
cmovsq %rax, %r14
leaq (,%r13,4), %r12
cmovsq %rax, %r12
movq %r14, %rdi
callq 0x2030
movq %rax, 0x10(%rsp)
movq %r12, %rdi
callq 0x2030
movq %rax, %r12
movq %r14, %rdi
callq 0x2030
movq %rax, %rbp
movq %r13, %rdi
callq 0x2030
movsd 0x40(%rsp), %xmm2
movq %rax, 0x18(%rsp)
movsd 0x28ed(%rip), %xmm0     # 0xdc08
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, 0x60(%rsp)
movsd 0x20(%rsp), %xmm1
movq $0x0, 0x68(%rsp)
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movapd 0x2b86(%rip), %xmm0     # 0xded0
movapd %xmm0, 0xa0(%rsp)
movabsq $0x7ff0000000000000, %rax # imm = 0x7FF0000000000000
movq %rax, 0xb0(%rsp)
cmpl $0x3, %r15d
jne 0xb389
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movq %rax, 0x70(%rsp)
movsd %xmm2, 0xa0(%rsp)
movsd %xmm1, 0xb0(%rsp)
testl %r13d, %r13d
movq 0x18(%rsp), %r15
jle 0xb3bf
movq 0x8(%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, (%r15,%rcx)
incq %rcx
cmpq %rcx, %r13
jne 0xb3a2
testl %r13d, %r13d
movq 0x10(%rsp), %r14
jle 0xb3db
movq %rbp, %rdi
xorl %esi, %esi
movq 0x80(%rsp), %rdx
callq 0x20c0
movq 0x30(%rsp), %rax
testl %eax, %eax
jle 0xb3f6
leaq (,%rax,8), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20c0
testl %r13d, %r13d
jle 0xb4a3
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
xorl %ecx, %ecx
movsbq (%r15,%rcx), %rdi
movsd 0x68(%rsp,%rdi,8), %xmm0
movsd %xmm0, (%r14,%rcx,8)
movq (%rax,%rcx,8), %rdx
movl (%rdx), %esi
cmpl $-0x1, %esi
je 0xb448
leaq 0x10(%rdx), %r8
xorpd %xmm1, %xmm1
movsd -0x8(%r8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm1
cmpl $-0x1, (%r8)
leaq 0x10(%r8), %r8
jne 0xb42e
jmp 0xb44c
xorpd %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%r14,%rcx,8)
cmpl $-0x1, %esi
je 0xb493
movsbl %dil, %edi
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
mulsd (%rbp,%rcx,8), %xmm0
addq $0x10, %rdx
movslq %esi, %rsi
movsd -0x8(%rdx), %xmm1
mulsd %xmm0, %xmm1
addsd -0x8(%rbx,%rsi,8), %xmm1
movsd %xmm1, -0x8(%rbx,%rsi,8)
movl (%rdx), %esi
addq $0x10, %rdx
cmpl $-0x1, %esi
jne 0xb470
movl %ecx, (%r12,%rcx,4)
incq %rcx
cmpq %r13, %rcx
jne 0xb40a
movsd 0x1b5d(%rip), %xmm1     # 0xd008
movsd 0x28f5(%rip), %xmm2     # 0xdda8
xorl %esi, %esi
movl %r13d, %r15d
movq %r13, 0x58(%rsp)
movapd %xmm2, 0x80(%rsp)
movapd %xmm1, 0x40(%rsp)
movl %esi, 0x20(%rsp)
testl %r15d, %r15d
jle 0xb502
movl %r15d, %r13d
xorl %r14d, %r14d
callq 0x2090
cltd
idivl %r13d
movslq %edx, %rax
addq %r14, %rax
movl (%r12,%r14,4), %ecx
movl (%r12,%rax,4), %edx
movl %edx, (%r12,%r14,4)
movl %ecx, (%r12,%rax,4)
incq %r14
decq %r13
jne 0xb4db
testl %r15d, %r15d
jle 0xb6b3
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
xorl %ecx, %ecx
movsd 0x1ae9(%rip), %xmm13    # 0xd008
movsd 0x2880(%rip), %xmm14    # 0xdda8
movq 0x58(%rsp), %r13
movq 0x10(%rsp), %r14
movsd 0x38(%rsp), %xmm6
movsd 0x1ad8(%rip), %xmm7     # 0xd018
xorpd %xmm8, %xmm8
movapd 0x26f2(%rip), %xmm9     # 0xdc40
movsd 0x26d1(%rip), %xmm10    # 0xdc28
movapd 0x40(%rsp), %xmm11
movapd 0x80(%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 0xb5ab
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 0xb588
jmp 0xb5af
xorpd %xmm1, %xmm1
movq 0x18(%rsp), %r10
movsbq (%r10,%rdi), %r10
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
mulsd %xmm0, %xmm1
addsd %xmm7, %xmm1
movsd 0xa8(%rsp,%r10,8), %xmm3
movsd (%rbp,%rdi,8), %xmm2
movsd 0x68(%rsp,%r10,8), %xmm4
mulsd %xmm2, %xmm4
addsd %xmm1, %xmm4
ucomisd %xmm8, %xmm2
jne 0xb604
jp 0xb604
ucomisd %xmm11, %xmm4
ja 0xb617
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm1
ja 0xb637
jmp 0xb63b
ucomisd %xmm3, %xmm2
movapd %xmm4, %xmm1
jne 0xb63b
jp 0xb63b
ucomisd %xmm4, %xmm12
jbe 0xb62d
movslq %r15d, %rdx
decl %r15d
movl -0x4(%r12,%rdx,4), %esi
movl %esi, (%r12,%r9,4)
movl %r8d, -0x4(%r12,%rdx,4)
jmp 0xb6a8
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm4
jbe 0xb63b
movapd %xmm4, %xmm1
movapd %xmm1, %xmm5
andpd %xmm9, %xmm5
ucomisd %xmm10, %xmm5
jbe 0xb69c
divsd (%r14,%rdi,8), %xmm4
movapd %xmm2, %xmm5
subsd %xmm4, %xmm5
maxsd %xmm8, %xmm5
minsd %xmm3, %xmm5
movsd %xmm5, (%rbp,%rdi,8)
cmpl $-0x1, %esi
je 0xb69c
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 0xb679
maxsd %xmm1, %xmm14
minsd %xmm1, %xmm13
incl %ecx
cmpl %r15d, %ecx
jl 0xb568
jmp 0xb6da
movsd 0x26ec(%rip), %xmm14    # 0xdda8
movsd 0x1943(%rip), %xmm13    # 0xd008
movq 0x58(%rsp), %r13
movq 0x10(%rsp), %r14
movsd 0x38(%rsp), %xmm6
xorpd %xmm8, %xmm8
movl 0x20(%rsp), %esi
incl %esi
imull $0xcccccccd, %esi, %eax # imm = 0xCCCCCCCD
rorl %eax
cmpl $0x19999999, %eax       # imm = 0x19999999
ja 0xb735
leaq 0x27f2(%rip), %rdi      # 0xdee8
xorl %eax, %eax
movq %r13, %r14
movl %esi, %r13d
movapd %xmm13, 0x20(%rsp)
movapd %xmm14, 0x40(%rsp)
callq 0x5ab4
movapd 0x40(%rsp), %xmm14
movapd 0x20(%rsp), %xmm13
xorpd %xmm8, %xmm8
movl %r13d, %esi
movq %r14, %r13
movq 0x10(%rsp), %r14
movsd 0x38(%rsp), %xmm6
movapd %xmm14, %xmm0
andpd 0x24fe(%rip), %xmm0     # 0xdc40
ucomisd %xmm0, %xmm6
jb 0xb799
movapd %xmm14, %xmm0
subsd %xmm13, %xmm0
ucomisd %xmm0, %xmm6
jb 0xb799
movapd %xmm13, %xmm0
andpd 0x24db(%rip), %xmm0     # 0xdc40
ucomisd %xmm0, %xmm6
jb 0xb799
cmpl %r13d, %r15d
je 0xb7eb
leaq 0x2773(%rip), %rdi      # 0xdeea
xorl %eax, %eax
movl %esi, %r15d
callq 0x5ab4
movl %r15d, %esi
movsd 0x187c(%rip), %xmm1     # 0xd008
movsd 0x2614(%rip), %xmm2     # 0xdda8
movl %r13d, %r15d
jmp 0xb7da
movapd %xmm14, %xmm1
cmpnlesd %xmm8, %xmm1
andpd %xmm1, %xmm14
movsd 0x1857(%rip), %xmm0     # 0xd008
andnpd %xmm0, %xmm1
orpd %xmm14, %xmm1
xorpd %xmm2, %xmm2
cmpnlesd %xmm13, %xmm2
andpd %xmm2, %xmm13
movsd 0x25d7(%rip), %xmm0     # 0xdda8
andnpd %xmm0, %xmm2
orpd %xmm13, %xmm2
cmpl $0x12c, %esi            # imm = 0x12C
jne 0xb4bd
movl $0x12c, %esi            # imm = 0x12C
leaq 0x26fa(%rip), %rdi      # 0xdeec
xorl %r15d, %r15d
movl %esi, 0x20(%rsp)
xorl %eax, %eax
callq 0x5ab4
movq 0x30(%rsp), %rcx
xorpd %xmm0, %xmm0
testl %ecx, %ecx
jle 0xb824
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %rcx
jne 0xb80f
movq %r13, %rcx
testl %r13d, %r13d
jle 0xb87c
xorl %eax, %eax
movsd 0x25ea(%rip), %xmm1     # 0xde20
xorpd %xmm2, %xmm2
xorl %r15d, %r15d
movq 0x18(%rsp), %rdx
movq %rcx, %r13
movsd (%rbp,%rax,8), %xmm3
movsbq (%rdx,%rax), %rcx
movsd 0x68(%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, %r13
jne 0xb845
jmp 0xb87f
movq %rcx, %r13
mulsd 0x2381(%rip), %xmm0     # 0xdc08
leaq 0x26af(%rip), %rdi      # 0xdf3d
movb $0x1, %al
callq 0x5ab4
leaq 0x26b8(%rip), %rdi      # 0xdf54
movl %r15d, %esi
xorl %eax, %eax
callq 0x5ab4
movq 0x98(%rsp), %rax
cmpb $0x1, 0x4c(%rax)
movq 0x18(%rsp), %r15
jne 0xb93e
movq 0x30(%rsp), %rdx
testl %edx, %edx
jle 0xb8d4
shlq $0x3, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20c0
testl %r13d, %r13d
jle 0xb93e
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd (%rbp,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0xb936
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx,%rax,8), %rcx
movl (%rcx), %edx
cmpl $-0x1, %edx
je 0xb936
movsbl (%r15,%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 0xb913
incq %rax
cmpq %r13, %rax
jne 0xb8df
movq %r14, %rdi
callq 0x21e0
movq %rbp, %rdi
callq 0x21e0
movq %r15, %rdi
callq 0x21e0
movq %r12, %rdi
callq 0x21e0
movl 0x20(%rsp), %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq