// File: /workspace/github_c_makefile_star2_O3/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), %r9d
cmpl %r9d, 0x84(%rdi)
je 0x281ad
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
movl 0x10(%rdi), %eax
movq %rax, 0x20(%rsp)
movl 0x88(%rdi), %r12d
movq 0x8(%rdi), %rbp
movslq (%rdi), %rax
movq %rax, 0x28(%rsp)
movq 0x18(%rdi), %r14
movl 0x40(%rdi), %eax
movq %rax, 0x58(%rsp)
movq 0x38(%rdi), %rax
movq %rax, 0x50(%rsp)
movq 0x48(%rdi), %rax
movq %rax, 0x38(%rsp)
movq 0x58(%rdi), %rax
movq %rax, 0x8(%rsp)
cmpl %r9d, 0x80(%rdi)
movq %r9, (%rsp)
jge 0x27f24
movq 0x78(%rbx), %rdi
movl %r12d, %eax
movl %r9d, %ecx
testq %rdi, %rdi
je 0x27f00
callq 0x9580
movl 0x64(%rbx), %ecx
movl 0x88(%rbx), %eax
movslq %ecx, %rcx
movslq %eax, %rdi
imulq %rcx, %rdi
shlq $0x2, %rdi
callq 0x95b0
movq %rax, 0x78(%rbx)
movl 0x64(%rbx), %eax
movl %eax, 0x80(%rbx)
movq (%rsp), %r9
movq 0x70(%rbx), %r15
testq %r15, %r15
jne 0x27f45
movslq 0x10(%rbx), %rdi
shlq $0x2, %rdi
callq 0x95b0
movq (%rsp), %r9
movq %rax, %r15
movq %rax, 0x70(%rbx)
movq %r12, 0x40(%rsp)
movslq %r12d, %rax
movq %rax, 0x18(%rsp)
movq 0x78(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %rbx, 0x30(%rsp)
cmpl $0x0, 0x88(%rbx)
movq 0x28(%rsp), %r11
jle 0x28102
movl %r11d, %r13d
leaq (,%r11,4), %rax
movq %rax, 0x48(%rsp)
movq 0x18(%rsp), %rax
leaq (,%rax,4), %rbx
xorl %r12d, %r12d
movq 0x10(%rsp), %rsi
movq %rsi, 0x60(%rsp)
movq %r14, %rdi
xorl %esi, %esi
movq 0x48(%rsp), %rdx
callq 0x9400
movq 0x28(%rsp), %r11
movq %r12, %rcx
subq %r11, %rcx
jge 0x2800b
movq 0x20(%rsp), %rsi
testl %esi, %esi
movq (%rsp), %r9
movq 0x8(%rsp), %r8
jle 0x28085
xorl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2ssl (%r14,%r12,4), %xmm0
movss %xmm0, (%r15,%rax,4)
xorl %ecx, %ecx
movl (%r14,%rcx,4), %edx
incl %edx
movl %edx, (%r14,%rcx,4)
cmpl (%rbp,%rcx,4), %edx
jl 0x28001
movl $0x0, (%r14,%rcx,4)
incq %rcx
cmpq %rcx, %r13
jne 0x27fe1
incq %rax
cmpq %rsi, %rax
jne 0x27fd0
jmp 0x28085
testq %rcx, %rcx
movq 0x20(%rsp), %r10
jle 0x2802b
xorl %eax, %eax
movq (%rsp), %r9
movq 0x8(%rsp), %r8
incq %rax
subl %eax, %ecx
cmpl %eax, %ecx
jg 0x28020
jmp 0x28036
xorl %eax, %eax
movq (%rsp), %r9
movq 0x8(%rsp), %r8
testl %r10d, %r10d
jle 0x28085
movslq %ecx, %rcx
xorl %edx, %edx
movl (%r14,%rax,4), %esi
imull (%r14,%rcx,4), %esi
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
movss %xmm0, (%r15,%rdx,4)
testl %r13d, %r13d
jle 0x2807d
xorl %esi, %esi
movl (%r14,%rsi,4), %edi
incl %edi
movl %edi, (%r14,%rsi,4)
cmpl (%rbp,%rsi,4), %edi
jl 0x2807d
movl $0x0, (%r14,%rsi,4)
incq %rsi
cmpq %rsi, %r13
jne 0x2805d
incq %rdx
cmpq %r10, %rdx
jne 0x28040
movq 0x58(%rsp), %rsi
testl %esi, %esi
movq 0x38(%rsp), %rdx
movq 0x50(%rsp), %rdi
jle 0x280be
xorl %eax, %eax
movl (%rdi,%rax,4), %ecx
movss (%r15,%rcx,4), %xmm0
shlq $0x4, %rcx
movl (%rdx,%rcx), %ecx
addss (%r15,%rcx,4), %xmm0
movss %xmm0, (%r15,%rcx,4)
incq %rax
cmpq %rax, %rsi
jne 0x2809a
testl %r9d, %r9d
movq 0x60(%rsp), %rsi
jle 0x280e6
movq %rsi, %rax
xorl %ecx, %ecx
movl (%r8,%rcx,4), %edx
movss (%r15,%rdx,4), %xmm0
movss %xmm0, (%rax)
incq %rcx
addq %rbx, %rax
cmpq %rcx, %r9
jne 0x280cd
incq %r12
movq 0x30(%rsp), %rax
movslq 0x88(%rax), %rax
addq $0x4, %rsi
cmpq %rax, %r12
jl 0x27f97
testl %r9d, %r9d
movq 0x30(%rsp), %r8
movq 0x40(%rsp), %r10
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r14
jle 0x28198
xorl %eax, %eax
testl %r10d, %r10d
jle 0x28190
movq %rax, %rcx
imulq %rbx, %rcx
movq 0x8(%rsp), %rdx
movl (%rdx,%rax,4), %edx
shlq $0x4, %rdx
leaq (%r14,%rcx,4), %rcx
movq 0x38(%rsp), %rsi
movl 0xc(%rsi,%rdx), %edx
xorps %xmm0, %xmm0
cvtsi2ss %rdx, %xmm0
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rcx,%rdx,4)
movq %rdx, %rsi
subq %r11, %rsi
jl 0x28188
xorl %edi, %edi
testq %rsi, %rsi
jle 0x28172
incq %rdi
subl %edi, %esi
cmpl %edi, %esi
jg 0x28169
movslq %esi, %rsi
movss (%rcx,%rsi,4), %xmm2
mulss (%rcx,%rdi,4), %xmm2
subss %xmm2, %xmm1
movss %xmm1, (%rcx,%rdx,4)
incq %rdx
cmpq %r10, %rdx
jne 0x2814c
incq %rax
cmpq %r9, %rax
jne 0x2811d
movl %r9d, 0x84(%r8)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop