// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movq %r8, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movsd %xmm1, 0x70(%rsp)
movsd %xmm0, 0x40(%rsp)
movl %edx, %r15d
movq %rsi, 0x78(%rsp)
movq %rdi, %rbp
movl (%rdi), %r14d
movslq %r14d, %r12
leaq (,%r12,4), %rdi
callq 0x21f0
movq %rax, %r13
movslq %r15d, %rbx
movq %rbx, %rdi
shlq $0x5, %rdi
callq 0x21f0
movq %r12, 0x88(%rsp)
cmpl %ebx, %r12d
jl 0x9f7d
movq %rax, 0x58(%rsp)
movslq %r15d, %rax
leaq 0x4(,%rax,4), %rdi
callq 0x21f0
movq %rax, %r12
testl %r14d, %r14d
jle 0x9b6e
xorl %eax, %eax
movl %eax, (%r13,%rax,4)
incq %rax
cmpq %rax, %r14
jne 0x9b61
movq %r14, 0x18(%rsp)
testl %r14d, %r14d
jle 0x9baa
xorl %ebx, %ebx
movq 0x18(%rsp), %r14
callq 0x2090
cltd
idivl %r14d
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 %r14
jne 0x9b7f
testl %r15d, %r15d
movq 0x18(%rsp), %r8
js 0x9bd1
leal 0x1(%r15), %esi
xorl %ecx, %ecx
xorl %edi, %edi
movl %ecx, %eax
cltd
idivl %r15d
movl %eax, (%r12,%rdi,4)
incq %rdi
addl %r8d, %ecx
cmpq %rdi, %rsi
jne 0x9bbc
movq %r12, 0x10(%rsp)
movl %r15d, %eax
movq %rax, 0x50(%rsp)
movq %r15, 0x80(%rsp)
testl %r15d, %r15d
jle 0x9d0c
movl 0x4(%rbp), %eax
movl %eax, 0x2c(%rsp)
xorl %ecx, %ecx
movq %r13, 0x48(%rsp)
movq 0x58(%rsp), %r15
movq 0x10(%rsp), %rax
movslq (%rax,%rcx,4), %r14
movslq 0x4(%rax,%rcx,4), %rbx
movsd 0x18(%rbp), %xmm0
movq %rcx, 0x20(%rsp)
movq %rcx, %r13
shlq $0x5, %r13
leaq (%r15,%r13), %r12
movsd %xmm0, 0x18(%r15,%r13)
movl 0x2c(%rsp), %eax
movl %eax, 0x4(%r15,%r13)
movl %r14d, %eax
subl %ebx, %eax
addl %r8d, %eax
movl %eax, (%r15,%r13)
movslq %eax, %rdi
shlq $0x3, %rdi
movq %rdi, 0x90(%rsp)
callq 0x21f0
movq %rax, 0x10(%r15,%r13)
movq 0x90(%rsp), %rdi
callq 0x21f0
movq %rax, 0x8(%r15,%r13)
xorl %eax, %eax
testq %r14, %r14
jle 0x9cab
movq 0x48(%rsp), %r13
movq 0x10(%rbp), %rcx
movslq (%r13,%rax,4), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x10(%r12), %rsi
movq %rcx, (%rsi,%rax,8)
movq 0x8(%rbp), %rcx
movsd (%rcx,%rdx,8), %xmm0
movq 0x8(%r12), %rcx
movsd %xmm0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x9c78
jmp 0x9cb0
movq 0x48(%rsp), %r13
movq 0x18(%rsp), %r8
cmpl %r8d, %ebx
movq 0x50(%rsp), %rdi
movq 0x88(%rsp), %r9
jge 0x9cfb
movq 0x10(%rbp), %rcx
movslq (%r13,%rbx,4), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x10(%r12), %rsi
movq %rcx, (%rsi,%rax,8)
movq 0x8(%rbp), %rcx
movsd (%rcx,%rdx,8), %xmm0
movq 0x8(%r12), %rcx
movsd %xmm0, (%rcx,%rax,8)
incq %rax
incq %rbx
cmpq %rbx, %r9
jne 0x9cc7
movq 0x20(%rsp), %rcx
incq %rcx
cmpq %rdi, %rcx
jne 0x9c02
leaq 0x98(%rsp), %rdi
movl $0xa, %ecx
movq 0x78(%rsp), %rdx
movq %rdx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq 0x38(%rsp), %rcx
movq %rax, (%rcx)
movl (%rdx), %eax
testl %eax, %eax
movq 0x58(%rsp), %r14
movq 0x80(%rsp), %r12
je 0x9d5b
cmpl $0xb, %eax
je 0x9ddb
cmpl $0x2, %eax
jne 0x9f25
xorpd %xmm0, %xmm0
movsd 0x40(%rsp), %xmm1
ucomisd %xmm1, %xmm0
movapd %xmm1, %xmm0
jb 0x9d7f
leaq 0x98(%rsp), %rsi
movq %rbp, %rdi
callq 0x9f90
movsd 0x4071(%rip), %xmm1     # 0xddf8
minsd %xmm1, %xmm0
leaq 0x98(%rsp), %rsi
leaq 0x68(%rsp), %r15
leaq 0x60(%rsp), %rbx
movq %rbp, %rdi
movq %r15, %rdx
movq %rbx, %rcx
movq 0x10(%rsp), %r8
movq %r13, %r9
pushq %r12
pushq %r14
callq 0xa0b0
addq $0x10, %rsp
movsd (%r15), %xmm0
movq 0x30(%rsp), %rax
movsd %xmm0, (%rax)
movsd (%rbx), %xmm0
movq 0x8(%rsp), %rax
movsd %xmm0, (%rax)
jmp 0x9f25
movslq (%rbp), %rax
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
testq %rax, %rax
jle 0x9e0f
movq 0x8(%rbp), %rcx
xorl %edx, %edx
movapd 0x3e46(%rip), %xmm1     # 0xdc40
movsd (%rcx,%rdx,8), %xmm2
andpd %xmm1, %xmm2
maxsd %xmm2, %xmm3
incq %rdx
cmpq %rdx, %rax
jne 0x9dfa
movabsq $0x7ff0000000000000, %rax # imm = 0x7FF0000000000000
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movl $0x13, %ebx
movsd 0x70(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x9e51
movapd %xmm3, %xmm0
divsd 0x3faa(%rip), %xmm0     # 0xdde8
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl $0x13, %eax
movl $0x13, %ebx
cmovll %eax, %ebx
testl %ebx, %ebx
js 0x9f25
leaq 0x98(%rsp), %r15
movsd %xmm3, 0x20(%rsp)
movsd 0x40(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd %xmm3, %xmm0
divsd 0x3f60(%rip), %xmm0     # 0xdde8
movsd %xmm0, 0xc8(%rsp)
movapd %xmm1, %xmm0
jb 0x9ea2
movq %rbp, %rdi
movq %r15, %rsi
callq 0x9f90
movsd 0x3f46(%rip), %xmm1     # 0xddf0
minsd %xmm1, %xmm0
movq %rbp, %rdi
movq %r15, %rsi
leaq 0x68(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq 0x10(%rsp), %r8
movq %r13, %r9
pushq %r12
pushq %r14
callq 0xa0b0
addq $0x10, %rsp
movsd 0x60(%rsp), %xmm0
movq 0x8(%rsp), %rax
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x9f12
movsd 0xc8(%rsp), %xmm1
movq 0x38(%rsp), %rax
movsd %xmm1, (%rax)
movsd 0x68(%rsp), %xmm1
movq 0x30(%rsp), %rax
movsd %xmm1, (%rax)
movq 0x8(%rsp), %rax
movsd %xmm0, (%rax)
leal -0x1(%rbx), %eax
testl %ebx, %ebx
movl %eax, %ebx
movsd 0x20(%rsp), %xmm3
jg 0x9e67
movq 0x10(%rsp), %rdi
callq 0x21a0
movq %r13, %rdi
callq 0x21a0
testl %r12d, %r12d
movq 0x50(%rsp), %r15
jle 0x9f64
shlq $0x5, %r15
xorl %ebx, %ebx
movq 0x10(%r14,%rbx), %rdi
callq 0x21a0
movq 0x8(%r14,%rbx), %rdi
callq 0x21a0
addq $0x20, %rbx
cmpq %rbx, %r15
jne 0x9f47
movq %r14, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21a0
movq %rax, %rbx
callq 0x250e
movq %rbx, %rax
movl %r14d, %r15d
jmp 0x9b42