// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/classification.c
// Function: train_naivebayes
void* train_naivebayes(Instanceptr* trdata, Instanceptr *cvdata, void* parameters)
{
 /*!Last Changed 22.01.2008 changed from variance to standard deviation*/
 /*!Last Changed 11.08.2007*/
 int i, j, k, *counts, size = data_size(*trdata);
 int ***feature_counts;
 Instanceptr tmp, classmeans;
 Model_naivebayesptr m;
 Instance s;
 m = safemalloc(sizeof(Model_naivebayes), "train_naivebayes", 5);
 counts = find_class_counts(*trdata); 
 classmeans = find_class_means(*trdata, counts);
 s = find_variance(*trdata);
 feature_counts = find_feature_counts(*trdata);
 m->s = (double *) safemalloc(current_dataset->featurecount * sizeof(double), "train_naivebayes", 9);
 m->m = (double **) safemalloc_2d(sizeof(double *), sizeof(double), current_dataset->classno, current_dataset->featurecount, "train_naivebayes", 10);
 m->priors = (double *)safemalloc(current_dataset->classno * sizeof(double), "train_naivebayes", 11);
 for (i = 0; i < current_dataset->featurecount; i++)
   m->s[i] = sqrt(s.values[i].floatvalue);
 tmp = classmeans;
 m->p = (double ***) safemalloc(current_dataset->classno * sizeof(double **), "train_naivebayes", 15);
 for (i = 0; i < current_dataset->classno; i++)
  {
   m->p[i] = (double **) safemalloc(current_dataset->featurecount * sizeof(double *), "train_naivebayes", 18);
   m->priors[i] = counts[i] / (size + 0.0);
   for (j = 0; j < current_dataset->featurecount; j++)
    {
     if (current_dataset->features[j].type == STRING)
      {
       m->p[i][j] = (double *) safemalloc(current_dataset->features[j].valuecount * sizeof(double), "train_naivebayes", 24);
       for (k = 0; k < current_dataset->features[j].valuecount; k++)
         m->p[i][j][k] = feature_counts[i][j][k] / (counts[i] + 0.0);
      }
     m->m[i][j] = tmp->values[j].floatvalue;
    }
   tmp = tmp->next;
  }
 free_feature_counts(feature_counts);
 safe_free(s.values);
 safe_free(counts);
 deallocate(classmeans);
 return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r13
movq (%rdi), %rdi
callq 0x2fa14
movl %eax, (%rsp)
leaq 0x993af(%rip), %rbp     # 0xa2eda
movl $0x20, %edi
movq %rbp, %rsi
movl $0x5, %edx
callq 0x390f5
movq %rax, %rbx
movq (%r13), %rdi
callq 0x115e1
movq (%r13), %rdi
movq %rax, 0x8(%rsp)
movq %rax, %rsi
callq 0x11918
movq %rax, 0x10(%rsp)
movq (%r13), %rsi
leaq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x1286d
movq (%r12), %r12
movq (%r13), %rdi
callq 0x1189d
movq %rax, 0x18(%rsp)
leaq 0xc8ce7(%rip), %r15     # 0xd2870
movq (%r15), %rax
movslq 0x1bc(%rax), %rdi
shlq $0x3, %rdi
movq %rbp, %rsi
movl $0x9, %edx
callq 0x390f5
movq %rax, 0x8(%rbx)
movq (%r15), %rax
movl 0x10(%rax), %edx
movl 0x1bc(%rax), %ecx
movl $0x8, %edi
movl $0x8, %esi
movq %rbp, %r8
movl $0xa, %r9d
callq 0x393b5
movq %rax, (%rbx)
movq (%r15), %rax
movslq 0x10(%rax), %rdi
shlq $0x3, %rdi
movq %rbp, %rsi
movl $0xb, %edx
callq 0x390f5
movq %rax, 0x10(%rbx)
movq (%r15), %rax
cmpl $0x0, 0x1bc(%rax)
jle 0x9c35
xorl %r14d, %r14d
xorpd %xmm1, %xmm1
movsd (%r12,%r14,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x9c10
sqrtsd %xmm0, %xmm0
jmp 0x9c19
callq 0x4300
xorpd %xmm1, %xmm1
movq 0x8(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
movq (%r15), %rax
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %r14
jl 0x9bfe
movq %r12, 0x20(%rsp)
movslq 0x10(%rax), %rdi
shlq $0x3, %rdi
leaq 0x99291(%rip), %rsi     # 0xa2eda
movl $0xf, %edx
callq 0x390f5
movq %rax, 0x18(%rbx)
movq (%r15), %rax
cmpl $0x0, 0x10(%rax)
jle 0x9da6
xorps %xmm0, %xmm0
cvtsi2sdl (%rsp), %xmm0
movsd %xmm0, (%rsp)
xorl %r12d, %r12d
movq 0x10(%rsp), %r13
movslq 0x1bc(%rax), %rdi
shlq $0x3, %rdi
leaq 0x9924f(%rip), %rsi     # 0xa2eda
movl $0x12, %edx
callq 0x390f5
movq 0x18(%rbx), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x8(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r12,4), %xmm0
divsd (%rsp), %xmm0
movq 0x10(%rbx), %rax
movsd %xmm0, (%rax,%r12,8)
movq (%r15), %rax
cmpl $0x0, 0x1bc(%rax)
jle 0x9d8f
xorl %r14d, %r14d
imulq $0x340, %r14, %rbp      # imm = 0x340
cmpl $0x1, 0x30f08(%rax,%rbp)
jne 0x9d62
addq %rbp, %rax
addq $0x30f08, %rax          # imm = 0x30F08
movslq 0x18(%rax), %rdi
shlq $0x3, %rdi
leaq 0x991e0(%rip), %rsi     # 0xa2eda
movl $0x18, %edx
callq 0x390f5
movq 0x18(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
movq %rax, (%rcx,%r14,8)
movq (%r15), %rax
movslq 0x30f20(%rax,%rbp), %rax
testq %rax, %rax
jle 0x9d62
movq 0x18(%rsp), %rcx
movq (%rcx,%r12,8), %rcx
movq (%rcx,%r14,8), %rcx
movq 0x8(%rsp), %rdx
xorps %xmm0, %xmm0
cvtsi2sdl (%rdx,%r12,4), %xmm0
movq 0x18(%rbx), %rdx
movq (%rdx,%r12,8), %rdx
movq (%rdx,%r14,8), %rdx
xorl %esi, %esi
xorps %xmm1, %xmm1
cvtsi2sdl (%rcx,%rsi,4), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rsi,8)
incq %rsi
cmpq %rsi, %rax
jne 0x9d49
movq (%r13), %rax
movsd (%rax,%r14,8), %xmm0
movq (%rbx), %rax
movq (%rax,%r12,8), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
movq (%r15), %rax
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %r14
jl 0x9ccd
movq 0x10(%r13), %r13
incq %r12
movq (%r15), %rax
movslq 0x10(%rax), %rcx
cmpq %rcx, %r12
jl 0x9c79
movq 0x18(%rsp), %rdi
callq 0x11816
movq 0x20(%rsp), %rdi
callq 0x39194
movq 0x8(%rsp), %rdi
callq 0x39194
movq 0x10(%rsp), %rdi
callq 0x2f616
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq