// File: /workspace/github_c_makefile_star2_O3/zpole[P]IICV/vl/gmm.c
// Function: vl_get_gmm_data_posteriors_d
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, 0x60(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, 0x50(%rsp)
movq %rsi, %r12
movq %rdi, %r15
movl $0x5, %edi
callq 0x99d0
movq %rax, 0x78(%rsp)
leaq (,%r12,8), %r14
movq %r14, %rdi
callq 0x95b0
movq %rax, %r13
movq %r14, %rdi
imulq %rbx, %rdi
callq 0x95b0
movq %rax, 0x40(%rsp)
movq %r14, 0x70(%rsp)
movq %r14, %rdi
callq 0x95b0
movq %rbx, %rcx
movslq %r12d, %rdx
movq %rdx, 0x8(%rsp)
testq %rdx, %rdx
movq %rbx, 0x18(%rsp)
movq %r13, 0x38(%rsp)
movq %rax, 0x28(%rsp)
jle 0x11dff
movq 0xc0(%rsp), %rbx
leaq (,%rcx,8), %r12
xorl %ebp, %ebp
movq 0x40(%rsp), %r14
movq 0x30(%rsp), %rdx
movsd (%rdx,%rbp,8), %xmm1
movsd 0x20660(%rip), %xmm0    # 0x323c0
ucomisd %xmm1, %xmm0
movsd 0x2077c(%rip), %xmm0    # 0x324e8
ja 0x11d81
movapd %xmm1, %xmm0
callq 0x9430
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rcx
movsd %xmm0, (%rax,%rbp,8)
xorpd %xmm0, %xmm0
testq %rcx, %rcx
je 0x11dda
xorl %r13d, %r13d
movsd %xmm0, 0x10(%rsp)
movsd (%rbx,%r13,8), %xmm0
callq 0x9430
movq 0x18(%rsp), %rcx
movsd 0x204c0(%rip), %xmm1    # 0x32270
divsd (%rbx,%r13,8), %xmm1
movsd 0x10(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
movsd %xmm1, (%r14,%r13,8)
incq %r13
cmpq %r13, %rcx
jne 0x11d92
movq 0x38(%rsp), %r13
movsd %xmm0, (%r13,%rbp,8)
incq %rbp
addq %r12, %r14
addq %r12, %rbx
cmpq 0x8(%rsp), %rbp
movq 0x28(%rsp), %rax
jne 0x11d4e
movslq 0x50(%rsp), %rax
movq %rax, 0x68(%rsp)
testq %rax, %rax
jle 0x1205b
movq %rcx, %xmm0
punpckldq 0x20361(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x20369(%rip), %xmm0    # 0x32190
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x203a5(%rip), %xmm1    # 0x321e0
mulsd 0x2069d(%rip), %xmm1    # 0x324e0
leaq (,%rcx,8), %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %r12d, %r12d
movsd 0x203c8(%rip), %xmm2    # 0x32228
movq 0x8(%rsp), %rbx
movapd %xmm1, 0x50(%rsp)
movsd 0x20675(%rip), %xmm0    # 0x324e8
testq %rbx, %rbx
jle 0x11ff0
movq %r12, 0x48(%rsp)
movq %r12, %rax
imulq %rcx, %rax
movq 0xc8(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x80(%rsp)
movq 0x60(%rsp), %rbp
movq 0x40(%rsp), %r12
movq %r15, 0x30(%rsp)
xorl %r13d, %r13d
movsd 0x20632(%rip), %xmm0    # 0x324e8
movq 0x28(%rsp), %r15
movsd %xmm0, 0x10(%rsp)
movsd (%r15,%r13,8), %xmm0
subsd %xmm1, %xmm0
movq 0x38(%rsp), %rax
movsd (%rax,%r13,8), %xmm1
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq 0x18(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq *0x78(%rsp)
movsd 0x20325(%rip), %xmm2    # 0x32228
movapd 0x50(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd 0x8(%rsp), %xmm0
movq 0x30(%rsp), %rax
movsd %xmm0, (%rax,%r13,8)
maxsd 0x10(%rsp), %xmm0
incq %r13
addq %r14, %r12
addq %r14, %rbp
cmpq %r13, %rbx
jne 0x11ebb
movq %r14, %r12
movq 0x30(%rsp), %r15
testq %rbx, %rbx
jle 0x11fe3
xorpd %xmm1, %xmm1
xorl %r14d, %r14d
movq 0x38(%rsp), %r13
movsd %xmm0, 0x8(%rsp)
movsd %xmm1, 0x10(%rsp)
movsd (%r15,%r14,8), %xmm0
subsd 0x8(%rsp), %xmm0
callq 0x9a60
movsd 0x10(%rsp), %xmm1
movsd %xmm0, (%r15,%r14,8)
addsd %xmm0, %xmm1
incq %r14
cmpq %r14, %rbx
jne 0x11f55
movsd %xmm1, 0x10(%rsp)
movapd %xmm1, %xmm0
callq 0x9430
movsd 0x8(%rsp), %xmm3
addsd %xmm0, %xmm3
testq %rbx, %rbx
movq %r12, %r14
jle 0x12041
xorl %eax, %eax
movq 0x18(%rsp), %rcx
movapd 0x50(%rsp), %xmm1
movq 0x48(%rsp), %r12
movsd 0x20265(%rip), %xmm2    # 0x32228
movsd 0x10(%rsp), %xmm4
movsd (%r15,%rax,8), %xmm0
divsd %xmm4, %xmm0
movsd %xmm0, (%r15,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x11fc9
jmp 0x1201c
movq 0x38(%rsp), %r13
movq %r12, %r14
movq 0x48(%rsp), %r12
movsd %xmm0, 0x8(%rsp)
xorpd %xmm0, %xmm0
callq 0x9430
movsd 0x8(%rsp), %xmm3
movsd 0x2021b(%rip), %xmm2    # 0x32228
movapd 0x50(%rsp), %xmm1
movq 0x18(%rsp), %rcx
addsd %xmm0, %xmm3
movsd 0x20(%rsp), %xmm0
addsd %xmm3, %xmm0
movsd %xmm0, 0x20(%rsp)
incq %r12
addq 0x70(%rsp), %r15
cmpq 0x68(%rsp), %r12
jne 0x11e6b
jmp 0x12065
movq 0x18(%rsp), %rcx
movapd 0x50(%rsp), %xmm1
movq 0x48(%rsp), %r12
movsd 0x201cf(%rip), %xmm2    # 0x32228
jmp 0x1201c
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r13, %rdi
callq 0x9580
movq 0x28(%rsp), %rdi
callq 0x9580
movq 0x40(%rsp), %rdi
callq 0x9580
movsd 0x20(%rsp), %xmm0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq