// File: /workspace/github_cpp_makefile_star2_O3/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 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
movq %rsp, %rsi
movl $0x7f800000, (%rsi)     # imm = 0x7F800000
movq %rax, 0x8(%rsi)
callq 0x5596
movq 0x8(%r14), %r12
testq %r12, %r12
je 0x5002
movq 0x7322(%rip), %rbp      # 0xbfd0
cmpl $0x0, 0xc(%r12)
movl 0x1c(%r12), %ecx
je 0x4e8d
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq *0x20(%rax)
movq 0x10(%rsp), %rdi
movss (%rdi), %xmm0
ucomiss 0x10(%r15), %xmm0
jae 0x500c
movq 0x8(%rdi), %r12
testq %r12, %r12
jne 0x4e75
movl $0x1d, %edx
movq %rbp, %rdi
leaq 0x4956(%rip), %rsi      # 0x9654
callq 0x2200
movl $0x2, %edx
movq %rbp, %rdi
leaq 0x45ce(%rip), %rsi      # 0x92e0
callq 0x2200
movl $0x43, %edx
movq %rbp, %rdi
leaq 0x494c(%rip), %rsi      # 0x9672
callq 0x2200
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movl $0x4, %edx
movq %rbp, %rdi
leaq 0x4954(%rip), %rsi      # 0x96b6
callq 0x2200
movl $0x3, %edx
movq %rbp, %rdi
leaq 0x4945(%rip), %rsi      # 0x96bb
callq 0x2200
movq %rbp, %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 %rbp, %rdi
leaq 0x4901(%rip), %rsi      # 0x96bf
callq 0x2200
movl $0x3, %edx
movq %rbp, %rdi
leaq 0x48e9(%rip), %rsi      # 0x96bb
callq 0x2200
cvtss2sd 0x10(%r15), %xmm0
movq %rbp, %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 %rbp, %rdi
leaq 0x48b1(%rip), %rsi      # 0x96cf
callq 0x2200
movl $0x3, %edx
movq %rbp, %rdi
leaq 0x4889(%rip), %rsi      # 0x96bb
callq 0x2200
movq 0x10(%rsp), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
movq %rbp, %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 0x10(%rsp), %rdi
callq 0x55f4
testq %r12, %r12
jne 0x4cae
jmp 0x5002
movq 0x8(%r14), %rax
movq %rcx, %rdx
shlq $0x5, %rdx
leaq (%rax,%rdx), %r12
incl %ecx
shlq $0x5, %rcx
leaq (%rax,%rcx), %r13
movups (%rbx), %xmm0
movaps %xmm0, %xmm1
shufps $0x0, %xmm0, %xmm1      # xmm1 = xmm1[0,0],xmm0[0,0]
movss (%rax,%rdx), %xmm4
movss 0x4(%rax,%rdx), %xmm3
movss 0x8(%rax,%rdx), %xmm2
movss 0x10(%rax,%rdx), %xmm5
movss (%rax,%rcx), %xmm6
unpcklps %xmm6, %xmm4            # xmm4 = xmm4[0],xmm6[0],xmm4[1],xmm6[1]
movss 0x4(%rax,%rcx), %xmm6
unpcklps %xmm6, %xmm3            # xmm3 = xmm3[0],xmm6[0],xmm3[1],xmm6[1]
movss 0x8(%rax,%rcx), %xmm6
unpcklps %xmm6, %xmm2            # xmm2 = xmm2[0],xmm6[0],xmm2[1],xmm6[1]
movss 0x10(%rax,%rcx), %xmm6
unpcklps %xmm6, %xmm5            # xmm5 = xmm5[0],xmm6[0],xmm5[1],xmm6[1]
maxps %xmm1, %xmm4
minps %xmm5, %xmm4
movaps %xmm0, %xmm5
shufps $0x55, %xmm0, %xmm5     # xmm5 = xmm5[1,1],xmm0[1,1]
maxps %xmm5, %xmm3
shufps $0xaa, %xmm0, %xmm0     # xmm0 = xmm0[2,2,2,2]
maxps %xmm0, %xmm2
movss 0x14(%rax,%rdx), %xmm6
movss 0x14(%rax,%rcx), %xmm7
unpcklps %xmm7, %xmm6            # xmm6 = xmm6[0],xmm7[0],xmm6[1],xmm7[1]
minps %xmm6, %xmm3
movss 0x18(%rax,%rdx), %xmm6
movss 0x18(%rax,%rcx), %xmm7
unpcklps %xmm7, %xmm6            # xmm6 = xmm6[0],xmm7[0],xmm6[1],xmm7[1]
minps %xmm6, %xmm2
subps %xmm4, %xmm1
subps %xmm3, %xmm5
subps %xmm2, %xmm0
mulps %xmm5, %xmm5
mulps %xmm1, %xmm1
addps %xmm5, %xmm1
mulps %xmm0, %xmm0
addps %xmm1, %xmm0
sqrtps %xmm0, %xmm0
movaps %xmm0, %xmm3
shufps $0x55, %xmm0, %xmm3     # xmm3 = xmm3[1,1],xmm0[1,1]
ucomiss %xmm0, %xmm3
movq 0x10(%rsp), %rax
movss 0x10(%r15), %xmm1
movaps %xmm1, %xmm2
minss (%rax), %xmm2
jbe 0x4f88
ucomiss %xmm0, %xmm2
jb 0x4f8d
ucomiss %xmm3, %xmm1
jbe 0x4e7f
movss %xmm3, (%rsp)
movq %r13, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x5596
jmp 0x4e7f
ucomiss %xmm3, %xmm2
jae 0x4fde
ucomiss %xmm0, %xmm1
jbe 0x4fb9
movaps %xmm3, 0x30(%rsp)
movss %xmm0, (%rsp)
movq %r12, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x5596
movss 0x10(%r15), %xmm1
movaps 0x30(%rsp), %xmm3
ucomiss %xmm3, %xmm1
jbe 0x4cce
movss %xmm3, (%rsp)
movq %r13, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x5596
jmp 0x4cce
ucomiss %xmm0, %xmm1
jbe 0x4ffa
movss %xmm0, (%rsp)
movq %r12, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x5596
movq %r13, %r12
jmp 0x4e7f
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5019
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5032
jmp 0x5032
jmp 0x5032
jmp 0x5032
jmp 0x5032
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x504c
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x22f0