// File: /workspace/github_c_makefile_star2_O0/zpole[P]IICV/src/test_gmm.c
// Function: main
int main(int argc VL_UNUSED, char ** argv VL_UNUSED)
{
  VlKMeans * kmeans = 0;
  VlRand rand ;
  vl_size dataIdx, d, cIdx;
  VlGMM * gmm;

  double sigmaLowerBound = 0.000001;

  vl_size numData = 1000;
  vl_size dimension = 3;
  vl_size numClusters = 20;
  vl_size maxiter = 5;
  vl_size maxrep = 1;

  vl_size maxiterKM = 5;
  vl_size ntrees = 3;
  vl_size maxComp = 20;

  typedef enum _init {
    KMeans,
    Rand,
    Custom
  } Init ;

  vl_bool computeFisher = VL_TRUE;
  vl_bool computeVlad = VL_FALSE;

  Init init = KMeans;

  //char * dataFileResults = "/home/dave/vlfeat/data/gmm/gmm-results.mat";
  //char * dataFileData = "/home/dave/vlfeat/data/gmm/gmm-data.mat";

  TYPE * data = vl_malloc(sizeof(TYPE)*numData*dimension);
  TYPE * enc = vl_malloc(sizeof(TYPE)*2*dimension*numClusters);
  vl_uint32 * assign;

  vl_set_num_threads(0) ; /* use the default number of threads */

  vl_rand_init (&rand) ;
  vl_rand_seed (&rand, 49000) ;

  for(dataIdx = 0; dataIdx < numData; dataIdx++) {
    for(d = 0; d < dimension; d++) {
      data[dataIdx*dimension+d] = (TYPE)vl_rand_real3(&rand);
      //VL_PRINT("%f ",data[dataIdx*dimension+d]);
    }
    //VL_PRINT("\n");
  }

  gmm = vl_gmm_new (VL_F_TYPE, dimension, numClusters) ;

  switch(init) {
    case KMeans:
      kmeans = vl_kmeans_new(VL_F_TYPE,VlDistanceL2);
      vl_kmeans_set_verbosity	(kmeans,1);
      vl_kmeans_set_max_num_iterations (kmeans, maxiterKM) ;
      vl_kmeans_set_max_num_comparisons (kmeans, maxComp) ;
      vl_kmeans_set_num_trees (kmeans, ntrees);
      vl_kmeans_set_algorithm (kmeans, VlKMeansANN);
      vl_kmeans_set_initialization(kmeans, VlKMeansRandomSelection);
      vl_gmm_set_initialization (gmm,VlGMMKMeans);
      vl_gmm_set_kmeans_init_object(gmm,kmeans);
      break;

    case Rand:
      vl_gmm_set_initialization (gmm,VlGMMRand);
      break;

    case Custom: {
      TYPE * initSigmas;
      TYPE * initMeans;
      TYPE * initWeights;

      initSigmas = vl_malloc(sizeof(TYPE) * numClusters * dimension);
      initWeights = vl_malloc(sizeof(TYPE) * numClusters);
      initMeans = vl_malloc(sizeof(TYPE) * numClusters * dimension);

      vl_gmm_set_initialization (gmm,VlGMMCustom);

      for(cIdx = 0; cIdx < numClusters; cIdx++) {
        for(d = 0; d < dimension; d++) {
          initMeans[cIdx*dimension+d] = (TYPE)vl_rand_real3(&rand);
          initSigmas[cIdx*dimension+d] = (TYPE)vl_rand_real3(&rand);
        }
        initWeights[cIdx] = (TYPE)vl_rand_real3(&rand);
      }

      vl_gmm_set_priors(gmm,initWeights);
      vl_gmm_set_covariances(gmm,initSigmas);
      vl_gmm_set_means(gmm,initMeans);

      break;
    }

    default:
      abort();
  }

  vl_gmm_set_max_num_iterations (gmm, maxiter) ;
  vl_gmm_set_num_repetitions(gmm, maxrep);
  vl_gmm_set_verbosity(gmm,1);
  vl_gmm_set_covariance_lower_bound (gmm,sigmaLowerBound);

  //struct timeval t1,t2;
  //gettimeofday(&t1, NULL);

  vl_gmm_cluster (gmm, data, numData);

  //gettimeofday(&t2, NULL);
  //VL_PRINT("elapsed vlfeat: %f s\n",(double)(t2.tv_sec - t1.tv_sec) + ((double)(t2.tv_usec - t1.tv_usec))/1000000.);

//    VL_PRINT("posterior:\n");
//    for(cIdx = 0; cIdx < clusterNum; cIdx++){
//        for(dataIdx = 0; dataIdx < Ndata; dataIdx++){
//            VL_PRINT("%f ",((float*)posteriors)[cIdx*Ndata+dataIdx]);
//        }
//        VL_PRINT("\n");
//    }

//  VL_PRINT("mean:\n");
//  for(cIdx = 0; cIdx < numClusters; cIdx++) {
//    for(d = 0; d < dimension; d++) {
//      VL_PRINT("%f ",((TYPE*)means)[cIdx*dimension+d]);
//    }
//    VL_PRINT("\n");
//  }
//
//  VL_PRINT("sigma:\n");
//  for(cIdx = 0; cIdx < numClusters; cIdx++) {
//    for(d = 0; d < dimension; d++) {
//      VL_PRINT("%f ",((TYPE*)sigmas)[cIdx*dimension+d]);
//    }
//    VL_PRINT("\n");
//  }
//
//  VL_PRINT("w:\n");
//  for(cIdx = 0; cIdx < numClusters; cIdx++) {
//    VL_PRINT("%f ",((TYPE*)weights)[cIdx]);
//    VL_PRINT("\n");
//  }

  //saveResults(dataFileData,dataFileResults,gmm,(void*) data, numData);

//  VL_PRINT("fisher:\n");
//  for(cIdx = 0; cIdx < clusterNum; cIdx++) {
//    for(d = 0; d < dimension*2; d++) {
//      VL_PRINT("%f ",enc[cIdx*dimension*2+d]);
//    }
//    VL_PRINT("\n");
//  }

  vl_free(data);
  numData = 2000;
  data = vl_malloc(numData*dimension*sizeof(TYPE));
  for(dataIdx = 0; dataIdx < numData; dataIdx++) {
    for(d = 0; d < dimension; d++) {
      data[dataIdx*dimension+d] = (TYPE)vl_rand_real3(&rand);
    }
  }

  if(computeFisher) {
    vl_fisher_encode
    (enc, VL_F_TYPE,
     vl_gmm_get_means(gmm), dimension, numClusters,
     vl_gmm_get_covariances(gmm),
     vl_gmm_get_priors(gmm),
     data, numData,
     VL_FISHER_FLAG_IMPROVED
     ) ;
  }

  assign = vl_malloc(numData*numClusters*sizeof(vl_uint32));
  for(dataIdx = 0; dataIdx < numData; dataIdx++) {
    for(cIdx = 0; cIdx < numClusters; cIdx++) {
      assign[cIdx*numData+dataIdx] = (vl_uint32)vl_rand_real3(&rand);
    }
  }

  if(computeVlad) {
    vl_free(enc);
    enc = vl_malloc(sizeof(TYPE)*dimension*numClusters);
    vl_vlad_encode
    (enc, VL_F_TYPE,
     vl_gmm_get_means(gmm), dimension, numClusters,
     data, numData,
     assign,
     0) ;
  }

  vl_gmm_delete(gmm);
  vl_free(data);
  if(enc){
    vl_free(enc);
  }
  if(kmeans) {
    vl_kmeans_delete(kmeans);
  }
  return 0 ;
}

// Assembly:
subq $0xae8, %rsp            # imm = 0xAE8
movl $0x0, 0xae4(%rsp)
movl %edi, 0xae0(%rsp)
movq %rsi, 0xad8(%rsp)
movq $0x0, 0xad0(%rsp)
movsd 0x1c93(%rip), %xmm0     # 0x3008
movsd %xmm0, 0xe0(%rsp)
movq $0x3e8, 0xd8(%rsp) # imm = 0x3E8
movq $0x3, 0xd0(%rsp)
movq $0x14, 0xc8(%rsp)
movq $0x5, 0xc0(%rsp)
movq $0x1, 0xb8(%rsp)
movq $0x5, 0xb0(%rsp)
movq $0x3, 0xa8(%rsp)
movq $0x14, 0xa0(%rsp)
movl $0x1, 0x9c(%rsp)
movl $0x0, 0x98(%rsp)
movl $0x0, 0x94(%rsp)
movq 0xd8(%rsp), %rdi
shlq $0x2, %rdi
imulq 0xd0(%rsp), %rdi
callq 0x1140
movq %rax, 0x88(%rsp)
movq 0xd0(%rsp), %rdi
shlq $0x3, %rdi
imulq 0xc8(%rsp), %rdi
callq 0x1140
movq %rax, 0x80(%rsp)
xorl %eax, %eax
movl %eax, %edi
callq 0x10e0
leaq 0x10c(%rsp), %rdi
callq 0x1230
leaq 0x10c(%rsp), %rdi
movl $0xbf68, %esi           # imm = 0xBF68
callq 0x10f0
movq $0x0, 0x100(%rsp)
movq 0x100(%rsp), %rax
cmpq 0xd8(%rsp), %rax
jae 0x1513
movq $0x0, 0xf8(%rsp)
movq 0xf8(%rsp), %rax
cmpq 0xd0(%rsp), %rax
jae 0x14f8
leaq 0x10c(%rsp), %rdi
callq 0x1b90
cvtsd2ss %xmm0, %xmm0
movq 0x88(%rsp), %rax
movq 0x100(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
addq 0xf8(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movq 0xf8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf8(%rsp)
jmp 0x1499
jmp 0x14fa
movq 0x100(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x100(%rsp)
jmp 0x1477
movq 0xd0(%rsp), %rsi
movq 0xc8(%rsp), %rdx
movl $0x1, %edi
callq 0x11a0
movq %rax, 0xe8(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x5c(%rsp)
testl %eax, %eax
je 0x1567
jmp 0x1546
movl 0x5c(%rsp), %eax
subl $0x1, %eax
je 0x1616
jmp 0x1555
movl 0x5c(%rsp), %eax
subl $0x2, %eax
je 0x162d
jmp 0x17cc
movl $0x1, %esi
movl %esi, %edi
callq 0x1070
movq %rax, 0xad0(%rsp)
movq 0xad0(%rsp), %rdi
movl $0x1, %esi
callq 0x1bc0
movq 0xad0(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1be0
movq 0xad0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x1c00
movq 0xad0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x1c20
movq 0xad0(%rsp), %rdi
movl $0x2, %esi
callq 0x1c40
movq 0xad0(%rsp), %rdi
xorl %esi, %esi
callq 0x1c60
movq 0xe8(%rsp), %rdi
xorl %esi, %esi
callq 0x1210
movq 0xe8(%rsp), %rdi
movq 0xad0(%rsp), %rsi
callq 0x1200
jmp 0x17d1
movq 0xe8(%rsp), %rdi
movl $0x1, %esi
callq 0x1210
jmp 0x17d1
movq 0xc8(%rsp), %rdi
shlq $0x2, %rdi
imulq 0xd0(%rsp), %rdi
callq 0x1140
movq %rax, 0x70(%rsp)
movq 0xc8(%rsp), %rdi
shlq $0x2, %rdi
callq 0x1140
movq %rax, 0x60(%rsp)
movq 0xc8(%rsp), %rdi
shlq $0x2, %rdi
imulq 0xd0(%rsp), %rdi
callq 0x1140
movq %rax, 0x68(%rsp)
movq 0xe8(%rsp), %rdi
movl $0x2, %esi
callq 0x1210
movq $0x0, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
cmpq 0xc8(%rsp), %rax
jae 0x1794
movq $0x0, 0xf8(%rsp)
movq 0xf8(%rsp), %rax
cmpq 0xd0(%rsp), %rax
jae 0x1758
leaq 0x10c(%rsp), %rdi
callq 0x1b90
cvtsd2ss %xmm0, %xmm0
movq 0x68(%rsp), %rax
movq 0xf0(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
addq 0xf8(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
leaq 0x10c(%rsp), %rdi
callq 0x1b90
cvtsd2ss %xmm0, %xmm0
movq 0x70(%rsp), %rax
movq 0xf0(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
addq 0xf8(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movq 0xf8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf8(%rsp)
jmp 0x16c1
leaq 0x10c(%rsp), %rdi
callq 0x1b90
cvtsd2ss %xmm0, %xmm0
movq 0x60(%rsp), %rax
movq 0xf0(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movq 0xf0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf0(%rsp)
jmp 0x169f
movq 0xe8(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x11d0
movq 0xe8(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x1150
movq 0xe8(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x1030
jmp 0x17d1
callq 0x1050
movq 0xe8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x1180
movq 0xe8(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x10c0
movq 0xe8(%rsp), %rdi
movl $0x1, %esi
callq 0x1130
movq 0xe8(%rsp), %rdi
movsd 0xe0(%rsp), %xmm0
callq 0x1120
movq 0xe8(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0xd8(%rsp), %rdx
callq 0x10a0
movq 0x88(%rsp), %rdi
callq 0x1110
movq $0x7d0, 0xd8(%rsp) # imm = 0x7D0
movq 0xd8(%rsp), %rdi
imulq 0xd0(%rsp), %rdi
shlq $0x2, %rdi
callq 0x1140
movq %rax, 0x88(%rsp)
movq $0x0, 0x100(%rsp)
movq 0x100(%rsp), %rax
cmpq 0xd8(%rsp), %rax
jae 0x1923
movq $0x0, 0xf8(%rsp)
movq 0xf8(%rsp), %rax
cmpq 0xd0(%rsp), %rax
jae 0x1908
leaq 0x10c(%rsp), %rdi
callq 0x1b90
cvtsd2ss %xmm0, %xmm0
movq 0x88(%rsp), %rax
movq 0x100(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
addq 0xf8(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movq 0xf8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf8(%rsp)
jmp 0x18a9
jmp 0x190a
movq 0x100(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x100(%rsp)
jmp 0x1887
cmpl $0x0, 0x9c(%rsp)
je 0x19d5
movq 0x80(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x1190
movq %rax, 0x38(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x1170
movq %rax, 0x50(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x11b0
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %r8
movq 0x50(%rsp), %r9
movq %rax, %r11
movq 0x88(%rsp), %r10
movq 0xd8(%rsp), %rax
movl $0x1, %esi
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl $0x3, 0x18(%rsp)
callq 0x1220
movq 0xd8(%rsp), %rdi
imulq 0xc8(%rsp), %rdi
shlq $0x2, %rdi
callq 0x1140
movq %rax, 0x78(%rsp)
movq $0x0, 0x100(%rsp)
movq 0x100(%rsp), %rax
cmpq 0xd8(%rsp), %rax
jae 0x1a9a
movq $0x0, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
cmpq 0xc8(%rsp), %rax
jae 0x1a7f
leaq 0x10c(%rsp), %rdi
callq 0x1b90
cvttsd2si %xmm0, %rax
movl %eax, %edx
movq 0x78(%rsp), %rax
movq 0xf0(%rsp), %rcx
imulq 0xd8(%rsp), %rcx
addq 0x100(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movq 0xf0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf0(%rsp)
jmp 0x1a22
jmp 0x1a81
movq 0x100(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x100(%rsp)
jmp 0x1a00
cmpl $0x0, 0x98(%rsp)
je 0x1b3c
movq 0x80(%rsp), %rdi
callq 0x1110
movq 0xd0(%rsp), %rdi
shlq $0x2, %rdi
imulq 0xc8(%rsp), %rdi
callq 0x1140
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x1190
movq 0x28(%rsp), %rdi
movq %rax, %rdx
movq 0xd0(%rsp), %rcx
movq 0xc8(%rsp), %r8
movq 0x88(%rsp), %r9
movq 0xd8(%rsp), %r10
movq 0x78(%rsp), %rax
movl $0x1, %esi
xorl %r11d, %r11d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x11c0
movq 0xe8(%rsp), %rdi
callq 0x1160
movq 0x88(%rsp), %rdi
callq 0x1110
cmpq $0x0, 0x80(%rsp)
je 0x1b6e
movq 0x80(%rsp), %rdi
callq 0x1110
cmpq $0x0, 0xad0(%rsp)
je 0x1b86
movq 0xad0(%rsp), %rdi
callq 0x1080
xorl %eax, %eax
addq $0xae8, %rsp            # imm = 0xAE8
retq