// File: /workspace/github_cpp_makefile_star2_O3/cjlin1[P]liblinear/linear.cpp
// Function: find_parameters
void find_parameters(const problem *prob, const parameter *param, int nr_fold, double start_C, double start_p, double *best_C, double *best_p, double *best_score)
{
	// prepare CV folds

	int i;
	int *fold_start;
	int l = prob->l;
	int *perm = Malloc(int, l);
	struct problem *subprob = Malloc(problem,nr_fold);

	if (nr_fold > l)
	{
		nr_fold = l;
		fprintf(stderr,"WARNING: # folds > # data. Will use # folds = # data instead (i.e., leave-one-out cross validation)\n");
	}
	fold_start = Malloc(int,nr_fold+1);
	for(i=0;i<l;i++) perm[i]=i;
	for(i=0;i<l;i++)
	{
		int j = i+rand()%(l-i);
		swap(perm[i],perm[j]);
	}
	for(i=0;i<=nr_fold;i++)
		fold_start[i]=i*l/nr_fold;

	for(i=0;i<nr_fold;i++)
	{
		int begin = fold_start[i];
		int end = fold_start[i+1];
		int j,k;

		subprob[i].bias = prob->bias;
		subprob[i].n = prob->n;
		subprob[i].l = l-(end-begin);
		subprob[i].x = Malloc(struct feature_node*,subprob[i].l);
		subprob[i].y = Malloc(double,subprob[i].l);

		k=0;
		for(j=0;j<begin;j++)
		{
			subprob[i].x[k] = prob->x[perm[j]];
			subprob[i].y[k] = prob->y[perm[j]];
			++k;
		}
		for(j=end;j<l;j++)
		{
			subprob[i].x[k] = prob->x[perm[j]];
			subprob[i].y[k] = prob->y[perm[j]];
			++k;
		}
	}

	struct parameter param_tmp = *param;
	*best_p = -1;
	if(param->solver_type == L2R_LR || param->solver_type == L2R_L2LOSS_SVC)
	{
		if(start_C <= 0)
			start_C = calc_start_C(prob, &param_tmp);
		double max_C = 1024;
		start_C = min(start_C, max_C);
		double best_C_tmp, best_score_tmp;

		find_parameter_C(prob, &param_tmp, start_C, max_C, &best_C_tmp, &best_score_tmp, fold_start, perm, subprob, nr_fold);

		*best_C = best_C_tmp;
		*best_score = best_score_tmp;
	}
	else if(param->solver_type == L2R_L2LOSS_SVR)
	{
		double max_p = calc_max_p(prob);
		int num_p_steps = 20;
		double max_C = 1048576;
		*best_score = INF;

		i = num_p_steps-1;
		if(start_p > 0)
			i = min((int)(start_p/(max_p/num_p_steps)), i);
		for(; i >= 0; i--)
		{
			param_tmp.p = i*max_p/num_p_steps;
			double start_C_tmp;
			if(start_C <= 0)
				start_C_tmp = calc_start_C(prob, &param_tmp);
			else
				start_C_tmp = start_C;
			start_C_tmp = min(start_C_tmp, max_C);
			double best_C_tmp, best_score_tmp;

			find_parameter_C(prob, &param_tmp, start_C_tmp, max_C, &best_C_tmp, &best_score_tmp, fold_start, perm, subprob, nr_fold);

			if(best_score_tmp < *best_score)
			{
				*best_p = param_tmp.p;
				*best_C = best_C_tmp;
				*best_score = best_score_tmp;
			}
		}
	}

	free(fold_start);
	free(perm);
	for(i=0; i<nr_fold; i++)
	{
		free(subprob[i].x);
		free(subprob[i].y);
	}
	free(subprob);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, 0x40(%rsp)
movq %r8, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %xmm1, 0x38(%rsp)
movq %xmm0, 0x60(%rsp)
movl %edx, %r12d
movq %rsi, 0x80(%rsp)
movq %rdi, (%rsp)
movl (%rdi), %r15d
movslq %r15d, %r14
leaq (,%r14,4), %rdi
callq 0x21f0
movq %rax, %r13
movslq %r12d, %rbx
movq %rbx, %rdi
shlq $0x5, %rdi
callq 0x21f0
movq %rax, 0x10(%rsp)
movq %r14, 0x88(%rsp)
cmpl %ebx, %r14d
jl 0xaacc
leal 0x1(%r12), %r14d
movslq %r14d, %rdi
shlq $0x2, %rdi
callq 0x21f0
movq %rax, %rbp
movq %r15, 0x8(%rsp)
testl %r15d, %r15d
jle 0xa62c
movq 0x8(%rsp), %rsi
leal 0x1(%rsi), %eax
andl $-0x2, %eax
leaq -0x1(%rsi), %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x48dd(%rip), %xmm1     # 0xee60
xorl %ecx, %ecx
movdqa 0x48e3(%rip), %xmm2     # 0xee70
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x48e3(%rip), %xmm4     # 0xee80
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0xa5d3
movl %ecx, (%r13,%rcx,4)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0xa5e9
leal 0x1(%rcx), %edx
movl %edx, 0x4(%r13,%rcx,4)
addq $0x2, %rcx
paddq %xmm4, %xmm1
cmpq %rcx, %rax
jne 0xa59d
testl %esi, %esi
jle 0xa62c
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
callq 0x2090
cltd
idivl %r15d
movslq %edx, %rax
addq %rbx, %rax
movl (%r13,%rbx,4), %ecx
movl (%r13,%rax,4), %edx
movl %edx, (%r13,%rbx,4)
movl %ecx, (%r13,%rax,4)
incq %rbx
decq %r15
jne 0xa601
movq %rbp, 0x28(%rsp)
movl %r12d, %eax
movq %rax, 0x20(%rsp)
movq %r12, 0x48(%rsp)
testl %r12d, %r12d
movq 0x8(%rsp), %r11
js 0xa83d
movl %r14d, %eax
leaq -0x1(%rax), %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq 0x4(,%rax,4), %rsi
andq $-0x8, %rsi
leal (%r11,%r11), %edi
movdqa 0x47eb(%rip), %xmm1     # 0xee60
xorl %ecx, %ecx
movdqa 0x47f1(%rip), %xmm2     # 0xee70
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x47f1(%rip), %xmm4     # 0xee80
xorl %r8d, %r8d
movq 0x48(%rsp), %r9
movq 0x28(%rsp), %r10
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %eax
notl %eax
testb $0x1, %al
je 0xa6d6
movl %ecx, %eax
cltd
idivl %r9d
movl %eax, (%r10,%r8)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %eax
testb $0x1, %al
je 0xa6f0
leal (%r11,%rcx), %eax
cltd
idivl %r9d
movl %eax, 0x4(%r10,%r8)
paddq %xmm4, %xmm1
addq $0x8, %r8
addl %edi, %ecx
cmpq %r8, %rsi
jne 0xa69c
movq (%rsp), %rax
movl 0x4(%rax), %ecx
movl %ecx, 0x34(%rsp)
movl (%r10), %ebp
movq 0x18(%rax), %xmm0
movq %xmm0, 0x90(%rsp)
xorl %r12d, %r12d
movq %r13, 0x68(%rsp)
movq 0x28(%rsp), %rax
movl 0x4(%rax,%r12,4), %ecx
movq %r12, %rbx
shlq $0x5, %rbx
movq 0x10(%rsp), %r13
movq 0x90(%rsp), %xmm0
movq %xmm0, 0x18(%r13,%rbx)
movl 0x34(%rsp), %eax
movl %eax, 0x4(%r13,%rbx)
movl %ebp, %eax
movl %ecx, 0x1c(%rsp)
subl %ecx, %eax
addl %r11d, %eax
movl %eax, (%r13,%rbx)
movslq %eax, %r14
shlq $0x3, %r14
movq %r14, %rdi
callq 0x21f0
movq %rax, %r15
movq %rax, 0x10(%r13,%rbx)
movq %r14, %rdi
callq 0x21f0
movq %rax, 0x8(%r13,%rbx)
testl %ebp, %ebp
jle 0xa7c6
movl %ebp, %ecx
movq (%rsp), %rsi
movq 0x8(%rsi), %rdx
movq 0x10(%rsi), %rsi
xorl %edi, %edi
movq 0x68(%rsp), %r13
movq 0x20(%rsp), %rbx
movslq (%r13,%rdi,4), %r8
movq (%rsi,%r8,8), %r9
movq %r9, (%r15,%rdi,8)
movq (%rdx,%r8,8), %xmm0
movq %xmm0, (%rax,%rdi,8)
incq %rdi
cmpq %rdi, %rcx
jne 0xa7a4
jmp 0xa7d2
xorl %ecx, %ecx
movq 0x68(%rsp), %r13
movq 0x20(%rsp), %rbx
movl 0x1c(%rsp), %ebp
cmpl 0x8(%rsp), %ebp
jge 0xa82c
movslq %ebp, %r9
movq (%rsp), %rsi
movq 0x8(%rsi), %rdx
movq 0x10(%rsi), %rsi
leaq (%rax,%rcx,8), %rax
leaq (%r15,%rcx,8), %rcx
leaq (,%r9,4), %rdi
addq %r13, %rdi
movq 0x88(%rsp), %r8
subq %r9, %r8
xorl %r9d, %r9d
movslq (%rdi,%r9,4), %r10
movq (%rsi,%r10,8), %r11
movq %r11, (%rcx,%r9,8)
movq (%rdx,%r10,8), %xmm0
movq %xmm0, (%rax,%r9,8)
incq %r9
cmpq %r9, %r8
jne 0xa80c
incq %r12
cmpq %rbx, %r12
movq 0x8(%rsp), %r11
jne 0xa723
leaq 0x98(%rsp), %rdi
movl $0xa, %ecx
movq 0x80(%rsp), %rdx
movq %rdx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq 0x58(%rsp), %rcx
movq %rax, (%rcx)
movl (%rdx), %eax
testl %eax, %eax
movq 0x48(%rsp), %r12
movq 0x40(%rsp), %r14
movsd 0x60(%rsp), %xmm3
movq 0x28(%rsp), %rbp
je 0xa897
cmpl $0xb, %eax
je 0xa920
cmpl $0x2, %eax
jne 0xaa74
pxor %xmm0, %xmm0
ucomisd %xmm3, %xmm0
jb 0xa8be
movl 0x98(%rsp), %esi
movsd 0xc8(%rsp), %xmm0
movq (%rsp), %rdi
callq 0xaad9
movapd %xmm0, %xmm3
movsd 0x4562(%rip), %xmm1     # 0xee28
minsd %xmm1, %xmm3
leaq 0x98(%rsp), %rsi
leaq 0x78(%rsp), %r14
leaq 0x70(%rsp), %r15
movq (%rsp), %rdi
movapd %xmm3, %xmm0
movq %r14, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
pushq %r12
pushq 0x18(%rsp)
callq 0xac04
addq $0x10, %rsp
movsd (%r14), %xmm0
movq 0x50(%rsp), %rax
movsd %xmm0, (%rax)
movq (%r15), %xmm0
movq 0x40(%rsp), %rax
movq %xmm0, (%rax)
jmp 0xaa74
movq (%rsp), %rax
movslq (%rax), %rax
pxor %xmm0, %xmm0
pxor %xmm4, %xmm4
testq %rax, %rax
jle 0xa95b
movq (%rsp), %rcx
movq 0x8(%rcx), %rcx
xorl %edx, %edx
movapd 0x432a(%rip), %xmm1     # 0xec70
movsd (%rcx,%rdx,8), %xmm2
andpd %xmm1, %xmm2
maxsd %xmm2, %xmm4
incq %rdx
cmpq %rdx, %rax
jne 0xa946
movabsq $0x7ff0000000000000, %rax # imm = 0x7FF0000000000000
movq %rax, (%r14)
movl $0x13, %ebx
movsd 0x38(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0xa9a6
movapd %xmm4, %xmm0
divsd 0x4493(%rip), %xmm0     # 0xee18
movsd 0x38(%rsp), %xmm1
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl $0x13, %eax
movl $0x13, %ebx
cmovll %eax, %ebx
testl %eax, %eax
js 0xaa74
leaq 0x70(%rsp), %r15
movsd %xmm4, 0x8(%rsp)
xorpd %xmm0, %xmm0
ucomisd %xmm3, %xmm0
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd %xmm4, %xmm0
divsd 0x444c(%rip), %xmm0     # 0xee18
movsd %xmm0, 0xc8(%rsp)
jb 0xa9eb
movl 0x98(%rsp), %esi
movq (%rsp), %rdi
callq 0xaad9
movapd %xmm0, %xmm3
movsd 0x442d(%rip), %xmm1     # 0xee20
minsd %xmm1, %xmm3
movq (%rsp), %rdi
leaq 0x98(%rsp), %rsi
movapd %xmm3, %xmm0
leaq 0x78(%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
pushq %r12
pushq 0x18(%rsp)
callq 0xac04
addq $0x10, %rsp
movsd 0x70(%rsp), %xmm0
movsd (%r14), %xmm1
ucomisd %xmm0, %xmm1
jbe 0xaa5b
movsd 0xc8(%rsp), %xmm1
movq 0x58(%rsp), %rax
movsd %xmm1, (%rax)
movsd 0x78(%rsp), %xmm1
movq 0x50(%rsp), %rax
movsd %xmm1, (%rax)
movsd %xmm0, (%r14)
leal -0x1(%rbx), %eax
testl %ebx, %ebx
movl %eax, %ebx
movsd 0x60(%rsp), %xmm3
movsd 0x8(%rsp), %xmm4
jg 0xa9b1
movq %rbp, %rdi
callq 0x21a0
movq %r13, %rdi
callq 0x21a0
testl %r12d, %r12d
movq 0x20(%rsp), %r14
js 0xaab1
movq 0x10(%rsp), %rbx
addq $0x10, %rbx
movq (%rbx), %rdi
callq 0x21a0
movq -0x8(%rbx), %rdi
callq 0x21a0
addq $0x20, %rbx
decq %r14
jne 0xaa97
movq 0x10(%rsp), %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21a0
callq 0x250e
movl %r15d, %r12d
jmp 0xa540