// File: /workspace/github_cpp_makefile_star2_O1/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, %rbp
movq %rdi, %r15
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
leaq 0x10(%rsp), %rsi
movl $0x7f800000, (%rsi)     # imm = 0x7F800000
movq %rax, 0x8(%rsi)
callq 0x5622
movq 0x8(%rbp), %r12
testq %r12, %r12
je 0x5086
movq %rbp, 0x40(%rsp)
cmpl $0x0, 0xc(%r12)
movl 0x1c(%r12), %r13d
je 0x4cc7
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq *0x20(%rax)
jmp 0x4ec2
movq 0x8(%rbp), %r14
movq %r13, %rbp
shlq $0x5, %rbp
movss (%r14,%rbp), %xmm1
movss 0x4(%r14,%rbp), %xmm2
movss 0x8(%r14,%rbp), %xmm3
movss (%rbx), %xmm4
movss 0x4(%rbx), %xmm5
maxss %xmm4, %xmm1
maxss %xmm5, %xmm2
movss 0x8(%rbx), %xmm0
maxss %xmm0, %xmm3
minss 0x10(%r14,%rbp), %xmm1
minss 0x14(%r14,%rbp), %xmm2
minss 0x18(%r14,%rbp), %xmm3
subss %xmm1, %xmm4
subss %xmm2, %xmm5
subss %xmm3, %xmm0
mulss %xmm5, %xmm5
mulss %xmm4, %xmm4
addss %xmm5, %xmm4
mulss %xmm0, %xmm0
addss %xmm4, %xmm0
ucomiss 0x5910(%rip), %xmm0     # 0xa64c
jb 0x4d47
xorps %xmm6, %xmm6
sqrtss %xmm0, %xmm6
jmp 0x4d4f
callq 0x2310
movaps %xmm0, %xmm6
incl %r13d
shlq $0x5, %r13
addq %r14, %r13
movss (%r13), %xmm1
movss 0x4(%r13), %xmm2
movss 0x8(%r13), %xmm3
movss (%rbx), %xmm4
movss 0x4(%rbx), %xmm5
maxss %xmm4, %xmm1
maxss %xmm5, %xmm2
movss 0x8(%rbx), %xmm0
maxss %xmm0, %xmm3
minss 0x10(%r13), %xmm1
minss 0x14(%r13), %xmm2
minss 0x18(%r13), %xmm3
subss %xmm1, %xmm4
subss %xmm2, %xmm5
subss %xmm3, %xmm0
mulss %xmm5, %xmm5
mulss %xmm4, %xmm4
addss %xmm5, %xmm4
mulss %xmm0, %xmm0
addss %xmm4, %xmm0
ucomiss 0x588e(%rip), %xmm0     # 0xa64c
jb 0x4dc6
sqrtss %xmm0, %xmm0
jmp 0x4dd7
movss %xmm6, 0xc(%rsp)
callq 0x2310
movss 0xc(%rsp), %xmm6
addq %rbp, %r14
ucomiss %xmm6, %xmm0
movq 0x20(%rsp), %rax
movss (%rax), %xmm3
jbe 0x4e20
movss 0x10(%r15), %xmm1
movaps %xmm1, %xmm2
minss %xmm3, %xmm2
ucomiss %xmm6, %xmm2
jb 0x4e32
ucomiss %xmm0, %xmm1
jbe 0x4e19
movss %xmm0, 0x10(%rsp)
movq %r13, 0x18(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5622
xorl %ebp, %ebp
jmp 0x4eb4
movss 0x10(%r15), %xmm1
movaps %xmm1, %xmm2
minss %xmm3, %xmm2
ucomiss %xmm0, %xmm2
jae 0x4e90
movss 0x10(%r15), %xmm1
ucomiss %xmm6, %xmm1
jbe 0x4e63
movss %xmm0, 0xc(%rsp)
movss %xmm6, 0x10(%rsp)
movq %r14, 0x18(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5622
movss 0xc(%rsp), %xmm0
movss 0x10(%r15), %xmm1
movb $0x1, %bpl
ucomiss %xmm0, %xmm1
jbe 0x4e8b
movss %xmm0, 0x10(%rsp)
movq %r13, 0x18(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5622
movq %r12, %r14
jmp 0x4eb4
ucomiss %xmm6, %xmm1
jbe 0x4eaf
movss %xmm6, 0x10(%rsp)
movq %r14, 0x18(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5622
xorl %ebp, %ebp
movq %r13, %r14
testb %bpl, %bpl
movq 0x40(%rsp), %rbp
je 0x507a
movq 0x20(%rsp), %rax
movss (%rax), %xmm0
ucomiss 0x10(%r15), %xmm0
jae 0x5086
movq 0x8(%rax), %r14
testq %r14, %r14
movq 0x80ec(%rip), %r12      # 0xcfd0
jne 0x5070
movl $0x1d, %edx
movq %r12, %rdi
leaq 0x575f(%rip), %rsi      # 0xa658
callq 0x2200
movl $0x2, %edx
movq %r12, %rdi
leaq 0x53d3(%rip), %rsi      # 0xa2e0
callq 0x2200
movl $0x43, %edx
movq %r12, %rdi
leaq 0x5755(%rip), %rsi      # 0xa676
callq 0x2200
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movl $0x4, %edx
movq %r12, %rdi
leaq 0x575d(%rip), %rsi      # 0xa6ba
callq 0x2200
movl $0x3, %edx
movq %r12, %rdi
leaq 0x574e(%rip), %rsi      # 0xa6bf
callq 0x2200
movq %r12, %rdi
xorl %esi, %esi
callq 0x2170
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movl $0xf, %edx
movq %r12, %rdi
leaq 0x570a(%rip), %rsi      # 0xa6c3
callq 0x2200
movl $0x3, %edx
movq %r12, %rdi
leaq 0x56f2(%rip), %rsi      # 0xa6bf
callq 0x2200
cvtss2sd 0x10(%r15), %xmm0
movq %r12, %rdi
callq 0x22a0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movl $0x1a, %edx
movq %r12, %rdi
leaq 0x56ba(%rip), %rsi      # 0xa6d3
callq 0x2200
movl $0x3, %edx
movq %r12, %rdi
leaq 0x5692(%rip), %rsi      # 0xa6bf
callq 0x2200
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
movq %r12, %rdi
callq 0x22a0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
leaq 0x20(%rsp), %rdi
callq 0x5678
movq %r14, %r12
testq %r14, %r14
jne 0x4ca2
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x509d
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x50b6
jmp 0x50b6
jmp 0x50b6
jmp 0x50b6
jmp 0x50b6
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x50d0
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x22f0