// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rcx, 0x58(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdi, %r12
movq %rdx, %r14
movslq %edx, %rax
movq %rax, 0x10(%rsp)
leaq 0x4(,%rax,4), %rdi
callq 0x3320
movq %rax, 0x18(%rsp)
movl (%r12), %eax
movl %eax, 0xc(%rsp)
movslq %eax, %rbx
leaq (,%rbx,4), %rdi
callq 0x3320
movq %rax, %rbp
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %r15d
movq %rbx, 0x40(%rsp)
cmovgl %ebx, %r15d
cmpq %rax, %r15
je 0x11502
movl %eax, (%rbp,%rax,4)
incq %rax
jmp 0x114f4
xorl %r13d, %r13d
movl 0xc(%rsp), %ebx
cmpq %r13, %r15
je 0x11535
callq 0x30a0
cltd
idivl %ebx
movslq %edx, %rax
addq %r13, %rax
movl (%rbp,%r13,4), %ecx
movl (%rbp,%rax,4), %edx
movl %edx, (%rbp,%r13,4)
movl %ecx, (%rbp,%rax,4)
incq %r13
decl %ebx
jmp 0x11509
xorl %ecx, %ecx
xorl %esi, %esi
movq 0x10(%rsp), %rdi
cmpq %rdi, %rsi
jg 0x1155a
movl %ecx, %eax
cltd
idivl %r14d
movq 0x18(%rsp), %rdx
movl %eax, (%rdx,%rsi,4)
incq %rsi
addl 0xc(%rsp), %ecx
jmp 0x1153e
xorl %ecx, %ecx
testl %r14d, %r14d
cmovlel %ecx, %r14d
movq %r14, 0x50(%rsp)
cmpq %r14, %rcx
je 0x116bd
movq 0x18(%rsp), %rax
movslq (%rax,%rcx,4), %r13
movslq 0x4(%rax,%rcx,4), %rbx
incq %rcx
movq %rcx, 0x10(%rsp)
movsd 0x18(%r12), %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x4(%r12), %eax
movl %eax, 0x24(%rsp)
movl %r13d, %eax
subl %ebx, %eax
addl 0xc(%rsp), %eax
movl %eax, 0x20(%rsp)
movslq %eax, %r15
shlq $0x3, %r15
movq %r15, %rdi
callq 0x3320
movq %rax, 0x30(%rsp)
movq %r15, %rdi
callq 0x3320
movq %rax, 0x28(%rsp)
testl %r13d, %r13d
movl $0x0, %eax
cmovgl %r13d, %eax
xorl %ecx, %ecx
movq 0x40(%rsp), %r8
cmpq %rcx, %rax
je 0x11613
movq 0x10(%r12), %rdx
movslq (%rbp,%rcx,4), %rsi
movq (%rdx,%rsi,8), %rdx
movq 0x30(%rsp), %rdi
movq %rdx, (%rdi,%rcx,8)
movq 0x8(%r12), %rdx
movsd (%rdx,%rsi,8), %xmm0
movq 0x28(%rsp), %rdx
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
jmp 0x115de
movq %rbx, %rcx
cmpq %r8, %rcx
jge 0x1164e
movq 0x10(%r12), %rdx
movslq (%rbp,%rcx,4), %rsi
movq (%rdx,%rsi,8), %rdx
movq 0x30(%rsp), %rdi
movq %rdx, (%rdi,%rax,8)
movq 0x8(%r12), %rdx
movsd (%rdx,%rsi,8), %xmm0
movq 0x28(%rsp), %rdx
movsd %xmm0, (%rdx,%rax,8)
incq %rax
incq %rcx
jmp 0x11616
leaq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x7297
movq %rax, %r15
movq %rax, 0x60(%rsp)
cmpq %rbx, %r13
jge 0x11690
movq 0x10(%r12), %rax
movslq (%rbp,%r13,4), %r14
movq (%rax,%r14,8), %rsi
movq %r15, %rdi
callq 0x116de
movq 0x58(%rsp), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r13
jmp 0x11665
leaq 0x60(%rsp), %rdi
callq 0x1172c
movq 0x30(%rsp), %rdi
callq 0x32b0
movq 0x28(%rsp), %rdi
callq 0x32b0
movq 0x50(%rsp), %r14
movq 0x10(%rsp), %rcx
jmp 0x11568
movq 0x18(%rsp), %rdi
callq 0x32b0
movq %rbp, %rdi
callq 0x32b0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq