// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/poly.c
// Function: polylearnvalidation
int polylearnvalidation(char* definitionfile, int maxdegree, int regression)
{
 /*!Last Changed 23.01.2005*/
	char polyst[MAXLENGTH];
 char varname[MAXLENGTH];
 char varvalue[MAXLENGTH], pst[READLINELENGTH];
	double** traindata, **testdata, besterror = +LONG_MAX, beststdev, trainerror, *testerror, *mean, *stdev, bayeserror;
	int i, bestdegree, functioncount, j, traincount, testcount;
	Functionptr fx;
	Mixtureptr mix;
	Polynomptr p;
	if (regression)
		 fx = read_function_definition(definitionfile, &functioncount);
	else
		 mix = read_mixture_from_file(definitionfile);
	testerror = safemalloc(100 * sizeof(double), "polylearnvalidation", 14);
	mean = safemalloc(maxdegree * sizeof(double), "polylearnvalidation", 15);
	stdev = safemalloc(maxdegree * sizeof(double), "polylearnvalidation", 16);
	traincount = 100;
	testcount = 1000;
	if (!regression)
	 {
   testdata = generate_data_from_mixture_model(mix, testcount);
			bayeserror = 0.0;
			for (i = 0; i < testcount; i++)
				 if (mixture_class_at_value(mix, testdata[i][0]) != testdata[i][1])
						 bayeserror++;
			set_default_variable("out2", bayeserror / testcount);
	 }
	for (i = 1; i <= maxdegree; i++)
	 {
		 trainerror = 0;
		 for (j = 0; j < 100; j++)
			 {
				 if (regression)
					 {
						 traindata = functiondatagenerate(traincount, fx, functioncount, 1);
							testdata = functiondatagenerate(testcount, fx, functioncount, 1);
		     p = polyfit(traindata, traincount, i);
					 }
					else
					 {
						 traindata = generate_data_from_mixture_model(mix, traincount);
						 testdata = generate_data_from_mixture_model(mix, testcount);
		     p = polyfitcls(traindata, traincount, i);
					 }
					if (j == 0)
					 {
    			print_polynom(p, polyst);
      	sprintf(varname,"aout3[%d]", i);
	    		set_variable(varname, polyst);
					 }
					if (regression)
					 {
			    trainerror += calculate_mse(traindata, traincount, p);
  	  		testerror[j] = calculate_mse(testdata, testcount, p);
					 }
					else
					 {
						 trainerror += calculate_poly_clserror(traindata, traincount, p);
							testerror[j] = calculate_poly_clserror(testdata, testcount, p);
					 }
			  free_polynom(p);
					free_2d((void**)traindata, traincount);
					free_2d((void**)testdata, testcount);
			 }
  	sprintf(varname,"aout1[%d]", i);
			set_precision(pst, NULL, NULL);
  	sprintf(varvalue, pst, trainerror / traincount);
  	set_variable(varname, varvalue); 
			mean[i - 1] = find_mean_stdev_of_list(testerror, 100, &(stdev[i - 1]));
			if (mean[i - 1] < besterror)
			 {
				 besterror = mean[i - 1];
					beststdev = stdev[i - 1];
			 }
  	sprintf(varname,"aout2[%d]", i);
			set_precision(pst, NULL, NULL);
  	sprintf(varvalue, pst, mean[i - 1]);
  	set_variable(varname, varvalue); 
  	sprintf(varname,"aout4[%d]", i);
			set_precision(pst, NULL, NULL);
  	sprintf(varvalue, pst, stdev[i - 1]);
  	set_variable(varname, varvalue); 
	 }
	bestdegree = 0;
	while (mean[bestdegree] - stdev[bestdegree] > besterror + beststdev)
		 bestdegree++;
	safe_free(mean);
	safe_free(stdev);
 safe_free(testerror);
	if (regression)
	 {
  	for (i = 0;i < functioncount; i++)
	  	 free_function(fx[i]);
	  safe_free(fx);
	 }
	else
		 free_mixture(mix);
	return bestdegree;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8528, %rsp         # imm = 0x1E8528
movl %esi, %r15d
movl %edx, 0x2c(%rsp)
testl %edx, %edx
je 0x543c1
leaq 0x5c(%rsp), %rsi
callq 0x52886
movq %rax, 0x48(%rsp)
jmp 0x543cb
callq 0x3d330
movq %rax, 0x38(%rsp)
leaq 0x469b2(%rip), %rbx     # 0x9ad84
pushq $0xe
popq %rdx
movl $0x320, %edi            # imm = 0x320
movq %rbx, %rsi
callq 0x34f59
movq %rax, 0x50(%rsp)
movslq %r15d, %r14
shlq $0x3, %r14
pushq $0xf
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x34f59
movq %rax, %r12
pushq $0x10
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x34f59
movq %rax, %r13
cmpl $0x0, 0x2c(%rsp)
jne 0x54496
movq 0x38(%rsp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x3d77a
movq %rax, %rbx
xorpd %xmm0, %xmm0
xorl %r14d, %r14d
cmpq $0x3e8, %r14            # imm = 0x3E8
je 0x54482
movq (%rbx,%r14,8), %rax
movapd %xmm0, 0x60(%rsp)
movsd (%rax), %xmm0
movq 0x38(%rsp), %rdi
callq 0x3d571
cvtsi2sd %eax, %xmm0
movq (%rbx,%r14,8), %rax
movapd 0x60(%rsp), %xmm2
movapd %xmm2, %xmm1
addsd 0x410b5(%rip), %xmm1    # 0x95520
cmpneqsd 0x8(%rax), %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
incq %r14
jmp 0x54430
divsd 0x46836(%rip), %xmm0    # 0x9acc0
leaq 0x44acd(%rip), %rdi     # 0x98f5e
callq 0x30597
movl 0x5c(%rsp), %eax
movq %rax, 0x80(%rsp)
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
incl %r15d
movsd 0x41e7a(%rip), %xmm0    # 0x96330
movsd %xmm0, 0x40(%rsp)
pushq $0x1
popq %rax
movq %rax, 0x30(%rsp)
pushq $0x64
popq %r14
movq %r15, 0x98(%rsp)
movq %r12, 0x90(%rsp)
movq %r13, 0x70(%rsp)
cmpq %r15, 0x30(%rsp)
je 0x547dc
xorpd %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
xorl %ebp, %ebp
movsd %xmm1, 0x78(%rsp)
cmpq $0x64, %rbp
je 0x54673
cmpl $0x0, 0x2c(%rsp)
je 0x54558
movl %r14d, %edi
movq 0x48(%rsp), %rbx
movq %rbx, %rsi
movq 0x80(%rsp), %r15
movl %r15d, %edx
pushq $0x1
popq %r13
movl %r13d, %ecx
callq 0x52608
movq %rax, %r12
movl $0x3e8, %edi            # imm = 0x3E8
movq %rbx, %rsi
movl %r15d, %edx
movl %r13d, %ecx
callq 0x52608
movq %rax, %rbx
movq %r12, %rdi
movl %r14d, %esi
movq 0x30(%rsp), %rdx
callq 0x535d2
jmp 0x5458b
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %esi
callq 0x3d77a
movq %rax, %r12
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x3d77a
movq %rax, %rbx
movq %r12, %rdi
movl %r14d, %esi
movq 0x30(%rsp), %rdx
callq 0x537d7
movq %rax, %r15
testq %rbp, %rbp
jne 0x545d5
movq %r15, %rdi
leaq 0x16e400(%rsp), %rsi
callq 0x5324b
leaq 0xa0(%rsp), %r13
movq %r13, %rdi
leaq 0x467e3(%rip), %rsi     # 0x9ad98
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x41b0
movq %r13, %rdi
pushq $0x64
popq %r14
leaq 0x16e400(%rsp), %rsi
callq 0x2f4fc
cmpl $0x0, 0x2c(%rsp)
je 0x54605
movq %r12, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq 0x5345e
movsd %xmm0, 0x88(%rsp)
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %r15, %rdx
callq 0x5345e
jmp 0x5462c
movq %r12, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq 0x5330b
movsd %xmm0, 0x88(%rsp)
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %r15, %rdx
callq 0x5330b
movq 0x50(%rsp), %rax
movsd %xmm0, (%rax,%rbp,8)
movsd 0x60(%rsp), %xmm0
addsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movq %r15, %rdi
callq 0x52a0f
movq %r12, %rdi
movl %r14d, %esi
callq 0x34fc3
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x34fc3
incq %rbp
jmp 0x544fa
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x44ebc(%rip), %rsi     # 0x99541
movq 0x30(%rsp), %r13
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
movl %r14d, %ebp
leaq 0x7a1c0(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x35305
movsd 0x60(%rsp), %xmm0
divsd 0x416ef(%rip), %xmm0    # 0x95da8
leaq 0xf42e0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x41b0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f4fc
movq 0x70(%rsp), %rax
leaq (%rax,%r13,8), %rbx
addq $-0x8, %rbx
movq 0x50(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x3380f
movq 0x90(%rsp), %r12
movsd %xmm0, -0x8(%r12,%r13,8)
movsd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x54720
movsd (%rbx), %xmm1
movsd %xmm1, 0x78(%rsp)
movsd %xmm0, 0x40(%rsp)
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
leaq 0x45c68(%rip), %rsi     # 0x9a39a
movq 0x30(%rsp), %r13
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
leaq 0x7a1c0(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x35305
movsd -0x8(%r12,%r13,8), %xmm0
leaq 0xf42e0(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x41b0
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2f4fc
movq %r15, %rdi
leaq 0x4661c(%rip), %rsi     # 0x9ada2
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x35305
movsd (%rbx), %xmm0
movq %rbp, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x41b0
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2f4fc
incq %r13
movq %r13, 0x30(%rsp)
movq 0x98(%rsp), %r15
movsd 0x78(%rsp), %xmm1
pushq $0x64
popq %r14
movq 0x70(%rsp), %r13
jmp 0x544dd
addsd 0x40(%rsp), %xmm1
xorl %ebx, %ebx
movsd (%r12,%rbx,8), %xmm0
subsd (%r13,%rbx,8), %xmm0
incq %rbx
ucomisd %xmm1, %xmm0
ja 0x547e4
movq %r12, %rdi
callq 0x34ff7
movq %r13, %rdi
callq 0x34ff7
movq 0x50(%rsp), %rdi
callq 0x34ff7
cmpl $0x0, 0x2c(%rsp)
je 0x5486a
xorl %r14d, %r14d
movq 0x80(%rsp), %r15
testl %r15d, %r15d
cmovlel %r14d, %r15d
shlq $0x5, %r15
cmpq %r14, %r15
je 0x5485e
movq 0x48(%rsp), %rax
movupd (%rax,%r14), %xmm0
movupd 0x10(%rax,%r14), %xmm1
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x5295c
addq $0x20, %r14
jmp 0x54831
movq 0x48(%rsp), %rdi
callq 0x34ff7
jmp 0x54874
movq 0x38(%rsp), %rdi
callq 0x3d70c
decl %ebx
movl %ebx, %eax
addq $0x1e8528, %rsp         # imm = 0x1E8528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq