// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/msh2df/../../pkgs/R3Shapes/R3Kdtree.cpp
// Function: gaps::R3Kdtree<Point*>::FindClosest(gaps::R3KdtreeNode<Point*>*, gaps::R3Box const&, Point*, gaps::R3Point const&, double, double, int (*)(Point*, Point*, void*), void*, Point*&, double&) const
void R3Kdtree<PtrType>::
FindClosest(R3KdtreeNode<PtrType> *node, const R3Box& node_box, 
  PtrType query_point, const R3Point& query_position, 
  RNScalar min_distance_squared, RNScalar max_distance_squared,
  int (*IsCompatible)(PtrType, PtrType, void *), void *compatible_data, 
  PtrType& closest_point, RNScalar& closest_distance_squared) const
{
  // Check if node is interior
  if (node->children[0]) {
    assert(node->children[1]);

    // Find and check axial distances from point to node box
    RNLength dx, dy, dz;
    if (RNIsGreater(query_position.X(), node_box.XMax())) dx = query_position.X() - node_box.XMax();
    else if (RNIsLess(query_position.X(), node_box.XMin())) dx = node_box.XMin()- query_position.X();
    else dx = 0.0;
    RNLength dx_squared = dx * dx;
    if (dx_squared >= closest_distance_squared) return;
    if (RNIsGreater(query_position.Y(), node_box.YMax())) dy = query_position.Y() - node_box.YMax();
    else if (RNIsLess(query_position.Y(), node_box.YMin())) dy = node_box.YMin()- query_position.Y();
    else dy = 0.0;
    RNLength dy_squared = dy * dy;
    if (dy_squared >= closest_distance_squared) return;
    if (RNIsGreater(query_position.Z(), node_box.ZMax())) dz = query_position.Z() - node_box.ZMax();
    else if (RNIsLess(query_position.Z(), node_box.ZMin())) dz = node_box.ZMin()- query_position.Z();
    else dz = 0.0;
    RNLength dz_squared = dz * dz;
    if (dz_squared >= closest_distance_squared) return;
    
    // Find and check actual distance from point to node box
    RNLength distance_squared = 0;
    if ((dy == 0.0) && (dz == 0.0)) distance_squared = dx_squared;
    else if ((dx == 0.0) && (dz == 0.0)) distance_squared = dy_squared;
    else if ((dx == 0.0) && (dy == 0.0)) distance_squared = dz_squared;
    else distance_squared = dx_squared + dy_squared + dz_squared;
    if (distance_squared >= closest_distance_squared) return;

    // Compute distance from point to split plane
    RNLength side = query_position[node->split_dimension] - node->split_coordinate;

    // Search children nodes
    if (side <= 0) {
      // Search negative side first
      R3Box child_box(node_box);
      child_box[RN_HI][node->split_dimension] = node->split_coordinate;
      FindClosest(node->children[0], child_box, 
        query_point, query_position, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data,
        closest_point, closest_distance_squared);
      if (side*side < closest_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_LO][node->split_dimension] = node->split_coordinate;
        FindClosest(node->children[1], child_box, 
          query_point, query_position, 
          min_distance_squared, max_distance_squared, 
          IsCompatible, compatible_data,
          closest_point, closest_distance_squared);
      }
    }
    else {
      // Search positive side first
      R3Box child_box(node_box);
      child_box[RN_LO][node->split_dimension] = node->split_coordinate;
      FindClosest(node->children[1], child_box, 
        query_point, query_position, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data,
        closest_point, closest_distance_squared);
      if (side*side < closest_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_HI][node->split_dimension] = node->split_coordinate;
        FindClosest(node->children[0], child_box, 
          query_point, query_position, 
          min_distance_squared, max_distance_squared, 
          IsCompatible, compatible_data,
          closest_point, closest_distance_squared);
      }
    }
  }
  else {
    for (int i = 0; i < node->npoints; i++) {
      PtrType point = node->points[i];
      RNLength distance_squared = R3SquaredDistance(query_position, Position(point));
      if ((distance_squared >= min_distance_squared) && 
         (distance_squared <= closest_distance_squared)) {
        if (!IsCompatible || !query_point || IsCompatible(query_point, point, compatible_data)) {
          closest_distance_squared = distance_squared;
          closest_point = point;
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, 0x8(%rsp)
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %rcx, (%rsp)
movq %rdi, 0x18(%rsp)
movq 0xf0(%rsp), %r15
cmpq $0x0, 0x8(%rsi)
movq %rsi, 0x28(%rsp)
je 0x15a5d
movq %rdx, %r14
movq %r8, %rcx
movsd (%r8), %xmm0
leaq 0xda28f(%rip), %rax     # 0xefba8
movsd (%rax), %xmm3
movapd %xmm0, %xmm2
subsd 0x20(%rdx), %xmm2
ucomisd %xmm3, %xmm2
ja 0x15958
movsd 0x8(%r14), %xmm1
movapd %xmm0, %xmm4
subsd %xmm1, %xmm4
movapd 0x946ee(%rip), %xmm5    # 0xaa030
xorpd %xmm3, %xmm5
xorpd %xmm2, %xmm2
ucomisd %xmm4, %xmm5
jbe 0x15958
subsd %xmm0, %xmm1
movapd %xmm1, %xmm2
movapd %xmm2, %xmm1
mulsd %xmm2, %xmm1
movsd (%r15), %xmm0
ucomisd %xmm0, %xmm1
jae 0x15b09
movq %r15, %r12
movsd 0x8(%rcx), %xmm5
movapd %xmm5, %xmm4
subsd 0x28(%r14), %xmm4
ucomisd %xmm3, %xmm4
ja 0x159b6
movsd 0x10(%r14), %xmm6
movapd %xmm5, %xmm7
subsd %xmm6, %xmm7
movapd 0x94692(%rip), %xmm8    # 0xaa030
xorpd %xmm3, %xmm8
xorpd %xmm4, %xmm4
ucomisd %xmm7, %xmm8
jbe 0x159b6
subsd %xmm5, %xmm6
movapd %xmm6, %xmm4
movapd %xmm4, %xmm5
mulsd %xmm4, %xmm5
ucomisd %xmm0, %xmm5
jae 0x15b09
movsd 0x10(%rcx), %xmm7
movapd %xmm7, %xmm6
subsd 0x30(%r14), %xmm6
ucomisd %xmm3, %xmm6
ja 0x15a0a
movsd 0x18(%r14), %xmm8
movapd %xmm7, %xmm9
xorpd 0x94640(%rip), %xmm3    # 0xaa030
subsd %xmm8, %xmm9
xorpd %xmm6, %xmm6
ucomisd %xmm9, %xmm3
jbe 0x15a0a
subsd %xmm7, %xmm8
movapd %xmm8, %xmm6
movapd %xmm6, %xmm3
mulsd %xmm6, %xmm3
ucomisd %xmm0, %xmm3
jae 0x15b09
xorpd %xmm7, %xmm7
ucomisd %xmm7, %xmm4
jne 0x15a34
jp 0x15a34
ucomisd %xmm7, %xmm6
jne 0x15a34
jnp 0x15b3d
ucomisd %xmm7, %xmm2
jne 0x15b1b
jp 0x15b1b
ucomisd %xmm7, %xmm6
jne 0x15b1b
jp 0x15b1b
movapd %xmm5, %xmm1
jmp 0x15b3d
cmpl $0x0, 0x128(%rsi)
jle 0x15b09
movq %r8, %rbp
cmpq $0x0, 0x8(%rsp)
setne %al
cmpq $0x0, (%rsp)
setne %r12b
andb %al, %r12b
xorl %r13d, %r13d
leaq 0x70(%rsp), %r14
movq 0x28(%rsi,%r13,8), %rbx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
callq 0x14f6e
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2070f
ucomisd 0x20(%rsp), %xmm0
jb 0x15af5
movsd (%r15), %xmm1
ucomisd %xmm0, %xmm1
jb 0x15af5
testb %r12b, %r12b
je 0x15ae5
movq (%rsp), %rdi
movq %rbx, %rsi
movq 0xe0(%rsp), %rdx
movsd %xmm0, 0x10(%rsp)
callq *0x8(%rsp)
movsd 0x10(%rsp), %xmm0
testl %eax, %eax
je 0x15af5
movsd %xmm0, (%r15)
movq 0xe8(%rsp), %rax
movq %rbx, (%rax)
incq %r13
movq 0x28(%rsp), %rsi
movslq 0x128(%rsi), %rax
cmpq %rax, %r13
jl 0x15a8a
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm2
jne 0x15b35
jp 0x15b35
ucomisd %xmm6, %xmm4
jne 0x15b35
jp 0x15b35
movapd %xmm3, %xmm1
jmp 0x15b3d
addsd %xmm5, %xmm1
addsd %xmm3, %xmm1
ucomisd %xmm0, %xmm1
jae 0x15b09
movslq 0x20(%rsi), %rax
movq %rcx, %r15
movsd (%rcx,%rax,8), %xmm1
subsd 0x18(%rsi), %xmm1
xorpd %xmm0, %xmm0
movsd %xmm1, 0x30(%rsp)
ucomisd %xmm1, %xmm0
movq %rsi, %r13
jae 0x15c57
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3aa88
movsd 0x18(%r13), %xmm0
movslq 0x20(%r13), %rax
movsd %xmm0, 0x78(%rsp,%rax,8)
movq 0x10(%r13), %rsi
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq %rbx, %rdx
movq 0x8(%rsp), %rcx
movq %r15, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movq 0x10(%rsp), %r9
pushq %r12
pushq 0xf8(%rsp)
pushq 0xf8(%rsp)
callq 0x158c4
addq $0x20, %rsp
movsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd (%r12), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x15c52
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x3aa88
movq 0x28(%rsp), %rcx
movsd 0x18(%rcx), %xmm0
movslq 0x20(%rcx), %rax
movsd %xmm0, 0x58(%rsp,%rax,8)
movq 0x8(%rcx), %rsi
subq $0x8, %rsp
leaq 0x40(%rsp), %rdx
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rcx
movq %r15, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movq 0x10(%rsp), %r9
pushq %r12
pushq 0xf8(%rsp)
pushq 0xf8(%rsp)
callq 0x158c4
addq $0x20, %rsp
leaq 0x38(%rsp), %rdi
callq 0x3b8a8
jmp 0x15d41
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3aa88
movsd 0x18(%r13), %xmm0
movslq 0x20(%r13), %rax
movsd %xmm0, 0x90(%rsp,%rax,8)
movq 0x8(%r13), %rsi
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq %rbx, %rdx
movq 0x8(%rsp), %rcx
movq %r15, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movq 0x10(%rsp), %r9
pushq %r12
pushq 0xf8(%rsp)
pushq 0xf8(%rsp)
callq 0x158c4
addq $0x20, %rsp
movsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd (%r12), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x15d41
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x3aa88
movq 0x28(%rsp), %rcx
movsd 0x18(%rcx), %xmm0
movslq 0x20(%rcx), %rax
movsd %xmm0, 0x40(%rsp,%rax,8)
movq 0x10(%rcx), %rsi
subq $0x8, %rsp
leaq 0x40(%rsp), %rdx
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rcx
movq %r15, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movq 0x10(%rsp), %r9
pushq %r12
pushq 0xf8(%rsp)
pushq 0xf8(%rsp)
callq 0x158c4
addq $0x20, %rsp
leaq 0x38(%rsp), %rdi
callq 0x3b8a8
leaq 0x70(%rsp), %rdi
callq 0x3b8a8
jmp 0x15b09
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3b8a8
jmp 0x15d7a
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3b8a8
jmp 0x15d7a
jmp 0x15d72
jmp 0x15d77
movq %rax, %rbx
jmp 0x15d7a
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3b8a8
movq %rbx, %rdi
callq 0xe7f0