// File: /workspace/github_c_makefile_star2_O0/olcaytaner[P]Isell/poly.c
// Function: bias_variance_polynom
void bias_variance_polynom(char* definitionfile, int maxdegree, int M, int N, int regression)
{
 /*!Last Changed 23.01.2005*/
 /*!Last Changed 21.01.2005*/
 char varname[MAXLENGTH];
 char varvalue[MAXLENGTH];
 char polyst[MAXLENGTH], pst[READLINELENGTH];
 int functioncount, i, degree, j, bestdegree = 0, polynomindex;
 Functionptr fx;
 Mixtureptr mix;
 double ***samples, bias, variance, gxt, fxt, besterror = +LONG_MAX, error, **testsample, *ghat, sigmo;
 Polynomptr *g, gmean;
 if (regression)
  {
   fx = read_function_definition(definitionfile, &functioncount);
   if (!fx)
     return;
  }
 else
   mix = read_mixture_from_file(definitionfile);
 samples = safemalloc(M * sizeof(double **), "bias_variance_polynom", 17);
 g = safemalloc(M * sizeof(Polynomptr), "bias_variance_polynom", 18);
 if (regression)
  {
   for (i = 0; i < M; i++)
     samples[i] = functiondatagenerate(N, fx, functioncount, 1);
   testsample = functiondatagenerate(1000, fx, functioncount, 1);
  }
 else
  {
   for (i = 0; i < M; i++)
     samples[i] = generate_data_from_mixture_model(mix, N);
   testsample = generate_data_from_mixture_model(mix, 1000);
  }
 for (degree = 1; degree <= maxdegree; degree++)
  {
   ghat = safecalloc(1000, sizeof(double), "bias_variance_polynom", 33);
   for (i = 0; i < M; i++)
    {
     if (regression)
       g[i] = polyfit(samples[i], N, degree);
     else
       g[i] = polyfitcls(samples[i], N, degree);
     for (j = 0; j < 1000; j++)
       ghat[j] += polynom_value(g[i], testsample[j][0]);
     polynomindex = maxdegree + (degree - 1) * M + i + 1;
     if (polynomindex < 256)
      {
       print_polynom(g[i], polyst);
       sprintf(varname,"aout3[%d]", polynomindex);
       set_variable(varname, polyst); 
      }
    }
   gmean = mean_of_polynoms(g, M);
   print_polynom(gmean, polyst);
   sprintf(varname,"aout3[%d]", degree);
   set_variable(varname, polyst);
   for (j = 0; j < 1000; j++)
     ghat[j] /= M;
   /*Calculate bias*/
   bias = 0; 
   for (j = 0; j < 1000; j++)
     if (regression)
      {
       fxt = function_value(fx, functioncount, testsample[j][0]);
       bias += (ghat[j] - fxt) * (ghat[j] - fxt);
      }
     else
      {
       fxt = mixture_value(mix, testsample[j][0], (int) (testsample[j][1]));
       if (testsample[j][1] == 0)
         sigmo = 1 - sigmoid(ghat[j]);
       else
         sigmo = sigmoid(ghat[j]);
       bias += (sigmo - fxt) * (sigmo - fxt);
      }
   bias /= 1000;
   sprintf(varname,"aout1[%d]", degree);
   set_precision(pst, NULL, NULL);
   sprintf(varvalue, pst, bias);
   set_variable(varname, varvalue); 
   /*Calculate variance*/
   variance = 0;
   for (i = 0; i < M; i++)
     for (j = 0; j < 1000; j++)
      {
       gxt = polynom_value(g[i], testsample[j][0]);
       if (regression)
         variance += (gxt - ghat[j]) * (gxt - ghat[j]);
       else
        {
         sigmo = sigmoid(gxt) - sigmoid(ghat[j]);
         variance += sigmo * sigmo;
        }
      }
   variance /= (M * 1000);
   sprintf(varname,"aout2[%d]", degree);
   set_precision(pst, NULL, NULL);
   sprintf(varvalue, pst, variance);
   set_variable(varname, varvalue); 
   free_polynom(gmean);
   safe_free(ghat);
   for (i = 0; i < M; i++)
     free_polynom(g[i]);
   error = bias + variance;
   if (error < besterror)
    {
     besterror = error;
     bestdegree = degree;
    }
  }
 set_default_variable("out1", bestdegree + 0.0);
 if (regression)
  {
   for (i = 0;i < functioncount; i++)
     free_function(fx[i]);
   safe_free(fx);
  }
 else
   free_mixture(mix);
 free_3d((void***)samples, M, N);
 free_2d((void**)testsample, 1000);
 safe_free(g);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e8550, %rsp         # imm = 0x1E8550
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
movl $0x0, -0x1e84b4(%rbp)
movsd 0x5e8b2(%rip), %xmm0    # 0xe2fb0
movsd %xmm0, -0x1e84f8(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x84734
movq -0x8(%rbp), %rdi
leaq -0x1e84a4(%rbp), %rsi
callq 0x81360
movq %rax, -0x1e84c0(%rbp)
cmpq $0x0, -0x1e84c0(%rbp)
jne 0x84732
jmp 0x85056
jmp 0x84744
movq -0x8(%rbp), %rdi
callq 0x60d90
movq %rax, -0x1e84c8(%rbp)
movslq -0x10(%rbp), %rdi
shlq $0x3, %rdi
leaq 0x63177(%rip), %rsi     # 0xe78ca
movl $0x11, %edx
callq 0x52d70
movq %rax, -0x1e84d0(%rbp)
movslq -0x10(%rbp), %rdi
shlq $0x3, %rdi
leaq 0x63157(%rip), %rsi     # 0xe78ca
movl $0x12, %edx
callq 0x52d70
movq %rax, -0x1e8520(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x84804
movl $0x0, -0x1e84a8(%rbp)
movl -0x1e84a8(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x847df
movl -0x14(%rbp), %edi
movq -0x1e84c0(%rbp), %rsi
movl -0x1e84a4(%rbp), %edx
movl $0x1, %ecx
callq 0x81020
movq %rax, %rdx
movq -0x1e84d0(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x1e84a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84a8(%rbp)
jmp 0x84794
movq -0x1e84c0(%rbp), %rsi
movl -0x1e84a4(%rbp), %edx
movl $0x3e8, %edi            # imm = 0x3E8
movl $0x1, %ecx
callq 0x81020
movq %rax, -0x1e8508(%rbp)
jmp 0x84866
movl $0x0, -0x1e84a8(%rbp)
movl -0x1e84a8(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x8484e
movq -0x1e84c8(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x613e0
movq %rax, %rdx
movq -0x1e84d0(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x1e84a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84a8(%rbp)
jmp 0x8480e
movq -0x1e84c8(%rbp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x613e0
movq %rax, -0x1e8508(%rbp)
movl $0x1, -0x1e84ac(%rbp)
movl -0x1e84ac(%rbp), %eax
cmpl -0xc(%rbp), %eax
jg 0x84f87
movl $0x3e8, %edi            # imm = 0x3E8
movl $0x8, %esi
leaq 0x6303a(%rip), %rdx     # 0xe78ca
movl $0x21, %ecx
callq 0x52f40
movq %rax, -0x1e8510(%rbp)
movl $0x0, -0x1e84a8(%rbp)
movl -0x1e84a8(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x84a27
cmpl $0x0, -0x18(%rbp)
je 0x848f7
movq -0x1e84d0(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl -0x14(%rbp), %esi
movl -0x1e84ac(%rbp), %edx
callq 0x82990
movq %rax, %rdx
movq -0x1e8520(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x8492c
movq -0x1e84d0(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl -0x14(%rbp), %esi
movl -0x1e84ac(%rbp), %edx
callq 0x82ca0
movq %rax, %rdx
movq -0x1e8520(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x0, -0x1e84b0(%rbp)
cmpl $0x3e8, -0x1e84b0(%rbp) # imm = 0x3E8
jge 0x84998
movq -0x1e8520(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x1e8508(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax), %xmm0
callq 0x81500
movq -0x1e8510(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x1e84b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84b0(%rbp)
jmp 0x84936
movl -0xc(%rbp), %eax
movl -0x1e84ac(%rbp), %ecx
subl $0x1, %ecx
imull -0x10(%rbp), %ecx
addl %ecx, %eax
addl -0x1e84a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84b8(%rbp)
cmpl $0x100, -0x1e84b8(%rbp) # imm = 0x100
jge 0x84a11
movq -0x1e8520(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x16e380(%rbp), %rsi
callq 0x82390
leaq -0x7a140(%rbp), %rdi
movl -0x1e84b8(%rbp), %edx
leaq 0x62ebf(%rip), %rsi     # 0xe78b6
movb $0x0, %al
callq 0x4170
leaq -0x7a140(%rbp), %rdi
leaq -0x16e380(%rbp), %rsi
callq 0x493b0
jmp 0x84a13
movl -0x1e84a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84a8(%rbp)
jmp 0x848ab
movq -0x1e8520(%rbp), %rdi
movl -0x10(%rbp), %esi
callq 0x80c70
movq %rax, -0x1e8528(%rbp)
movq -0x1e8528(%rbp), %rdi
leaq -0x16e380(%rbp), %rsi
callq 0x82390
leaq -0x7a140(%rbp), %rdi
movl -0x1e84ac(%rbp), %edx
leaq 0x62e52(%rip), %rsi     # 0xe78b6
movb $0x0, %al
callq 0x4170
leaq -0x7a140(%rbp), %rdi
leaq -0x16e380(%rbp), %rsi
callq 0x493b0
movl $0x0, -0x1e84b0(%rbp)
cmpl $0x3e8, -0x1e84b0(%rbp) # imm = 0x3E8
jge 0x84ac6
cvtsi2sdl -0x10(%rbp), %xmm1
movq -0x1e8510(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x1e84b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84b0(%rbp)
jmp 0x84a88
xorps %xmm0, %xmm0
movsd %xmm0, -0x1e84d8(%rbp)
movl $0x0, -0x1e84b0(%rbp)
cmpl $0x3e8, -0x1e84b0(%rbp) # imm = 0x3E8
jge 0x84c7a
cmpl $0x0, -0x18(%rbp)
je 0x84b78
movq -0x1e84c0(%rbp), %rdi
movl -0x1e84a4(%rbp), %esi
movq -0x1e8508(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax), %xmm0
callq 0x81130
movsd %xmm0, -0x1e84f0(%rbp)
movq -0x1e8510(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd -0x1e84f0(%rbp), %xmm0
movq -0x1e8510(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
subsd -0x1e84f0(%rbp), %xmm2
movsd -0x1e84d8(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x1e84d8(%rbp)
jmp 0x84c64
movq -0x1e84c8(%rbp), %rdi
movq -0x1e8508(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax), %xmm0
movq -0x1e8508(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cvttsd2si 0x8(%rax), %esi
callq 0x61210
movsd %xmm0, -0x1e84f0(%rbp)
movq -0x1e8508(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x84c0c
jp 0x84c0c
movq -0x1e8510(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x7aab0
movaps %xmm0, %xmm1
movsd 0x5d41a(%rip), %xmm0    # 0xe2018
subsd %xmm1, %xmm0
movsd %xmm0, -0x1e8518(%rbp)
jmp 0x84c2c
movq -0x1e8510(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x7aab0
movsd %xmm0, -0x1e8518(%rbp)
movsd -0x1e8518(%rbp), %xmm0
subsd -0x1e84f0(%rbp), %xmm0
movsd -0x1e8518(%rbp), %xmm2
subsd -0x1e84f0(%rbp), %xmm2
movsd -0x1e84d8(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x1e84d8(%rbp)
jmp 0x84c66
movl -0x1e84b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84b0(%rbp)
jmp 0x84adb
movsd -0x1e84d8(%rbp), %xmm0
movsd 0x62b46(%rip), %xmm1    # 0xe77d0
divsd %xmm1, %xmm0
movsd %xmm0, -0x1e84d8(%rbp)
leaq -0x7a140(%rbp), %rdi
movl -0x1e84ac(%rbp), %edx
leaq 0x614c9(%rip), %rsi     # 0xe6173
movb $0x0, %al
callq 0x4170
leaq -0x1e84a0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x53310
leaq -0xf4260(%rbp), %rdi
leaq -0x1e84a0(%rbp), %rsi
movsd -0x1e84d8(%rbp), %xmm0
movb $0x1, %al
callq 0x4170
leaq -0x7a140(%rbp), %rdi
leaq -0xf4260(%rbp), %rsi
callq 0x493b0
xorps %xmm0, %xmm0
movsd %xmm0, -0x1e84e0(%rbp)
movl $0x0, -0x1e84a8(%rbp)
movl -0x1e84a8(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x84e55
movl $0x0, -0x1e84b0(%rbp)
cmpl $0x3e8, -0x1e84b0(%rbp) # imm = 0x3E8
jge 0x84e3f
movq -0x1e8520(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x1e8508(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax), %xmm0
callq 0x81500
movsd %xmm0, -0x1e84e8(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x84dbd
movsd -0x1e84e8(%rbp), %xmm0
movq -0x1e8510(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd -0x1e84e8(%rbp), %xmm2
movq -0x1e8510(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm2
movsd -0x1e84e0(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x1e84e0(%rbp)
jmp 0x84e29
movsd -0x1e84e8(%rbp), %xmm0
callq 0x7aab0
movsd %xmm0, -0x1e8530(%rbp)
movq -0x1e8510(%rbp), %rax
movslq -0x1e84b0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x7aab0
movaps %xmm0, %xmm1
movsd -0x1e8530(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x1e8518(%rbp)
movsd -0x1e8518(%rbp), %xmm0
movsd -0x1e8518(%rbp), %xmm2
movsd -0x1e84e0(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x1e84e0(%rbp)
jmp 0x84e2b
movl -0x1e84b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84b0(%rbp)
jmp 0x84d22
jmp 0x84e41
movl -0x1e84a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84a8(%rbp)
jmp 0x84d09
imull $0x3e8, -0x10(%rbp), %eax # imm = 0x3E8
cvtsi2sd %eax, %xmm1
movsd -0x1e84e0(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x1e84e0(%rbp)
leaq -0x7a140(%rbp), %rdi
movl -0x1e84ac(%rbp), %edx
leaq 0x62102(%rip), %rsi     # 0xe6f8a
movb $0x0, %al
callq 0x4170
leaq -0x1e84a0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x53310
leaq -0xf4260(%rbp), %rdi
leaq -0x1e84a0(%rbp), %rsi
movsd -0x1e84e0(%rbp), %xmm0
movb $0x1, %al
callq 0x4170
leaq -0x7a140(%rbp), %rdi
leaq -0xf4260(%rbp), %rsi
callq 0x493b0
movq -0x1e8528(%rbp), %rdi
callq 0x815c0
movq -0x1e8510(%rbp), %rdi
callq 0x52e80
movl $0x0, -0x1e84a8(%rbp)
movl -0x1e84a8(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x84f27
movq -0x1e8520(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x815c0
movl -0x1e84a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84a8(%rbp)
jmp 0x84ef4
movsd -0x1e84d8(%rbp), %xmm0
addsd -0x1e84e0(%rbp), %xmm0
movsd %xmm0, -0x1e8500(%rbp)
movsd -0x1e8500(%rbp), %xmm1
movsd -0x1e84f8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x84f71
movsd -0x1e8500(%rbp), %xmm0
movsd %xmm0, -0x1e84f8(%rbp)
movl -0x1e84ac(%rbp), %eax
movl %eax, -0x1e84b4(%rbp)
jmp 0x84f73
movl -0x1e84ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84ac(%rbp)
jmp 0x84870
cvtsi2sdl -0x1e84b4(%rbp), %xmm0
xorps %xmm1, %xmm1
addsd %xmm1, %xmm0
leaq 0x60beb(%rip), %rdi     # 0xe5b88
callq 0x4b070
cmpl $0x0, -0x18(%rbp)
je 0x8501b
movl $0x0, -0x1e84a8(%rbp)
movl -0x1e84a8(%rbp), %eax
cmpl -0x1e84a4(%rbp), %eax
jge 0x8500d
movq -0x1e84c0(%rbp), %rax
movslq -0x1e84a8(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x814b0
movl -0x1e84a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e84a8(%rbp)
jmp 0x84fb2
movq -0x1e84c0(%rbp), %rdi
callq 0x52e80
jmp 0x85027
movq -0x1e84c8(%rbp), %rdi
callq 0x61330
movq -0x1e84d0(%rbp), %rdi
movl -0x10(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x52ea0
movq -0x1e8508(%rbp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x52e20
movq -0x1e8520(%rbp), %rdi
callq 0x52e80
addq $0x1e8550, %rsp         # imm = 0x1E8550
popq %rbp
retq
nop