// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rcx, 0x58(%rsp)
movl %edx, %r14d
movq %rsi, 0x50(%rsp)
movq %rdi, %r15
movl (%rdi), %ebx
movslq %ebx, %r13
leaq (,%r13,4), %rdi
callq 0x21f0
movq %rax, %r12
cmpl %r14d, %r13d
jl 0x8d32
movq %r14, 0x10(%rsp)
movslq %r14d, %rax
movq %rax, 0x18(%rsp)
leaq 0x4(,%rax,4), %rdi
callq 0x21f0
movq %rax, 0x20(%rsp)
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %r14d
movl %ebx, 0xc(%rsp)
cmovgl %ebx, %r14d
cmpq %rax, %r14
je 0x8b58
movl %eax, (%r12,%rax,4)
incq %rax
jmp 0x8b4a
xorl %ebp, %ebp
movl 0xc(%rsp), %ebx
cmpq %rbp, %r14
je 0x8b88
callq 0x2090
cltd
idivl %ebx
movslq %edx, %rax
addq %rbp, %rax
movl (%r12,%rbp,4), %ecx
movl (%r12,%rax,4), %edx
movl %edx, (%r12,%rbp,4)
movl %ecx, (%r12,%rax,4)
incq %rbp
decl %ebx
jmp 0x8b5e
xorl %ecx, %ecx
xorl %esi, %esi
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r9
cmpq %r9, %rsi
jg 0x8bb4
movl %ecx, %eax
cltd
idivl %r8d
movq 0x20(%rsp), %rdx
movl %eax, (%rdx,%rsi,4)
incq %rsi
addl %edi, %ecx
jmp 0x8b9a
xorl %ecx, %ecx
testl %r8d, %r8d
cmovlel %ecx, %r8d
movq %r13, 0x48(%rsp)
movq %r8, 0x10(%rsp)
cmpq %r8, %rcx
je 0x8d11
movq 0x20(%rsp), %rax
movslq (%rax,%rcx,4), %rbp
movslq 0x4(%rax,%rcx,4), %rbx
incq %rcx
movq %rcx, 0x18(%rsp)
movsd 0x18(%r15), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x4(%r15), %eax
movl %eax, 0x2c(%rsp)
movl %ebp, %eax
subl %ebx, %eax
addl %edi, %eax
movl %eax, 0x28(%rsp)
movslq %eax, %r14
shlq $0x3, %r14
movq %r14, %rdi
callq 0x21f0
movq %rax, 0x38(%rsp)
movq %r14, %rdi
callq 0x21f0
movq %rax, 0x30(%rsp)
testl %ebp, %ebp
movl $0x0, %eax
cmovgl %ebp, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x8c63
movq 0x10(%r15), %rdx
movslq (%r12,%rcx,4), %rsi
movq (%rdx,%rsi,8), %rdx
movq 0x38(%rsp), %rdi
movq %rdx, (%rdi,%rcx,8)
movq 0x8(%r15), %rdx
movsd (%rdx,%rsi,8), %xmm0
movq 0x30(%rsp), %rdx
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
jmp 0x8c31
movq %rbx, %rcx
cmpq %r13, %rcx
jge 0x8c9b
movq 0x10(%r15), %rdx
movslq (%r12,%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
jmp 0x8c66
leaq 0x28(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x537c
movq %rax, %r14
movq %rax, 0x60(%rsp)
cmpq %rbx, %rbp
jge 0x8cdb
movq 0x10(%r15), %rax
movslq (%r12,%rbp,4), %r13
movq (%rax,%r13,8), %rsi
movq %r14, %rdi
callq 0x8d56
movq 0x58(%rsp), %rax
movsd %xmm0, (%rax,%r13,8)
incq %rbp
jmp 0x8cb2
leaq 0x60(%rsp), %rdi
callq 0x8da1
movq 0x38(%rsp), %rdi
callq 0x21a0
movq 0x30(%rsp), %rdi
callq 0x21a0
movl 0xc(%rsp), %edi
movq 0x48(%rsp), %r13
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rcx
jmp 0x8bc7
movq 0x20(%rsp), %rdi
callq 0x21a0
movq %r12, %rdi
callq 0x21a0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x82a7(%rip), %rax      # 0x10fe0
movq (%rax), %rcx
leaq 0x42a5(%rip), %rdi      # 0xcfe8
pushq $0x64
popq %rsi
pushq $0x1
popq %rdx
callq 0x22f0
movl %ebx, %r14d
jmp 0x8b19