// File: /workspace/github_cpp_makefile_star2_O2/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 $0x158, %rsp            # imm = 0x158
movq %rcx, %r13
movq %rsi, %r15
movq %rdi, %r12
movq (%r8), %rax
movq %r8, 0x8(%rsp)
movq 0x8(%r8), %rsi
subq %rax, %rsi
cmpq $0x8, %rsi
jne 0x661c
movq (%rax), %r14
leaq 0x18(%r12), %rbx
movq %rbx, %rdi
callq 0x2260
movq %rbx, %rdi
callq 0x2170
movq %r14, %rax
shlq $0x5, %rax
movups (%r13,%rax), %xmm0
movq (%r12), %rcx
shlq $0x5, %r15
movlps %xmm0, (%rcx,%r15)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movss %xmm0, 0x8(%rcx,%r15)
movups 0x10(%r13,%rax), %xmm0
movq (%r12), %rax
movlps %xmm0, 0x10(%rax,%r15)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movss %xmm0, 0x18(%rax,%r15)
movq (%r12), %rax
movl $0x1, 0xc(%rax,%r15)
movl %r14d, 0x1c(%rax,%r15)
jmp 0x6a97
movq %rdx, %rbx
sarq $0x3, %rsi
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
andq $0x0, 0xd0(%rsp)
movaps %xmm0, 0xb0(%rsp)
andq $0x0, 0x18(%rsp)
andq $0x0, 0x10(%rsp)
leaq 0x90(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
callq 0x6e8c
movq %r13, 0x50(%rsp)
movq %r12, 0xd8(%rsp)
movq %r15, 0xe0(%rsp)
leaq 0x70(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x3, %rsi
callq 0x6e8c
movsd 0x210e(%rip), %xmm0     # 0x87b0
movsd %xmm0, 0x118(%rsp)
movl $0x7f800000, %eax       # imm = 0x7F800000
movl %eax, 0x120(%rsp)
movsd 0x2101(%rip), %xmm1     # 0x87c0
movsd %xmm1, 0x128(%rsp)
movl $0xff800000, %ecx       # imm = 0xFF800000
movl %ecx, 0x130(%rsp)
movsd %xmm0, 0xf8(%rsp)
movl %eax, 0x100(%rsp)
movsd %xmm1, 0x108(%rsp)
movl %ecx, 0x110(%rsp)
movq %rbx, %rdi
callq 0x6ebd
movq %rax, %r13
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x6f0e
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x6f3e
movss (%rax), %xmm0
movss %xmm0, 0x5c(%rsp)
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x3, %rax
addq $0x3e7, %rax            # imm = 0x3E7
xorl %ecx, %ecx
movl $0x3e8, %esi            # imm = 0x3E8
xorl %edx, %edx
divq %rsi
movq %rax, 0xe8(%rsp)
leaq 0x60(%rsp), %rbx
leaq 0x20(%rsp), %r14
cmpq 0xe8(%rsp), %rcx
je 0x6905
movq %rcx, 0xf0(%rsp)
imulq $0x3e8, %rcx, %rbp      # imm = 0x3E8
movl $0x3e8, %eax            # imm = 0x3E8
addq %rbp, %rax
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %r15
subq (%rcx), %r15
sarq $0x3, %r15
cmpq %rax, %r15
cmovaeq %rax, %r15
movsd 0x2015(%rip), %xmm0     # 0x87b0
movsd %xmm0, 0x30(%rsp)
movl $0x7f800000, %eax       # imm = 0x7F800000
movl %eax, 0x38(%rsp)
movsd 0x200e(%rip), %xmm1     # 0x87c0
movsd %xmm1, 0x40(%rsp)
movl $0xff800000, %ecx       # imm = 0xFF800000
movl %ecx, 0x48(%rsp)
movsd %xmm0, 0x138(%rsp)
movl %eax, 0x140(%rsp)
movsd %xmm1, 0x148(%rsp)
movl %ecx, 0x150(%rsp)
cmpq %r15, %rbp
jae 0x68b6
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,8), %r12
movq %r12, %rsi
shlq $0x5, %rsi
addq 0x50(%rsp), %rsi
movq %rbx, %rdi
callq 0x6f68
movaps 0x60(%rsp), %xmm0
movlps %xmm0, 0x20(%rsp)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movss %xmm0, 0x28(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x6f98
movss 0x5c(%rsp), %xmm0
ucomiss (%rax), %xmm0
jbe 0x6870
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movss 0x28(%rsp), %xmm0
movss %xmm0, 0x68(%rsp)
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x6fc2
pushq $0x1
popq %rax
lock
xaddq %rax, 0x18(%rsp)
shlq $0x3, %rax
addq 0x90(%rsp), %rax
jmp 0x68ab
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movss 0x28(%rsp), %xmm0
movss %xmm0, 0x68(%rsp)
leaq 0x138(%rsp), %rdi
movq %rbx, %rsi
callq 0x6fc2
pushq $0x1
popq %rax
lock
xaddq %rax, 0x10(%rsp)
shlq $0x3, %rax
addq 0x70(%rsp), %rax
movq %r12, (%rax)
incq %rbp
jmp 0x67e1
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
callq 0x2260
leaq 0x118(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x7010
leaq 0xf8(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x7010
movq %r15, %rdi
callq 0x2170
movq 0xf0(%rsp), %rcx
incq %rcx
jmp 0x6757
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x6aa9
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x6aa9
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
cmpq %rax, %rcx
movq 0xe0(%rsp), %r12
movq 0xd8(%rsp), %r13
je 0x694b
movq 0x8(%rsp), %rcx
movq %rax, 0x8(%rcx)
movq 0x18(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x6e8c
movq 0x10(%rsp), %rsi
leaq 0x70(%rsp), %rdi
callq 0x6e8c
leaq 0xb0(%rsp), %rdi
callq 0x2260
movq 0x8(%r13), %r15
subq (%r13), %r15
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rsi
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
movq %r13, %rdi
callq 0x731c
leaq 0x30(%rsp), %rsi
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
movq %r13, %rdi
callq 0x731c
movq %r15, %rbx
sarq $0x5, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x2170
leaq 0x118(%rsp), %rdx
leaq 0x90(%rsp), %r8
movq %r13, %rdi
movq %rbx, %rsi
movq 0x50(%rsp), %rbp
movq %rbp, %rcx
callq 0x6580
leaq 0x1(%rbx), %r14
leaq 0xf8(%rsp), %rdx
leaq 0x70(%rsp), %r8
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rcx
callq 0x6580
leaq 0xb0(%rsp), %rdi
callq 0x2260
movq (%r13), %rsi
leaq (%rsi,%r15), %rdi
shlq $0x5, %r14
addq %r14, %rsi
callq 0x7061
movq (%r13), %rax
shlq $0x5, %r12
movlps %xmm0, (%rax,%r12)
movss %xmm1, 0x8(%rax,%r12)
movq (%r13), %rax
leaq (%rax,%r15), %rdi
addq $0x10, %rdi
leaq (%rax,%r14), %rsi
addq $0x10, %rsi
callq 0x7077
movq (%r13), %rax
movlps %xmm0, 0x10(%rax,%r12)
movss %xmm1, 0x18(%rax,%r12)
movq (%r13), %rax
andl $0x0, 0xc(%rax,%r12)
movl %ebx, 0x1c(%rax,%r12)
leaq 0xb0(%rsp), %rdi
callq 0x2170
leaq 0x70(%rsp), %rdi
callq 0x70ec
leaq 0x90(%rsp), %rdi
callq 0x70ec
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
xorl %eax, %eax
xchgq %rax, 0x18(%rsp)
xorl %eax, %eax
xchgq %rax, 0x10(%rsp)
leaq 0x118(%rsp), %rbx
movsd 0x1ce6(%rip), %xmm0     # 0x87b0
movsd %xmm0, (%rbx)
movl $0x7f800000, %eax       # imm = 0x7F800000
movl %eax, 0x8(%rbx)
movsd 0x1ce2(%rip), %xmm1     # 0x87c0
movsd %xmm1, 0x10(%rbx)
movl $0xff800000, %ecx       # imm = 0xFF800000
movl %ecx, 0x18(%rbx)
leaq 0xf8(%rsp), %r14
movsd %xmm0, (%r14)
movl %eax, 0x8(%r14)
movsd %xmm1, 0x10(%r14)
movl %ecx, 0x18(%r14)
leaq 0x30(%rsp), %r13
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x692d
movq (%rax,%r15,8), %r12
movq %r12, %rsi
shlq $0x5, %rsi
addq 0x50(%rsp), %rsi
movq %r13, %rdi
callq 0x6f68
movaps 0x30(%rsp), %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
testb $0x1, %r15b
jne 0x6b82
movlps %xmm1, 0x30(%rsp)
movss %xmm0, 0x38(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6fc2
pushq $0x1
popq %rax
lock
xaddq %rax, 0x18(%rsp)
shlq $0x3, %rax
addq 0x90(%rsp), %rax
jmp 0x6bab
movlps %xmm1, 0x30(%rsp)
movss %xmm0, 0x38(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x6fc2
pushq $0x1
popq %rax
lock
xaddq %rax, 0x10(%rsp)
shlq $0x3, %rax
addq 0x70(%rsp), %rax
movq %r12, (%rax)
incq %r15
jmp 0x6b0b
jmp 0x6bd3
jmp 0x6bd3
jmp 0x6bd3
jmp 0x6bd3
jmp 0x6be9
jmp 0x6be9
jmp 0x6be9
movq %rax, %rbx
jmp 0x6bf6
jmp 0x6be9
jmp 0x6be9
jmp 0x6be9
jmp 0x6be9
jmp 0x6be9
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x2170
jmp 0x6bec
jmp 0x6be9
jmp 0x6be9
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x70ec
leaq 0x90(%rsp), %rdi
callq 0x70ec
movq %rbx, %rdi
callq 0x2310
nop