// File: /workspace/github_cpp_makefile_star2_O1/ingowald[P]closestSurfacePointQueries/src/bvh.cpp
// Function: bvhlib::BVH::buildRec(unsigned long, ospcommon::box_t<float, 3, 1> const&, bvhlib::BuildPrim const*, std::vector<unsigned long, std::allocator<unsigned long>>&)
void BVH::buildRec(size_t nodeID,
                     const box3fa &centBounds,
                     const BuildPrim *const buildPrim,
                     std::vector<size_t> &primList)
  {
    if (primList.size() == 1) {
      size_t primID = primList[0];
      LockGuard(this->mutex);
      nodeList[nodeID].lower       = buildPrim[primID].lower;
      nodeList[nodeID].upper       = buildPrim[primID].upper;
      nodeList[nodeID].isLeaf      = 1;
      nodeList[nodeID].child       = primID;
      return;
    } else {
      Mutex mutex;

      std::atomic<size_t> lCount(0);
      std::atomic<size_t> rCount(0);
      std::vector<size_t> lPrim; lPrim.resize(primList.size());
      std::vector<size_t> rPrim; rPrim.resize(primList.size());
      box3fa lCentBounds = ospcommon::empty;
      box3fa rCentBounds = ospcommon::empty;

      size_t splitDim = widestDimension(centBounds);
      float  splitPos = centBounds.center()[splitDim];
      // bool   zeroVolume = (centBounds.lower == centBounds.upper);
        
      size_t blockSize = 1000;
      size_t numBlocks = (primList.size()+blockSize-1)/blockSize;
      for (size_t blockID=0;blockID<numBlocks;blockID++) {
        size_t begin   = blockID * blockSize;
        size_t end     = std::min(begin+blockSize,primList.size());
        box3fa lBounds = ospcommon::empty;
        box3fa rBounds = ospcommon::empty;
        for (size_t i=begin;i<end;i++) {
          const size_t primID = primList[i];
          const vec3f center = buildPrim[primID].center();
          if (center[splitDim] < splitPos) {
          // if ((center[splitDim] < splitPos) || (zeroVolume && (i&1))) {
            lBounds.extend(center);
            lPrim[lCount++] = primID;
          } else {
            rBounds.extend(center);
            rPrim[rCount++] = primID;
          }
        }
        LockGuard lock(mutex);
        lCentBounds.extend(lBounds);
        rCentBounds.extend(rBounds);
      }

      // sanity-check: if we got at least one empty side, simply split
      // randomly
      if (lCount == 0 || rCount == 0) {
        // std::cout << "abnormal case detected - all prims falling on same side of partitioning place (fixing right now...)" << std::endl;
        lCount = 0;
        rCount = 0;
        lCentBounds = ospcommon::empty;
        rCentBounds = ospcommon::empty;
        for (int i=0;i<primList.size();i++) {
          const size_t primID = primList[i];
          const vec3f center = buildPrim[primID].center();
          if ((i&1) == 0) {
            lCentBounds.extend(center);
            lPrim[lCount++] = primID;
          } else {
            rCentBounds.extend(center);
            rPrim[rCount++] = primID;
          }
        }
      }
      
      // OK, we've done the split ...

      // resize the lists so we don't unnecessarily waste memory
      primList.clear();
      lPrim.resize(lCount);
      rPrim.resize(rCount);

      size_t childID=0;
      { LockGuard lock(mutex);
        childID = nodeList.size();
        nodeList.push_back(Node());
        nodeList.push_back(Node());
      }

      buildRec(childID+0,lCentBounds,buildPrim,lPrim);
      buildRec(childID+1,rCentBounds,buildPrim,rPrim);
      { LockGuard lock(mutex);
        nodeList[nodeID].lower = min(nodeList[childID+0].lower,
                                     nodeList[childID+1].lower);
        nodeList[nodeID].upper = max(nodeList[childID+0].upper,
                                     nodeList[childID+1].upper);
        nodeList[nodeID].isLeaf   = 0;
        nodeList[nodeID].child    = childID;
      }
    }
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rcx, %r15
movq %rsi, %r14
movq %rdi, %rbp
movq (%r8), %rax
movq 0x8(%r8), %rsi
subq %rax, %rsi
cmpq $0x8, %rsi
jne 0x839b
movq (%rax), %rbx
leaq 0x18(%rbp), %r12
movq %r12, %rdi
callq 0x2240
movq %r12, %rdi
callq 0x2160
movq %rbx, %rax
shlq $0x5, %rax
movss 0x8(%r15,%rax), %xmm0
movsd (%r15,%rax), %xmm1
movq (%rbp), %rcx
shlq $0x5, %r14
movlps %xmm1, (%rcx,%r14)
movss %xmm0, 0x8(%rcx,%r14)
movss 0x18(%r15,%rax), %xmm0
movsd 0x10(%r15,%rax), %xmm1
movq (%rbp), %rax
movlps %xmm1, 0x10(%rax,%r14)
movss %xmm0, 0x18(%rax,%r14)
movq (%rbp), %rax
movl $0x1, 0xc(%rax,%r14)
movl %ebx, 0x1c(%rax,%r14)
jmp 0x8c20
movq %r8, %r12
movq %rdx, %r13
sarq $0x3, %rsi
xorps %xmm0, %xmm0
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
xorl %eax, %eax
movq %rax, 0x120(%rsp)
movq %rax, 0x58(%rsp)
movq %rax, 0x50(%rsp)
leaq 0xd0(%rsp), %rdi
movq %rax, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0x91a8
movq %rbp, 0xf8(%rsp)
movq %r14, 0xf0(%rsp)
leaq 0xb0(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x8(%r12), %rsi
subq (%r12), %rsi
sarq $0x3, %rsi
callq 0x91a8
movabsq $0x7f8000007f800000, %rdx # imm = 0x7F8000007F800000
movq %rdx, 0x30(%rsp)
movl $0x7f800000, %eax       # imm = 0x7F800000
movl %eax, 0x38(%rsp)
movabsq $-0x7fffff00800000, %rsi # imm = 0xFF800000FF800000
movq %rsi, 0x40(%rsp)
movl $0xff800000, %ecx       # imm = 0xFF800000
movl %ecx, 0x48(%rsp)
movq %rdx, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movl %ecx, 0x28(%rsp)
movss 0x10(%r13), %xmm0
movss (%r13), %xmm3
movss 0x4(%r13), %xmm4
movaps %xmm0, %xmm5
subss %xmm3, %xmm5
movss 0x14(%r13), %xmm1
movaps %xmm1, %xmm6
subss %xmm4, %xmm6
movss 0x18(%r13), %xmm2
movss 0x8(%r13), %xmm7
movaps %xmm2, %xmm8
subss %xmm7, %xmm8
movss %xmm5, 0x60(%rsp)
movss %xmm6, 0x64(%rsp)
xorl %eax, %eax
ucomiss %xmm5, %xmm6
seta %al
movss %xmm8, 0x68(%rsp)
ucomiss 0x60(%rsp,%rax,4), %xmm8
movl $0x2, %ebx
cmovbeq %rax, %rbx
addss %xmm3, %xmm0
addss %xmm4, %xmm1
addss %xmm7, %xmm2
movss 0x22e1(%rip), %xmm6     # 0xa7b4
mulss %xmm6, %xmm0
mulss %xmm6, %xmm1
mulss %xmm6, %xmm2
movss %xmm0, 0x60(%rsp)
movss %xmm1, 0x64(%rsp)
movss %xmm2, 0x68(%rsp)
movq 0x8(%r12), %rcx
subq (%r12), %rcx
sarq $0x3, %rcx
addq $0x3e7, %rcx            # imm = 0x3E7
movq %rcx, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
mulq %rdx
cmpq $0x3e8, %rcx            # imm = 0x3E8
jb 0x8880
movq %rdx, %r13
movss 0x60(%rsp,%rbx,4), %xmm3
shrq $0x4, %r13
xorl %r14d, %r14d
leaq 0x100(%rsp), %rbp
movss %xmm3, 0x80(%rsp)
imulq $0x3e8, %r14, %rax      # imm = 0x3E8
movl $0x3e8, %ecx            # imm = 0x3E8
addq %rax, %rcx
movq 0x8(%r12), %rdx
subq (%r12), %rdx
sarq $0x3, %rdx
cmpq %rcx, %rdx
cmovbq %rdx, %rcx
movss 0x20de(%rip), %xmm1     # 0xa650
movaps %xmm1, %xmm11
movaps %xmm1, %xmm9
movaps %xmm1, %xmm7
movss 0x2233(%rip), %xmm0     # 0xa7b8
movaps %xmm0, %xmm10
movaps %xmm0, %xmm5
movaps %xmm0, %xmm4
movaps %xmm1, %xmm15
movaps %xmm1, %xmm13
movaps %xmm1, %xmm12
movss %xmm0, 0xc(%rsp)
movaps %xmm0, %xmm14
movaps %xmm0, %xmm8
cmpq %rax, %rdx
jbe 0x86ef
movss 0x21fe(%rip), %xmm0     # 0xa7b8
movaps %xmm0, %xmm8
movaps %xmm0, %xmm14
movss %xmm0, 0xc(%rsp)
movss 0x2080(%rip), %xmm1     # 0xa650
movaps %xmm1, %xmm12
movaps %xmm1, %xmm13
movaps %xmm1, %xmm15
movaps %xmm0, %xmm4
movaps %xmm0, %xmm5
movaps %xmm0, %xmm10
movaps %xmm1, %xmm7
movaps %xmm1, %xmm9
movaps %xmm1, %xmm11
movq (%r12), %rdx
movq (%rdx,%rax,8), %rdx
movq %rdx, %rsi
shlq $0x5, %rsi
movss (%r15,%rsi), %xmm1
addss 0x10(%r15,%rsi), %xmm1
movss 0x4(%r15,%rsi), %xmm2
addss 0x14(%r15,%rsi), %xmm2
movss 0x8(%r15,%rsi), %xmm0
addss 0x18(%r15,%rsi), %xmm0
mulss %xmm6, %xmm1
mulss %xmm6, %xmm2
unpcklps %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
mulss %xmm6, %xmm0
movlps %xmm1, 0x60(%rsp)
movss %xmm0, 0x68(%rsp)
ucomiss 0x60(%rsp,%rbx,4), %xmm3
movss 0x60(%rsp), %xmm1
jbe 0x869d
movss 0x64(%rsp), %xmm2
minss %xmm1, %xmm15
minss %xmm2, %xmm13
minss %xmm0, %xmm12
maxss 0xc(%rsp), %xmm1
movl $0x1, %esi
lock
xaddq %rsi, 0x58(%rsp)
maxss %xmm14, %xmm2
shlq $0x3, %rsi
addq 0xd0(%rsp), %rsi
maxss %xmm8, %xmm0
movss %xmm1, 0xc(%rsp)
movaps %xmm2, %xmm14
movaps %xmm0, %xmm8
jmp 0x86e0
movss 0x64(%rsp), %xmm2
minss %xmm1, %xmm11
minss %xmm2, %xmm9
minss %xmm0, %xmm7
maxss %xmm10, %xmm1
movl $0x1, %esi
lock
xaddq %rsi, 0x50(%rsp)
maxss %xmm5, %xmm2
shlq $0x3, %rsi
addq 0xb0(%rsp), %rsi
maxss %xmm4, %xmm0
movaps %xmm1, %xmm10
movaps %xmm2, %xmm5
movaps %xmm0, %xmm4
movq %rdx, (%rsi)
incq %rax
cmpq %rcx, %rax
jb 0x85f1
movss %xmm15, 0x84(%rsp)
movss %xmm13, 0x88(%rsp)
movss %xmm12, 0x8c(%rsp)
movss %xmm14, 0x90(%rsp)
movss %xmm8, 0x94(%rsp)
movss %xmm11, 0x98(%rsp)
movss %xmm9, 0x9c(%rsp)
movss %xmm7, 0xa0(%rsp)
movss %xmm10, 0xa4(%rsp)
movss %xmm5, 0xa8(%rsp)
movss %xmm4, 0xac(%rsp)
movq %rbp, %rdi
callq 0x2240
movss 0x30(%rsp), %xmm0
minss 0x84(%rsp), %xmm0
movss 0x34(%rsp), %xmm1
minss 0x88(%rsp), %xmm1
movss 0x38(%rsp), %xmm2
minss 0x8c(%rsp), %xmm2
movss %xmm0, 0x30(%rsp)
movss %xmm1, 0x34(%rsp)
movss %xmm2, 0x38(%rsp)
movss 0xc(%rsp), %xmm0
maxss 0x40(%rsp), %xmm0
movss 0x90(%rsp), %xmm1
maxss 0x44(%rsp), %xmm1
movss 0x94(%rsp), %xmm3
maxss 0x48(%rsp), %xmm3
movss %xmm0, 0x40(%rsp)
movss %xmm1, 0x44(%rsp)
movss 0x10(%rsp), %xmm0
minss 0x98(%rsp), %xmm0
movss 0x14(%rsp), %xmm1
minss 0x9c(%rsp), %xmm1
movss 0x18(%rsp), %xmm2
minss 0xa0(%rsp), %xmm2
movss %xmm0, 0x10(%rsp)
movss %xmm1, 0x14(%rsp)
movss %xmm2, 0x18(%rsp)
movss 0xa4(%rsp), %xmm2
maxss 0x20(%rsp), %xmm2
movss 0xa8(%rsp), %xmm1
maxss 0x24(%rsp), %xmm1
movss 0xac(%rsp), %xmm0
maxss 0x28(%rsp), %xmm0
movss %xmm3, 0x48(%rsp)
movss %xmm2, 0x20(%rsp)
movss %xmm1, 0x24(%rsp)
movss %xmm0, 0x28(%rsp)
movq %rbp, %rdi
callq 0x2160
movss 0x80(%rsp), %xmm3
movss 0x1f40(%rip), %xmm6     # 0xa7b4
incq %r14
cmpq %r13, %r14
jne 0x8547
movq 0x58(%rsp), %rax
testq %rax, %rax
movq 0xf8(%rsp), %rbp
je 0x88a0
movq 0x50(%rsp), %rax
testq %rax, %rax
jne 0x8a3e
xorl %eax, %eax
xorl %ecx, %ecx
xchgq %rcx, 0x58(%rsp)
xchgq %rax, 0x50(%rsp)
movabsq $0x7f8000007f800000, %rdx # imm = 0x7F8000007F800000
movq %rdx, 0x30(%rsp)
movl $0x7f800000, %eax       # imm = 0x7F800000
movl %eax, 0x38(%rsp)
movabsq $-0x7fffff00800000, %rsi # imm = 0xFF800000FF800000
movq %rsi, 0x40(%rsp)
movl $0xff800000, %ecx       # imm = 0xFF800000
movl %ecx, 0x48(%rsp)
movq %rdx, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movl %ecx, 0x28(%rsp)
movq (%r12), %rcx
cmpq %rcx, 0x8(%r12)
je 0x8a3e
xorl %eax, %eax
movq (%rcx,%rax,8), %rcx
movq %rcx, %rdx
shlq $0x5, %rdx
movss (%r15,%rdx), %xmm1
movss 0x4(%r15,%rdx), %xmm0
addss 0x10(%r15,%rdx), %xmm1
addss 0x14(%r15,%rdx), %xmm0
movss 0x8(%r15,%rdx), %xmm2
addss 0x18(%r15,%rdx), %xmm2
mulss %xmm6, %xmm1
mulss %xmm6, %xmm0
mulss %xmm6, %xmm2
testb $0x1, %al
jne 0x89b3
movss 0x30(%rsp), %xmm3
movss 0x34(%rsp), %xmm4
minss %xmm1, %xmm3
minss %xmm0, %xmm4
movss 0x38(%rsp), %xmm5
minss %xmm2, %xmm5
movss %xmm3, 0x30(%rsp)
movss %xmm4, 0x34(%rsp)
movss %xmm5, 0x38(%rsp)
maxss 0x40(%rsp), %xmm1
maxss 0x44(%rsp), %xmm0
maxss 0x48(%rsp), %xmm2
movss %xmm1, 0x40(%rsp)
movss %xmm0, 0x44(%rsp)
movss %xmm2, 0x48(%rsp)
movl $0x1, %edx
lock
xaddq %rdx, 0x58(%rsp)
shlq $0x3, %rdx
addq 0xd0(%rsp), %rdx
jmp 0x8a1f
movss 0x10(%rsp), %xmm3
movss 0x14(%rsp), %xmm4
minss %xmm1, %xmm3
minss %xmm0, %xmm4
movss 0x18(%rsp), %xmm5
minss %xmm2, %xmm5
movss %xmm3, 0x10(%rsp)
movss %xmm4, 0x14(%rsp)
movss %xmm5, 0x18(%rsp)
maxss 0x20(%rsp), %xmm1
maxss 0x24(%rsp), %xmm0
maxss 0x28(%rsp), %xmm2
movss %xmm1, 0x20(%rsp)
movss %xmm0, 0x24(%rsp)
movss %xmm2, 0x28(%rsp)
movl $0x1, %edx
lock
xaddq %rdx, 0x50(%rsp)
shlq $0x3, %rdx
addq 0xb0(%rsp), %rdx
movq %rcx, (%rdx)
incq %rax
movq (%r12), %rcx
movq 0x8(%r12), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
ja 0x8901
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x8a4e
movq %rax, 0x8(%r12)
movq 0x58(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x91a8
movq 0x50(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x91a8
leaq 0x100(%rsp), %rdi
callq 0x2240
movq 0x8(%rbp), %rsi
movq %rsi, %rbx
subq (%rbp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
cmpq 0x10(%rbp), %rsi
movq 0xf0(%rsp), %r14
je 0x8ab3
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x20, 0x8(%rbp)
jmp 0x8ac0
leaq 0x60(%rsp), %rdx
movq %rbp, %rdi
callq 0x9376
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq 0x8(%rbp), %rsi
cmpq 0x10(%rbp), %rsi
je 0x8ae5
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x20, 0x8(%rbp)
jmp 0x8af2
leaq 0x60(%rsp), %rdx
movq %rbp, %rdi
callq 0x9376
movq %rbx, %r12
sarq $0x5, %r12
leaq 0x100(%rsp), %rdi
callq 0x2160
leaq 0x30(%rsp), %rdx
leaq 0xd0(%rsp), %r8
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x82fc
leaq 0x1(%r12), %r13
leaq 0x10(%rsp), %rdx
leaq 0xb0(%rsp), %r8
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x82fc
leaq 0x100(%rsp), %r15
movq %r15, %rdi
callq 0x2240
movq (%rbp), %rax
shlq $0x5, %r13
movss (%rax,%rbx), %xmm0
movss 0x4(%rax,%rbx), %xmm1
minss (%rax,%r13), %xmm0
minss 0x4(%rax,%r13), %xmm1
movss 0x8(%rax,%rbx), %xmm2
minss 0x8(%rax,%r13), %xmm2
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
shlq $0x5, %r14
movlps %xmm0, (%rax,%r14)
movss %xmm2, 0x8(%rax,%r14)
movq (%rbp), %rax
movss 0x10(%rax,%r13), %xmm0
maxss 0x10(%rax,%rbx), %xmm0
movss 0x14(%rax,%r13), %xmm1
maxss 0x14(%rax,%rbx), %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movss 0x18(%rax,%r13), %xmm1
maxss 0x18(%rax,%rbx), %xmm1
movlps %xmm0, 0x10(%rax,%r14)
movss %xmm1, 0x18(%rax,%r14)
movq (%rbp), %rax
movl $0x0, 0xc(%rax,%r14)
movl %r12d, 0x1c(%rax,%r14)
movq %r15, %rdi
callq 0x2160
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x8c03
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x8c20
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8c34
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x2160
jmp 0x8c52
jmp 0x8c4f
movq %rax, %rbx
jmp 0x8c6f
jmp 0x8c4f
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x8c6f
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x8c8c
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x22f0