// File: /workspace/github_c_makefile_star2_O3/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
movl (%rdi), %eax
decl %eax
cmpl $0xa, %eax
jae 0x12cbf
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movl %eax, %eax
leaq 0x1fe8a(%rip), %rcx     # 0x32900
movq (%rcx,%rax,8), %r14
movq 0x10(%rdi), %rdi
imulq %r14, %rdi
callq 0x95b0
movq %rax, 0x20(%rsp)
movq 0x8(%rbx), %rdi
imulq %r14, %rdi
imulq 0x10(%rbx), %rdi
callq 0x95b0
movq %rax, 0x18(%rsp)
movq 0x8(%rbx), %rdi
imulq %r14, %rdi
imulq 0x10(%rbx), %rdi
callq 0x95b0
movq %rax, 0x10(%rsp)
imulq %r15, %r14
imulq 0x10(%rbx), %r14
movq %r14, %rdi
callq 0x95b0
movq %rax, %r14
cmpq $0x0, 0x28(%rbx)
je 0x12c30
movsd 0x1fa07(%rip), %xmm0    # 0x324e8
movsd %xmm0, 0x28(%rsp)
xorl %ebp, %ebp
cmpl $0x0, 0x30(%rbx)
je 0x12b0a
callq 0x9180
movq %rax, %rcx
leaq 0x1(%rbp), %rsi
movq 0x28(%rbx), %rdx
leaq 0x1fa0e(%rip), %rdi     # 0x32514
xorl %eax, %eax
callq *%rcx
callq 0x9350
movsd %xmm0, 0x8(%rsp)
movl 0x60(%rbx), %eax
cmpl $0x2, %eax
je 0x12b4a
cmpl $0x1, %eax
je 0x12b3c
testl %eax, %eax
jne 0x12cbf
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x9850
jmp 0x12b4a
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x9160
cmpl $0x0, 0x30(%rbx)
je 0x12b6f
callq 0x9180
movq %rax, %r13
callq 0x9350
subsd 0x8(%rsp), %xmm0
leaq 0x1f9d9(%rip), %rdi     # 0x32543
movb $0x1, %al
callq *%r13
callq 0x9350
movsd %xmm0, 0x30(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x9950
cmpl $0x0, 0x30(%rbx)
movsd %xmm0, 0x8(%rsp)
je 0x12bbf
callq 0x9180
movq %rax, %r13
callq 0x9350
subsd 0x30(%rsp), %xmm0
leaq 0x1f9b7(%rip), %rdi     # 0x32565
movsd 0x8(%rsp), %xmm1
movb $0x2, %al
callq *%r13
movsd 0x8(%rsp), %xmm0
testq %rbp, %rbp
je 0x12bda
ucomisd 0x28(%rsp), %xmm0
ja 0x12bda
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0x12c21
movq 0x48(%rbx), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x48(%rbx)
movq 0x38(%rbx), %rcx
movq 0x40(%rbx), %rdx
movq 0x18(%rsp), %rsi
movq %rsi, 0x38(%rbx)
movq 0x10(%rsp), %rsi
movq %rsi, 0x40(%rbx)
movq 0x50(%rbx), %rsi
movq %r14, 0x50(%rbx)
movq %rsi, %r14
movq %rdx, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movsd %xmm0, 0x28(%rsp)
incq %rbp
cmpq 0x28(%rbx), %rbp
jb 0x12ae9
jmp 0x12c3e
movsd 0x1f8b0(%rip), %xmm0    # 0x324e8
movsd %xmm0, 0x8(%rsp)
movq 0x48(%rbx), %rdi
callq 0x9580
movq 0x38(%rbx), %rdi
callq 0x9580
movq 0x40(%rbx), %rdi
callq 0x9580
movq 0x50(%rbx), %rdi
callq 0x9580
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rbx)
movq 0x18(%rsp), %rax
movq %rax, 0x38(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x40(%rbx)
movq %r14, 0x50(%rbx)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x70(%rbx)
cmpl $0x0, 0x30(%rbx)
je 0x12cb0
callq 0x9180
movq %rax, %rcx
movsd 0x70(%rbx), %xmm0
leaq 0x1f8fd(%rip), %rdi     # 0x325a3
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