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

  double Dx=0,Dy=0,Dz=0,Dxx=0,Dyy=0,Dzz=0,Dxy=0,Dxz=0,Dyz=0 ;
  double A [3*3], b [3] ;

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

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

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

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

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

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

    Dxy = 0.25 * (at(+1,+1,0) + at(-1,-1,0) - at(-1,+1,0) - at(+1,-1,0)) ;
    Dxz = 0.25 * (at(+1,0,+1) + at(-1,0,-1) - at(-1,0,+1) - at(+1,0,-1)) ;
    Dyz = 0.25 * (at(0,+1,+1) + at(0,-1,-1) - at(0,-1,+1) - at(0,+1,-1)) ;

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

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

    err = vl_solve_linear_system_3(b, A, b) ;

    if (err != VL_ERR_OK) {
      b[0] = 0 ;
      b[1] = 0 ;
      b[2] = 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)
    + 0.5 * (Dx * b[0] + Dy * b[1] + Dz * b[2]) ;
    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->zi = z ;
    refined->x = x + b[0] ;
    refined->y = y + b[1] ;
    refined->z = z + b[2] ;
    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 &&
    vl_abs_d(b[2]) < 1.5 &&
    0 <= refined->x && refined->x <= (signed)width - 1 &&
    0 <= refined->y && refined->y <= (signed)height - 1 &&
    0 <= refined->z && refined->z <= (signed)depth - 1 ;
  }
#undef Aat
#undef at
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r9, %r13
movq %r8, 0x48(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x58(%rsp)
movq 0x130(%rsp), %rbx
movq %rcx, %rbp
imulq %rdx, %rbp
movq %rbp, %rax
imulq 0x138(%rsp), %rax
movq %rax, 0x88(%rsp)
movq %rdx, %rax
negq %rax
movq %rax, 0x80(%rsp)
movq %rbp, %r14
negq %r14
movq %rdx, 0x28(%rsp)
leal -0x2(%rdx), %eax
cltq
movq %rax, 0x68(%rsp)
movq %rcx, 0x50(%rsp)
leal -0x2(%rcx), %eax
cltq
movq %rax, 0x60(%rsp)
movl $0x4, %r15d
xorl %ecx, %ecx
xorl %eax, %eax
addq %rcx, %rbx
addq %rax, %r13
movq 0x8(%rsp), %rax
leaq (%rax,%r13,4), %rax
movq %rbx, %rcx
movq 0x28(%rsp), %rdx
imulq %rdx, %rcx
leaq (%rax,%rcx,4), %rax
movq 0x88(%rsp), %rcx
leaq (%rax,%rcx,4), %r12
movss -0x4(%r12), %xmm1
movss 0x4(%r12), %xmm0
leaq (%r12,%rdx,4), %rax
movss (%rax), %xmm5
movq 0x80(%rsp), %rcx
leaq (%r12,%rcx,4), %rcx
movss (%rcx), %xmm2
movss (%r12,%rbp,4), %xmm4
movss (%r12,%r14,4), %xmm6
movaps %xmm4, %xmm3
subss %xmm6, %xmm3
movaps %xmm0, %xmm7
addss %xmm1, %xmm7
cvtss2sd %xmm7, %xmm8
xorps %xmm7, %xmm7
cvtss2sd (%r12), %xmm7
mulsd 0x26dc6(%rip), %xmm7    # 0x321e8
addsd %xmm7, %xmm8
unpcklps %xmm5, %xmm0            # xmm0 = xmm0[0],xmm5[0],xmm0[1],xmm5[1]
addss %xmm2, %xmm5
cvtss2sd %xmm5, %xmm9
addsd %xmm7, %xmm9
addss %xmm6, %xmm4
cvtss2sd %xmm4, %xmm4
addsd %xmm7, %xmm4
movss 0x4(%r12,%rbp,4), %xmm5
movss 0x4(%rax), %xmm6
unpcklps %xmm5, %xmm6            # xmm6 = xmm6[0],xmm5[0],xmm6[1],xmm5[1]
movss -0x4(%r12,%r14,4), %xmm5
movss -0x4(%rcx), %xmm7
unpcklps %xmm5, %xmm7            # xmm7 = xmm7[0],xmm5[0],xmm7[1],xmm5[1]
addps %xmm6, %xmm7
movss -0x4(%r12,%rbp,4), %xmm5
movss -0x4(%rax), %xmm6
unpcklps %xmm5, %xmm6            # xmm6 = xmm6[0],xmm5[0],xmm6[1],xmm5[1]
subps %xmm6, %xmm7
movss 0x4(%r12,%r14,4), %xmm5
movss 0x4(%rcx), %xmm6
unpcklps %xmm5, %xmm6            # xmm6 = xmm6[0],xmm5[0],xmm6[1],xmm5[1]
subps %xmm6, %xmm7
movss (%rax,%rbp,4), %xmm5
addss (%rcx,%r14,4), %xmm5
subss (%rcx,%rbp,4), %xmm5
subss (%rax,%r14,4), %xmm5
movsd %xmm8, 0x78(%rsp)
movsd %xmm8, 0xb0(%rsp)
movsd %xmm9, 0x70(%rsp)
movsd %xmm9, 0xd0(%rsp)
movsd %xmm4, 0xf0(%rsp)
unpcklps %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
subps %xmm1, %xmm0
xorps %xmm2, %xmm2
cvtss2sd %xmm3, %xmm2
mulsd 0x26d01(%rip), %xmm2    # 0x321e0
xorps %xmm1, %xmm1
cvtss2sd %xmm5, %xmm1
cvtps2pd %xmm7, %xmm3
mulsd 0x26cff(%rip), %xmm1    # 0x321f0
mulpd 0x26be7(%rip), %xmm3    # 0x320e0
movlpd %xmm3, 0xc8(%rsp)
movupd %xmm3, 0xb8(%rsp)
movapd %xmm3, 0xa0(%rsp)
movhpd %xmm3, 0xe0(%rsp)
movsd %xmm1, 0xd8(%rsp)
cvtps2pd %xmm0, %xmm0
movsd %xmm1, 0xe8(%rsp)
mulpd 0x26bb6(%rip), %xmm0    # 0x320f0
movapd %xmm0, 0x90(%rsp)
movapd 0x26b85(%rip), %xmm1    # 0x320d0
xorpd %xmm1, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd %xmm2, 0x30(%rsp)
movapd %xmm2, %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, 0x20(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movq %rdi, %rdx
callq 0x9410
movl %eax, %edi
testl %eax, %eax
jne 0xb61a
movsd 0x10(%rsp), %xmm1
movsd 0x26c62(%rip), %xmm2    # 0x321f8
ucomisd %xmm2, %xmm1
seta %al
movsd 0x18(%rsp), %xmm0
cmpq 0x68(%rsp), %r13
setl %cl
andb %al, %cl
movsd 0x26c4b(%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 0x60(%rsp), %rbx
setl %dl
andb %cl, %dl
ucomisd %xmm0, %xmm3
movzbl %dl, %ecx
seta %dl
cmpq $0x2, %rbx
setge %sil
andb %dl, %sil
movzbl %sil, %edx
subq %rdx, %rcx
subq $0x1, %r15
setb %dl
movq %rax, %rsi
orq %rcx, %rsi
je 0xb612
testb %dl, %dl
je 0xb3a1
movsd 0x20(%rsp), %xmm2
jmp 0xb639
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm3, %xmm3
cvtss2sd (%r12), %xmm3
movapd 0x90(%rsp), %xmm5
movapd %xmm5, %xmm4
unpckhpd %xmm5, %xmm4            # xmm4 = xmm4[1],xmm5[1]
mulsd %xmm0, %xmm4
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm5
movapd 0x30(%rsp), %xmm7
mulsd %xmm2, %xmm7
addsd %xmm5, %xmm7
mulsd 0x26b6b(%rip), %xmm7    # 0x321e0
addsd %xmm3, %xmm7
movsd 0x78(%rsp), %xmm4
movapd %xmm4, %xmm3
movsd 0x70(%rsp), %xmm5
addsd %xmm5, %xmm3
mulsd %xmm3, %xmm3
movapd 0xa0(%rsp), %xmm6
mulsd %xmm6, %xmm6
mulsd %xmm5, %xmm4
subsd %xmm6, %xmm4
divsd %xmm4, %xmm3
xorpd %xmm4, %xmm4
ucomisd %xmm3, %xmm4
jbe 0xb6be
movss 0x26bec(%rip), %xmm6    # 0x322a8
jmp 0xb738
movsd 0x26b1a(%rip), %xmm2    # 0x321e0
mulsd %xmm3, %xmm2
movsd 0x26b36(%rip), %xmm1    # 0x32208
addsd %xmm1, %xmm2
movsd 0x26b12(%rip), %xmm0    # 0x321f0
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
maxsd %xmm4, %xmm0
mulsd %xmm3, %xmm0
ucomisd %xmm4, %xmm0
jb 0xb6fa
sqrtsd %xmm0, %xmm0
jmp 0xb71b
movapd %xmm7, 0x30(%rsp)
movl %edi, %ebp
movsd %xmm2, 0x8(%rsp)
callq 0x9990
movsd 0x8(%rsp), %xmm2
movl %ebp, %edi
movapd 0x30(%rsp), %xmm7
addsd %xmm0, %xmm2
xorps %xmm6, %xmm6
cvtsd2ss %xmm2, %xmm6
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm2
xorl %eax, %eax
movq 0x58(%rsp), %rcx
movq %r13, (%rcx)
movq %rbx, 0x8(%rcx)
xorps %xmm3, %xmm3
cvtsi2sd %r13, %xmm3
movq 0x138(%rsp), %rdx
movq %rdx, 0x10(%rcx)
addsd %xmm1, %xmm3
xorps %xmm5, %xmm5
cvtsd2ss %xmm3, %xmm5
movss %xmm5, 0x18(%rcx)
xorps %xmm3, %xmm3
cvtsi2sd %rbx, %xmm3
addsd %xmm0, %xmm3
xorps %xmm4, %xmm4
cvtsd2ss %xmm3, %xmm4
xorps %xmm3, %xmm3
cvtsi2sd %rdx, %xmm3
addsd %xmm2, %xmm3
unpcklpd %xmm7, %xmm3            # xmm3 = xmm3[0],xmm7[0]
cvtpd2ps %xmm3, %xmm3
movss %xmm4, 0x1c(%rcx)
movlpd %xmm3, 0x20(%rcx)
movss %xmm6, 0x28(%rcx)
testl %edi, %edi
jne 0xb82b
movapd 0x26950(%rip), %xmm6    # 0x32100
andpd %xmm6, %xmm1
movsd 0x26a54(%rip), %xmm7    # 0x32210
ucomisd %xmm1, %xmm7
jbe 0xb82b
andpd %xmm6, %xmm0
ucomisd %xmm0, %xmm7
jbe 0xb82b
andpd %xmm6, %xmm2
ucomisd %xmm2, %xmm7
jbe 0xb82b
xorpd %xmm0, %xmm0
ucomiss %xmm0, %xmm5
jb 0xb82b
movq 0x28(%rsp), %rcx
decl %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
ucomiss %xmm5, %xmm0
jb 0xb82b
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm4
jb 0xb82b
movq 0x50(%rsp), %rcx
decl %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
ucomiss %xmm4, %xmm0
jb 0xb82b
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm3
jb 0xb82b
movq 0x48(%rsp), %rax
decl %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
xorl %eax, %eax
ucomiss %xmm3, %xmm0
setae %al
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq