// File: /workspace/github_c_makefile_star2_O1/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 $0x1e84f8, %rsp         # imm = 0x1E84F8
movl %edx, %ebp
movl %esi, %r12d
testl %edx, %edx
je 0x5b309
leaq 0xc(%rsp), %rsi
callq 0x594cb
movq %rax, 0x18(%rsp)
jmp 0x5b311
callq 0x42408
movq %rax, %r15
leaq 0x4ca8a(%rip), %rbx     # 0xa7da2
movl $0x320, %edi            # imm = 0x320
movq %rbx, %rsi
movl $0xe, %edx
callq 0x390f5
movq %rax, 0x20(%rsp)
movslq %r12d, %r14
shlq $0x3, %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0xf, %edx
callq 0x390f5
movq %rax, 0x48(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x390f5
movq %rax, %r13
testl %ebp, %ebp
jne 0x5b3e6
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x428ae
movq %rax, %rbx
xorpd %xmm1, %xmm1
xorl %r14d, %r14d
movapd %xmm1, 0x30(%rsp)
movq (%rbx,%r14,8), %rax
movsd (%rax), %xmm0
movq %r15, %rdi
callq 0x4267a
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq (%rbx,%r14,8), %rax
movapd 0x30(%rsp), %xmm2
movapd %xmm2, %xmm1
addsd 0x47170(%rip), %xmm1    # 0xa2520
cmpneqsd 0x8(%rax), %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
incq %r14
movapd %xmm0, %xmm1
cmpq $0x3e8, %r14            # imm = 0x3E8
jne 0x5b37d
divsd 0x4c906(%rip), %xmm0    # 0xa7ce0
leaq 0x4abdd(%rip), %rdi     # 0xa5fbe
callq 0x3403a
testl %r12d, %r12d
jle 0x5b6f6
movq %r13, 0x40(%rsp)
movq %r15, 0x50(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x2c(%rsp)
incl %r12d
movsd 0x47f24(%rip), %xmm0    # 0xa3330
movsd %xmm0, 0x10(%rsp)
movl $0x1, %eax
movq %rax, (%rsp)
movq %r12, 0x68(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %r12d, %r12d
testl %ebp, %ebp
je 0x5b47e
movl $0x64, %edi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
movl 0x2c(%rsp), %r15d
movl %r15d, %edx
movl $0x1, %ecx
callq 0x59176
movq %rax, %r14
movl $0x3e8, %edi            # imm = 0x3E8
movq %rbx, %rsi
movl %r15d, %edx
movl $0x1, %ecx
callq 0x59176
movq %rax, %r15
movq %r14, %rdi
movl $0x64, %esi
movq (%rsp), %rdx
callq 0x5a3f9
jmp 0x5b4b4
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
movl $0x64, %esi
callq 0x428ae
movq %rax, %r14
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x428ae
movq %rax, %r15
movq %r14, %rdi
movl $0x64, %esi
movq (%rsp), %rdx
callq 0x5a616
movq %rax, %rbx
testq %r12, %r12
jne 0x5b4fc
movq %rbx, %rdi
movl %ebp, %r13d
leaq 0x16e3d0(%rsp), %rsi
callq 0x59f0a
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
leaq 0x4c8d8(%rip), %rsi     # 0xa7db6
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x41b0
movq %rbp, %rdi
leaq 0x16e3d0(%rsp), %rsi
movl %r13d, %ebp
callq 0x32e47
movq %r14, %rdi
movl $0x64, %esi
movq %rbx, %rdx
testl %ebp, %ebp
je 0x5b528
callq 0x5a1e4
movsd %xmm0, 0x58(%rsp)
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdx
callq 0x5a1e4
jmp 0x5b543
callq 0x59fd1
movsd %xmm0, 0x58(%rsp)
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdx
callq 0x59fd1
movsd 0x30(%rsp), %xmm1
addsd 0x58(%rsp), %xmm1
movsd %xmm1, 0x30(%rsp)
movq 0x20(%rsp), %rax
movsd %xmm0, (%rax,%r12,8)
movq %rbx, %rdi
callq 0x59683
movq %r14, %rdi
movl $0x64, %esi
callq 0x3915f
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x3915f
incq %r12
cmpq $0x64, %r12
jne 0x5b42d
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4b005(%rip), %rsi     # 0xa65a3
movq (%rsp), %r15
movl %r15d, %edx
xorl %eax, %eax
callq 0x41b0
leaq 0x7a190(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x39529
movsd 0x30(%rsp), %xmm0
divsd 0x477d2(%rip), %xmm0    # 0xa2da0
leaq 0xf42b0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x41b0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x32e47
movq 0x40(%rsp), %rax
leaq (%rax,%r15,8), %rbx
addq $-0x8, %rbx
movq 0x20(%rsp), %rdi
movl $0x64, %esi
movq %rbx, %rdx
callq 0x3767c
movq 0x48(%rsp), %r14
movsd %xmm0, -0x8(%r14,%r15,8)
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x5b635
movsd (%rbx), %xmm1
movsd %xmm1, 0x60(%rsp)
movsd %xmm0, 0x10(%rsp)
leaq 0x70(%rsp), %r12
movq %r12, %rdi
leaq 0x4bd86(%rip), %rsi     # 0xa73ca
movq (%rsp), %r15
movl %r15d, %edx
xorl %eax, %eax
callq 0x41b0
leaq 0x7a190(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x39529
movsd -0x8(%r14,%r15,8), %xmm0
leaq 0xf42b0(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x41b0
movq %r12, %rdi
movq %r14, %rsi
callq 0x32e47
movq %r12, %rdi
leaq 0x4c729(%rip), %rsi     # 0xa7dc0
movl %r15d, %edx
xorl %eax, %eax
callq 0x41b0
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x39529
movsd (%rbx), %xmm0
movq %r14, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x41b0
movq %r12, %rdi
movq %r14, %rsi
callq 0x32e47
incq %r15
movq 0x68(%rsp), %r12
movq %r15, (%rsp)
cmpq %r12, %r15
jne 0x5b420
movsd 0x60(%rsp), %xmm1
addsd 0x10(%rsp), %xmm1
movq 0x50(%rsp), %r15
movq 0x40(%rsp), %r13
jmp 0x5b6fe
movsd 0x47612(%rip), %xmm1    # 0xa2d10
xorl %r14d, %r14d
movq 0x48(%rsp), %rdi
movsd (%rdi,%r14,8), %xmm0
subsd (%r13,%r14,8), %xmm0
incq %r14
ucomisd %xmm1, %xmm0
ja 0x5b706
callq 0x39194
movq %r13, %rdi
callq 0x39194
movq 0x20(%rsp), %rdi
callq 0x39194
testl %ebp, %ebp
je 0x5b789
movslq 0xc(%rsp), %r15
testq %r15, %r15
jle 0x5b77d
xorl %r12d, %r12d
movq %r12, %rax
shlq $0x5, %rax
movq 0x18(%rsp), %rcx
movq (%rcx,%rax), %rbx
testq %rbx, %rbx
je 0x5b775
movq 0x28(%rbx), %r13
movq %rbx, %rdi
callq 0x5965e
movq %rbx, %rdi
callq 0x39194
movq %r13, %rbx
testq %r13, %r13
jne 0x5b759
incq %r12
cmpq %r15, %r12
jne 0x5b744
movq 0x18(%rsp), %rdi
callq 0x39194
jmp 0x5b791
movq %r15, %rdi
callq 0x42840
decl %r14d
movl %r14d, %eax
addq $0x1e84f8, %rsp         # imm = 0x1E84F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq