// File: /workspace/github_cpp_makefile_star2_O3/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 $0x168, %rsp            # imm = 0x168
movq %rcx, %r15
movq %rsi, %r14
movq %rdi, %rbp
movq (%r8), %rax
movq 0x8(%r8), %rsi
subq %rax, %rsi
cmpq $0x8, %rsi
jne 0x7921
movq (%rax), %rbx
leaq 0x18(%rbp), %r12
movq %r12, %rdi
callq 0x2240
movq %r12, %rdi
callq 0x2160
movq %rbx, %rax
shlq $0x5, %rax
movups (%r15,%rax), %xmm0
movq (%rbp), %rcx
shlq $0x5, %r14
movlps %xmm0, (%rcx,%r14)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movss %xmm0, 0x8(%rcx,%r14)
movups 0x10(%r15,%rax), %xmm0
movq (%rbp), %rax
movlps %xmm0, 0x10(%rax,%r14)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movss %xmm0, 0x18(%rax,%r14)
movq (%rbp), %rax
movl $0x1, 0xc(%rax,%r14)
movl %ebx, 0x1c(%rax,%r14)
jmp 0x815c
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 0xb0(%rsp), %rdi
movq %rax, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0x85b8
movq %rbp, 0xc8(%rsp)
movq %r14, 0xf8(%rsp)
leaq 0x90(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x8(%r12), %rsi
subq (%r12), %rsi
sarq $0x3, %rsi
callq 0x85b8
movsd 0x1e0a(%rip), %xmm0     # 0x97b0
movsd %xmm0, 0x30(%rsp)
movl $0x7f800000, %eax       # imm = 0x7F800000
movl %eax, 0x38(%rsp)
movsd 0x1e03(%rip), %xmm1     # 0x97c0
movsd %xmm1, 0x40(%rsp)
movl $0xff800000, %ecx       # imm = 0xFF800000
movl %ecx, 0x48(%rsp)
movsd %xmm0, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movsd %xmm1, 0x20(%rsp)
movl %ecx, 0x28(%rsp)
movss 0x18(%r13), %xmm0
movss 0x8(%r13), %xmm1
movaps %xmm0, %xmm2
subss %xmm1, %xmm2
movsd 0x10(%r13), %xmm3
movsd (%r13), %xmm4
movaps %xmm3, %xmm5
subps %xmm4, %xmm5
movaps %xmm5, %xmm6
shufps $0x55, %xmm5, %xmm6     # xmm6 = xmm6[1,1],xmm5[1,1]
xorl %eax, %eax
ucomiss %xmm5, %xmm6
seta %al
movlps %xmm5, 0x70(%rsp)
ucomiss 0x70(%rsp,%rax,4), %xmm2
movl $0x2, %ebx
cmovbeq %rax, %rbx
addss %xmm1, %xmm0
mulss 0x1dad(%rip), %xmm0     # 0x97e0
addps %xmm4, %xmm3
mulps 0x1d93(%rip), %xmm3     # 0x97d0
movlps %xmm3, 0x70(%rsp)
movss %xmm0, 0x78(%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 0x7df0
movq %rdx, %r13
movss 0x70(%rsp,%rbx,4), %xmm8
shrq $0x4, %r13
movaps 0x1d2e(%rip), %xmm7     # 0x97c0
movaps 0x1d17(%rip), %xmm0     # 0x97b0
movss 0x1bab(%rip), %xmm1     # 0x964c
movss 0x1d3b(%rip), %xmm4     # 0x97e4
xorl %r14d, %r14d
movaps 0x1d1c(%rip), %xmm9     # 0x97d0
movss 0x1d23(%rip), %xmm10    # 0x97e0
leaq 0x100(%rsp), %rbp
movaps %xmm4, %xmm5
movss %xmm1, 0x8(%rsp)
movss %xmm1, 0xc(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm7, %xmm6
movaps %xmm0, 0xd0(%rsp)
movss %xmm8, 0x6c(%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
cmpq %rax, %rdx
movaps 0x150(%rsp), %xmm11
jbe 0x7c7d
movss 0x1cb9(%rip), %xmm2     # 0x97e4
movaps %xmm2, %xmm12
movss 0x1b15(%rip), %xmm1     # 0x964c
movaps %xmm1, %xmm0
movaps %xmm2, %xmm13
movaps 0x1c6b(%rip), %xmm5     # 0x97b0
movaps %xmm5, %xmm2
movaps 0x1c71(%rip), %xmm4     # 0x97c0
movaps %xmm4, %xmm14
movaps %xmm5, %xmm3
movaps %xmm4, %xmm15
movq (%r12), %rdx
movq (%rdx,%rax,8), %rdx
movq %rdx, %rsi
shlq $0x5, %rsi
movss 0x8(%r15,%rsi), %xmm5
addss 0x18(%r15,%rsi), %xmm5
movsd (%r15,%rsi), %xmm7
mulss %xmm10, %xmm5
movaps %xmm5, %xmm4
shufps $0xf4, %xmm11, %xmm4    # xmm4 = xmm4[0,1],xmm11[3,3]
movsd 0x10(%r15,%rsi), %xmm11
addps %xmm7, %xmm11
mulps %xmm9, %xmm11
movq %xmm11, %rsi
movlpd %xmm11, 0x70(%rsp)
movss %xmm5, 0x78(%rsp)
shrq $0x20, %rsi
ucomiss 0x70(%rsp,%rbx,4), %xmm8
movd %esi, %xmm7
movdqa %xmm11, %xmm6
punpcklqdq %xmm7, %xmm6    # xmm6 = xmm6[0],xmm7[0]
shufps $0xe8, %xmm6, %xmm6     # xmm6 = xmm6[0,2,2,3]
jbe 0x7bfa
minps %xmm6, %xmm2
minss %xmm5, %xmm0
maxps %xmm14, %xmm6
maxss %xmm12, %xmm5
movl $0x1, %esi
lock
xaddq %rsi, 0x58(%rsp)
shlq $0x3, %rsi
addq 0xb0(%rsp), %rsi
movaps %xmm5, %xmm12
movaps %xmm6, %xmm14
jmp 0x7c2a
minps %xmm6, %xmm3
minss %xmm5, %xmm1
maxps %xmm15, %xmm6
maxss %xmm13, %xmm5
movl $0x1, %esi
lock
xaddq %rsi, 0x50(%rsp)
shlq $0x3, %rsi
addq 0x90(%rsp), %rsi
movaps %xmm5, %xmm13
movaps %xmm6, %xmm15
shufps $0x84, %xmm4, %xmm11    # xmm11 = xmm11[0,1],xmm4[0,2]
movq %rdx, (%rsi)
incq %rax
cmpq %rcx, %rax
jb 0x7b5a
movaps %xmm11, 0x150(%rsp)
movsd 0x30(%rsp), %xmm10
movss 0x38(%rsp), %xmm8
movsd 0x40(%rsp), %xmm6
movss 0x48(%rsp), %xmm4
movsd 0x10(%rsp), %xmm11
movss 0x18(%rsp), %xmm9
movsd 0x20(%rsp), %xmm7
movss 0x28(%rsp), %xmm5
jmp 0x7cd8
movss 0x19c7(%rip), %xmm0     # 0x964c
movaps %xmm0, %xmm1
movss 0x1b54(%rip), %xmm2     # 0x97e4
movaps %xmm2, %xmm13
movaps %xmm2, %xmm12
movaps 0x1b10(%rip), %xmm9     # 0x97b0
movaps %xmm9, %xmm2
movaps 0x1b14(%rip), %xmm8     # 0x97c0
movaps %xmm8, %xmm14
movaps %xmm9, %xmm3
movaps %xmm8, %xmm15
movss 0xc(%rsp), %xmm8
movss 0x8(%rsp), %xmm9
movaps 0xe0(%rsp), %xmm10
movaps 0xd0(%rsp), %xmm11
maxps %xmm7, %xmm15
movaps %xmm15, 0x140(%rsp)
maxss %xmm5, %xmm13
movss %xmm13, 0x64(%rsp)
minps %xmm3, %xmm11
movaps %xmm11, 0xd0(%rsp)
minss %xmm1, %xmm9
movss %xmm9, 0x8(%rsp)
maxps %xmm6, %xmm14
movaps %xmm14, 0x130(%rsp)
maxss %xmm4, %xmm12
movss %xmm12, 0x68(%rsp)
minps %xmm2, %xmm10
movaps %xmm10, 0xe0(%rsp)
minss %xmm0, %xmm8
movss %xmm8, 0xc(%rsp)
movq %rbp, %rdi
callq 0x2240
movaps 0xe0(%rsp), %xmm0
movlps %xmm0, 0x30(%rsp)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x38(%rsp)
movaps 0x130(%rsp), %xmm0
movlps %xmm0, 0x40(%rsp)
movss 0x68(%rsp), %xmm0
movss %xmm0, 0x48(%rsp)
movaps 0xd0(%rsp), %xmm0
movlps %xmm0, 0x10(%rsp)
movss 0x8(%rsp), %xmm0
movss %xmm0, 0x18(%rsp)
movaps 0x140(%rsp), %xmm0
movlps %xmm0, 0x20(%rsp)
movss 0x64(%rsp), %xmm0
movss %xmm0, 0x28(%rsp)
movq %rbp, %rdi
callq 0x2160
incq %r14
movss 0x64(%rsp), %xmm5
movss 0x68(%rsp), %xmm4
movaps 0x130(%rsp), %xmm6
movaps 0x140(%rsp), %xmm7
cmpq %r13, %r14
movss 0x6c(%rsp), %xmm8
movaps 0x19ef(%rip), %xmm9     # 0x97d0
movss 0x19f6(%rip), %xmm10    # 0x97e0
jne 0x7aee
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x7e1a
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x7e1a
movq (%r12), %rcx
movq 0x8(%r12), %rdx
movq 0xc8(%rsp), %rbp
jmp 0x7f8d
xorl %eax, %eax
xorl %ecx, %ecx
xchgq %rcx, 0x58(%rsp)
xchgq %rax, 0x50(%rsp)
movsd 0x1980(%rip), %xmm0     # 0x97b0
movsd %xmm0, 0x30(%rsp)
movl $0x7f800000, %eax       # imm = 0x7F800000
movl %eax, 0x38(%rsp)
movsd 0x1979(%rip), %xmm1     # 0x97c0
movsd %xmm1, 0x40(%rsp)
movl $0xff800000, %ecx       # imm = 0xFF800000
movl %ecx, 0x48(%rsp)
movsd %xmm0, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movsd %xmm1, 0x20(%rsp)
movl %ecx, 0x28(%rsp)
movq (%r12), %rcx
movq 0x8(%r12), %rdx
cmpq %rcx, %rdx
movq 0xc8(%rsp), %rbp
je 0x7f8d
xorl %eax, %eax
movaps 0x1943(%rip), %xmm0     # 0x97d0
movss 0x194b(%rip), %xmm1     # 0x97e0
movq (%rcx,%rax,8), %rcx
movq %rcx, %rdx
shlq $0x5, %rdx
movsd (%r15,%rdx), %xmm4
movsd 0x10(%r15,%rdx), %xmm2
movss 0x8(%r15,%rdx), %xmm3
addss 0x18(%r15,%rdx), %xmm3
addps %xmm4, %xmm2
mulps %xmm0, %xmm2
mulss %xmm1, %xmm3
testb $0x1, %al
jne 0x7f1b
movss 0x38(%rsp), %xmm4
minss %xmm3, %xmm4
movsd 0x30(%rsp), %xmm5
minps %xmm2, %xmm5
movlps %xmm5, 0x30(%rsp)
movss %xmm4, 0x38(%rsp)
maxss 0x48(%rsp), %xmm3
movsd 0x40(%rsp), %xmm4
maxps %xmm4, %xmm2
movlps %xmm2, 0x40(%rsp)
movss %xmm3, 0x48(%rsp)
movl $0x1, %edx
lock
xaddq %rdx, 0x58(%rsp)
shlq $0x3, %rdx
addq 0xb0(%rsp), %rdx
jmp 0x7f6b
movss 0x18(%rsp), %xmm4
minss %xmm3, %xmm4
movsd 0x10(%rsp), %xmm5
minps %xmm2, %xmm5
movlps %xmm5, 0x10(%rsp)
movss %xmm4, 0x18(%rsp)
maxss 0x28(%rsp), %xmm3
movsd 0x20(%rsp), %xmm4
maxps %xmm4, %xmm2
movlps %xmm2, 0x20(%rsp)
movss %xmm3, 0x28(%rsp)
movl $0x1, %edx
lock
xaddq %rdx, 0x50(%rsp)
shlq $0x3, %rdx
addq 0x90(%rsp), %rdx
movq %rcx, (%rdx)
incq %rax
movq (%r12), %rcx
movq 0x8(%r12), %rdx
movq %rdx, %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
cmpq %rax, %rsi
ja 0x7e95
cmpq %rcx, %rdx
je 0x7f97
movq %rcx, 0x8(%r12)
movq 0x58(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x85b8
movq 0xf8(%rsp), %r14
movq 0x50(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x85b8
leaq 0x100(%rsp), %rdi
callq 0x2240
movq 0x8(%rbp), %rsi
movq %rsi, %rbx
subq (%rbp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
cmpq 0x10(%rbp), %rsi
je 0x8006
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
movq 0x8(%rbp), %rsi
addq $0x20, %rsi
movq %rsi, 0x8(%rbp)
jmp 0x8017
leaq 0x70(%rsp), %rdx
movq %rbp, %rdi
callq 0x878c
movq 0x8(%rbp), %rsi
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
cmpq 0x10(%rbp), %rsi
je 0x803b
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x20, 0x8(%rbp)
jmp 0x8048
leaq 0x70(%rsp), %rdx
movq %rbp, %rdi
callq 0x878c
movq %rbx, %r12
sarq $0x5, %r12
leaq 0x100(%rsp), %rdi
callq 0x2160
leaq 0x30(%rsp), %rdx
leaq 0xb0(%rsp), %r8
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x788c
leaq 0x1(%r12), %r13
leaq 0x10(%rsp), %rdx
leaq 0x90(%rsp), %r8
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x788c
leaq 0x100(%rsp), %r15
movq %r15, %rdi
callq 0x2240
movq (%rbp), %rax
shlq $0x5, %r13
movsd (%rax,%rbx), %xmm0
movsd (%rax,%r13), %xmm1
movss 0x8(%rax,%rbx), %xmm2
minss 0x8(%rax,%r13), %xmm2
minps %xmm1, %xmm0
shlq $0x5, %r14
movlps %xmm0, (%rax,%r14)
movss %xmm2, 0x8(%rax,%r14)
movq (%rbp), %rax
movsd 0x10(%rax,%rbx), %xmm0
movsd 0x10(%rax,%r13), %xmm1
maxps %xmm0, %xmm1
movss 0x18(%rax,%r13), %xmm0
maxss 0x18(%rax,%rbx), %xmm0
movlps %xmm1, 0x10(%rax,%r14)
movss %xmm0, 0x18(%rax,%r14)
movq (%rbp), %rax
movl $0x0, 0xc(%rax,%r14)
movl %r12d, 0x1c(%rax,%r14)
movq %r15, %rdi
callq 0x2160
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x813f
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x815c
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8170
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x2160
jmp 0x818e
jmp 0x818b
movq %rax, %rbx
jmp 0x81ab
jmp 0x818b
movq %rax, %rbx
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x81ab
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x81c8
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x22f0