// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movq %r9, 0x68(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movsd %xmm1, 0x58(%rsp)
movsd %xmm0, 0x20(%rsp)
movl %edx, %r14d
movq %rsi, 0x60(%rsp)
movq %rdi, %r13
movl (%rdi), %r12d
movslq %r12d, %rbp
leaq (,%rbp,4), %rdi
callq 0x21f0
movq %rax, 0x10(%rsp)
movslq %r14d, %rbx
movq %rbx, %rdi
shlq $0x5, %rdi
callq 0x21f0
movq %rax, %r15
movq %rbp, 0x70(%rsp)
cmpl %ebx, %ebp
jl 0x9240
movq %r14, 0x28(%rsp)
movslq %r14d, %r14
leaq 0x4(,%r14,4), %rdi
callq 0x21f0
movq %rax, 0x18(%rsp)
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %ebx
cmovgl %r12d, %ebx
movq 0x10(%rsp), %rcx
movsd 0x20(%rsp), %xmm0
cmpq %rax, %rbx
je 0x8e7a
movl %eax, (%rcx,%rax,4)
incq %rax
jmp 0x8e6d
movq %r15, 0x8(%rsp)
xorl %r15d, %r15d
movl %r12d, %ebp
cmpq %r15, %rbx
je 0x8eb8
callq 0x2090
movsd 0x20(%rsp), %xmm0
cltd
idivl %ebp
movslq %edx, %rax
addq %r15, %rax
movq 0x10(%rsp), %rsi
movl (%rsi,%r15,4), %ecx
movl (%rsi,%rax,4), %edx
movl %edx, (%rsi,%r15,4)
movl %ecx, (%rsi,%rax,4)
incq %r15
decl %ebp
jmp 0x8e85
xorl %ecx, %ecx
xorl %esi, %esi
movq 0x8(%rsp), %rbp
movq 0x28(%rsp), %rdi
cmpq %r14, %rsi
jg 0x8ee0
movl %ecx, %eax
cltd
idivl %edi
movq 0x18(%rsp), %rdx
movl %eax, (%rdx,%rsi,4)
incq %rsi
addl %r12d, %ecx
jmp 0x8ec6
movl %r12d, 0x34(%rsp)
testl %edi, %edi
movl $0x0, %r12d
cmovgl %edi, %r12d
xorl %r15d, %r15d
movq %r12, 0x78(%rsp)
cmpq %r12, %r15
je 0x9005
movq 0x18(%rsp), %rax
movl (%rax,%r15,4), %r12d
leaq 0x1(%r15), %rcx
movq %rcx, 0x80(%rsp)
movslq 0x4(%rax,%r15,4), %rbx
movsd 0x18(%r13), %xmm0
shlq $0x5, %r15
movsd %xmm0, 0x18(%rbp,%r15)
movl 0x4(%r13), %eax
movl %eax, 0x4(%rbp,%r15)
movl %r12d, %eax
subl %ebx, %eax
addl 0x34(%rsp), %eax
movl %eax, (%rbp,%r15)
movslq %eax, %rbp
shlq $0x3, %rbp
movq %rbp, %rdi
callq 0x21f0
movq %rax, %r14
movq 0x8(%rsp), %rax
movq %r14, 0x10(%rax,%r15)
movq %rbp, %rdi
movq 0x8(%rsp), %rbp
callq 0x21f0
movq %rax, 0x8(%rbp,%r15)
testl %r12d, %r12d
movl $0x0, %ecx
cmovlel %ecx, %r12d
xorl %ecx, %ecx
movq 0x10(%rsp), %rdi
movq 0x70(%rsp), %r9
cmpq %rcx, %r12
je 0x8fb2
movq 0x10(%r13), %rdx
movslq (%rdi,%rcx,4), %rsi
movq (%rdx,%rsi,8), %rdx
movq %rdx, (%r14,%rcx,8)
movq 0x8(%r13), %rdx
movsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
jmp 0x8f8a
leaq (%rdi,%rbx,4), %rcx
leaq (%rax,%r12,8), %rax
leaq (%r14,%r12,8), %rdx
xorl %esi, %esi
movsd 0x20(%rsp), %xmm0
movq 0x78(%rsp), %r12
leaq (%rbx,%rsi), %rdi
cmpq %r9, %rdi
jge 0x8ff8
movq 0x10(%r13), %rdi
movslq (%rcx,%rsi,4), %r8
movq (%rdi,%r8,8), %rdi
movq %rdi, (%rdx,%rsi,8)
movq 0x8(%r13), %rdi
movsd (%rdi,%r8,8), %xmm1
movsd %xmm1, (%rax,%rsi,8)
incq %rsi
jmp 0x8fcb
movq 0x80(%rsp), %r15
jmp 0x8ef9
pushq $0xa
popq %rcx
leaq 0x88(%rsp), %rdi
movq 0x60(%rsp), %rdx
movq %rdx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq 0x40(%rsp), %rcx
movq %rax, (%rcx)
movl (%rdx), %eax
testl %eax, %eax
movq 0x68(%rsp), %r15
je 0x9046
cmpl $0xb, %eax
je 0x910f
cmpl $0x2, %eax
jne 0x90bd
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jb 0x9060
leaq 0x88(%rsp), %rsi
movq %r13, %rdi
callq 0x9264
movsd 0x3db8(%rip), %xmm1     # 0xce20
minsd %xmm1, %xmm0
leaq 0x88(%rsp), %rsi
leaq 0x50(%rsp), %r14
leaq 0x48(%rsp), %rbp
movq %r13, %rdi
movq %r14, %rdx
movq %rbp, %rcx
movq 0x18(%rsp), %r8
movq 0x10(%rsp), %r9
pushq 0x28(%rsp)
movq 0x10(%rsp), %rbx
pushq %rbx
callq 0x936f
popq %rax
popq %rcx
movsd (%r14), %xmm0
movq 0x38(%rsp), %rax
movsd %xmm0, (%rax)
movsd (%rbp), %xmm0
movq %rbx, %rbp
movsd %xmm0, (%r15)
movq 0x18(%rsp), %rdi
callq 0x21a0
movq 0x10(%rsp), %rdi
callq 0x21a0
shlq $0x5, %r12
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x90f6
movq 0x10(%rbp,%rbx), %rdi
callq 0x21a0
movq 0x8(%rbp,%rbx), %rdi
callq 0x21a0
addq $0x20, %rbx
jmp 0x90d7
movq %rbp, %rdi
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21a0
movl (%r13), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
xorpd %xmm3, %xmm3
movapd 0x3b4a(%rip), %xmm2     # 0xcc70
cmpq %rcx, %rax
je 0x9141
movq 0x8(%r13), %rdx
movsd (%rdx,%rcx,8), %xmm1
andpd %xmm2, %xmm1
maxsd %xmm1, %xmm3
incq %rcx
jmp 0x9126
movabsq $0x7ff0000000000000, %rax # imm = 0x7FF0000000000000
movq %rax, (%r15)
xorpd %xmm1, %xmm1
movsd 0x58(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x917d
movapd %xmm3, %xmm1
divsd 0x3ca6(%rip), %xmm1     # 0xce10
divsd %xmm1, %xmm2
cvttsd2si %xmm2, %eax
cmpl $0x13, %eax
pushq $0x13
popq %rbx
cmovll %eax, %ebx
jmp 0x9180
pushq $0x13
popq %rbx
leaq 0x88(%rsp), %r14
movsd %xmm3, 0x8(%rsp)
testl %ebx, %ebx
js 0x90bd
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
cvtsi2sd %ebx, %xmm1
mulsd %xmm3, %xmm1
divsd 0x3c62(%rip), %xmm1     # 0xce10
movsd %xmm1, 0xb8(%rsp)
jb 0x91c4
movq %r13, %rdi
movq %r14, %rsi
callq 0x9264
movsd 0x3c4c(%rip), %xmm1     # 0xce18
minsd %xmm1, %xmm0
movq %r13, %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
movq 0x18(%rsp), %r8
movq 0x10(%rsp), %r9
pushq 0x28(%rsp)
pushq %rbp
callq 0x936f
popq %rax
popq %rcx
movsd 0x48(%rsp), %xmm0
movsd (%r15), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x922d
movsd 0xb8(%rsp), %xmm1
movq 0x40(%rsp), %rax
movsd %xmm1, (%rax)
movsd 0x50(%rsp), %xmm1
movq 0x38(%rsp), %rax
movsd %xmm1, (%rax)
movsd %xmm0, (%r15)
decl %ebx
movsd 0x20(%rsp), %xmm0
movsd 0x8(%rsp), %xmm3
jmp 0x918e
movq 0x7d99(%rip), %rax      # 0x10fe0
movq (%rax), %rcx
leaq 0x3d97(%rip), %rdi      # 0xcfe8
pushq $0x64
popq %rsi
pushq $0x1
popq %rdx
callq 0x22f0
movl %r12d, %r14d
jmp 0x8e3a