// File: /workspace/github_c_makefile_star2_O3/zpole[P]IICV/vl/covdet.c
// Function: vl_refine_local_extreum_2
VL_EXPORT vl_bool
vl_refine_local_extreum_2 (VlCovDetExtremum2 * refined,
                           float const * map,
                           vl_size width, vl_size height,
                           vl_index x, vl_index y)
{
  vl_size const xo = 1 ;
  vl_size const yo = width ;

  double Dx=0,Dy=0,Dxx=0,Dyy=0,Dxy=0;
  double A [2*2], b [2] ;

#define at(dx,dy) (*(pt + (dx)*xo + (dy)*yo ))
#define Aat(i,j) (A[(i)+(j)*2])

  float const * pt ;
  vl_index dx = 0 ;
  vl_index dy = 0 ;
  vl_index iter ;
  int err ;

  assert (map) ;
  assert (1 <= x && x <= (signed)width - 2) ;
  assert (1 <= y && y <= (signed)height - 2) ;

  for (iter = 0 ; iter < 5 ; ++iter) {
    x += dx ;
    y += dy ;
    pt = map + x*xo + y*yo  ;

    /* compute the gradient */
    Dx = 0.5 * (at(+1,0) - at(-1,0)) ;
    Dy = 0.5 * (at(0,+1) - at(0,-1));

    /* compute the Hessian */
    Dxx = (at(+1,0) + at(-1,0) - 2.0 * at(0,0)) ;
    Dyy = (at(0,+1) + at(0,-1) - 2.0 * at(0,0)) ;
    Dxy = 0.25 * (at(+1,+1) + at(-1,-1) - at(-1,+1) - at(+1,-1)) ;

    /* solve linear system */
    Aat(0,0) = Dxx ;
    Aat(1,1) = Dyy ;
    Aat(0,1) = Aat(1,0) = Dxy ;

    b[0] = - Dx ;
    b[1] = - Dy ;

    err = vl_solve_linear_system_2(b, A, b) ;

    if (err != VL_ERR_OK) {
      b[0] = 0 ;
      b[1] = 0 ;
      break ;
    }

    /* Keep going if there is sufficient translation */

    dx = (b[0] > 0.6 && x < (signed)width - 2 ?  1 : 0)
    + (b[0] < -0.6 && x > 1 ? -1 : 0) ;

    dy = (b[1] > 0.6 && y < (signed)height - 2 ?  1 : 0)
    + (b[1] < -0.6 && y > 1 ? -1 : 0) ;

    if (dx == 0 && dy == 0) break ;
  }

  /* check threshold and other conditions */
  {
    double peakScore = at(0,0) + 0.5 * (Dx * b[0] + Dy * b[1]) ;
    double alpha = (Dxx+Dyy)*(Dxx+Dyy) / (Dxx*Dyy - Dxy*Dxy) ;
    double edgeScore ;

    if (alpha < 0) {
      /* not an extremum */
      edgeScore = VL_INFINITY_D ;
    } else {
      edgeScore = (0.5*alpha - 1) + sqrt(VL_MAX(0.25*alpha - 1,0)*alpha) ;
    }

    refined->xi = x ;
    refined->yi = y ;
    refined->x = x + b[0] ;
    refined->y = y + b[1] ;
    refined->peakScore = peakScore ;
    refined->edgeScore = edgeScore ;

    return
    err == VL_ERR_OK &&
    vl_abs_d(b[0]) < 1.5 &&
    vl_abs_d(b[1]) < 1.5 &&
    0 <= refined->x && refined->x <= (signed)width - 1 &&
    0 <= refined->y && refined->y <= (signed)height - 1 ;
  }
#undef Aat
#undef at
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, %r12
movq %r8, %r13
movq %rsi, 0x8(%rsp)
movq %rdi, 0x50(%rsp)
movq %rdx, %r14
negq %r14
movq %rdx, 0x28(%rsp)
leal -0x2(%rdx), %eax
cltq
movq %rax, 0x60(%rsp)
movq %rcx, 0x48(%rsp)
leal -0x2(%rcx), %eax
cltq
movq %rax, 0x58(%rsp)
movl $0x4, %r15d
xorl %ecx, %ecx
xorl %eax, %eax
addq %rcx, %r12
addq %rax, %r13
movq 0x8(%rsp), %rax
leaq (%rax,%r13,4), %rax
movq %r12, %rcx
movq 0x28(%rsp), %rdx
imulq %rdx, %rcx
leaq (%rax,%rcx,4), %rbx
movss -0x4(%rbx), %xmm0
movss 0x4(%rbx), %xmm1
movss (%rbx,%r14,4), %xmm2
movaps %xmm1, %xmm3
addss %xmm0, %xmm3
cvtss2sd %xmm3, %xmm6
xorps %xmm3, %xmm3
cvtss2sd (%rbx), %xmm3
mulsd 0x26913(%rip), %xmm3    # 0x321e8
addsd %xmm3, %xmm6
movss (%rbx,%rdx,4), %xmm4
movss 0x4(%rbx,%rdx,4), %xmm5
unpcklps %xmm4, %xmm1            # xmm1 = xmm1[0],xmm4[0],xmm1[1],xmm4[1]
addss %xmm2, %xmm4
cvtss2sd %xmm4, %xmm4
addsd %xmm3, %xmm4
addss -0x4(%rbx,%r14,4), %xmm5
subss -0x4(%rbx,%rdx,4), %xmm5
subss 0x4(%rbx,%r14,4), %xmm5
xorps %xmm3, %xmm3
cvtss2sd %xmm5, %xmm3
mulsd 0x268da(%rip), %xmm3    # 0x321f0
movsd %xmm6, 0x78(%rsp)
movsd %xmm6, 0x80(%rsp)
movsd %xmm4, 0x70(%rsp)
movsd %xmm4, 0x98(%rsp)
movsd %xmm3, 0x88(%rsp)
movsd %xmm3, 0x68(%rsp)
movsd %xmm3, 0x90(%rsp)
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subps %xmm0, %xmm1
cvtps2pd %xmm1, %xmm0
mulpd 0x26793(%rip), %xmm0    # 0x320f0
movapd %xmm0, 0x30(%rsp)
xorpd 0x26765(%rip), %xmm0    # 0x320d0
movapd %xmm0, 0x10(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq %rdi, %rdx
callq 0x98e0
movl %eax, %ebp
testl %eax, %eax
jne 0xba1c
movsd 0x10(%rsp), %xmm1
movsd 0x2685a(%rip), %xmm2    # 0x321f8
ucomisd %xmm2, %xmm1
seta %al
movsd 0x18(%rsp), %xmm0
cmpq 0x60(%rsp), %r13
setl %cl
andb %al, %cl
movsd 0x26843(%rip), %xmm3    # 0x32200
ucomisd %xmm1, %xmm3
seta %dl
movzbl %cl, %eax
cmpq $0x2, %r13
setge %cl
andb %dl, %cl
movzbl %cl, %ecx
subq %rcx, %rax
ucomisd %xmm2, %xmm0
seta %cl
cmpq 0x58(%rsp), %r12
setl %dl
andb %cl, %dl
ucomisd %xmm0, %xmm3
movzbl %dl, %ecx
seta %dl
cmpq $0x2, %r12
setge %sil
andb %dl, %sil
movzbl %sil, %edx
subq %rdx, %rcx
subq $0x1, %r15
setb %dl
movq %rax, %rsi
orq %rcx, %rsi
je 0xba2e
testb %dl, %dl
je 0xb88c
jmp 0xba2e
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
cvtss2sd (%rbx), %xmm2
movapd 0x30(%rsp), %xmm4
movapd %xmm4, %xmm3
unpckhpd %xmm4, %xmm3            # xmm3 = xmm3[1],xmm4[1]
mulsd %xmm0, %xmm3
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
mulsd 0x26789(%rip), %xmm4    # 0x321e0
addsd %xmm2, %xmm4
movapd %xmm4, 0x30(%rsp)
movsd 0x78(%rsp), %xmm3
movapd %xmm3, %xmm2
movsd 0x70(%rsp), %xmm4
addsd %xmm4, %xmm2
mulsd %xmm2, %xmm2
movsd 0x68(%rsp), %xmm5
mulsd %xmm5, %xmm5
mulsd %xmm4, %xmm3
subsd %xmm5, %xmm3
divsd %xmm3, %xmm2
xorpd %xmm3, %xmm3
ucomisd %xmm2, %xmm3
jbe 0xbaa3
movss 0x26807(%rip), %xmm4    # 0x322a8
jmp 0xbb04
movsd 0x26735(%rip), %xmm4    # 0x321e0
mulsd %xmm2, %xmm4
movsd 0x26751(%rip), %xmm1    # 0x32208
addsd %xmm1, %xmm4
movsd 0x2672d(%rip), %xmm0    # 0x321f0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
maxsd %xmm3, %xmm0
mulsd %xmm2, %xmm0
ucomisd %xmm3, %xmm0
jb 0xbadf
sqrtsd %xmm0, %xmm0
jmp 0xbaf0
movsd %xmm4, 0x8(%rsp)
callq 0x9990
movsd 0x8(%rsp), %xmm4
addsd %xmm0, %xmm4
cvtsd2ss %xmm4, %xmm4
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm0
xorl %eax, %eax
movq 0x50(%rsp), %rcx
movq %r13, (%rcx)
movq %r12, 0x8(%rcx)
xorps %xmm2, %xmm2
cvtsi2sd %r13, %xmm2
addsd %xmm1, %xmm2
xorps %xmm3, %xmm3
cvtsd2ss %xmm2, %xmm3
xorps %xmm2, %xmm2
cvtsi2sd %r12, %xmm2
addsd %xmm0, %xmm2
unpcklpd 0x30(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
cvtpd2ps %xmm2, %xmm2
movss %xmm3, 0x10(%rcx)
movlpd %xmm2, 0x14(%rcx)
movss %xmm4, 0x1c(%rcx)
testl %ebp, %ebp
jne 0xbbac
movapd 0x265aa(%rip), %xmm4    # 0x32100
andpd %xmm4, %xmm1
movsd 0x266ae(%rip), %xmm5    # 0x32210
ucomisd %xmm1, %xmm5
jbe 0xbbac
andpd %xmm4, %xmm0
ucomisd %xmm0, %xmm5
jbe 0xbbac
xorpd %xmm0, %xmm0
ucomiss %xmm0, %xmm3
jb 0xbbac
movq 0x28(%rsp), %rcx
decl %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
ucomiss %xmm3, %xmm0
jb 0xbbac
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm2
jb 0xbbac
movq 0x48(%rsp), %rax
decl %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
xorl %eax, %eax
ucomiss %xmm2, %xmm0
setae %al
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq