// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8568, %rsp         # imm = 0x1E8568
movl %r8d, %r12d
movl %ecx, 0x2c(%rsp)
movl %edx, %ebp
movl %esi, %ebx
testl %r8d, %r8d
je 0x548c4
leaq 0x54(%rsp), %rsi
callq 0x52886
movq %rax, 0x48(%rsp)
testq %rax, %rax
jne 0x548ce
jmp 0x54ec7
callq 0x3d330
movq %rax, 0x58(%rsp)
movslq %ebp, %rax
movq %rax, 0xb8(%rsp)
leaq (,%rax,8), %r14
leaq 0x464c4(%rip), %r15     # 0x9adac
pushq $0x11
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x34f59
movq %rax, 0x60(%rsp)
pushq $0x12
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x34f59
movq %rax, %r15
testl %r12d, %r12d
movl %ebp, 0x3c(%rsp)
je 0x54974
movl 0x54(%rsp), %eax
movq %rax, 0x40(%rsp)
xorl %r14d, %r14d
testl %ebp, %ebp
movl $0x0, %r13d
cmovgl %ebp, %r13d
pushq $0x1
popq %rbp
cmpq %r14, %r13
je 0x54958
movl 0x2c(%rsp), %edi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl %ebp, %ecx
callq 0x52608
movq 0x60(%rsp), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
jmp 0x54930
pushq $0x1
popq %rcx
movl $0x3e8, %edi            # imm = 0x3E8
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x52608
movq %rax, %r14
jmp 0x549bf
xorl %r14d, %r14d
testl %ebp, %ebp
movl $0x0, %r13d
cmovgl %ebp, %r13d
cmpq %r14, %r13
je 0x549a4
movq 0x58(%rsp), %rdi
movl 0x2c(%rsp), %esi
callq 0x3d77a
movq 0x60(%rsp), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
jmp 0x54983
movq 0x58(%rsp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x3d77a
movq %rax, %r14
movl 0x54(%rsp), %eax
movq %rax, 0x40(%rsp)
movl 0x3c(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xd8(%rsp)
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xb0(%rsp)
movslq %ebx, %rax
testl %eax, %eax
movl $0x0, %ecx
movq %rax, 0xa8(%rsp)
cmovgl %eax, %ecx
incl %ecx
movq %rcx, 0xc0(%rsp)
movl %r13d, %eax
movq %rax, 0x70(%rsp)
movsd 0x4191e(%rip), %xmm0    # 0x96330
pushq $0x1
popq %r13
xorl %eax, %eax
cmpq 0xc0(%rsp), %r13
je 0x54e38
movl %eax, 0x7c(%rsp)
movsd %xmm0, 0xd0(%rsp)
movl $0x3e8, %edi            # imm = 0x3E8
pushq $0x8
popq %rsi
leaq 0x4636a(%rip), %rdx     # 0x9adac
pushq $0x21
popq %rcx
callq 0x35067
movq %rax, %rbx
leaq -0x1(%r13), %rax
imulq 0xb8(%rsp), %rax
addq 0xa8(%rsp), %rax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movq %r13, 0x68(%rsp)
cmpq 0x70(%rsp), %rbp
je 0x54b34
movq 0x60(%rsp), %rax
movq (%rax,%rbp,8), %rdi
testl %r12d, %r12d
je 0x54a95
movl 0x2c(%rsp), %esi
movl %r13d, %edx
callq 0x535d2
jmp 0x54aa1
movl 0x2c(%rsp), %esi
movl %r13d, %edx
callq 0x537d7
movq %rax, (%r15,%rbp,8)
xorl %r13d, %r13d
cmpq $0x3e8, %r13            # imm = 0x3E8
je 0x54ad3
movq (%r15,%rbp,8), %rdi
movq (%r14,%r13,8), %rax
movsd (%rax), %xmm0
callq 0x5298b
addsd (%rbx,%r13,8), %xmm0
movsd %xmm0, (%rbx,%r13,8)
incq %r13
jmp 0x54aa8
movq 0x30(%rsp), %rax
leaq (%rax,%rbp), %r13
cmpq $0xfe, %r13
jg 0x54b27
movq (%r15,%rbp,8), %rdi
leaq 0x7a200(%rsp), %rsi
callq 0x5324b
incl %r13d
leaq 0xe0(%rsp), %rdi
leaq 0x46290(%rip), %rsi     # 0x9ad98
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
leaq 0xe0(%rsp), %rdi
leaq 0x7a200(%rsp), %rsi
callq 0x2f4fc
incq %rbp
movq 0x68(%rsp), %r13
jmp 0x54a6e
movq %r15, %rdi
movl 0x3c(%rsp), %esi
callq 0x5246a
movq %rax, 0xc8(%rsp)
movq %rax, %rdi
leaq 0x7a200(%rsp), %rsi
callq 0x5324b
leaq 0xe0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x4622e(%rip), %rsi     # 0x9ad98
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
movq %rbp, %rdi
leaq 0x7a200(%rsp), %rsi
callq 0x2f4fc
xorl %eax, %eax
cmpq $0x3e8, %rax            # imm = 0x3E8
je 0x54ba6
movsd (%rbx,%rax,8), %xmm0
divsd 0xd8(%rsp), %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %rax
jmp 0x54b86
xorpd %xmm0, %xmm0
xorl %r13d, %r13d
cmpq $0x3e8, %r13            # imm = 0x3E8
je 0x54c6a
movapd %xmm0, 0x80(%rsp)
movq (%r14,%r13,8), %rax
movsd (%rax), %xmm0
testl %r12d, %r12d
je 0x54beb
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x526fc
movsd (%rbx,%r13,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x54c51
cvttsd2si 0x8(%rax), %esi
movq 0x58(%rsp), %rdi
callq 0x3d638
movsd %xmm0, 0x30(%rsp)
movq (%r14,%r13,8), %rax
movsd (%rbx,%r13,8), %xmm0
movsd 0x8(%rax), %xmm1
cmpeqsd 0x41100(%rip), %xmm1    # 0x95d18
movapd %xmm1, 0x90(%rsp)
callq 0x4dd58
movsd 0x408f2(%rip), %xmm1    # 0x95520
subsd %xmm0, %xmm1
movapd 0x90(%rsp), %xmm3
movapd %xmm3, %xmm2
andnpd %xmm0, %xmm2
andpd %xmm3, %xmm1
orpd %xmm2, %xmm1
subsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
movapd 0x80(%rsp), %xmm0
addsd %xmm1, %xmm0
incq %r13
jmp 0x54bad
divsd 0x4604e(%rip), %xmm0    # 0x9acc0
movapd %xmm0, 0x80(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x448b7(%rip), %rsi     # 0x99541
movq 0x68(%rsp), %rdx
xorl %eax, %eax
callq 0x41b0
leaq 0x16e440(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x35305
leaq 0xf4320(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
movapd 0x80(%rsp), %xmm0
movb $0x1, %al
callq 0x41b0
leaq 0xe0(%rsp), %rdi
movq %r13, %rsi
callq 0x2f4fc
xorpd %xmm0, %xmm0
xorl %r13d, %r13d
cmpq 0x70(%rsp), %r13
je 0x54d6a
xorl %ebp, %ebp
cmpq $0x3e8, %rbp            # imm = 0x3E8
je 0x54d62
movsd %xmm0, 0x30(%rsp)
movq (%r15,%r13,8), %rdi
movq (%r14,%rbp,8), %rax
movsd (%rax), %xmm0
callq 0x5298b
testl %r12d, %r12d
je 0x54d2a
subsd (%rbx,%rbp,8), %xmm0
mulsd %xmm0, %xmm0
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
jmp 0x54d5d
callq 0x4dd58
movsd %xmm0, 0x90(%rsp)
movsd (%rbx,%rbp,8), %xmm0
callq 0x4dd58
movsd 0x90(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd 0x30(%rsp), %xmm0
addsd %xmm1, %xmm0
incq %rbp
jmp 0x54cec
incq %r13
jmp 0x54cdf
divsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x45612(%rip), %rsi     # 0x9a39a
movq 0x68(%rsp), %r13
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
leaq 0x16e440(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x35305
leaq 0xf4320(%rsp), %rdi
movq %rbp, %rsi
movsd 0x30(%rsp), %xmm0
movb $0x1, %al
callq 0x41b0
leaq 0xe0(%rsp), %rdi
leaq 0xf4320(%rsp), %rsi
callq 0x2f4fc
movq 0xc8(%rsp), %rdi
callq 0x52a0f
movq %rbx, %rdi
callq 0x34ff7
xorl %ebx, %ebx
cmpq %rbx, 0x70(%rsp)
je 0x54e04
movq (%r15,%rbx,8), %rdi
callq 0x52a0f
incq %rbx
jmp 0x54def
movapd 0x80(%rsp), %xmm1
addsd 0x30(%rsp), %xmm1
movsd 0xd0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movl 0x7c(%rsp), %eax
cmoval %r13d, %eax
minsd %xmm0, %xmm1
incq %r13
movapd %xmm1, %xmm0
jmp 0x54a18
cvtsi2sd %eax, %xmm0
leaq 0x44115(%rip), %rdi     # 0x98f58
callq 0x30597
testl %r12d, %r12d
je 0x54e96
xorl %ebx, %ebx
movq 0x40(%rsp), %r12
testl %r12d, %r12d
cmovlel %ebx, %r12d
shlq $0x5, %r12
cmpq %rbx, %r12
je 0x54e8a
movq 0x48(%rsp), %rax
movupd (%rax,%rbx), %xmm0
movupd 0x10(%rax,%rbx), %xmm1
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x5295c
addq $0x20, %rbx
jmp 0x54e5f
movq 0x48(%rsp), %rdi
callq 0x34ff7
jmp 0x54ea0
movq 0x58(%rsp), %rdi
callq 0x3d70c
movq 0x60(%rsp), %rdi
movl 0x3c(%rsp), %esi
movl 0x2c(%rsp), %edx
callq 0x34ffc
movq %r14, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x34fc3
movq %r15, %rdi
callq 0x34ff7
addq $0x1e8568, %rsp         # imm = 0x1E8568
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)