// File: /workspace/github_c_makefile_star2_O1/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 $0x1e8548, %rsp         # imm = 0x1E8548
movl %ecx, %r12d
movl %edx, %r14d
movq %rsi, 0x50(%rsp)
movl %r8d, 0x18(%rsp)
testl %r8d, %r8d
je 0x5b7e9
leaq 0x1c(%rsp), %rsi
callq 0x594cb
testq %rax, %rax
je 0x5beee
movq %rax, 0x38(%rsp)
jmp 0x5b7f3
callq 0x42408
movq %rax, 0x48(%rsp)
movslq %r14d, %rax
movq %rax, 0x98(%rsp)
leaq (,%rax,8), %rbx
movl %r14d, 0xc(%rsp)
leaq 0x4c5b8(%rip), %r14     # 0xa7dca
movq %rbx, %rdi
movq %r14, %rsi
movl $0x11, %edx
callq 0x390f5
movq %rax, 0x40(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movl 0xc(%rsp), %ebx
movl $0x12, %edx
callq 0x390f5
movq %rax, %r15
movl %ebx, %r13d
cmpl $0x0, 0x18(%rsp)
movq %r13, 0x58(%rsp)
movl %r12d, 0x34(%rsp)
je 0x5b8bb
testl %ebx, %ebx
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %rbp
jle 0x5b893
movl %r12d, %ebx
movl 0x1c(%rsp), %edx
movl %edx, 0x10(%rsp)
xorl %r14d, %r14d
movl %ebx, %edi
movq %rbp, %rsi
movl 0x10(%rsp), %edx
movq %rcx, %r12
movl $0x1, %ecx
callq 0x59176
movq %r12, %rcx
movq %rax, (%r12,%r14,8)
incq %r14
cmpq %r14, %r13
jne 0x5b86e
movl 0x1c(%rsp), %edx
movl $0x3e8, %edi            # imm = 0x3E8
movq %rbp, %rbx
movq %rbp, %rsi
movl $0x1, %ecx
callq 0x59176
movq %rax, %rbp
movl 0xc(%rsp), %r14d
movq 0x48(%rsp), %r12
jmp 0x5b90c
testl %ebx, %ebx
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rbp
jle 0x5b8ef
movl %r12d, %r14d
movq %r13, %r12
xorl %ebx, %ebx
movq %rbp, %rdi
movl %r14d, %esi
movq %rcx, %r13
callq 0x428ae
movq %r13, %rcx
movq %rax, (%r13,%rbx,8)
incq %rbx
cmpq %rbx, %r12
jne 0x5b8d1
movq %rbp, %r12
movq %rbp, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x428ae
movq %rax, %rbp
movq 0x38(%rsp), %rbx
movl 0xc(%rsp), %r14d
movq 0x50(%rsp), %rcx
testl %ecx, %ecx
movq %rbx, 0x38(%rsp)
jle 0x5be52
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0xa8(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x6c(%rsp)
imull $0x3e8, %r14d, %eax     # imm = 0x3E8
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xa0(%rsp)
movl %ecx, %eax
movq %rax, 0x90(%rsp)
incl %ecx
movsd 0x479d1(%rip), %xmm0    # 0xa3330
movl $0x1, %eax
movq %rax, 0x20(%rsp)
xorl %edx, %edx
movq %rcx, 0x50(%rsp)
movl %edx, 0x68(%rsp)
movsd %xmm0, 0xb8(%rsp)
movl $0x3e8, %edi            # imm = 0x3E8
movl $0x8, %esi
leaq 0x4c43c(%rip), %rdx     # 0xa7dca
movl $0x21, %ecx
callq 0x39210
movq %rax, %r12
testl %r14d, %r14d
movq 0x40(%rsp), %rax
jle 0x5bae8
movq 0x20(%rsp), %rcx
decq %rcx
imulq 0x98(%rsp), %rcx
addq 0x90(%rsp), %rcx
movq %rcx, 0x28(%rsp)
xorl %r14d, %r14d
movq (%rax,%r14,8), %rdi
cmpl $0x0, 0x18(%rsp)
je 0x5b9e5
movl 0x34(%rsp), %esi
movq 0x20(%rsp), %rdx
callq 0x5a3f9
jmp 0x5b9f3
movl 0x34(%rsp), %esi
movq 0x20(%rsp), %rdx
callq 0x5a616
movq %rax, (%r15,%r14,8)
xorl %ebx, %ebx
movq (%r15,%r14,8), %rax
movq (%rax), %r13
testq %r13, %r13
je 0x5ba65
movq (%rbp,%rbx,8), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd (%r13), %xmm0
movsd %xmm0, 0x80(%rsp)
movl 0x8(%r13), %edi
movsd 0x60(%rsp), %xmm0
callq 0x38db2
mulsd 0x80(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0x5ba17
jmp 0x5ba69
xorpd %xmm0, %xmm0
addsd (%r12,%rbx,8), %xmm0
movsd %xmm0, (%r12,%rbx,8)
incq %rbx
cmpq $0x3e8, %rbx            # imm = 0x3E8
jne 0x5b9f9
movq 0x28(%rsp), %rax
leaq (%rax,%r14), %rbx
cmpq $0xfe, %rbx
jg 0x5bad5
movq (%r15,%r14,8), %rdi
leaq 0x7a1e0(%rsp), %rsi
callq 0x59f0a
incl %ebx
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
leaq 0x4c2fa(%rip), %rsi     # 0xa7db6
movl %ebx, %edx
xorl %eax, %eax
callq 0x41b0
movq %r13, %rdi
leaq 0x7a1e0(%rsp), %rsi
callq 0x32e47
incq %r14
cmpq 0x58(%rsp), %r14
movq 0x40(%rsp), %rax
jne 0x5b9ca
movq %r15, %rdi
movl 0xc(%rsp), %esi
callq 0x58f81
movq %rax, 0xb0(%rsp)
movq %rax, %rdi
leaq 0x7a1e0(%rsp), %rbx
movq %rbx, %rsi
callq 0x59f0a
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
leaq 0x4c295(%rip), %rsi     # 0xa7db6
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x41b0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x32e47
xorl %eax, %eax
movsd 0xa8(%rsp), %xmm1
movsd (%r12,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%r12,%rax,8)
incq %rax
cmpq $0x3e8, %rax            # imm = 0x3E8
jne 0x5bb43
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
movapd %xmm0, 0x70(%rsp)
movq (%rbp,%rbx,8), %rax
movsd (%rax), %xmm0
cmpl $0x0, 0x18(%rsp)
je 0x5bb94
movq 0x38(%rsp), %rdi
movl 0x6c(%rsp), %esi
callq 0x59299
movsd (%r12,%rbx,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x5bbfb
cvttsd2si 0x8(%rax), %esi
movq 0x48(%rsp), %rdi
callq 0x42753
movsd %xmm0, 0x10(%rsp)
movq (%rbp,%rbx,8), %rax
movsd (%r12,%rbx,8), %xmm0
movsd 0x8(%rax), %xmm1
cmpeqsd 0x47146(%rip), %xmm1    # 0xa2d08
movapd %xmm1, 0x80(%rsp)
callq 0x54568
movsd 0x46948(%rip), %xmm1    # 0xa2520
subsd %xmm0, %xmm1
movapd 0x80(%rsp), %xmm3
movapd %xmm3, %xmm2
andnpd %xmm0, %xmm2
andpd %xmm3, %xmm1
orpd %xmm2, %xmm1
subsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm1
movapd 0x70(%rsp), %xmm0
addsd %xmm1, %xmm0
incq %rbx
cmpq $0x3e8, %rbx            # imm = 0x3E8
jne 0x5bb64
divsd 0x4c0bf(%rip), %xmm0    # 0xa7ce0
movapd %xmm0, 0x70(%rsp)
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4a96a(%rip), %rsi     # 0xa65a3
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x41b0
leaq 0xf4300(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x39529
leaq 0x16e420(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movapd 0x70(%rsp), %xmm0
movb $0x1, %al
callq 0x41b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x32e47
cmpl $0x0, 0xc(%rsp)
jle 0x5bd5f
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
xorl %r14d, %r14d
movsd %xmm0, 0x28(%rsp)
movq (%r15,%rbx,8), %rax
movq (%rax), %r13
testq %r13, %r13
je 0x5bcfa
movq (%rbp,%r14,8), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
xorpd %xmm1, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd (%r13), %xmm0
movsd %xmm0, 0x80(%rsp)
movl 0x8(%r13), %edi
movsd 0x60(%rsp), %xmm0
callq 0x38db2
movsd 0x10(%rsp), %xmm1
mulsd 0x80(%rsp), %xmm0
addsd %xmm0, %xmm1
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0x5bcb8
jmp 0x5bcfe
xorpd %xmm1, %xmm1
cmpl $0x0, 0x18(%rsp)
je 0x5bd0d
subsd (%r12,%r14,8), %xmm1
jmp 0x5bd31
movapd %xmm1, %xmm0
callq 0x54568
movsd %xmm0, 0x10(%rsp)
movsd (%r12,%r14,8), %xmm0
callq 0x54568
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd 0x28(%rsp), %xmm0
addsd %xmm1, %xmm0
incq %r14
cmpq $0x3e8, %r14            # imm = 0x3E8
jne 0x5bc93
incq %rbx
cmpq 0x58(%rsp), %rbx
jne 0x5bc90
jmp 0x5bd63
xorpd %xmm0, %xmm0
divsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4b646(%rip), %rsi     # 0xa73ca
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x41b0
leaq 0xf4300(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x39529
leaq 0x16e420(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x41b0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x32e47
movq 0xb0(%rsp), %rdi
callq 0x59683
movq %r12, %rdi
callq 0x39194
movl 0xc(%rsp), %r14d
testl %r14d, %r14d
movq 0x58(%rsp), %r12
jle 0x5be01
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rdi
callq 0x59683
incq %rbx
cmpq %rbx, %r12
jne 0x5bdf0
movapd 0x70(%rsp), %xmm1
addsd 0x28(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movq 0x20(%rsp), %rax
movl 0x68(%rsp), %edx
cmoval %eax, %edx
minsd %xmm0, %xmm1
incq %rax
movapd %xmm1, %xmm0
movq 0x50(%rsp), %rcx
movq %rax, 0x20(%rsp)
cmpq %rcx, %rax
movq 0x48(%rsp), %r12
jne 0x5b970
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
jmp 0x5be56
xorpd %xmm0, %xmm0
leaq 0x4a15b(%rip), %rdi     # 0xa5fb8
callq 0x3403a
cmpl $0x0, 0x18(%rsp)
je 0x5bec0
movslq 0x1c(%rsp), %r14
testq %r14, %r14
jle 0x5beaf
xorl %r12d, %r12d
movq %r12, %rax
shlq $0x5, %rax
movq 0x38(%rsp), %rcx
movq (%rcx,%rax), %rbx
testq %rbx, %rbx
je 0x5bea7
movq 0x28(%rbx), %r13
movq %rbx, %rdi
callq 0x5965e
movq %rbx, %rdi
callq 0x39194
movq %r13, %rbx
testq %r13, %r13
jne 0x5be8b
incq %r12
cmpq %r14, %r12
jne 0x5be76
movq 0x38(%rsp), %rdi
callq 0x39194
movl 0xc(%rsp), %r14d
jmp 0x5bec8
movq %r12, %rdi
callq 0x42840
movq 0x40(%rsp), %rdi
movl %r14d, %esi
movl 0x34(%rsp), %edx
callq 0x39199
movq %rbp, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x3915f
movq %r15, %rdi
callq 0x39194
addq $0x1e8548, %rsp         # imm = 0x1E8548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq