// File: /workspace/github_c_makefile_star2_O1/zpole[P]IICV/vl/gmm.c
// Function: vl_get_gmm_data_posteriors_f
double
VL_XCAT(vl_get_gmm_data_posteriors_, SFX)
(TYPE * posteriors,
 vl_size numClusters,
 vl_size numData,
 TYPE const * priors,
 TYPE const * means,
 vl_size dimension,
 TYPE const * covariances,
 TYPE const * data)
{
  vl_index i_d, i_cl;
  vl_size dim;
  double LL = 0;

  TYPE halfDimLog2Pi = (dimension / 2.0) * log(2.0*VL_PI);
  TYPE * logCovariances ;
  TYPE * logWeights ;
  TYPE * invCovariances ;

#if (FLT == VL_TYPE_FLOAT)
  VlFloatVector3ComparisonFunction distFn = vl_get_vector_3_comparison_function_f(VlDistanceMahalanobis) ;
#else
  VlDoubleVector3ComparisonFunction distFn = vl_get_vector_3_comparison_function_d(VlDistanceMahalanobis) ;
#endif

  logCovariances = vl_malloc(sizeof(TYPE) * numClusters) ;
  invCovariances = vl_malloc(sizeof(TYPE) * numClusters * dimension) ;
  logWeights = vl_malloc(sizeof(TYPE) * numClusters) ;

#if defined(_OPENMP)
#pragma omp parallel for private(i_cl,dim) num_threads(vl_get_max_threads())
#endif
  for (i_cl = 0 ; i_cl < (signed)numClusters ; ++ i_cl) {
    TYPE logSigma = 0 ;
    if (priors[i_cl] < VL_GMM_MIN_PRIOR) {
      logWeights[i_cl] = - (TYPE) VL_INFINITY_D ;
    } else {
      logWeights[i_cl] = log(priors[i_cl]);
    }
    for(dim = 0 ; dim < dimension ; ++ dim) {
      logSigma += log(covariances[i_cl*dimension + dim]);
      invCovariances [i_cl*dimension + dim] = (TYPE) 1.0 / covariances[i_cl*dimension + dim];
    }
    logCovariances[i_cl] = logSigma;
  } /* end of parallel region */

#if defined(_OPENMP)
#pragma omp parallel for private(i_cl,i_d) reduction(+:LL) \
num_threads(vl_get_max_threads())
#endif
  for (i_d = 0 ; i_d < (signed)numData ; ++ i_d) {
    TYPE clusterPosteriorsSum = 0;
    TYPE maxPosterior = (TYPE)(-VL_INFINITY_D) ;

    for (i_cl = 0 ; i_cl < (signed)numClusters ; ++ i_cl) {
      TYPE p =
      logWeights[i_cl]
      - halfDimLog2Pi
      - 0.5 * logCovariances[i_cl]
      - 0.5 * distFn (dimension,
                      data + i_d * dimension,
                      means + i_cl * dimension,
                      invCovariances + i_cl * dimension) ;
      posteriors[i_cl + i_d * numClusters] = p ;
      if (p > maxPosterior) { maxPosterior = p ; }
    }

    for (i_cl = 0 ; i_cl < (signed)numClusters ; ++i_cl) {
      TYPE p = posteriors[i_cl + i_d * numClusters] ;
      p =  exp(p - maxPosterior) ;
      posteriors[i_cl + i_d * numClusters] = p ;
      clusterPosteriorsSum += p ;
    }

    LL +=  log(clusterPosteriorsSum) + (double) maxPosterior ;

    for (i_cl = 0 ; i_cl < (signed)numClusters ; ++i_cl) {
      posteriors[i_cl + i_d * numClusters] /= clusterPosteriorsSum ;
    }
  } /* end of parallel region */

  vl_free(logCovariances);
  vl_free(logWeights);
  vl_free(invCovariances);

  return LL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, %rbx
movq %r8, 0x58(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %r12
movq %rdi, %r15
movl $0x5, %edi
callq 0x99b0
movq %rax, 0x70(%rsp)
leaq (,%r12,4), %r14
movq %r14, %rdi
callq 0x95b0
movq %rax, %r13
movq %r14, %rdi
imulq %rbx, %rdi
callq 0x95b0
movq %rax, 0x38(%rsp)
movq %r14, 0x68(%rsp)
movq %r14, %rdi
callq 0x95b0
movq %rbx, %rcx
movslq %r12d, %rdx
movq %rdx, 0x50(%rsp)
testq %rdx, %rdx
movq %rbx, 0x30(%rsp)
movq %r13, 0x40(%rsp)
movq %rax, 0x18(%rsp)
jle 0x1196d
movq 0xc0(%rsp), %rbx
leaq (,%rcx,4), %r12
xorl %ebp, %ebp
movq 0x38(%rsp), %r14
movq 0x10(%rsp), %rdx
xorps %xmm0, %xmm0
cvtss2sd (%rdx,%rbp,4), %xmm0
movsd 0x21ae2(%rip), %xmm1    # 0x333b0
ucomisd %xmm0, %xmm1
movss 0x21c36(%rip), %xmm1    # 0x33510
ja 0x118f2
callq 0x9430
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rcx
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movss %xmm1, (%rax,%rbp,4)
xorps %xmm1, %xmm1
testq %rcx, %rcx
je 0x11948
xorl %r13d, %r13d
movss %xmm1, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtss2sd (%rbx,%r13,4), %xmm0
callq 0x9430
movq 0x30(%rsp), %rcx
xorps %xmm1, %xmm1
cvtss2sd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss 0x21a18(%rip), %xmm0    # 0x3334c
divss (%rbx,%r13,4), %xmm0
movss %xmm0, (%r14,%r13,4)
incq %r13
cmpq %r13, %rcx
jne 0x11902
movq 0x40(%rsp), %r13
movss %xmm1, (%r13,%rbp,4)
incq %rbp
addq %r12, %r14
addq %r12, %rbx
cmpq 0x50(%rsp), %rbp
movq 0x18(%rsp), %rax
jne 0x118b9
movslq 0x28(%rsp), %rax
movq %rax, 0x60(%rsp)
testq %rax, %rax
jle 0x11bb2
movq %rcx, %xmm0
punpckldq 0x217f3(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x217fb(%rip), %xmm0    # 0x33190
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x21827(%rip), %xmm1    # 0x331d0
mulsd 0x21b2f(%rip), %xmm1    # 0x334e0
cvtsd2ss %xmm1, %xmm1
leaq (,%rcx,4), %rax
movq %rax, 0x78(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq $0x0, 0x48(%rsp)
movsd 0x2183b(%rip), %xmm2    # 0x33218
movq 0x50(%rsp), %rbx
movss %xmm1, 0x28(%rsp)
movss 0x21b20(%rip), %xmm0    # 0x33510
testq %rbx, %rbx
movq 0x70(%rsp), %r8
movq 0x18(%rsp), %rsi
jle 0x11add
movq %r13, %rdx
movq 0x48(%rsp), %rax
imulq 0x30(%rsp), %rax
movq 0xc8(%rsp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, 0x80(%rsp)
movq 0x58(%rsp), %rbp
movq 0x38(%rsp), %r12
xorl %r13d, %r13d
movss 0x21ad6(%rip), %xmm3    # 0x33510
movss %xmm3, 0x10(%rsp)
movss (%rsi,%r13,4), %xmm0
subss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%rdx,%r13,4), %xmm1
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq 0x30(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq %rdx, %rbx
movq %rbp, %rdx
movq %r12, %rcx
movq %r15, %r14
movq %r8, %r15
callq *%r8
movq 0x18(%rsp), %rsi
movq %r15, %r8
movq %r14, %r15
movq %rbx, %rdx
movq 0x50(%rsp), %rbx
movsd 0x21779(%rip), %xmm2    # 0x33218
movq 0x78(%rsp), %rax
movss 0x28(%rsp), %xmm1
cvtss2sd %xmm0, %xmm0
mulsd %xmm2, %xmm0
addsd 0x8(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%r13,4)
maxss 0x10(%rsp), %xmm0
incq %r13
addq %rax, %r12
addq %rax, %rbp
movaps %xmm0, %xmm3
cmpq %r13, %rbx
jne 0x11a3a
movss %xmm0, 0x10(%rsp)
xorps %xmm1, %xmm1
testq %rbx, %rbx
jle 0x11b2c
xorl %r14d, %r14d
movq 0x40(%rsp), %r13
movss %xmm1, 0x8(%rsp)
movss (%r15,%r14,4), %xmm0
subss 0x10(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x9a60
movss 0x8(%rsp), %xmm1
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r15,%r14,4)
addss %xmm0, %xmm1
incq %r14
cmpq %r14, %rbx
jne 0x11af3
jmp 0x11b31
movq 0x40(%rsp), %r13
movss %xmm1, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
callq 0x9430
movss 0x8(%rsp), %xmm2
testq %rbx, %rbx
jle 0x11b68
xorl %eax, %eax
movss (%r15,%rax,4), %xmm1
divss %xmm2, %xmm1
movss %xmm1, (%r15,%rax,4)
incq %rax
cmpq %rax, %rbx
jne 0x11b50
xorps %xmm1, %xmm1
cvtss2sd 0x10(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movq 0x48(%rsp), %rcx
incq %rcx
addq 0x68(%rsp), %r15
movq %rcx, 0x48(%rsp)
cmpq 0x60(%rsp), %rcx
movss 0x28(%rsp), %xmm1
movsd 0x2166e(%rip), %xmm2    # 0x33218
jne 0x119e8
jmp 0x11bbb
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r13, %rdi
callq 0x9580
movq 0x18(%rsp), %rdi
callq 0x9580
movq 0x38(%rsp), %rdi
callq 0x9580
movsd 0x20(%rsp), %xmm0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq