// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq 0x90(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x8c(%rsp)
movq 0x90(%rsp), %rax
movl 0x88(%rax), %eax
movl %eax, 0x88(%rsp)
movq 0x90(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x80(%rsp)
movq 0x90(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x7c(%rsp)
movq 0x90(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x70(%rsp)
movq 0x90(%rsp), %rax
movl 0x40(%rax), %eax
movl %eax, 0x6c(%rsp)
movq 0x90(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x90(%rsp), %rax
movq 0x48(%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x90(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x90(%rsp), %rax
movl 0x64(%rax), %eax
movl %eax, 0x4c(%rsp)
movq 0x90(%rsp), %rax
movq 0x70(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x90(%rsp), %rax
movq 0x78(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x90(%rsp), %rax
movl 0x84(%rax), %eax
movq 0x90(%rsp), %rcx
cmpl 0x64(%rcx), %eax
jne 0x461dc
jmp 0x46631
movq 0x90(%rsp), %rax
movl 0x80(%rax), %eax
movq 0x90(%rsp), %rcx
cmpl 0x64(%rcx), %eax
jge 0x46267
movq 0x90(%rsp), %rax
cmpq $0x0, 0x78(%rax)
je 0x46217
movq 0x90(%rsp), %rax
movq 0x78(%rax), %rdi
callq 0x9620
movq 0x90(%rsp), %rax
movslq 0x64(%rax), %rdi
shlq $0x2, %rdi
movq 0x90(%rsp), %rax
movslq 0x88(%rax), %rax
imulq %rax, %rdi
callq 0x9660
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x78(%rax)
movq 0x90(%rsp), %rax
movl 0x64(%rax), %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0x80(%rax)
movq 0x90(%rsp), %rax
cmpq $0x0, 0x70(%rax)
jne 0x4629a
movq 0x90(%rsp), %rax
movslq 0x10(%rax), %rdi
shlq $0x2, %rdi
callq 0x9660
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x70(%rax)
movq 0x90(%rsp), %rax
movq 0x70(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x90(%rsp), %rax
movq 0x78(%rax), %rax
movq %rax, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
movq 0x90(%rsp), %rcx
cmpl 0x88(%rcx), %eax
jge 0x464d0
movq 0x70(%rsp), %rdi
movslq 0x7c(%rsp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x9480
movl 0x34(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jge 0x46356
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
cmpl 0x8c(%rsp), %eax
jge 0x46351
movq 0x70(%rsp), %rax
movslq 0x34(%rsp), %rcx
cvtsi2ssl (%rax,%rcx,4), %xmm0
movq 0x40(%rsp), %rax
movslq 0x30(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl 0x7c(%rsp), %edi
movq 0x80(%rsp), %rsi
movq 0x70(%rsp), %rdx
callq 0x46640
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x46303
jmp 0x463fa
movl 0x34(%rsp), %eax
subl 0x7c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x28(%rsp), %eax
jle 0x46392
movl 0x28(%rsp), %ecx
addl $0x1, %ecx
movl 0x2c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x2c(%rsp)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x4636a
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
cmpl 0x8c(%rsp), %eax
jge 0x463f8
movq 0x70(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movq 0x70(%rsp), %rcx
movslq 0x28(%rsp), %rdx
imull (%rcx,%rdx,4), %eax
cvtsi2ss %eax, %xmm0
movq 0x40(%rsp), %rax
movslq 0x30(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl 0x7c(%rsp), %edi
movq 0x80(%rsp), %rsi
movq 0x70(%rsp), %rdx
callq 0x46640
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x4639a
jmp 0x463fa
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jge 0x46461
movq 0x60(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x24(%rsp)
movq 0x58(%rsp), %rax
movl 0x24(%rsp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, 0x20(%rsp)
movq 0x40(%rsp), %rax
movl 0x24(%rsp), %ecx
movss (%rax,%rcx,4), %xmm0
movq 0x40(%rsp), %rax
movl 0x20(%rsp), %ecx
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x46402
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0x464be
movq 0x50(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x1c(%rsp)
movq 0x40(%rsp), %rax
movl 0x1c(%rsp), %ecx
movss (%rax,%rcx,4), %xmm0
movq 0x38(%rsp), %rax
movl 0x34(%rsp), %ecx
movl 0x88(%rsp), %edx
imull 0x2c(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x46469
jmp 0x464c0
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x462c4
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0x4661f
movq 0x38(%rsp), %rax
movl 0x30(%rsp), %ecx
movl 0x88(%rsp), %edx
imull %edx, %ecx
movslq %ecx, %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, 0x10(%rsp)
movq 0x50(%rsp), %rax
movslq 0x30(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
movq 0x58(%rsp), %rax
movl 0xc(%rsp), %ecx
shlq $0x4, %rcx
movl 0xc(%rax,%rcx), %eax
cvtsi2ss %rax, %xmm0
movss %xmm0, 0x8(%rsp)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x88(%rsp), %eax
jge 0x4660d
movss 0x8(%rsp), %xmm1
movq 0x10(%rsp), %rax
movslq 0x34(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
divss %xmm1, %xmm0
movss %xmm0, (%rax,%rcx,4)
movl 0x34(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jl 0x465fb
movl 0x34(%rsp), %eax
subl 0x7c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x28(%rsp), %eax
jle 0x465b3
movl 0x28(%rsp), %ecx
addl $0x1, %ecx
movl 0x2c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x2c(%rsp)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x4658b
movq 0x10(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
movq 0x10(%rsp), %rax
movslq 0x28(%rsp), %rcx
movss (%rax,%rcx,4), %xmm2
movq 0x10(%rsp), %rax
movslq 0x34(%rsp), %rcx
movss (%rax,%rcx,4), %xmm1
movd %xmm0, %edx
xorl $0x80000000, %edx       # imm = 0x80000000
movd %edx, %xmm0
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rax,%rcx,4)
jmp 0x465fd
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x4653a
jmp 0x4660f
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x464d8
movl 0x4c(%rsp), %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0x84(%rax)
addq $0x98, %rsp
retq
nopl (%rax)