// File: /workspace/github_c_makefile_star2_O2/zpole[P]IICV/vl/mser.c
// Function: vl_mser_ell_fit
VL_EXPORT
void
vl_mser_ell_fit (VlMserFilt* f)
{
  /* shortcuts */
  int                nel = f-> nel ;
  int                dof = f-> dof ;
  int              *dims = f-> dims ;
  int              ndims = f-> ndims ;
  int              *subs = f-> subs ;
  int             njoins = f-> njoins ;
  vl_uint         *joins = f-> joins ;
  VlMserReg           *r = f-> r ;
  vl_uint           *mer = f-> mer ;
  int               nmer = f-> nmer ;
  vl_mser_acc       *acc = f-> acc ;
  vl_mser_acc       *ell = f-> ell ;

  int d, index, i, j ;

  /* already fit ? */
  if (f->nell == f->nmer) return ;

  /* make room */
  if (f->rell < f->nmer) {
    if (f->ell) vl_free (f->ell) ;
    f->ell  = vl_malloc (sizeof(float) * f->nmer * f->dof) ;
    f->rell = f-> nmer ;
  }

  if (f->acc == 0) {
    f->acc = vl_malloc (sizeof(float) * f->nel) ;
  }

  acc = f-> acc ;
  ell = f-> ell ;

  /* -----------------------------------------------------------------
   *                                                 Integrate moments
   * -------------------------------------------------------------- */

  /* for each dof */
  for(d = 0 ; d < f->dof ; ++d) {

    /* start from the upper-left pixel (0,0,...,0) */
    memset (subs, 0, sizeof(int) * ndims) ;

    /* step 1: fill acc pretending that each region has only one pixel */
    if(d < ndims) {
      /* 1-order ................................................... */

      for(index = 0 ; index < nel ; ++ index) {
        acc [index] = subs [d] ;
        adv(ndims, dims, subs) ;
      }
    }
    else {
      /* 2-order ................................................... */

      /* map the dof d to a second order moment E[x_i x_j] */
      i = d - ndims ;
      j = 0 ;
      while(i > j) {
        i -= j + 1 ;
        j ++ ;
      }
      /* initialize acc with  x_i * x_j */
      for(index = 0 ; index < nel ; ++ index){
        acc [index] = subs [i] * subs [j] ;
        adv(ndims, dims, subs) ;
      }
    }

    /* step 2: integrate */
    for(i = 0 ; i < njoins ; ++i) {
      vl_uint index  = joins [i] ;
      vl_uint parent = r [index] .parent ;
      acc [parent] += acc [index] ;
    }

    /* step 3: save back to ellpises */
    for(i = 0 ; i < nmer ; ++i) {
      vl_uint idx = mer [i] ;
      ell [d + dof*i] = acc [idx] ;
    }

  }  /* next dof */

  /* -----------------------------------------------------------------
   *                                           Compute central moments
   * -------------------------------------------------------------- */

  for(index = 0 ; index < nmer ; ++index) {
    float  *pt  = ell + index * dof ;
    vl_uint    idx  = mer [index] ;
    float  area = r [idx] .area ;

    for(d = 0 ; d < dof ; ++d) {

      pt [d] /= area ;

      if(d >= ndims) {
        /* remove squared mean from moment to get variance */
        i = d - ndims ;
        j = 0 ;
        while(i > j) {
          i -= j + 1 ;
          j ++ ;
        }
        pt [d] -= pt [i] * pt [j] ;
      }

    }
  }

  /* save back */
  f-> nell = nmer ;
}

// Assembly:
movl 0x64(%rdi), %ecx
cmpl %ecx, 0x84(%rdi)
je 0x23a88
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
movl 0x10(%rdi), %eax
movq %rax, 0x10(%rsp)
movl 0x88(%rdi), %r15d
movq 0x8(%rdi), %rax
movq %rax, 0x38(%rsp)
movslq (%rdi), %r14
movq 0x18(%rdi), %r12
movl 0x40(%rdi), %r8d
movq 0x38(%rdi), %rax
movq %rax, 0x60(%rsp)
movq 0x48(%rdi), %rbp
movq 0x58(%rdi), %r10
cmpl %ecx, 0x80(%rdi)
movl %ecx, 0xc(%rsp)
movq %r10, 0x18(%rsp)
jge 0x2380c
movq %r8, %r13
movq 0x78(%rbx), %rdi
movl %r15d, %eax
testq %rdi, %rdi
je 0x237e0
callq 0x95b0
movl 0x64(%rbx), %ecx
movl 0x88(%rbx), %eax
movslq %ecx, %rcx
movslq %eax, %rdi
imulq %rcx, %rdi
shlq $0x2, %rdi
callq 0x95e0
movq %rax, 0x78(%rbx)
movl 0x64(%rbx), %eax
movl %eax, 0x80(%rbx)
movl 0xc(%rsp), %ecx
movq 0x18(%rsp), %r10
movq %r13, %r8
movq %r15, 0x50(%rsp)
movslq %r15d, %r15
movq 0x70(%rbx), %r13
testq %r13, %r13
jne 0x23840
movslq 0x10(%rbx), %rdi
shlq $0x2, %rdi
movq %r8, %r13
callq 0x95e0
movq %r13, %r8
movq 0x18(%rsp), %r10
movl 0xc(%rsp), %ecx
movq %rax, %r13
movq %rax, 0x70(%rbx)
movq 0x78(%rbx), %rax
leaq (,%r14,4), %rdx
movq %rdx, 0x58(%rsp)
xorl %esi, %esi
movq 0x10(%rsp), %rdx
testl %edx, %edx
cmovlel %esi, %edx
movq %rdx, 0x10(%rsp)
testl %r8d, %r8d
cmovlel %esi, %r8d
testl %ecx, %ecx
movl $0x0, %esi
cmovgl %ecx, %esi
movq %r15, 0x48(%rsp)
leaq (,%r15,4), %rcx
xorl %r15d, %r15d
movq %rcx, 0x20(%rsp)
movq %rax, 0x40(%rsp)
movq %rax, %r9
movq %rbx, 0x78(%rsp)
movq %rsi, 0x28(%rsp)
movq %rbp, 0x70(%rsp)
movq %r8, 0x68(%rsp)
movslq 0x88(%rbx), %rax
cmpq %rax, %r15
jge 0x239d7
movq %r9, 0x80(%rsp)
movq %r12, %rdi
xorl %esi, %esi
movq 0x58(%rsp), %rdx
callq 0x9420
cmpq %r14, %r15
movq %r15, 0x30(%rsp)
jge 0x23905
xorl %ebx, %ebx
cmpq %rbx, 0x10(%rsp)
je 0x2394e
movq 0x30(%rsp), %rax
cvtsi2ssl (%r12,%rax,4), %xmm0
movss %xmm0, (%r13,%rbx,4)
movl %r14d, %edi
movq 0x38(%rsp), %rsi
movq %r12, %rdx
callq 0x23a89
incq %rbx
jmp 0x238d7
movl %r15d, %eax
subl %r14d, %eax
xorl %ebp, %ebp
cmpl %ebp, %eax
jle 0x23918
incq %rbp
subl %ebp, %eax
jmp 0x2390d
movslq %eax, %rbx
xorl %r15d, %r15d
cmpq %r15, 0x10(%rsp)
je 0x2394e
movl (%r12,%rbp,4), %eax
imull (%r12,%rbx,4), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, (%r13,%r15,4)
movl %r14d, %edi
movq 0x38(%rsp), %rsi
movq %r12, %rdx
callq 0x23a89
incq %r15
jmp 0x2391e
xorl %eax, %eax
movq 0x78(%rsp), %rbx
movq 0x70(%rsp), %rbp
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdi
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %rdx
movq 0x80(%rsp), %r9
cmpq %rax, %r8
je 0x239a5
movl (%rdx,%rax,4), %ecx
movss (%r13,%rcx,4), %xmm0
shlq $0x4, %rcx
movl (%rbp,%rcx), %ecx
addss (%r13,%rcx,4), %xmm0
movss %xmm0, (%r13,%rcx,4)
incq %rax
jmp 0x2397b
movq %r9, %rax
xorl %ecx, %ecx
movq 0x30(%rsp), %r15
cmpq %rcx, %rsi
je 0x239cb
movl (%r10,%rcx,4), %edx
movss (%r13,%rdx,4), %xmm0
movss %xmm0, (%rax)
incq %rcx
addq %rdi, %rax
jmp 0x239af
incq %r15
addq $0x4, %r9
jmp 0x238a4
xorl %eax, %eax
movq 0x50(%rsp), %r9
testl %r9d, %r9d
cmovlel %eax, %r9d
movq 0x40(%rsp), %r15
movq %r15, %rcx
movq 0x48(%rsp), %r11
cmpq %rsi, %rax
je 0x23a6d
movq %rax, %rdx
imulq %r11, %rdx
leaq (%r15,%rdx,4), %rdx
movl (%r10,%rax,4), %esi
shlq $0x4, %rsi
movl 0xc(%rbp,%rsi), %esi
cvtsi2ss %rsi, %xmm0
xorl %esi, %esi
cmpq %r9, %rsi
je 0x23a5e
movss (%rdx,%rsi,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rdx,%rsi,4)
cmpq %r14, %rsi
jl 0x23a59
movl %esi, %r8d
subl %r14d, %r8d
xorl %edi, %edi
cmpl %edi, %r8d
jle 0x23a42
incq %rdi
subl %edi, %r8d
jmp 0x23a35
movslq %r8d, %r8
movss (%rdx,%r8,4), %xmm2
mulss (%rcx,%rdi,4), %xmm2
subss %xmm2, %xmm1
movss %xmm1, (%rdx,%rsi,4)
incq %rsi
jmp 0x23a15
incq %rax
addq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rsi
jmp 0x239f2
movl 0xc(%rsp), %eax
movl %eax, 0x84(%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq