// File: /workspace/github_c_makefile_star2_O2/zpole[P]IICV/vl/gmm.c
// Function: vl_gmm_cluster
double vl_gmm_cluster (VlGMM * self,
                       void const * data,
                       vl_size numData)
{
  void * bestPriors = NULL ;
  void * bestMeans = NULL;
  void * bestCovariances = NULL;
  void * bestPosteriors = NULL;
  vl_size size = vl_get_type_size(self->dataType) ;
  double bestLL = -VL_INFINITY_D;
  vl_uindex repetition;

  assert(self->numRepetitions >=1) ;

  bestPriors = vl_malloc(size * self->numClusters) ;
  bestMeans = vl_malloc(size * self->dimension * self->numClusters) ;
  bestCovariances = vl_malloc(size * self->dimension * self->numClusters) ;
  bestPosteriors = vl_malloc(size * self->numClusters * numData) ;

#if 0
  feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
#endif

  for (repetition = 0 ; repetition < self->numRepetitions ; ++ repetition) {
    double LL ;
    double timeRef ;

    if (self->verbosity) {
      VL_PRINTF("gmm: clustering: starting repetition %d of %d\n", repetition + 1, self->numRepetitions) ;
    }

    /* initialize a new mixture model */
    timeRef = vl_get_cpu_time() ;
    switch (self->initialization) {
      case VlGMMKMeans : vl_gmm_init_with_kmeans (self, data, numData, NULL) ; break ;
      case VlGMMRand : vl_gmm_init_with_rand_data (self, data, numData) ; break ;
      case VlGMMCustom : break ;
      default: abort() ;
    }
    if (self->verbosity) {
      VL_PRINTF("gmm: model initialized in %.2f s\n",
                vl_get_cpu_time() - timeRef) ;
    }

    /* fit the model to data by running EM */
    timeRef = vl_get_cpu_time () ;
    LL = vl_gmm_em (self, data, numData) ;
    if (self->verbosity) {
      VL_PRINTF("gmm: optimization terminated in %.2f s with loglikelihood %f\n",
                vl_get_cpu_time() - timeRef, LL) ;
    }

    if (LL > bestLL || repetition == 0) {
      void * temp ;

      temp = bestPriors ;
      bestPriors = self->priors ;
      self->priors = temp ;

      temp = bestMeans ;
      bestMeans = self->means ;
      self->means = temp ;

      temp = bestCovariances ;
      bestCovariances = self->covariances ;
      self->covariances = temp ;

      temp = bestPosteriors ;
      bestPosteriors = self->posteriors ;
      self->posteriors = temp ;

      bestLL = LL;
    }
  }

  vl_free (self->priors) ;
  vl_free (self->means) ;
  vl_free (self->covariances) ;
  vl_free (self->posteriors) ;

  self->priors = bestPriors ;
  self->means = bestMeans ;
  self->covariances = bestCovariances ;
  self->posteriors = bestPosteriors ;
  self->LL = bestLL;

  if (self->verbosity) {
    VL_PRINTF("gmm: all repetitions terminated with final loglikelihood %f\n", self->LL) ;
  }

  return bestLL ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movl (%rdi), %edi
callq 0x10760
movq %rax, %r12
movq 0x10(%rbx), %rdi
imulq %rax, %rdi
callq 0x95e0
movq %rax, 0x28(%rsp)
movq 0x8(%rbx), %rdi
imulq %r12, %rdi
imulq 0x10(%rbx), %rdi
callq 0x95e0
movq %rax, 0x20(%rsp)
movq 0x8(%rbx), %rdi
imulq %r12, %rdi
imulq 0x10(%rbx), %rdi
callq 0x95e0
movq %rax, 0x18(%rsp)
imulq %r14, %r12
imulq 0x10(%rbx), %r12
movq %r12, %rdi
callq 0x95e0
movq %rax, %r12
movsd 0x1bd67(%rip), %xmm0    # 0x2d4a8
movsd %xmm0, 0x8(%rsp)
xorl %ebp, %ebp
cmpq 0x28(%rbx), %rbp
jae 0x11885
cmpl $0x0, 0x30(%rbx)
je 0x11774
callq 0x9180
movq %rax, %rcx
leaq 0x1(%rbp), %rsi
movq 0x28(%rbx), %rdx
leaq 0x1bd64(%rip), %rdi     # 0x2d4d4
xorl %eax, %eax
callq *%rcx
callq 0x9370
movsd %xmm0, 0x10(%rsp)
movl 0x60(%rbx), %eax
cmpl $0x2, %eax
je 0x117b4
cmpl $0x1, %eax
je 0x117a6
testl %eax, %eax
jne 0x11906
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x98b0
jmp 0x117b4
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x9160
cmpl $0x0, 0x30(%rbx)
je 0x117d9
callq 0x9180
movq %rax, %r13
callq 0x9370
subsd 0x10(%rsp), %xmm0
leaq 0x1bd2f(%rip), %rdi     # 0x2d503
movb $0x1, %al
callq *%r13
callq 0x9370
movsd %xmm0, 0x30(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x99c0
cmpl $0x0, 0x30(%rbx)
je 0x11829
movsd %xmm0, 0x10(%rsp)
callq 0x9180
movq %rax, %r13
callq 0x9370
subsd 0x30(%rsp), %xmm0
leaq 0x1bd0d(%rip), %rdi     # 0x2d525
movsd 0x10(%rsp), %xmm1
movb $0x2, %al
callq *%r13
movsd 0x10(%rsp), %xmm0
testq %rbp, %rbp
je 0x11836
ucomisd 0x8(%rsp), %xmm0
jbe 0x1187d
movq 0x48(%rbx), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x48(%rbx)
movq 0x38(%rbx), %rcx
movq 0x40(%rbx), %rdx
movq 0x20(%rsp), %rsi
movq %rsi, 0x38(%rbx)
movq 0x18(%rsp), %rsi
movq %rsi, 0x40(%rbx)
movq 0x50(%rbx), %rsi
movq %r12, 0x50(%rbx)
movq %rsi, %r12
movq %rdx, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movsd %xmm0, 0x8(%rsp)
incq %rbp
jmp 0x11749
movq 0x48(%rbx), %rdi
callq 0x95b0
movq 0x38(%rbx), %rdi
callq 0x95b0
movq 0x40(%rbx), %rdi
callq 0x95b0
movq 0x50(%rbx), %rdi
callq 0x95b0
movq 0x28(%rsp), %rax
movq %rax, 0x48(%rbx)
movq 0x20(%rsp), %rax
movq %rax, 0x38(%rbx)
movq 0x18(%rsp), %rax
movq %rax, 0x40(%rbx)
movq %r12, 0x50(%rbx)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x70(%rbx)
cmpl $0x0, 0x30(%rbx)
je 0x118f1
callq 0x9180
movq %rax, %rcx
movsd 0x70(%rbx), %xmm0
leaq 0x1bc76(%rip), %rdi     # 0x2d563
movb $0x1, %al
callq *%rcx
movsd 0x8(%rsp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x90b0