// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x449f0
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl $0x30, %edi
leaq 0xd48b5(%rip), %rsi     # 0xe2b92
movl $0x7, %edx
callq 0x52d70
movq %rax, -0x98(%rbp)
leaq 0xf754b(%rip), %rax     # 0x105840
movq (%rax), %rax
movslq 0x10(%rax), %rdi
shlq $0x3, %rdi
leaq 0xd85e7(%rip), %rsi     # 0xe68ee
movl $0x8, %edx
callq 0x52d70
movq %rax, %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x183a0
movq %rax, -0x28(%rbp)
leaq 0xf750a(%rip), %rax     # 0x105840
movq (%rax), %rax
movslq 0x10(%rax), %rdi
shlq $0x4, %rdi
leaq 0xd85a6(%rip), %rsi     # 0xe68ee
movl $0xa, %edx
callq 0x52d70
movq %rax, %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x188c0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rdx
xorl %esi, %esi
callq 0x17680
movq %rax, -0xb0(%rbp)
movq %rdx, -0xa8(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
leaq -0x90(%rbp), %rdi
callq 0x5a9d0
movl $0x1, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
leaq 0xf745e(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
cmpl 0x10(%rcx), %eax
jge 0xe47b
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl -0x1c(%rbp), %esi
movq -0x40(%rbp), %rdx
callq 0x17680
movq %rax, -0xc0(%rbp)
movq %rdx, -0xb8(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
leaq -0x50(%rbp), %rdi
callq 0x5a9d0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x5ac40
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x59ea0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xe3d8
movl -0x2c(%rbp), %eax
leaq 0xf73bb(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
subl 0x10(%rcx), %eax
cvtsi2sd %eax, %xmm0
leaq -0x90(%rbp), %rdi
callq 0x5aa40
leaq 0xf739e(%rip), %rax     # 0x105840
movq (%rax), %rax
movl 0x1c0(%rax), %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x98(%rbp), %rdi
addq $0x18, %rdi
movq -0xa0(%rbp), %rax
movsd (%rax), %xmm0
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
leaq -0x30(%rbp), %rcx
callq 0x5fba0
movq %rax, %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x28(%rax)
movl -0x30(%rbp), %ecx
addl $0x2, %ecx
movq -0x98(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x98(%rbp), %rax
movl (%rax), %esi
subl $0x1, %esi
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rdx
movq 0x20(%rax), %rcx
callq 0x170d0
movq -0x98(%rbp), %rax
movl (%rax), %ecx
leaq 0xf730a(%rip), %rax     # 0x105840
movq (%rax), %rax
movl %ecx, 0x1c0(%rax)
movl -0x30(%rbp), %esi
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rdx
movq 0x20(%rax), %rcx
leaq -0x90(%rbp), %rdi
callq 0x2c990
cmpl $0x1, -0x90(%rbp)
jne 0xe58c
movq -0x88(%rbp), %rax
movq (%rax), %rax
movsd 0xd3aa0(%rip), %xmm0    # 0xe2018
divsd (%rax), %xmm0
movq -0x88(%rbp), %rax
movq (%rax), %rax
movsd %xmm0, (%rax)
jmp 0xe598
leaq -0x90(%rbp), %rdi
callq 0x5c470
movq -0x38(%rbp), %rdi
callq 0x44510
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x188c0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x1c(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0xe7df
movq -0x98(%rbp), %rax
movl (%rax), %esi
subl $0x1, %esi
movl $0x1, %edi
callq 0x57fc0
movq %rax, -0xd0(%rbp)
movq %rdx, -0xc8(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x98(%rbp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xe64a
movq -0x40(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x42c40
movq -0x58(%rbp), %rax
movq (%rax), %rax
movslq -0x20(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xe610
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x108(%rbp)
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x88(%rbp), %rcx
callq 0x59fe0
movq %rax, %rcx
movq -0x108(%rbp), %rax
movq %rcx, -0xe0(%rbp)
movq %rdx, -0xd8(%rbp)
movq -0xe0(%rbp), %rcx
movq %rcx, (%rax)
movq -0xd8(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x59ed0
movq %rax, -0xf0(%rbp)
movq %rdx, -0xe8(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
movq -0x70(%rbp), %rdx
movq -0x68(%rbp), %rcx
callq 0x59fe0
movq %rax, -0x100(%rbp)
movq %rdx, -0xf8(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq (%rax), %rax
movsd 0xd432b(%rip), %xmm0    # 0xe2a68
mulsd (%rax), %xmm0
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x1c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0xe79e
movq -0x28(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
xorps %xmm1, %xmm1
addsd %xmm1, %xmm0
cvtsi2sdl -0x2c(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x4120
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x1c(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x59ea0
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x59ea0
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x59ea0
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xe5c4
movq -0x90(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x59ea0
movq -0x28(%rbp), %rdi
callq 0x52e80
movq -0x38(%rbp), %rdi
callq 0x44510
movq -0x98(%rbp), %rax
movl 0x4(%rax), %ecx
leaq 0xf702b(%rip), %rax     # 0x105840
movq (%rax), %rax
movl %ecx, 0x1c0(%rax)
movq -0x98(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nop