// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/classification.c
// Function: train_ldaclass
void* train_ldaclass(Instanceptr* trdata, Instanceptr *cvdata, void* parameters)
{
 /*!Last Changed 02.02.2004 added safemalloc*/
 int i, j, *counts, size = data_size(*trdata), howmany;
 Instanceptr classmeans, tmp;
 matrix ccov, temp, temp1, res, cov;
 Model_ldaclassptr m;
 Ldaclass_parameterptr p;
 p = (Ldaclass_parameterptr) parameters;
 m = safemalloc(sizeof(Model_ldaclass), "train_ldaclass", 7);
 m->w0 = (double *)safemalloc(current_dataset->classno * sizeof(double), "ldaclass", 8);
 counts = find_class_counts(*trdata); 
 m->w = (matrix *)safemalloc(current_dataset->classno * sizeof(matrix), "ldaclass", 10);
 classmeans = find_class_means(*trdata, counts);
 tmp = classmeans;
 cov = class_covariance(*trdata, 0, tmp);
 matrix_multiply_constant(&cov, counts[0] - 1);
 for (i = 1;i < current_dataset->classno;i++)
  {
   tmp = tmp->next;
   ccov = class_covariance(*trdata, i, tmp);
   matrix_multiply_constant(&ccov, counts[i] - 1);
   matrix_add(&cov, ccov);
   matrix_free(ccov);
  }
 matrix_divide_constant(&cov, size - current_dataset->classno);
 m->oldfeaturecount = current_dataset->multifeaturecount;
 m->eigenvalues = find_eigenvalues_eigenvectors(&(m->eigenvectors), cov, &howmany, p->variance_explained);
 m->newfeaturecount = howmany + 2;
 dimension_reduce_with_pca(*trdata, m->newfeaturecount - 1, m->eigenvectors);
 current_dataset->multifeaturecount = m->newfeaturecount;
 reduce_dimension_of_covariance_matrix(&cov, howmany, m->eigenvectors);
 if (cov.row == 1)
   cov.values[0][0] = 1.0 / cov.values[0][0];
 else
   matrix_inverse(&cov);
 deallocate(classmeans);
 classmeans = find_class_means(*trdata, counts);
 tmp = classmeans;
 i = 0;
 while (tmp)
  {
   temp = matrix_alloc(1, m->newfeaturecount - 1);
   for (j = 0;j < m->newfeaturecount - 1;j++)
     temp.values[0][j] = real_feature(tmp,j);
   m->w[i] = matrix_multiply(temp, cov);
   temp1 = matrix_transpose(temp);
   res = matrix_multiply(m->w[i], temp1);
   m->w0[i] = -0.5 * res.values[0][0];
   if (counts[i] != 0)
     m->w0[i] += log((counts[i] + 0.0) / size);
   matrix_free(temp1);
   matrix_free(temp);
   matrix_free(res);
   tmp = tmp->next;
   i++;
  }
 matrix_free(cov);
 safe_free(counts);
 deallocate(classmeans);
 current_dataset->multifeaturecount = m->oldfeaturecount;
 return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, 0x30(%rsp)
movq %rdi, %rbp
movq (%rdi), %rdi
callq 0x30309
movl %eax, %r13d
leaq 0x9b9a5(%rip), %rsi     # 0xa5efa
movl $0x30, %edi
movl $0x7, %edx
callq 0x3a0e1
movq %rax, %rbx
leaq 0xcb302(%rip), %r15     # 0xd5870
movq (%r15), %rax
movslq 0x10(%rax), %rdi
shlq $0x3, %rdi
leaq 0x9f826(%rip), %r14     # 0xa9da6
movq %r14, %rsi
movl $0x8, %edx
callq 0x3a0e1
movq %rax, 0x8(%rbx)
movq (%rbp), %rdi
callq 0x115a6
movq %rax, %r12
movq (%r15), %rax
movslq 0x10(%rax), %rdi
shlq $0x4, %rdi
movq %r14, %rsi
movl $0xa, %edx
callq 0x3a0e1
movq %rax, 0x10(%rbx)
movq (%rbp), %rdi
movq %r12, %rsi
callq 0x118da
movq %rbp, 0x8(%rsp)
movq (%rbp), %rdi
xorl %esi, %esi
movq %rax, 0x18(%rsp)
movq %rax, %rdx
callq 0x10d4d
leaq 0x20(%rsp), %rdi
movq %rax, (%rdi)
movq %rdx, 0x8(%rdi)
movl (%r12), %eax
decl %eax
cvtsi2sd %eax, %xmm0
callq 0x3f7c2
movq (%r15), %rax
movl 0x10(%rax), %eax
cmpl $0x2, %eax
jl 0xa68b
movl %r13d, 0x10(%rsp)
movl $0x1, %ebp
movq %r15, %r14
movq 0x18(%rsp), %r15
movq 0x8(%rsp), %r13
movq 0x10(%r15), %r15
movq (%r13), %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x10d4d
movq %rax, 0x38(%rsp)
movq %rdx, 0x40(%rsp)
movl (%r12,%rbp,4), %eax
decl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
leaq 0x38(%rsp), %rdi
callq 0x3f7c2
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
leaq 0x20(%rsp), %rdi
callq 0x3f993
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x3ed51
incq %rbp
movq (%r14), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %rbp
jl 0xa61e
movl 0x10(%rsp), %r13d
movq %r14, %r15
movl %r13d, %ecx
subl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
callq 0x3f7ff
movq (%r15), %rax
movl 0x1c0(%rax), %eax
movl %eax, 0x4(%rbx)
movq %rbx, %rdi
addq $0x18, %rdi
movq 0x30(%rsp), %rax
movsd (%rax), %xmm0
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
leaq 0x64(%rsp), %r14
movq %r14, %rcx
callq 0x42921
movq %rax, 0x28(%rbx)
movl (%r14), %esi
leal 0x2(%rsi), %eax
movl %eax, (%rbx)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
incl %esi
movq 0x18(%rbx), %rdx
movq 0x20(%rbx), %rcx
callq 0x10970
movl (%rbx), %eax
movq (%r15), %rcx
movl %eax, 0x1c0(%rcx)
movl (%r14), %esi
movq 0x18(%rbx), %rdx
movq 0x20(%rbx), %rcx
movq %rbp, %rdi
callq 0x1f3b9
cmpl $0x1, (%rbp)
jne 0xa736
movq 0x28(%rsp), %rax
movq (%rax), %rax
movsd 0x9adf4(%rip), %xmm0    # 0xa5520
divsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0xa740
leaq 0x20(%rsp), %rdi
callq 0x409b7
movq 0x18(%rsp), %rdi
callq 0x2ff55
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %r12, 0x10(%rsp)
movq %r12, %rsi
callq 0x118da
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0xa8af
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x50(%rsp)
xorl %r15d, %r15d
movq 0x48(%rsp), %r12
movq %rbx, 0x58(%rsp)
movl (%rbx), %esi
decl %esi
movl $0x1, %edi
callq 0x3d79d
movq %rax, 0x8(%rsp)
movq %rdx, %rbp
cmpl $0x2, (%rbx)
jl 0xa7c9
xorl %r14d, %r14d
movq %r12, %rdi
movl %r14d, %esi
callq 0x2ed6a
movq (%rbp), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
movslq (%rbx), %rax
decq %rax
cmpq %rax, %r14
jl 0xa7a6
movq 0x10(%rbx), %r14
movq %r15, %r13
shlq $0x4, %r13
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3ee2f
movq %rax, (%r14,%r13)
movq %rdx, 0x8(%r14,%r13)
movq %rbx, %rdi
movq 0x58(%rsp), %rbx
movq %rbp, %rsi
callq 0x3ed5e
movq %rdx, %r14
movq 0x10(%rbx), %rcx
movq (%rcx,%r13), %rdi
movq 0x8(%rcx,%r13), %rsi
movq %rax, 0x18(%rsp)
movq %rax, %rdx
movq %r14, %rcx
callq 0x3ee2f
movq %rax, 0x30(%rsp)
movq %rdx, %r13
movq (%rdx), %rax
movsd (%rax), %xmm0
mulsd 0x9b592(%rip), %xmm0    # 0xa5dd0
movq 0x8(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movq 0x10(%rsp), %rax
movl (%rax,%r15,4), %eax
testl %eax, %eax
je 0xa877
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x50(%rsp), %xmm0
callq 0x4160
movq 0x8(%rbx), %rax
addsd (%rax,%r15,8), %xmm0
movsd %xmm0, (%rax,%r15,8)
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x3ed51
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x3ed51
movq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x3ed51
movq 0x10(%r12), %r12
incq %r15
testq %r12, %r12
jne 0xa788
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x3ed51
movq 0x10(%rsp), %rdi
callq 0x3a180
movq 0x48(%rsp), %rdi
callq 0x2ff55
movl 0x4(%rbx), %eax
leaq 0xcaf94(%rip), %rcx     # 0xd5870
movq (%rcx), %rcx
movl %eax, 0x1c0(%rcx)
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq