// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movl %edx, %r13d
movsd %xmm2, 0x30(%rsp)
movsd %xmm1, (%rsp)
movsd %xmm0, 0x78(%rsp)
movq %rsi, %rbp
movslq (%rdi), %r12
movq %rdi, 0x10(%rsp)
movl 0x4(%rdi), %eax
movq %rax, 0x48(%rsp)
leaq (,%r12,8), %r15
testq %r12, %r12
movq $-0x1, %rax
movq %r15, 0x20(%rsp)
cmovsq %rax, %r15
leaq (,%r12,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, %r15
movq %r12, 0x8(%rsp)
movq %r12, %rdi
callq 0x3030
movsd 0x30(%rsp), %xmm2
movq %rax, 0x18(%rsp)
movsd 0x3b1b(%rip), %xmm0     # 0x170d0
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, 0x60(%rsp)
movsd (%rsp), %xmm1
movq $0x0, 0x68(%rsp)
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movapd 0x3e7d(%rip), %xmm0     # 0x17460
movapd %xmm0, 0x80(%rsp)
movabsq $0x7ff0000000000000, %rax # imm = 0x7FF0000000000000
movq %rax, 0x90(%rsp)
cmpl $0x3, %r13d
jne 0x13622
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movq %rax, 0x70(%rsp)
movsd %xmm2, 0x80(%rsp)
movsd %xmm1, 0x90(%rsp)
movq 0x8(%rsp), %r13
testl %r13d, %r13d
movq 0x18(%rsp), %r12
jle 0x1365d
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, (%r12,%rcx)
incq %rcx
cmpq %rcx, %r13
jne 0x13640
testl %r13d, %r13d
jle 0x13671
movq %r15, %rdi
xorl %esi, %esi
movq 0x20(%rsp), %rdx
callq 0x3110
movq 0x48(%rsp), %rax
testl %eax, %eax
jle 0x1368c
leaq (,%rax,8), %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x3110
testl %r13d, %r13d
jle 0x13717
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
xorl %ecx, %ecx
movsbq (%r12,%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 0x1370c
movsbl %dil, %edi
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
mulsd (%r15,%rcx,8), %xmm0
movsd (%r14,%rcx,8), %xmm1
addq $0x10, %rdx
movsd -0x8(%rdx), %xmm2
movapd %xmm2, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm1
movsd %xmm1, (%r14,%rcx,8)
movslq %esi, %rsi
mulsd %xmm0, %xmm2
addsd -0x8(%rbp,%rsi,8), %xmm2
movsd %xmm2, -0x8(%rbp,%rsi,8)
movl (%rdx), %esi
addq $0x10, %rdx
cmpl $-0x1, %esi
jne 0x136d7
movl %ecx, (%rbx,%rcx,4)
incq %rcx
cmpq %r13, %rcx
jne 0x136a0
movsd 0x28e9(%rip), %xmm1     # 0x16008
movsd 0x3b59(%rip), %xmm2     # 0x17280
movq $0x0, (%rsp)
movl %r13d, %r12d
movq %r14, 0x58(%rsp)
movq %r15, 0x50(%rsp)
movapd %xmm2, 0x20(%rsp)
movapd %xmm1, 0x30(%rsp)
testl %r12d, %r12d
jle 0x13778
movl %r12d, %r13d
xorl %r14d, %r14d
callq 0x30a0
cltd
idivl %r13d
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
decq %r13
jne 0x13753
testl %r12d, %r12d
jle 0x1391b
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
xorl %ecx, %ecx
movsd 0x2873(%rip), %xmm12    # 0x16008
movsd 0x3ae2(%rip), %xmm13    # 0x17280
movq 0x58(%rsp), %r14
movq 0x18(%rsp), %r11
movsd 0x3ac0(%rip), %xmm6     # 0x17270
xorpd %xmm7, %xmm7
movapd 0x3933(%rip), %xmm8     # 0x170f0
movsd 0x391a(%rip), %xmm9     # 0x170e0
movq (%rsp), %r13
movapd 0x30(%rsp), %xmm10
movapd 0x20(%rsp), %xmm11
movslq %ecx, %r8
movl (%rbx,%r8,4), %edi
movslq %edi, %rsi
movq (%rax,%rsi,8), %rdx
movl (%rdx), %r10d
cmpl $-0x1, %r10d
je 0x1381a
leaq 0x10(%rdx), %r9
xorpd %xmm1, %xmm1
movslq %r10d, %r10
movsd -0x8(%rbp,%r10,8), %xmm0
mulsd -0x8(%r9), %xmm0
addsd %xmm0, %xmm1
movl (%r9), %r10d
addq $0x10, %r9
cmpl $-0x1, %r10d
jne 0x137f7
jmp 0x1381e
xorpd %xmm1, %xmm1
movsbq (%r11,%rsi), %r9
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
mulsd %xmm0, %xmm1
addsd %xmm6, %xmm1
movsd 0x88(%rsp,%r9,8), %xmm3
movsd (%r15,%rsi,8), %xmm2
movsd 0x68(%rsp,%r9,8), %xmm4
mulsd %xmm2, %xmm4
addsd %xmm1, %xmm4
ucomisd %xmm7, %xmm2
jne 0x1386d
jp 0x1386d
ucomisd %xmm10, %xmm4
ja 0x13880
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm1
ja 0x1389e
jmp 0x138a2
ucomisd %xmm3, %xmm2
movapd %xmm4, %xmm1
jne 0x138a2
jp 0x138a2
ucomisd %xmm4, %xmm11
jbe 0x13894
movslq %r12d, %rdx
decl %r12d
movl -0x4(%rbx,%rdx,4), %esi
movl %esi, (%rbx,%r8,4)
movl %edi, -0x4(%rbx,%rdx,4)
jmp 0x13910
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm4
jbe 0x138a2
movapd %xmm4, %xmm1
movapd %xmm1, %xmm5
andpd %xmm8, %xmm5
ucomisd %xmm9, %xmm5
jbe 0x13904
divsd (%r14,%rsi,8), %xmm4
movapd %xmm2, %xmm5
subsd %xmm4, %xmm5
maxsd %xmm7, %xmm5
minsd %xmm3, %xmm5
movsd %xmm5, (%r15,%rsi,8)
movl (%rdx), %esi
cmpl $-0x1, %esi
je 0x13904
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 0x138e1
maxsd %xmm1, %xmm13
minsd %xmm1, %xmm12
incl %ecx
cmpl %r12d, %ecx
jl 0x137d8
jmp 0x1393a
movsd 0x395c(%rip), %xmm13    # 0x17280
movsd 0x26db(%rip), %xmm12    # 0x16008
movq 0x58(%rsp), %r14
xorpd %xmm7, %xmm7
movq (%rsp), %r13
incl %r13d
imull $0xcccccccd, %r13d, %eax # imm = 0xCCCCCCCD
rorl %eax
cmpl $0x19999999, %eax       # imm = 0x19999999
ja 0x1397b
leaq 0x3ffa(%rip), %rdi      # 0x1794e
xorl %eax, %eax
movapd %xmm12, 0x30(%rsp)
movapd %xmm13, 0x20(%rsp)
callq 0x7152
movapd 0x20(%rsp), %xmm13
movapd 0x30(%rsp), %xmm12
xorpd %xmm7, %xmm7
movapd %xmm13, %xmm0
subsd %xmm12, %xmm0
movsd 0x78(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x139d3
movapd %xmm13, %xmm1
cmpnlesd %xmm7, %xmm1
andpd %xmm1, %xmm13
movsd 0x2660(%rip), %xmm0     # 0x16008
andnpd %xmm0, %xmm1
orpd %xmm13, %xmm1
xorpd %xmm2, %xmm2
cmpnlesd %xmm12, %xmm2
andpd %xmm2, %xmm12
movsd 0x38b8(%rip), %xmm0     # 0x17280
andnpd %xmm0, %xmm2
orpd %xmm12, %xmm2
jmp 0x13a09
movl %r13d, %r15d
movq 0x8(%rsp), %r13
cmpl %r13d, %r12d
je 0x13a2a
leaq 0x3a91(%rip), %rdi      # 0x17478
xorl %eax, %eax
callq 0x7152
movl %r13d, %r12d
movsd 0x260f(%rip), %xmm1     # 0x16008
movsd 0x387f(%rip), %xmm2     # 0x17280
movl %r15d, %r13d
movq 0x50(%rsp), %r15
movl %r13d, %eax
movq %rax, (%rsp)
cmpl $0x3e8, %r13d           # imm = 0x3E8
jne 0x1373c
movl $0x3e7, %r12d           # imm = 0x3E7
movl $0x3e8, %esi            # imm = 0x3E8
jmp 0x13a36
movq (%rsp), %r12
movl %r15d, %esi
movq 0x50(%rsp), %r15
leaq 0x3a3d(%rip), %rdi      # 0x1747a
xorl %eax, %eax
callq 0x7152
cmpl $0x3e7, %r12d           # imm = 0x3E7
jb 0x13a5b
leaq 0x3c27(%rip), %rdi      # 0x1767b
xorl %eax, %eax
callq 0x7152
movq 0x48(%rsp), %rcx
testl %ecx, %ecx
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %rdx
xorpd %xmm0, %xmm0
jle 0x13a8a
xorl %eax, %eax
movsd (%rbp,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %rcx
jne 0x13a74
testl %edx, %edx
jle 0x13ad4
xorl %eax, %eax
movsd 0x3808(%rip), %xmm1     # 0x172a0
xorpd %xmm2, %xmm2
xorl %ebp, %ebp
movsd (%r15,%rax,8), %xmm3
movsbq (%r12,%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 0x13a9e
jmp 0x13ad6
xorl %ebp, %ebp
mulsd 0x35f2(%rip), %xmm0     # 0x170d0
leaq 0x39e6(%rip), %rdi      # 0x174cb
movb $0x1, %al
callq 0x7152
leaq 0x39ef(%rip), %rdi      # 0x174e2
movl %ebp, %esi
xorl %eax, %eax
callq 0x7152
movq %r14, %rdi
callq 0x32c0
movq %r15, %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x32c0
movq %rbx, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32c0