// File: 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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movsd %xmm0, -0x20(%rbp)
movsd %xmm1, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x54(%rbp)
movslq -0x54(%rbp), %rdi
shlq $0x2, %rdi
callq 0x21c0
movq %rax, -0x60(%rbp)
movslq -0x14(%rbp), %rdi
shlq $0x5, %rdi
callq 0x21c0
movq %rax, -0x68(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x54(%rbp), %eax
jle 0x9c6d
movl -0x54(%rbp), %eax
movl %eax, -0x14(%rbp)
movq 0xc384(%rip), %rax      # 0x15fe0
movq (%rax), %rdi
leaq 0x9332(%rip), %rsi      # 0x12f98
movb $0x0, %al
callq 0x21f0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x21c0
movq %rax, -0x50(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x9cab
movl -0x44(%rbp), %edx
movq -0x60(%rbp), %rax
movslq -0x44(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x9c8a
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x9d0a
movl -0x44(%rbp), %eax
movl %eax, -0x11c(%rbp)
callq 0x2090
movl -0x54(%rbp), %ecx
subl -0x44(%rbp), %ecx
cltd
idivl %ecx
movl -0x11c(%rbp), %eax
addl %edx, %eax
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rdi
movslq -0x44(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
movq -0x60(%rbp), %rsi
movslq -0x6c(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
callq 0x70b0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x9cb2
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x9d3c
movl -0x44(%rbp), %eax
imull -0x54(%rbp), %eax
cltd
idivl -0x14(%rbp)
movl %eax, %edx
movq -0x50(%rbp), %rax
movslq -0x44(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x9d11
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x9f56
movq -0x50(%rbp), %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x70(%rbp)
movq -0x50(%rbp), %rax
movl -0x44(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rax
movsd 0x18(%rax), %xmm0
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movsd %xmm0, 0x18(%rax)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0x54(%rbp), %ecx
movl -0x74(%rbp), %eax
subl -0x70(%rbp), %eax
subl %eax, %ecx
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rdi
shlq $0x3, %rdi
callq 0x21c0
movq %rax, %rcx
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rdi
shlq $0x3, %rdi
callq 0x21c0
movq %rax, %rcx
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x70(%rbp), %eax
jge 0x9eb9
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rcx
movslq -0x78(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movslq -0x7c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x60(%rbp), %rcx
movslq -0x78(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movslq -0x7c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x9e32
movl -0x74(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x9f46
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rcx
movslq -0x78(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movslq -0x7c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x60(%rbp), %rcx
movslq -0x78(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movslq -0x7c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x9ebf
jmp 0x9f48
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x9d43
movq -0x10(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
movl $0x50, %edx
callq 0x20f0
movq -0x38(%rbp), %rax
movsd 0x8091(%rip), %xmm0     # 0x12008
movsd %xmm0, (%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
je 0x9f91
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
jne 0xa03e
xorps %xmm0, %xmm0
ucomisd -0x20(%rbp), %xmm0
jb 0x9fb0
movq -0x8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0xa260
movsd %xmm0, -0x20(%rbp)
movsd 0x8e60(%rip), %xmm0     # 0x12e18
movsd %xmm0, -0xd8(%rbp)
movsd -0x20(%rbp), %xmm0
movsd -0xd8(%rbp), %xmm1
callq 0x5f30
movsd %xmm0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movsd -0x20(%rbp), %xmm0
movsd -0xd8(%rbp), %xmm1
movq -0x50(%rbp), %r8
movq -0x60(%rbp), %r9
movq -0x68(%rbp), %r10
movl -0x14(%rbp), %eax
leaq -0xd0(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
leaq -0xe8(%rbp), %rcx
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0xa4e0
movsd -0xe0(%rbp), %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0xe8(%rbp), %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0xa1e6
movq -0x10(%rbp), %rax
cmpl $0xb, (%rax)
jne 0xa1e4
movq -0x8(%rbp), %rdi
callq 0xabd0
movsd %xmm0, -0xf0(%rbp)
movl $0x14, -0xf4(%rbp)
movsd 0x8da2(%rip), %xmm0     # 0x12e10
movsd %xmm0, -0x100(%rbp)
movq -0x40(%rbp), %rax
movsd 0x7f8e(%rip), %xmm0     # 0x12010
movsd %xmm0, (%rax)
movl -0xf4(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x44(%rbp)
movsd -0x28(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xa0cc
movsd -0x28(%rbp), %xmm0
movsd -0xf0(%rbp), %xmm1
cvtsi2sdl -0xf4(%rbp), %xmm2
divsd %xmm2, %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
movl -0x44(%rbp), %esi
callq 0xac40
movl %eax, -0x44(%rbp)
jmp 0xa0ce
cmpl $0x0, -0x44(%rbp)
jl 0xa1e2
cvtsi2sdl -0x44(%rbp), %xmm0
mulsd -0xf0(%rbp), %xmm0
cvtsi2sdl -0xf4(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x20(%rbp), %xmm0
jb 0xa11d
movq -0x8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0xa260
movsd %xmm0, -0x108(%rbp)
jmp 0xa12a
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x108(%rbp)
movsd -0x108(%rbp), %xmm0
movsd -0x100(%rbp), %xmm1
callq 0x5f30
movsd %xmm0, -0x108(%rbp)
movq -0x8(%rbp), %rdi
movsd -0x108(%rbp), %xmm0
movsd -0x100(%rbp), %xmm1
movq -0x50(%rbp), %r8
movq -0x60(%rbp), %r9
movq -0x68(%rbp), %r10
movl -0x14(%rbp), %eax
leaq -0xd0(%rbp), %rsi
leaq -0x110(%rbp), %rdx
leaq -0x118(%rbp), %rcx
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0xa4e0
movsd -0x118(%rbp), %xmm1
movq -0x40(%rbp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xa1d2
movsd -0xa0(%rbp), %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x110(%rbp), %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x118(%rbp), %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0xa1d4
movl -0x44(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xa0ce
jmp 0xa1e4
jmp 0xa1e6
movq -0x50(%rbp), %rdi
callq 0x2180
movq -0x60(%rbp), %rdi
callq 0x2180
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xa242
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
callq 0x2180
movq -0x68(%rbp), %rax
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
callq 0x2180
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xa1ff
movq -0x68(%rbp), %rdi
callq 0x2180
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)