// File: cjlin1[P]liblinear/linear.cpp
// Function: cross_validation
void cross_validation(const problem *prob, const parameter *param, int nr_fold, double *target)
{
	int i;
	int *fold_start;
	int l = prob->l;
	int *perm = Malloc(int,l);
	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;
		struct problem subprob;

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

		k=0;
		for(j=0;j<begin;j++)
		{
			subprob.x[k] = prob->x[perm[j]];
			subprob.y[k] = prob->y[perm[j]];
			++k;
		}
		for(j=end;j<l;j++)
		{
			subprob.x[k] = prob->x[perm[j]];
			subprob.y[k] = prob->y[perm[j]];
			++k;
		}
		struct model *submodel = train(&subprob,param);
		for(j=begin;j<end;j++)
			target[perm[j]] = predict(submodel,prob->x[perm[j]]);
		free_and_destroy_model(&submodel);
		free(subprob.x);
		free(subprob.y);
	}
	free(fold_start);
	free(perm);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movslq -0x34(%rbp), %rdi
shlq $0x2, %rdi
callq 0x21c0
movq %rax, -0x40(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x987a
movl -0x34(%rbp), %eax
movl %eax, -0x14(%rbp)
movq 0xc777(%rip), %rax      # 0x15fe0
movq (%rax), %rdi
leaq 0x9725(%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, -0x30(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x98b8
movl -0x24(%rbp), %edx
movq -0x40(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x9897
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x9917
movl -0x24(%rbp), %eax
movl %eax, -0x84(%rbp)
callq 0x2090
movl -0x34(%rbp), %ecx
subl -0x24(%rbp), %ecx
cltd
idivl %ecx
movl -0x84(%rbp), %eax
addl %edx, %eax
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x24(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
movq -0x40(%rbp), %rsi
movslq -0x44(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
callq 0x70b0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x98bf
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x9949
movl -0x24(%rbp), %eax
imull -0x34(%rbp), %eax
cltd
idivl -0x14(%rbp)
movl %eax, %edx
movq -0x30(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x991e
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x9b2e
movq -0x30(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x74(%rbp)
movl -0x34(%rbp), %eax
movl -0x4c(%rbp), %ecx
subl -0x48(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x78(%rbp)
movslq -0x78(%rbp), %rdi
shlq $0x3, %rdi
callq 0x21c0
movq %rax, -0x68(%rbp)
movslq -0x78(%rbp), %rdi
shlq $0x3, %rdi
callq 0x21c0
movq %rax, -0x70(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0x9a39
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x68(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x70(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x99d3
movl -0x4c(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x9aa5
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x68(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x70(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x9a3f
movq -0x10(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x7200
movq %rax, -0x80(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x9b05
movq -0x80(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x9b50
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x9abc
leaq -0x80(%rbp), %rdi
callq 0x9ba0
movq -0x68(%rbp), %rdi
callq 0x2180
movq -0x70(%rbp), %rdi
callq 0x2180
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x9950
movq -0x30(%rbp), %rdi
callq 0x2180
movq -0x40(%rbp), %rdi
callq 0x2180
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)