// File: /workspace/github_cpp_makefile_star2_O1/qmmlib[P]QmmLibrary/linear.cpp
// Function: cross_validation
void cross_validation(const problem *prob, const parameter *param, int nr_fold, double *target)
{
	int i;
	int *fold_start = Malloc(int,nr_fold+1);
	int l = prob->l;
	int *perm = Malloc(int,l);

	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 $0x58, %rsp
movq %rcx, %r13
movl %edx, %ebp
movq %rsi, 0x48(%rsp)
movq %rdi, %r15
movslq %edx, %rax
leaq 0x4(,%rax,4), %rdi
callq 0x32e0
movq %rax, %r14
movslq (%r15), %rbx
leaq (,%rbx,4), %rdi
callq 0x32e0
testq %rbx, %rbx
jle 0x126aa
xorl %ecx, %ecx
movl %ecx, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %rbx
jne 0x1269f
movq %rbx, 0x10(%rsp)
testl %ebx, %ebx
movq %rax, (%rsp)
jle 0x126e5
xorl %ebx, %ebx
movq 0x10(%rsp), %r12
callq 0x30a0
movq (%rsp), %rsi
cltd
idivl %r12d
movslq %edx, %rax
addq %rbx, %rax
movl (%rsi,%rbx,4), %ecx
movl (%rsi,%rax,4), %edx
movl %edx, (%rsi,%rbx,4)
movl %ecx, (%rsi,%rax,4)
incq %rbx
decq %r12
jne 0x126be
testl %ebp, %ebp
movq 0x10(%rsp), %r12
js 0x12709
leal 0x1(%rbp), %esi
xorl %ecx, %ecx
xorl %edi, %edi
movl %ecx, %eax
cltd
idivl %ebp
movl %eax, (%r14,%rdi,4)
incq %rdi
addl %r12d, %ecx
cmpq %rdi, %rsi
jne 0x126f5
testl %ebp, %ebp
jle 0x1289e
movl %ebp, %eax
movq %rax, 0x38(%rsp)
xorl %ebp, %ebp
movq %r14, 0x40(%rsp)
movslq (%r14,%rbp,4), %rbx
movq %rbp, 0x50(%rsp)
movslq 0x4(%r14,%rbp,4), %rcx
movsd 0x18(%r15), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x4(%r15), %eax
movl %eax, 0x1c(%rsp)
movl %ebx, %eax
movq %rcx, 0x8(%rsp)
subl %ecx, %eax
addl %r12d, %eax
movl %eax, 0x18(%rsp)
movslq %eax, %rbp
shlq $0x3, %rbp
movq %rbp, %rdi
callq 0x32e0
movq %rax, 0x28(%rsp)
movq %rbp, %rdi
callq 0x32e0
movq %rax, 0x20(%rsp)
xorl %eax, %eax
testq %rbx, %rbx
jle 0x127af
movq (%rsp), %r8
movq 0x10(%r15), %rcx
movslq (%r8,%rax,4), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x28(%rsp), %rsi
movq %rcx, (%rsi,%rax,8)
movq 0x8(%r15), %rcx
movsd (%rcx,%rdx,8), %xmm0
movq 0x20(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x1277d
jmp 0x127b3
movq (%rsp), %r8
movq 0x8(%rsp), %r14
movq %r14, %rcx
cmpl %r12d, %r14d
jge 0x127f3
movq 0x10(%r15), %rdx
movslq (%r8,%rcx,4), %rsi
movq (%rdx,%rsi,8), %rdx
movq 0x28(%rsp), %rdi
movq %rdx, (%rdi,%rax,8)
movq 0x8(%r15), %rdx
movsd (%rdx,%rsi,8), %xmm0
movq 0x20(%rsp), %rdx
movsd %xmm0, (%rdx,%rax,8)
incq %rax
incq %rcx
cmpq %rcx, %r12
jne 0x127c0
leaq 0x18(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x79bf
movq %rax, %rcx
cmpl %r14d, %ebx
jge 0x12855
movq (%rsp), %rax
leaq (%rax,%rbx,4), %r14
movq 0x8(%rsp), %rax
subl %ebx, %eax
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
movq 0x10(%r15), %rax
movq %r13, %r12
movslq (%r14,%rbx,4), %r13
movq (%rax,%r13,8), %rsi
movq %r15, %rbp
movq %rcx, %r15
movq %rcx, %rdi
callq 0x128be
movq %r15, %rcx
movq %rbp, %r15
movsd %xmm0, (%r12,%r13,8)
movq %r12, %r13
incq %rbx
cmpl %ebx, 0x8(%rsp)
jne 0x12820
testq %rcx, %rcx
je 0x1286d
movq %rcx, %rbx
movq %rcx, %rdi
callq 0x133e1
movq %rbx, %rdi
callq 0x3280
movq 0x50(%rsp), %rbp
incq %rbp
movq 0x28(%rsp), %rdi
callq 0x3280
movq 0x20(%rsp), %rdi
callq 0x3280
cmpq 0x38(%rsp), %rbp
movq 0x40(%rsp), %r14
movq 0x10(%rsp), %r12
jne 0x1271f
movq %r14, %rdi
callq 0x3280
movq (%rsp), %rdi
callq 0x3280
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq