// File: /workspace/github_c_makefile_star2_O3/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, %r13d
movl %edx, %r14d
movq %rsi, 0x48(%rsp)
movl %r8d, 0x4(%rsp)
testl %r8d, %r8d
je 0x5cd36
leaq 0x24(%rsp), %rsi
callq 0x5aa85
testq %rax, %rax
je 0x5d417
movq %rax, %rcx
jmp 0x5cd40
callq 0x43774
movq %rax, 0x30(%rsp)
movq %rcx, 0x38(%rsp)
movslq %r14d, %rax
movq %rax, 0x90(%rsp)
leaq (,%rax,8), %rbx
movl %r14d, (%rsp)
leaq 0x4e119(%rip), %r14     # 0xaae7c
movq %rbx, %rdi
movq %r14, %rsi
movl $0x11, %edx
callq 0x3a0e1
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movl (%rsp), %ebx
movl $0x12, %edx
callq 0x3a0e1
movq %rax, %r15
movl %ebx, %r12d
cmpl $0x0, 0x4(%rsp)
movq %r12, 0x40(%rsp)
movl %r13d, 0x20(%rsp)
je 0x5cdfe
movl 0x24(%rsp), %edx
testl %ebx, %ebx
movq 0x28(%rsp), %rbx
movq 0x38(%rsp), %rbp
jle 0x5cdde
xorl %r14d, %r14d
movl %r13d, %edi
movq %rbp, %rsi
movl %edx, %r12d
movl $0x1, %ecx
callq 0x5a6fa
movl %r12d, %edx
movq 0x40(%rsp), %r12
movq %rax, (%rbx,%r14,8)
incq %r14
cmpq %r14, %r12
jne 0x5cdb7
movl $0x3e8, %edi            # imm = 0x3E8
movq %rbp, %rsi
movl $0x1, %ecx
callq 0x5a6fa
movq %rax, %rbp
movl (%rsp), %r14d
movq 0x30(%rsp), %r12
jmp 0x5ce4e
testl %ebx, %ebx
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rbp
jle 0x5ce37
movq %r12, %rcx
movl %r13d, %r14d
xorl %ebx, %ebx
movq %rbp, %rdi
movl %r14d, %esi
movq %rax, %r12
movq %rcx, %r13
callq 0x43c28
movq %r13, %rcx
movq %rax, (%r12,%rbx,8)
movq %r12, %rax
incq %rbx
cmpq %rbx, %r13
jne 0x5ce14
movq %rbp, %r12
movq %rbp, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x43c28
movq %rax, %rbp
movl (%rsp), %r14d
movq 0x48(%rsp), %rcx
testl %ecx, %ecx
jle 0x5d37c
cvtsi2sd %r14d, %xmm0
movl 0x24(%rsp), %eax
movl %eax, 0x5c(%rsp)
imull $0x3e8, %r14d, %eax     # imm = 0x3E8
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x98(%rsp)
movl %ecx, %eax
movq %rax, 0x88(%rsp)
incl %ecx
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd %xmm0, 0xb0(%rsp)
movsd 0x49493(%rip), %xmm0    # 0xa6330
movl $0x1, %eax
movq %rax, 0x10(%rsp)
xorl %edx, %edx
movq %rcx, 0x48(%rsp)
movl %edx, 0x58(%rsp)
movsd %xmm0, 0xa8(%rsp)
movl $0x3e8, %edi            # imm = 0x3E8
movl $0x8, %esi
leaq 0x4dfb0(%rip), %rdx     # 0xaae7c
movl $0x21, %ecx
callq 0x3a1fc
movq %rax, %r12
testl %r14d, %r14d
movq 0x28(%rsp), %rax
jle 0x5d020
movq 0x10(%rsp), %rcx
decq %rcx
imulq 0x90(%rsp), %rcx
addq 0x88(%rsp), %rcx
movq %rcx, 0x18(%rsp)
xorl %r14d, %r14d
movq (%rax,%r14,8), %rdi
cmpl $0x0, 0x4(%rsp)
je 0x5cf23
movl 0x20(%rsp), %esi
movq 0x10(%rsp), %rdx
callq 0x5b95f
jmp 0x5cf31
movl 0x20(%rsp), %esi
movq 0x10(%rsp), %rdx
callq 0x5bb7b
movq %rax, (%r15,%r14,8)
xorl %ebx, %ebx
movq (%r15,%r14,8), %rax
movq (%rax), %r13
testq %r13, %r13
je 0x5cf9d
movq (%rbp,%rbx,8), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd (%r13), %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x8(%r13), %edi
movsd 0x50(%rsp), %xmm0
callq 0x39b12
mulsd 0x70(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0x5cf55
jmp 0x5cfa1
xorpd %xmm0, %xmm0
addsd (%r12,%rbx,8), %xmm0
movsd %xmm0, (%r12,%rbx,8)
incq %rbx
cmpq $0x3e8, %rbx            # imm = 0x3E8
jne 0x5cf37
movq 0x18(%rsp), %rax
leaq (%rax,%r14), %rbx
cmpq $0xfe, %rbx
jg 0x5d00d
movq (%r15,%r14,8), %rdi
leaq 0x7a1e0(%rsp), %rsi
callq 0x5b474
incl %ebx
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
leaq 0x4de74(%rip), %rsi     # 0xaae68
movl %ebx, %edx
xorl %eax, %eax
callq 0x41b0
movq %r13, %rdi
leaq 0x7a1e0(%rsp), %rsi
callq 0x33827
incq %r14
cmpq 0x40(%rsp), %r14
movq 0x28(%rsp), %rax
jne 0x5cf08
movq %r15, %rdi
movl (%rsp), %esi
callq 0x5a509
movq %rax, 0xa0(%rsp)
movq %rax, %rdi
leaq 0x7a1e0(%rsp), %rbx
movq %rbx, %rsi
callq 0x5b474
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
leaq 0x4de10(%rip), %rsi     # 0xaae68
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x41b0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x33827
xorl %eax, %eax
movapd 0xb0(%rsp), %xmm1
movupd (%r12,%rax,8), %xmm0
divpd %xmm1, %xmm0
movupd %xmm0, (%r12,%rax,8)
addq $0x2, %rax
cmpq $0x3e8, %rax            # imm = 0x3E8
jne 0x5d07a
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
movapd %xmm0, 0x60(%rsp)
movq (%rbp,%rbx,8), %rax
movsd (%rax), %xmm0
cmpl $0x0, 0x4(%rsp)
je 0x5d0cc
movq 0x38(%rsp), %rdi
movl 0x5c(%rsp), %esi
callq 0x5a853
movsd (%r12,%rbx,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x5d12d
cvttsd2si 0x8(%rax), %esi
movq 0x30(%rsp), %rdi
callq 0x43abe
movsd %xmm0, 0x8(%rsp)
movq (%rbp,%rbx,8), %rax
movsd (%r12,%rbx,8), %xmm0
movsd 0x8(%rax), %xmm1
cmpeqsd 0x48c0e(%rip), %xmm1    # 0xa5d08
movapd %xmm1, 0x70(%rsp)
callq 0x55a80
movsd 0x48413(%rip), %xmm1    # 0xa5520
subsd %xmm0, %xmm1
movapd 0x70(%rsp), %xmm3
movapd %xmm3, %xmm2
andnpd %xmm0, %xmm2
andpd %xmm3, %xmm1
orpd %xmm2, %xmm1
subsd 0x8(%rsp), %xmm1
mulsd %xmm1, %xmm1
movapd 0x60(%rsp), %xmm0
addsd %xmm1, %xmm0
incq %rbx
cmpq $0x3e8, %rbx            # imm = 0x3E8
jne 0x5d09c
divsd 0x4dc3d(%rip), %xmm0    # 0xaad90
movapd %xmm0, 0x60(%rsp)
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4c466(%rip), %rsi     # 0xa95d1
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x41b0
leaq 0xf4300(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3a535
leaq 0x16e420(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movapd 0x60(%rsp), %xmm0
movb $0x1, %al
callq 0x41b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x33827
cmpl $0x0, (%rsp)
jle 0x5d28a
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
xorl %r14d, %r14d
movsd %xmm0, 0x18(%rsp)
movq (%r15,%rbx,8), %rax
movq (%rax), %r13
testq %r13, %r13
je 0x5d225
movq (%rbp,%r14,8), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
xorpd %xmm1, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd (%r13), %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x8(%r13), %edi
movsd 0x50(%rsp), %xmm0
callq 0x39b12
movsd 0x8(%rsp), %xmm1
mulsd 0x70(%rsp), %xmm0
addsd %xmm0, %xmm1
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0x5d1e9
jmp 0x5d229
xorpd %xmm1, %xmm1
cmpl $0x0, 0x4(%rsp)
je 0x5d238
subsd (%r12,%r14,8), %xmm1
jmp 0x5d25c
movapd %xmm1, %xmm0
callq 0x55a80
movsd %xmm0, 0x8(%rsp)
movsd (%r12,%r14,8), %xmm0
callq 0x55a80
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd 0x18(%rsp), %xmm0
addsd %xmm1, %xmm0
incq %r14
cmpq $0x3e8, %r14            # imm = 0x3E8
jne 0x5d1c4
incq %rbx
cmpq 0x40(%rsp), %rbx
jne 0x5d1c1
jmp 0x5d28e
xorpd %xmm0, %xmm0
divsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4d1bb(%rip), %rsi     # 0xaa46a
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x41b0
leaq 0xf4300(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3a535
leaq 0x16e420(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movsd 0x18(%rsp), %xmm0
movb $0x1, %al
callq 0x41b0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x33827
movq 0xa0(%rsp), %rdi
callq 0x5ac26
movq %r12, %rdi
callq 0x3a180
movl (%rsp), %r14d
testl %r14d, %r14d
movq 0x40(%rsp), %r12
jle 0x5d32b
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rdi
callq 0x5ac26
incq %rbx
cmpq %rbx, %r12
jne 0x5d31a
movapd 0x60(%rsp), %xmm1
addsd 0x18(%rsp), %xmm1
movsd 0xa8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movq 0x10(%rsp), %rax
movl 0x58(%rsp), %edx
cmoval %eax, %edx
minsd %xmm0, %xmm1
incq %rax
movapd %xmm1, %xmm0
movq 0x48(%rsp), %rcx
movq %rax, 0x10(%rsp)
cmpq %rcx, %rax
movq 0x30(%rsp), %r12
jne 0x5ceae
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
jmp 0x5d380
xorpd %xmm0, %xmm0
leaq 0x4bc61(%rip), %rdi     # 0xa8fe8
callq 0x34a17
cmpl $0x0, 0x4(%rsp)
je 0x5d3e9
movslq 0x24(%rsp), %r14
testq %r14, %r14
jle 0x5d3d9
xorl %r12d, %r12d
movq %r12, %rax
shlq $0x5, %rax
movq 0x38(%rsp), %rcx
movq (%rcx,%rax), %rbx
testq %rbx, %rbx
je 0x5d3d1
movq 0x28(%rbx), %r13
movq %rbx, %rdi
callq 0x5ac01
movq %rbx, %rdi
callq 0x3a180
movq %r13, %rbx
testq %r13, %r13
jne 0x5d3b5
incq %r12
cmpq %r14, %r12
jne 0x5d3a0
movq 0x38(%rsp), %rdi
callq 0x3a180
movl (%rsp), %r14d
jmp 0x5d3f1
movq %r12, %rdi
callq 0x43bba
movq 0x28(%rsp), %rdi
movl %r14d, %esi
movl 0x20(%rsp), %edx
callq 0x3a185
movq %rbp, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x3a14b
movq %r15, %rdi
callq 0x3a180
addq $0x1e8548, %rsp         # imm = 0x1E8548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)