// File: /workspace/github_c_makefile_star2_O3/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 0x5c858
leaq 0xc(%rsp), %rsi
callq 0x5aa85
movq %rax, 0x18(%rsp)
jmp 0x5c860
callq 0x43774
movq %rax, %r15
leaq 0x4e5ed(%rip), %rbx     # 0xaae54
movl $0x320, %edi            # imm = 0x320
movq %rbx, %rsi
movl $0xe, %edx
callq 0x3a0e1
movq %rax, 0x20(%rsp)
movslq %r12d, %r14
shlq $0x3, %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0xf, %edx
callq 0x3a0e1
movq %rax, 0x48(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x3a0e1
movq %rax, %r13
testl %ebp, %ebp
jne 0x5c935
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x43c28
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 0x439e2
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq (%rbx,%r14,8), %rax
movapd 0x30(%rsp), %xmm2
movapd %xmm2, %xmm1
addsd 0x48c21(%rip), %xmm1    # 0xa5520
cmpneqsd 0x8(%rax), %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
incq %r14
movapd %xmm0, %xmm1
cmpq $0x3e8, %r14            # imm = 0x3E8
jne 0x5c8cc
divsd 0x4e467(%rip), %xmm0    # 0xaad90
leaq 0x4c6be(%rip), %rdi     # 0xa8fee
callq 0x34a17
testl %r12d, %r12d
jle 0x5cc45
movq %r13, 0x40(%rsp)
movq %r15, 0x50(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x2c(%rsp)
incl %r12d
movsd 0x499d5(%rip), %xmm0    # 0xa6330
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 0x5c9cd
movl $0x64, %edi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
movl 0x2c(%rsp), %r15d
movl %r15d, %edx
movl $0x1, %ecx
callq 0x5a6fa
movq %rax, %r14
movl $0x3e8, %edi            # imm = 0x3E8
movq %rbx, %rsi
movl %r15d, %edx
movl $0x1, %ecx
callq 0x5a6fa
movq %rax, %r15
movq %r14, %rdi
movl $0x64, %esi
movq (%rsp), %rdx
callq 0x5b95f
jmp 0x5ca03
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
movl $0x64, %esi
callq 0x43c28
movq %rax, %r14
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x43c28
movq %rax, %r15
movq %r14, %rdi
movl $0x64, %esi
movq (%rsp), %rdx
callq 0x5bb7b
movq %rax, %rbx
testq %r12, %r12
jne 0x5ca4b
movq %rbx, %rdi
movl %ebp, %r13d
leaq 0x16e3d0(%rsp), %rsi
callq 0x5b474
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
leaq 0x4e43b(%rip), %rsi     # 0xaae68
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x41b0
movq %rbp, %rdi
leaq 0x16e3d0(%rsp), %rsi
movl %r13d, %ebp
callq 0x33827
movq %r14, %rdi
movl $0x64, %esi
movq %rbx, %rdx
testl %ebp, %ebp
je 0x5ca77
callq 0x5b74e
movsd %xmm0, 0x58(%rsp)
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdx
callq 0x5b74e
jmp 0x5ca92
callq 0x5b53b
movsd %xmm0, 0x58(%rsp)
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdx
callq 0x5b53b
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 0x5ac26
movq %r14, %rdi
movl $0x64, %esi
callq 0x3a14b
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x3a14b
incq %r12
cmpq $0x64, %r12
jne 0x5c97c
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4cae4(%rip), %rsi     # 0xa95d1
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 0x3a535
movsd 0x30(%rsp), %xmm0
divsd 0x49283(%rip), %xmm0    # 0xa5da0
leaq 0xf42b0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x41b0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x33827
movq 0x40(%rsp), %rax
leaq (%rax,%r15,8), %rbx
addq $-0x8, %rbx
movq 0x20(%rsp), %rdi
movl $0x64, %esi
movq %rbx, %rdx
callq 0x3817e
movq 0x48(%rsp), %r14
movsd %xmm0, -0x8(%r14,%r15,8)
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x5cb84
movsd (%rbx), %xmm1
movsd %xmm1, 0x60(%rsp)
movsd %xmm0, 0x10(%rsp)
leaq 0x70(%rsp), %r12
movq %r12, %rdi
leaq 0x4d8d7(%rip), %rsi     # 0xaa46a
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 0x3a535
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 0x33827
movq %r12, %rdi
leaq 0x4e28c(%rip), %rsi     # 0xaae72
movl %r15d, %edx
xorl %eax, %eax
callq 0x41b0
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3a535
movsd (%rbx), %xmm0
movq %r14, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x41b0
movq %r12, %rdi
movq %r14, %rsi
callq 0x33827
incq %r15
movq 0x68(%rsp), %r12
movq %r15, (%rsp)
cmpq %r12, %r15
jne 0x5c96f
movsd 0x60(%rsp), %xmm1
addsd 0x10(%rsp), %xmm1
movq 0x50(%rsp), %r15
movq 0x40(%rsp), %r13
jmp 0x5cc4d
movsd 0x490c3(%rip), %xmm1    # 0xa5d10
xorl %r14d, %r14d
movq 0x48(%rsp), %rdi
movsd (%rdi,%r14,8), %xmm0
subsd (%r13,%r14,8), %xmm0
incq %r14
ucomisd %xmm1, %xmm0
ja 0x5cc55
callq 0x3a180
movq %r13, %rdi
callq 0x3a180
movq 0x20(%rsp), %rdi
callq 0x3a180
testl %ebp, %ebp
je 0x5ccd8
movslq 0xc(%rsp), %r15
testq %r15, %r15
jle 0x5cccc
xorl %r12d, %r12d
movq %r12, %rax
shlq $0x5, %rax
movq 0x18(%rsp), %rcx
movq (%rcx,%rax), %rbx
testq %rbx, %rbx
je 0x5ccc4
movq 0x28(%rbx), %r13
movq %rbx, %rdi
callq 0x5ac01
movq %rbx, %rdi
callq 0x3a180
movq %r13, %rbx
testq %r13, %r13
jne 0x5cca8
incq %r12
cmpq %r15, %r12
jne 0x5cc93
movq 0x18(%rsp), %rdi
callq 0x3a180
jmp 0x5cce0
movq %r15, %rdi
callq 0x43bba
decl %r14d
movl %r14d, %eax
addq $0x1e84f8, %rsp         # imm = 0x1E84F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq