// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %r12
movl %edx, %ebp
movq %rsi, 0x60(%rsp)
movq %rdi, %r15
movl (%rdi), %r13d
movslq %r13d, %r14
leaq (,%r14,4), %rdi
callq 0x21f0
movq %rax, %rbx
movq %r14, 0x58(%rsp)
cmpl %ebp, %r14d
jl 0x9a3f
movq %rbp, 0x10(%rsp)
movslq %ebp, %rax
leaq 0x4(,%rax,4), %rdi
callq 0x21f0
movq %rax, 0x18(%rsp)
testl %r13d, %r13d
jle 0x9827
xorl %eax, %eax
movl %eax, (%rbx,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x981c
testl %r13d, %r13d
movq %rbx, %rbp
jle 0x985b
xorl %ebx, %ebx
movq %r13, %r14
callq 0x2090
cltd
idivl %r14d
movslq %edx, %rax
addq %rbx, %rax
movl (%rbp,%rbx,4), %ecx
movl (%rbp,%rax,4), %edx
movl %edx, (%rbp,%rbx,4)
movl %ecx, (%rbp,%rax,4)
incq %rbx
decq %r14
jne 0x9834
movq %rbp, 0x8(%rsp)
movq 0x10(%rsp), %r9
testl %r9d, %r9d
movq 0x18(%rsp), %rdi
js 0x988d
leal 0x1(%r9), %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %ecx, %eax
cltd
idivl %r9d
movl %eax, (%rdi,%r8,4)
incq %r8
addl %r13d, %ecx
cmpq %r8, %rsi
jne 0x9878
testl %r9d, %r9d
movq 0x8(%rsp), %r14
jle 0x9a23
movl %r9d, %eax
movq %rax, 0x48(%rsp)
xorl %ebp, %ebp
movq %r13, 0x50(%rsp)
movslq (%rdi,%rbp,4), %rbx
movq %rbp, 0x68(%rsp)
movslq 0x4(%rdi,%rbp,4), %rcx
movsd 0x18(%r15), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x4(%r15), %eax
movl %eax, 0x2c(%rsp)
movl %ebx, %eax
movq %rcx, 0x20(%rsp)
subl %ecx, %eax
addl %r13d, %eax
movl %eax, 0x28(%rsp)
movslq %eax, %rbp
shlq $0x3, %rbp
movq %rbp, %rdi
callq 0x21f0
movq %rax, 0x38(%rsp)
movq %rbp, %rdi
callq 0x21f0
movq %rax, 0x30(%rsp)
xorl %eax, %eax
testq %rbx, %rbx
jle 0x9934
movq 0x10(%r15), %rcx
movslq (%r14,%rax,4), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x38(%rsp), %rsi
movq %rcx, (%rsi,%rax,8)
movq 0x8(%r15), %rcx
movsd (%rcx,%rdx,8), %xmm0
movq 0x30(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x9904
movq 0x20(%rsp), %rdx
movq %rdx, %rcx
cmpl %r13d, %edx
movq 0x58(%rsp), %r8
jge 0x9979
movq 0x10(%r15), %rdx
movslq (%r14,%rcx,4), %rsi
movq (%rdx,%rsi,8), %rdx
movq 0x38(%rsp), %rdi
movq %rdx, (%rdi,%rax,8)
movq 0x8(%r15), %rdx
movsd (%rdx,%rsi,8), %xmm0
movq 0x30(%rsp), %rdx
movsd %xmm0, (%rdx,%rax,8)
incq %rax
incq %rcx
cmpq %rcx, %r8
jne 0x9946
leaq 0x28(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x5b58
movq %rax, 0x70(%rsp)
movq 0x20(%rsp), %rcx
cmpl %ecx, %ebx
jge 0x99e3
movq %rax, %r13
movq 0x8(%rsp), %rax
leaq (%rax,%rbx,4), %rax
movq %rax, 0x10(%rsp)
subl %ebx, %ecx
xorl %ebx, %ebx
movq 0x10(%r15), %rax
movq %r12, %r14
movq 0x10(%rsp), %rsi
movslq (%rsi,%rbx,4), %r12
movq (%rax,%r12,8), %rsi
movq %r13, %rdi
movq %r15, %rbp
movq %rcx, %r15
callq 0x9a4c
movq %r15, %rcx
movq %rbp, %r15
movsd %xmm0, (%r14,%r12,8)
movq %r14, %r12
incq %rbx
cmpl %ebx, %ecx
jne 0x99ab
movq 0x68(%rsp), %rbp
incq %rbp
leaq 0x70(%rsp), %rdi
callq 0x9a97
movq 0x38(%rsp), %rdi
callq 0x21a0
movq 0x30(%rsp), %rdi
callq 0x21a0
cmpq 0x48(%rsp), %rbp
movq 0x50(%rsp), %r13
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %rdi
jne 0x98aa
callq 0x21a0
movq %r14, %rdi
callq 0x21a0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x24f2
movl %r13d, %ebp
jmp 0x97fb