// File: /workspace/github_c_makefile_star2_O1/archer1357[P]Offline-Raytracer/utils.c
// Function: intersectKdtreeP
bool intersectKdtreeP(const float *rayOrigin,const float *rayVec,
                      float rayMax,
                      const float *minBound,const float *maxBound,
                      struct KdTraverseNode *stack,
                      const struct KdNode *rootNode,
                      const struct Triangle *triangles,int trianglesNum,
                      const struct Sphere *spheres,int spheresNum) {
  int stackSize=1;
  float invV[3];
  float bc[3];

  stack[0].node=rootNode;

  invV[0]=1.0f/rayVec[0];
  invV[1]=1.0f/rayVec[1];
  invV[2]=1.0f/rayVec[2];

  if(intersectRayBox(rayOrigin,invV,
                     minBound,maxBound,
                     &stack[0].tmin,
                     &stack[0].tmax
                     )) {

    bool result;

    while((result=searchKdTree(rayOrigin,rayVec,invV,
                               rayMax,
                               stack,&stackSize,rootNode))) {
      int i;

      for(i=0;i<stack[stackSize].node->thing.leaf.primsNum;i++) {
        int q;
        float colPt[3];
        float t;

        q=stack[stackSize].node->thing.leaf.primInds[i];

        if(q<trianglesNum) {
          if(intersectRayTriangle(rayOrigin,rayVec,
                                  triangles[q].pt0,
                                  triangles[q].pt1,
                                  triangles[q].pt2,
                                  triangles[q].plane,
                                  &t,colPt,bc)) {
            if(t <= rayMax) {
              return true;
            }
          }
        } else {
          if(intersectRaySphere(rayOrigin,rayVec,
                                spheres[q-trianglesNum].pos,
                                spheres[q-trianglesNum].radius,
                                &t)) {
            if(t <= rayMax) {
              return true;
            }
          }
        }

        //insert primitives here
      }
    }
  }

  return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r13
movq %r8, %r14
movq %rsi, %r15
movl $0x1, 0x10(%rsp)
movq %r9, (%r8)
movss 0xc058b(%rip), %xmm1    # 0x4d5490
movaps %xmm1, %xmm2
divss (%rsi), %xmm2
leaq 0x24(%rsp), %rsi
movss %xmm2, (%rsi)
movaps %xmm1, %xmm2
divss 0x4(%r15), %xmm2
movss %xmm0, 0xc(%rsp)
movss %xmm2, 0x4(%rsi)
divss 0x8(%r15), %xmm1
movq %rdi, %r12
movss %xmm1, 0x8(%rsi)
addq $0x8, %r8
leaq 0xc(%r14), %r9
callq 0x408878
testb %al, %al
je 0x41508f
leaq 0x24(%rsp), %rdx
leaq 0x10(%rsp), %r8
movq %r12, %rdi
movq %r15, %rsi
movss 0xc(%rsp), %xmm0
movq %r14, %rcx
movq %r13, %r9
callq 0x40628a
testb %al, %al
je 0x41508f
movl 0x88(%rsp), %ebx
movq 0x80(%rsp), %rbp
movq %r13, 0x18(%rsp)
movb %al, 0xb(%rsp)
movslq 0x10(%rsp), %rax
shlq $0x4, %rax
movq (%r14,%rax), %rax
cmpl $0x0, 0x10(%rax)
movss 0xc(%rsp), %xmm0
jle 0x41505f
xorl %r13d, %r13d
movq 0x8(%rax), %rax
movslq (%rax,%r13,4), %rax
cmpl %ebx, %eax
jge 0x415004
imulq $0x74, %rax, %rax
leaq (%rax,%rbp), %rdx
addq $0x1c, %rdx
leaq (%rax,%rbp), %rcx
addq $0x28, %rcx
leaq (%rax,%rbp), %r8
addq $0x34, %r8
leaq (%rax,%rbp), %r9
addq $0x64, %r9
subq $0x8, %rsp
movq %r12, %rdi
movq %r15, %rsi
leaq 0x44(%rsp), %rax
pushq %rax
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x2c(%rsp), %rax
pushq %rax
callq 0x40866d
addq $0x20, %rsp
jmp 0x415031
subl %ebx, %eax
cltq
imulq $0x2c, %rax, %rax
movq 0x90(%rsp), %rcx
leaq (%rcx,%rax), %rdx
addq $0x1c, %rdx
movss 0xc(%rdx), %xmm0
movq %r12, %rdi
movq %r15, %rsi
leaq 0x14(%rsp), %rcx
callq 0x4085bc
testb %al, %al
movss 0xc(%rsp), %xmm0
je 0x415042
ucomiss 0x14(%rsp), %xmm0
jae 0x415086
incq %r13
movslq 0x10(%rsp), %rax
shlq $0x4, %rax
movq (%r14,%rax), %rax
movslq 0x10(%rax), %rcx
cmpq %rcx, %r13
jl 0x414fad
movq %r12, %rdi
movq %r15, %rsi
leaq 0x24(%rsp), %rdx
movq %r14, %rcx
leaq 0x10(%rsp), %r8
movq 0x18(%rsp), %r9
callq 0x40628a
testb %al, %al
jne 0x414f89
jmp 0x41508f
movb $0x1, %al
testb $0x1, 0xb(%rsp)
jne 0x415091
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq