// File: /workspace/github_cpp_makefile_star2_O2/ingowald[P]closestSurfacePointQueries/src/distanceQueries.cpp
// Function: bvhlib::oneQuery(bvhlib::QueryResult&, bvhlib::QueryObject*, ospcommon::vec_t<float, 3, 1> const&)
void oneQuery(QueryResult &result,
                QueryObject *qo,
                const vec3fa &point)
  {
    std::priority_queue<std::pair<float,const BVH::Node *>,
                        std::vector<std::pair<float,const BVH::Node *>>,
                        std::greater<std::pair<float,const BVH::Node *>>
                        > traversalQueue;
    
    // push sentinel, so we never have to check if the queue runs dry.
    traversalQueue.push(std::pair<float,const BVH::Node *>
                        (std::numeric_limits<float>::infinity(),NULL));
    
    const std::vector<BVH::Node> &nodeList = qo->bvh.nodeList;
    const BVH::Node *node = &nodeList[0];
    while (node) {
      if (!node->isLeaf) {
        // this is a inner node ...
        const BVH::Node *const child0 = &nodeList[node->child+0];
        const BVH::Node *const child1 = &nodeList[node->child+1];
        const float dist0 = computeDistance(child0,point);
        const float dist1 = computeDistance(child1,point);

        /* fast path: check if closer of the two children is already
           as good as anything the queue has to offer - because if so,
           we can save the pushing and popping of this node, and set
           to it right away */
        if (dist0 < dist1) {
          if (dist0 <= std::min(traversalQueue.top().first,result.distance)) {
            node = child0;
            if (dist1 < result.distance)
              traversalQueue.push(std::pair<float,const BVH::Node*>(dist1,child1));
            continue;
          }
        } else {
          if (dist1 <= std::min(traversalQueue.top().first,result.distance)) {
            node = child1;
            if (dist0 < result.distance)
              traversalQueue.push(std::pair<float,const BVH::Node*>(dist0,child0));
            continue;
          }
        }

        /* fast-path optimization didn't apply - push both children
           (if they can't be culled, of course), and let the queue
           provide the next node */
        if (dist0 < result.distance)
          traversalQueue.push(std::pair<float,const BVH::Node*>(dist0,child0));
        if (dist1 < result.distance)
          traversalQueue.push(std::pair<float,const BVH::Node*>(dist1,child1));
      } else {
        qo->testPrimAndUpdateIfCloser(result,point,node->child);
      }


      /* need to get a next node to traverse, from the trv queue: */
      
      /* note - _not_ checking for empty, since we know to have a sentinel, anyway */
      // if (traversalQueue.empty()) break;
      
      // closest candidate is already too far?
      if (traversalQueue.top().first >= result.distance) break;

      // closest candidate might be closer: pop it and use it
      node = traversalQueue.top().second;


      if (node == NULL) {
        PING;
        PRINT(node);
        PRINT(result.distance);
        PRINT(traversalQueue.top().first);
      }

      traversalQueue.pop();
    }
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x8(%rsp), %rsi
movl $0x7f800000, (%rsi)     # imm = 0x7F800000
andq $0x0, 0x8(%rsi)
callq 0x4cc2
movq 0x8(%r14), %r12
testq %r12, %r12
je 0x47b0
cmpl $0x0, 0xc(%r12)
movl 0x1c(%r12), %r12d
je 0x46a0
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq *0x20(%rax)
movq 0x20(%rsp), %rax
movss (%rax), %xmm0
ucomiss 0x10(%r15), %xmm0
jae 0x47b0
movq 0x8(%rax), %r12
testq %r12, %r12
jne 0x4691
movq 0x7a18(%rip), %rdi      # 0xbfd0
leaq 0x4095(%rip), %rsi      # 0x8654
callq 0x21c0
movq %rax, %rdi
leaq 0x3d12(%rip), %rsi      # 0x82e0
callq 0x21c0
movq %rax, %rdi
leaq 0x4095(%rip), %rsi      # 0x8672
callq 0x21c0
movq %rax, %rdi
callq 0x20f0
movq 0x79df(%rip), %rdi      # 0xbfd0
leaq 0x40be(%rip), %rsi      # 0x86b6
callq 0x21c0
movq %rax, %rdi
leaq 0x40b4(%rip), %rsi      # 0x86bb
callq 0x21c0
movq %rax, %rdi
xorl %esi, %esi
callq 0x2180
movq %rax, %rdi
callq 0x20f0
movq 0x79ab(%rip), %rdi      # 0xbfd0
leaq 0x4093(%rip), %rsi      # 0x86bf
callq 0x21c0
movq %rax, %rdi
leaq 0x4080(%rip), %rsi      # 0x86bb
callq 0x21c0
movss 0x10(%r15), %xmm0
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20f0
movq 0x7973(%rip), %rdi      # 0xbfd0
leaq 0x406b(%rip), %rsi      # 0x86cf
callq 0x21c0
movq %rax, %rdi
leaq 0x4048(%rip), %rsi      # 0x86bb
callq 0x21c0
movq 0x20(%rsp), %rcx
movss (%rcx), %xmm0
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20f0
leaq 0x20(%rsp), %rdi
callq 0x4d2e
jmp 0x4564
movq 0x8(%r14), %r13
movq %r12, %rbp
shlq $0x5, %rbp
addq %r13, %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x4cd8
movss %xmm0, 0x1c(%rsp)
incl %r12d
shlq $0x5, %r12
addq %r12, %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4cd8
movss 0x1c(%rsp), %xmm3
ucomiss %xmm3, %xmm0
movq 0x20(%rsp), %rax
movss 0x10(%r15), %xmm2
movaps %xmm2, %xmm1
minss (%rax), %xmm1
jbe 0x4724
ucomiss %xmm3, %xmm1
jb 0x4729
ucomiss %xmm0, %xmm2
movq %rbp, %r12
jbe 0x4564
movss %xmm0, 0x8(%rsp)
movq %r13, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4cc2
movq %rbp, %r12
jmp 0x4564
ucomiss %xmm0, %xmm1
jae 0x4782
ucomiss %xmm3, %xmm2
jbe 0x475a
movss %xmm0, 0x18(%rsp)
movss %xmm3, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4cc2
movss 0x10(%r15), %xmm2
movss 0x18(%rsp), %xmm0
ucomiss %xmm0, %xmm2
jbe 0x4590
movss %xmm0, 0x8(%rsp)
movq %r13, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4cc2
jmp 0x4590
ucomiss %xmm3, %xmm2
movq %r13, %r12
jbe 0x4564
movss %xmm3, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4cc2
movq %r13, %r12
jmp 0x4564
leaq 0x20(%rsp), %rdi
callq 0x4d9e
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x47d7
jmp 0x47d7
jmp 0x47d7
jmp 0x47d7
jmp 0x47d7
jmp 0x47d7
jmp 0x47d7
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x4d9e
movq %rbx, %rdi
callq 0x2310