// File: /workspace/github_c_makefile_star2_O2/zpole[P]IICV/vl/liop.c
// Function: vl_liopdesc_process
void
vl_liopdesc_process (VlLiopDesc * self, float * desc, float const * patch)
{
  vl_index i,t ;
  vl_index offset,numPermutations ;
  vl_index spatialBinArea, spatialBinEnd, spatialBinIndex ;
  float threshold ;

  memset(desc, 0, sizeof(float) * self->dimension) ;

  /*
   * Sort pixels in the patch by increasing intensity.
   */

  for (i = 0 ; i < (signed)self->patchSize ; ++i) {
    vl_index pixel = self->patchPixels[i] ;
    self->patchIntensities[i] = patch[pixel] ;
    self->patchPermutation[i] = i ;
  }
  patch_sort(self, self->patchSize) ;

  /*
   * Tune the threshold if needed.
   */

  if (self->intensityThreshold < 0) {
    i = self->patchPermutation[0] ;
    t = self->patchPermutation[self->patchSize-1] ;
    threshold = - self->intensityThreshold
    * (self->patchIntensities[t] - self->patchIntensities[i]);
  } else {
    threshold = self->intensityThreshold ;
  }

  /*
   * Process pixels in order of increasing intenisity, dividing them into
   * spatial bins on the fly.
   */

  numPermutations = factorial(self->numNeighbours) ;
  spatialBinArea = self->patchSize / self->numSpatialBins ;
  spatialBinEnd = spatialBinArea ;
  spatialBinIndex = 0 ;
  offset = 0 ;

  for (i = 0 ; i < (signed)self->patchSize ; ++i) {
    vl_index permIndex ;
    double *sx, *sy ;

    /* advance to the next spatial bin if needed */
    if (i >= (signed)spatialBinEnd && spatialBinIndex < (signed)self->numSpatialBins - 1) {
      spatialBinEnd += spatialBinArea ;
      spatialBinIndex ++ ;
      offset += numPermutations ;
    }

    /* get intensities of neighbours of the current patch element and sort them */
    sx = self->neighSamplesX + self->numNeighbours * self->patchPermutation[i] ;
    sy = self->neighSamplesY + self->numNeighbours * self->patchPermutation[i] ;
    for (t = 0 ; t < self->numNeighbours ; ++t) {
      double x = *sx++ ;
      double y = *sy++ ;

      /* bilinear interpolation */
      vl_index ix = vl_floor_d(x) ;
      vl_index iy = vl_floor_d(y) ;

      double wx = x - ix ;
      double wy = y - iy ;

      double a = 0, b = 0, c = 0, d = 0 ;

      int L = (int) self->patchSideLength ;

      if (ix >= 0   && iy >= 0  ) { a = patch[ix   + iy * L] ; }
      if (ix <  L-1 && iy >= 0  ) { b = patch[ix+1 + iy * L] ; }
      if (ix >= 0   && iy <  L-1) { c = patch[ix   + (iy+1) * L] ; }
      if (ix <  L-1 && iy <  L-1) { d = patch[ix+1 + (iy+1) * L] ; }

      self->neighPermutation[t] = t;
      self->neighIntensities[t] = (1 - wy) * (a + (b - a) * wx) + wy * (c + (d - c) * wx) ;
    }
    neigh_sort (self, self->numNeighbours) ;

    /* get permutation index */
    permIndex = get_permutation_index(self->neighPermutation, self->numNeighbours);

    /*
     * Compute weight according to difference in intensity values and
     * accumulate.
     */
    {
      int k, t ;
      float weight = 0 ;
      for(k = 0; k < self->numNeighbours ; ++k) {
        for(t = k + 1; t < self->numNeighbours; ++t){
          double a = self->neighIntensities[k] ;
          double b = self->neighIntensities[t] ;
          weight += (a > b + threshold || b > a + threshold) ;
        }
      }
      desc[permIndex + offset] += weight ;
    }
  }

  /* normalization */
  {
    float norm = 0;
    for(i = 0; i < (signed)self->dimension; i++) {
      norm += desc[i]*desc[i];
    }
    norm = VL_MAX(sqrt(norm), 1e-12) ;
    for(i = 0; i < (signed)self->dimension; i++){
      desc[i] /= norm ;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r15
movq %rdi, %r14
movq 0x10(%rdi), %rdx
shlq $0x2, %rdx
xorl %ebx, %ebx
movq %rsi, 0x28(%rsp)
movq %rsi, %rdi
xorl %esi, %esi
callq 0x9420
movq 0x20(%r14), %rdx
movslq %edx, %rax
cmpq %rax, %rbx
jge 0x20148
movq 0x28(%r14), %rax
movq 0x30(%r14), %rcx
movq (%rax,%rbx,8), %rax
movss (%r15,%rax,4), %xmm0
movss %xmm0, (%rcx,%rbx,4)
movq 0x38(%r14), %rax
movq %rbx, (%rax,%rbx,8)
incq %rbx
jmp 0x20118
decq %rdx
movq $0x0, 0x20(%rsp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x2059e
movss 0x8(%r14), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
jbe 0x20195
movq 0x38(%r14), %rax
movq (%rax), %rcx
movq 0x20(%r14), %rdx
movq 0x30(%r14), %rsi
movq -0x8(%rax,%rdx,8), %rax
xorps 0xd1e9(%rip), %xmm1     # 0x2d370
movss (%rsi,%rax,4), %xmm0
subss (%rsi,%rcx,4), %xmm0
mulss %xmm0, %xmm1
movaps %xmm1, 0x10(%rsp)
movl (%r14), %ebp
movl %ebp, %edi
callq 0x20079
movl %eax, %eax
movq %rax, 0x38(%rsp)
movq 0x20(%r14), %rcx
movslq 0x4(%r14), %rsi
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
leaq 0x4(%r15), %r12
cvtss2sd 0x10(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
movsd 0xd05a(%rip), %xmm8     # 0x2d238
movq %rax, 0x30(%rsp)
movq %rax, 0x8(%rsp)
movq $0x0, (%rsp)
xorl %esi, %esi
movslq %ecx, %rax
cmpq %rax, %rsi
jge 0x204d0
movslq 0x8(%rsp), %rax
cmpq %rax, %rsi
jl 0x20240
movslq 0x4(%r14), %rax
decq %rax
movq 0x20(%rsp), %rcx
cmpq %rax, %rcx
jge 0x20240
movq %rcx, %rax
movq 0x8(%rsp), %rcx
addq 0x30(%rsp), %rcx
movq %rcx, 0x8(%rsp)
incq %rax
movq %rax, 0x20(%rsp)
movq (%rsp), %rax
addq 0x38(%rsp), %rax
movq %rax, (%rsp)
movslq %ebp, %rdx
movq 0x38(%r14), %rax
movq %rsi, 0x10(%rsp)
movq (%rax,%rsi,8), %rax
imulq %rdx, %rax
shlq $0x3, %rax
movq 0x58(%r14), %rcx
addq %rax, %rcx
addq 0x60(%r14), %rax
testq %rdx, %rdx
movl $0x0, %esi
cmovgq %rdx, %rsi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x203c7
movsd (%rcx,%rdi,8), %xmm0
cvttsd2si %xmm0, %r9
movsd (%rax,%rdi,8), %xmm1
cvtsi2sd %r9, %xmm3
cmpneqpd %xmm0, %xmm3
xorps %xmm2, %xmm2
xorpd %xmm4, %xmm4
cmpnlepd %xmm0, %xmm4
andpd %xmm3, %xmm4
movq %xmm4, %r8
addq %r9, %r8
cvttsd2si %xmm1, %r10
cvtsi2sd %r10, %xmm3
cmpneqpd %xmm1, %xmm3
xorpd %xmm4, %xmm4
cmpnlepd %xmm1, %xmm4
andpd %xmm3, %xmm4
movq %xmm4, %r9
addq %r10, %r9
movq 0x18(%r14), %r10
shlq $0x20, %r10
xorpd %xmm3, %xmm3
testq %r8, %r8
js 0x202fb
testq %r9, %r9
js 0x202fb
movq %r10, %r11
sarq $0x20, %r11
imulq %r9, %r11
leaq (%r15,%r8,4), %rbp
cvtss2sd (%rbp,%r11,4), %xmm3
leaq (%r10,%r13), %r11
sarq $0x20, %r11
cmpq %r11, %r8
jge 0x20321
testq %r9, %r9
js 0x20321
movq %r10, %rbp
sarq $0x20, %rbp
imulq %r9, %rbp
leaq (%r12,%r8,4), %rbx
cvtss2sd (%rbx,%rbp,4), %xmm2
xorpd %xmm4, %xmm4
xorps %xmm5, %xmm5
testq %r8, %r8
js 0x2034a
cmpq %r11, %r9
jge 0x2034a
leaq 0x1(%r9), %rbx
movq %r10, %rbp
sarq $0x20, %rbp
imulq %rbx, %rbp
leaq (%r15,%r8,4), %rbx
cvtss2sd (%rbx,%rbp,4), %xmm5
cmpq %r11, %r8
jge 0x2036a
cmpq %r11, %r9
jge 0x2036a
leaq 0x1(%r9), %r11
sarq $0x20, %r10
imulq %r11, %r10
leaq (%r12,%r8,4), %r11
cvtss2sd (%r11,%r10,4), %xmm4
cvtsi2sd %r8, %xmm6
cvtsi2sd %r9, %xmm7
subsd %xmm6, %xmm0
subsd %xmm7, %xmm1
movq 0x50(%r14), %r8
movapd %xmm8, %xmm6
subsd %xmm1, %xmm6
subsd %xmm3, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
mulsd %xmm6, %xmm2
subsd %xmm5, %xmm4
mulsd %xmm0, %xmm4
addsd %xmm5, %xmm4
mulsd %xmm1, %xmm4
addsd %xmm2, %xmm4
cvtsd2ss %xmm4, %xmm0
movq %rdi, (%r8,%rdi,8)
movq 0x48(%r14), %r8
movss %xmm0, (%r8,%rdi,4)
incq %rdi
jmp 0x20271
decq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20637
movss 0xcf48(%rip), %xmm5     # 0x2d324
movq 0x50(%r14), %rcx
movl (%r14), %ebp
movslq %ebp, %rdx
testq %rdx, %rdx
movl $0x0, %esi
cmovgq %rdx, %rsi
xorl %eax, %eax
xorl %edi, %edi
movsd 0x40(%rsp), %xmm4
cmpq %rsi, %rdi
je 0x2043c
movq (%rcx,%rdi,8), %r8
leaq 0x1(%rdi), %r9
movq %r9, %r10
cmpq %rdx, %r10
jge 0x20427
movq (%rcx,%r10,8), %r11
cmpq (%rcx,%rdi,8), %r11
jbe 0x20422
decq %r11
movq %r11, (%rcx,%r10,8)
incq %r10
jmp 0x2040c
movq %rdx, %r10
subq %rdi, %r10
imulq %rax, %r10
addq %r10, %r8
movq %r8, %rax
movq %r9, %rdi
jmp 0x203fc
testl %ebp, %ebp
movl $0x0, %ecx
cmovgl %ebp, %ecx
xorps %xmm0, %xmm0
xorl %edx, %edx
pushq $0x1
popq %rsi
movsd 0xcde1(%rip), %xmm8     # 0x2d238
cmpq %rcx, %rdx
je 0x204a8
movq %rsi, %rdi
cmpl %edi, %ebp
jle 0x204a0
movq 0x48(%r14), %r8
cvtss2sd (%r8,%rdx,4), %xmm1
cvtss2sd (%r8,%rdi,4), %xmm2
movapd %xmm4, %xmm3
addsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
movaps %xmm5, %xmm3
ja 0x20487
xorps %xmm3, %xmm3
addsd %xmm4, %xmm2
ucomisd %xmm2, %xmm1
movaps %xmm5, %xmm1
ja 0x20497
movaps %xmm3, %xmm1
addss %xmm1, %xmm0
incq %rdi
jmp 0x2045f
incq %rdx
incq %rsi
jmp 0x20457
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,4), %rax
movq (%rsp), %rcx
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
movq 0x10(%rsp), %rsi
incq %rsi
movq 0x20(%r14), %rcx
jmp 0x201f2
movslq 0x10(%r14), %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
xorps %xmm2, %xmm2
movq 0x28(%rsp), %rbx
cmpq %rcx, %rax
je 0x204fc
movss (%rbx,%rcx,4), %xmm0
mulss %xmm0, %xmm0
addss %xmm0, %xmm2
incq %rcx
jmp 0x204e5
cvtss2sd %xmm2, %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2050f
sqrtsd %xmm0, %xmm0
jmp 0x20520
movss %xmm2, 0x10(%rsp)
callq 0x9a00
movss 0x10(%rsp), %xmm2
ucomisd 0xce78(%rip), %xmm0     # 0x2d3a0
jbe 0x20538
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm2
jb 0x20542
sqrtss %xmm2, %xmm0
jmp 0x2054a
movss 0xce70(%rip), %xmm0     # 0x2d3b0
jmp 0x2054a
movaps %xmm2, %xmm0
callq 0x9610
movslq 0x10(%r14), %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
cmpq %rcx, %rax
je 0x2056f
movss (%rbx,%rcx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rbx,%rcx,4)
incq %rcx
jmp 0x20557
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq