// File: /workspace/github_cpp_makefile_star2_O3/qmmlib[P]QmmLibrary/linear.cpp
// Function: solve_l2r_l1l2_svc(problem const*, double*, double, double, double, int)
static void solve_l2r_l1l2_svc(
	const problem *prob, double *w, double eps, 
	double Cp, double Cn, int solver_type)
{
	int l = prob->l;
	int w_size = prob->n;
	int i, s, iter = 0;
	double C, d, G;
	double *QD = new double[l];
	int max_iter = 1000;
	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 *xi = prob->x[i];
		while (xi->index != -1)
		{
			double val = xi->value;
			QD[i] += val*val;
			w[xi->index-1] += y[i]*alpha[i]*val;
			xi++;
		}
		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];
			G = 0;
			schar yi = y[i];

			feature_node *xi = prob->x[i];
			while(xi->index!= -1)
			{
				G += w[xi->index-1]*(xi->value);
				xi++;
			}
			G = G*yi-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;
				xi = prob->x[i];
				while (xi->index != -1)
				{
					w[xi->index-1] += d*xi->value;
					xi++;
				}
			}
		}

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

		if(PGmax_new - 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);
	if (iter >= max_iter)
		info("\nWARNING: reaching max number of iterations\nUsing -s 2 may be faster (also see FAQ)\n\n");

	// 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);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %edx, 0x8(%rsp)
movsd %xmm2, 0x30(%rsp)
movsd %xmm1, 0x40(%rsp)
movsd %xmm0, 0x88(%rsp)
movq %rsi, %rbp
movslq (%rdi), %r13
movq %rdi, 0x28(%rsp)
movl 0x4(%rdi), %eax
movq %rax, 0x58(%rsp)
leaq (,%r13,8), %r15
testq %r13, %r13
movq $-0x1, %rax
movq %r15, 0x78(%rsp)
cmovsq %rax, %r15
leaq (,%r13,4), %rbx
cmovsq %rax, %rbx
movq %r15, %rdi
callq 0x3030
movq %rax, %r14
movq %rbx, %rdi
callq 0x3030
movq %rax, %rbx
movq %r15, %rdi
callq 0x3030
movq %rax, %r12
movq %r13, 0x18(%rsp)
movq %r13, %rdi
callq 0x3030
movsd 0x30(%rsp), %xmm2
movq %rax, 0x10(%rsp)
movsd 0x3efc(%rip), %xmm0     # 0x19100
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, 0x60(%rsp)
movsd 0x40(%rsp), %xmm1
movq $0x0, 0x68(%rsp)
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movapd 0x429d(%rip), %xmm0     # 0x194d0
movapd %xmm0, 0x90(%rsp)
movabsq $0x7ff0000000000000, %rax # imm = 0x7FF0000000000000
movq %rax, 0xa0(%rsp)
cmpl $0x3, 0x8(%rsp)
jne 0x15273
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movq %rax, 0x70(%rsp)
movsd %xmm2, 0x90(%rsp)
movsd %xmm1, 0xa0(%rsp)
movq 0x18(%rsp), %r13
testl %r13d, %r13d
movq 0x10(%rsp), %r15
jle 0x152c2
movq 0x28(%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 0x15291
testl %r13d, %r13d
jle 0x152c2
movq %r12, %rdi
xorl %esi, %esi
movq 0x78(%rsp), %rdx
callq 0x3110
movq 0x58(%rsp), %rax
testl %eax, %eax
jle 0x152dd
leaq (,%rax,8), %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x3110
testl %r13d, %r13d
jle 0x1535e
movq 0x28(%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 0x15353
movsbl %dil, %edi
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
mulsd (%r12,%rcx,8), %xmm1
addq $0x10, %rdx
movsd -0x8(%rdx), %xmm2
movapd %xmm2, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
movsd %xmm0, (%r14,%rcx,8)
movslq %esi, %rsi
mulsd %xmm1, %xmm2
addsd -0x8(%rbp,%rsi,8), %xmm2
movsd %xmm2, -0x8(%rbp,%rsi,8)
movl (%rdx), %esi
addq $0x10, %rdx
cmpl $-0x1, %esi
jne 0x1531e
movl %ecx, (%rbx,%rcx,4)
incq %rcx
cmpq %r13, %rcx
jne 0x152ed
movsd 0x2cca(%rip), %xmm1     # 0x18030
movsd 0x3f42(%rip), %xmm2     # 0x192b0
xorpd %xmm7, %xmm7
movq $0x0, 0x8(%rsp)
movl %r13d, %r15d
movq %r14, 0x20(%rsp)
movq %r12, 0x80(%rsp)
movl %r15d, %r13d
testl %r15d, %r15d
jle 0x15579
movapd %xmm2, 0x30(%rsp)
movapd %xmm1, 0x40(%rsp)
movl %r15d, %r12d
xorl %r14d, %r14d
callq 0x30a0
cltd
idivl %r12d
movslq %edx, %rax
addq %r14, %rax
movl (%rbx,%r14,4), %ecx
movl (%rbx,%rax,4), %edx
movl %edx, (%rbx,%r14,4)
movl %ecx, (%rbx,%rax,4)
incq %r14
decl %r12d
cmpq %r14, %r13
jne 0x153a9
testl %r15d, %r15d
movq 0x80(%rsp), %r12
jle 0x15590
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
xorl %ecx, %ecx
movsd 0x2c3a(%rip), %xmm12    # 0x18030
movsd 0x3eb1(%rip), %xmm13    # 0x192b0
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %r13
movsd 0x3e8f(%rip), %xmm6     # 0x192a0
xorpd %xmm7, %xmm7
movapd 0x3d02(%rip), %xmm8     # 0x19120
movsd 0x3ce9(%rip), %xmm9     # 0x19110
movapd 0x40(%rsp), %xmm10
movapd 0x30(%rsp), %xmm11
movslq %ecx, %r9
movl (%rbx,%r9,4), %r8d
movslq %r8d, %rdi
movq (%rax,%rdi,8), %rdx
movl (%rdx), %esi
cmpl $-0x1, %esi
je 0x15478
leaq 0x10(%rdx), %r10
xorpd %xmm1, %xmm1
movl %esi, %r11d
movslq %r11d, %r11
movsd -0x8(%rbp,%r11,8), %xmm0
mulsd -0x8(%r10), %xmm0
addsd %xmm0, %xmm1
movl (%r10), %r11d
addq $0x10, %r10
cmpl $-0x1, %r11d
jne 0x15455
jmp 0x1547c
xorpd %xmm1, %xmm1
movsbq (%r13,%rdi), %r10
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
mulsd %xmm0, %xmm1
addsd %xmm6, %xmm1
movsd 0x98(%rsp,%r10,8), %xmm3
movsd (%r12,%rdi,8), %xmm2
movsd 0x68(%rsp,%r10,8), %xmm4
mulsd %xmm2, %xmm4
addsd %xmm1, %xmm4
ucomisd %xmm7, %xmm2
jne 0x154cc
jp 0x154cc
ucomisd %xmm10, %xmm4
ja 0x154df
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm1
ja 0x154fe
jmp 0x15502
ucomisd %xmm3, %xmm2
movapd %xmm4, %xmm1
jne 0x15502
jp 0x15502
ucomisd %xmm4, %xmm11
jbe 0x154f4
movslq %r15d, %rdx
decl %r15d
movl -0x4(%rbx,%rdx,4), %esi
movl %esi, (%rbx,%r9,4)
movl %r8d, -0x4(%rbx,%rdx,4)
jmp 0x1556e
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm4
jbe 0x15502
movapd %xmm4, %xmm1
movapd %xmm1, %xmm5
andpd %xmm8, %xmm5
ucomisd %xmm9, %xmm5
jbe 0x15562
divsd (%r14,%rdi,8), %xmm4
movapd %xmm2, %xmm5
subsd %xmm4, %xmm5
maxsd %xmm7, %xmm5
minsd %xmm3, %xmm5
movsd %xmm5, (%r12,%rdi,8)
cmpl $-0x1, %esi
je 0x15562
subsd %xmm2, %xmm5
mulsd %xmm0, %xmm5
addq $0x10, %rdx
movslq %esi, %rsi
movsd -0x8(%rdx), %xmm0
mulsd %xmm5, %xmm0
addsd -0x8(%rbp,%rsi,8), %xmm0
movsd %xmm0, -0x8(%rbp,%rsi,8)
movl (%rdx), %esi
addq $0x10, %rdx
cmpl $-0x1, %esi
jne 0x1553f
maxsd %xmm1, %xmm13
minsd %xmm1, %xmm12
incl %ecx
cmpl %r15d, %ecx
jl 0x15435
jmp 0x155ae
movl %r13d, %r15d
movsd 0x3d2b(%rip), %xmm13    # 0x192b0
movsd 0x2aa2(%rip), %xmm12    # 0x18030
jmp 0x155ae
movl %r13d, %r15d
movsd 0x3d14(%rip), %xmm13    # 0x192b0
movsd 0x2a8b(%rip), %xmm12    # 0x18030
movq 0x20(%rsp), %r14
xorpd %xmm7, %xmm7
movq 0x8(%rsp), %rax
leal 0x1(%rax), %r13d
imull $0xcccccccd, %r13d, %eax # imm = 0xCCCCCCCD
rorl %eax
cmpl $0x19999999, %eax       # imm = 0x19999999
ja 0x155f5
leaq 0x43f0(%rip), %rdi      # 0x199be
xorl %eax, %eax
movapd %xmm12, 0x40(%rsp)
movapd %xmm13, 0x30(%rsp)
callq 0x735c
movapd 0x30(%rsp), %xmm13
movapd 0x40(%rsp), %xmm12
xorpd %xmm7, %xmm7
movapd %xmm13, %xmm0
subsd %xmm12, %xmm0
movsd 0x88(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x15650
movapd %xmm13, %xmm1
cmpnlesd %xmm7, %xmm1
andpd %xmm1, %xmm13
movsd 0x2a0b(%rip), %xmm0     # 0x18030
andnpd %xmm0, %xmm1
orpd %xmm13, %xmm1
xorpd %xmm2, %xmm2
cmpnlesd %xmm12, %xmm2
andpd %xmm2, %xmm12
movsd 0x3c6b(%rip), %xmm0     # 0x192b0
andnpd %xmm0, %xmm2
orpd %xmm12, %xmm2
jmp 0x1568a
movq %r12, %r14
movq 0x18(%rsp), %r12
cmpl %r12d, %r15d
je 0x156b4
leaq 0x3e84(%rip), %rdi      # 0x194e8
xorl %eax, %eax
callq 0x735c
xorpd %xmm7, %xmm7
movl %r12d, %r15d
movsd 0x29b6(%rip), %xmm1     # 0x18030
movsd 0x3c2e(%rip), %xmm2     # 0x192b0
movq %r14, %r12
movq 0x20(%rsp), %r14
movl %r13d, %eax
movq %rax, 0x8(%rsp)
cmpl $0x3e8, %r13d           # imm = 0x3E8
jne 0x1538b
leaq 0x3e44(%rip), %rdi      # 0x194ea
movl $0x3e8, %esi            # imm = 0x3E8
xorl %eax, %eax
callq 0x735c
jmp 0x156d7
leaq 0x3e2f(%rip), %rdi      # 0x194ea
movl %r13d, %esi
xorl %eax, %eax
callq 0x735c
cmpl $0x3e7, 0x8(%rsp)       # imm = 0x3E7
movq %r14, %r12
movq 0x20(%rsp), %r14
jne 0x156e5
leaq 0x400d(%rip), %rdi      # 0x196eb
xorl %eax, %eax
callq 0x735c
movq 0x58(%rsp), %rcx
xorpd %xmm0, %xmm0
testl %ecx, %ecx
jle 0x15716
xorl %eax, %eax
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rdx
movsd (%rbp,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %rcx
jne 0x156fe
jmp 0x15720
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rdx
testl %edx, %edx
jle 0x1576a
xorl %eax, %eax
movsd 0x3b9a(%rip), %xmm1     # 0x192c8
xorpd %xmm2, %xmm2
xorl %ebp, %ebp
movsd (%r12,%rax,8), %xmm3
movsbq (%r15,%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, %ebp
incq %rax
cmpq %rax, %rdx
jne 0x15734
jmp 0x1576c
xorl %ebp, %ebp
mulsd 0x398c(%rip), %xmm0     # 0x19100
leaq 0x3dc0(%rip), %rdi      # 0x1953b
movb $0x1, %al
callq 0x735c
leaq 0x3dc9(%rip), %rdi      # 0x19552
movl %ebp, %esi
xorl %eax, %eax
callq 0x735c
movq %r14, %rdi
callq 0x32d0
movq %r12, %rdi
callq 0x32d0
movq %r15, %rdi
callq 0x32d0
movq %rbx, %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32d0