// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %rdx, 0x18(%rsp)
movq %rdi, %r13
movq (%rdi), %rdi
callq 0x2c3cc
movl %eax, 0x10(%rsp)
leaq 0x8be8c(%rip), %rsi     # 0x95f02
pushq $0x30
popq %rdi
pushq $0x7
popq %rdx
callq 0x34f59
movq %rax, %rbx
leaq 0xc37e5(%rip), %r15     # 0xcd870
movq (%r15), %rax
movslq 0x10(%rax), %rdi
shlq $0x3, %rdi
leaq 0x8fc31(%rip), %r14     # 0x99cce
pushq $0x8
popq %rdx
movq %r14, %rsi
callq 0x34f59
movq %rax, 0x8(%rbx)
movq (%r13), %rdi
callq 0x108c8
movq %rax, %r12
movq (%r15), %rax
movslq 0x10(%rax), %rdi
shlq $0x4, %rdi
pushq $0xa
popq %rdx
movq %r14, %rsi
movq %r13, %r14
callq 0x34f59
movq %rax, 0x10(%rbx)
movq (%r13), %rdi
movq %r12, %rsi
callq 0x10bb9
movq %rax, %r15
movq (%r13), %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x10068
leaq 0x20(%rsp), %r13
movq %rax, (%r13)
movq %rdx, 0x8(%r13)
movq %r12, 0x8(%rsp)
movl (%r12), %eax
decl %eax
cvtsi2sd %eax, %xmm0
movq %r13, %rdi
callq 0x39a42
pushq $0x1
popq %rbp
leaq 0x30(%rsp), %r12
movq %r15, (%rsp)
leaq 0xc3747(%rip), %rax     # 0xcd870
movq (%rax), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %rbp
jge 0xa18c
movq 0x10(%r15), %r15
movq (%r14), %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x10068
movq %rax, 0x30(%rsp)
movq %rdx, 0x38(%rsp)
movq 0x8(%rsp), %rax
movl (%rax,%rbp,4), %eax
decl %eax
cvtsi2sd %eax, %xmm0
movq %r12, %rdi
callq 0x39a42
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r13, %rdi
callq 0x39b81
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x3929f
incq %rbp
jmp 0xa122
movl 0x10(%rsp), %ebp
movl %ebp, %ecx
subl %eax, %ecx
cvtsi2sd %ecx, %xmm0
leaq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x39a85
leaq 0xc36c4(%rip), %r15     # 0xcd870
movq (%r15), %rax
movl 0x1c0(%rax), %eax
movl %eax, 0x4(%rbx)
movq %rbx, %rdi
addq $0x18, %rdi
movq 0x18(%rsp), %rax
movsd (%rax), %xmm0
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %r14, %r13
leaq 0x54(%rsp), %r14
movq %r14, %rcx
callq 0x3c5f6
movq %rax, 0x28(%rbx)
movl (%r14), %esi
leal 0x2(%rsi), %eax
movl %eax, (%rbx)
movq %r13, %r15
movq (%r13), %rdi
incl %esi
movq 0x18(%rbx), %rdx
movq 0x20(%rbx), %rcx
callq 0xfc9a
movl (%rbx), %eax
leaq 0xc3664(%rip), %rcx     # 0xcd870
movq (%rcx), %rcx
movl %eax, 0x1c0(%rcx)
movl (%r14), %esi
movq 0x18(%rbx), %rdx
movq 0x20(%rbx), %rcx
movq %r12, %rdi
callq 0x1cd3e
cmpl $0x1, (%r12)
jne 0xa249
movq 0x28(%rsp), %rax
movq (%rax), %rax
movsd 0x8b2e1(%rip), %xmm0    # 0x95520
divsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0xa253
leaq 0x20(%rsp), %rdi
callq 0x3a9f7
movq (%rsp), %rdi
callq 0x2c010
movq (%r15), %rdi
movq 0x8(%rsp), %rsi
callq 0x10bb9
movq %rax, %r12
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x48(%rsp)
xorl %ebp, %ebp
movq %rax, 0x40(%rsp)
testq %r12, %r12
je 0xa39e
movl (%rbx), %esi
decl %esi
pushq $0x1
popq %rdi
callq 0x380d3
movq %rax, 0x10(%rsp)
movq %rdx, %r13
xorl %r14d, %r14d
movslq (%rbx), %rax
decq %rax
cmpq %rax, %r14
jge 0xa2c2
movq %r12, %rdi
movl %r14d, %esi
callq 0x2aea2
movq (%r13), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
jmp 0xa29d
movq 0x10(%rbx), %rax
movq %rax, (%rsp)
movq %rbp, %r15
shlq $0x4, %r15
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x3933d
movq (%rsp), %rcx
movq %rax, (%rcx,%r15)
movq %rdx, 0x8(%rcx,%r15)
movq %r14, %rdi
movq %r13, %rsi
callq 0x392ac
movq %rdx, %r14
movq 0x10(%rbx), %rcx
movq (%rcx,%r15), %rdi
movq 0x8(%rcx,%r15), %rsi
movq %rax, (%rsp)
movq %rax, %rdx
movq %r14, %rcx
callq 0x3933d
movq %rax, 0x18(%rsp)
movq %rdx, %r15
movq (%rdx), %rax
movsd (%rax), %xmm0
mulsd 0x8ba9f(%rip), %xmm0    # 0x95dd8
movq 0x8(%rbx), %rax
movsd %xmm0, (%rax,%rbp,8)
movq 0x8(%rsp), %rax
movl (%rax,%rbp,4), %eax
testl %eax, %eax
je 0xa36b
cvtsi2sd %eax, %xmm0
divsd 0x48(%rsp), %xmm0
callq 0x4160
movq 0x8(%rbx), %rax
addsd (%rax,%rbp,8), %xmm0
movsd %xmm0, (%rax,%rbp,8)
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x3929f
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x3929f
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x3929f
movq 0x10(%r12), %r12
incq %rbp
jmp 0xa27d
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x3929f
movq 0x8(%rsp), %rdi
callq 0x34ff7
movq 0x40(%rsp), %rdi
callq 0x2c010
movl 0x4(%rbx), %eax
leaq 0xc34a5(%rip), %rcx     # 0xcd870
movq (%rcx), %rcx
movl %eax, 0x1c0(%rcx)
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq