// File: /workspace/github_c_makefile_star2_O1/archer1357[P]Offline-Raytracer/utils.c
// Function: intersectKdtree
bool intersectKdtree(const float *rayOrigin,const float *rayVec,
                     const float *minBound,const float *maxBound,
                     struct KdTraverseNode *stack,
                     const struct KdNode *rootNode,
                     float *t2,float *colPt2,int *primInd,
                     float *bcOut,
                     const struct Triangle *triangles,int trianglesNum,
                     const struct Sphere *spheres,int spheresNum) {
  int stackSize=1;
  float invV[3];
  bool hit=false;

  stack[0].node=rootNode;

  *t2=-1.0f;

  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)) {
    return false;
  }

  //
  bool result;

  while((result=searchKdTree(rayOrigin,rayVec,invV,
                             INFINITY,
                             stack,&stackSize,rootNode))) {

    int i;

    for(i=0;i<stack[stackSize].node->thing.leaf.primsNum;i++) {
      int q;
      q=stack[stackSize].node->thing.leaf.primInds[i];


      float colPt[3];

      float t;//,uu,vv;

      if(q<trianglesNum) {
        if( intersectRayTriangle(rayOrigin,rayVec,
                                 triangles[q].pt0,
                                 triangles[q].pt1,
                                 triangles[q].pt2,
                                 triangles[q].plane,
                                 &t,colPt,bcOut)) {

          // if( t <stack[stackSize].tmin) {
          //   // continue;
          // }
          // if( t >stack[stackSize].tmax) {
          //    // continue;
          // }
          if( (t < (*t2) || !hit)) {

            // vec3_mulScalar(colPt,rayVec,t);
            // vec3_add(colPt,colPt,rayOrigin);

            *t2=t;
            colPt2[0]=colPt[0];
            colPt2[1]=colPt[1];
            colPt2[2]=colPt[2];
            *primInd=q;
            hit=true;
          }
        }
      } else {

        if(intersectRaySphere(rayOrigin,rayVec,
                              spheres[q-trianglesNum].pos,
                              spheres[q-trianglesNum].radius,
                              &t)) {

          if( (t < (*t2) || !hit)) {
            vec3_mulScalar(colPt,rayVec,t);
            vec3_add(colPt,colPt,rayOrigin);

            *t2=t;
            colPt2[0]=colPt[0];
            colPt2[1]=colPt[1];
            colPt2[2]=colPt[2];
            *primInd=q;
            hit=true;
          }
        }
      }


      //insert primitives here
    }

    if((*t2) >stack[stackSize].tmin && (*t2) <stack[stackSize].tmax) {
      return true;
    }
  }


  return hit;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rsi, %r15
movq 0x70(%rsp), %rax
movl $0x1, 0x4(%rsp)
movq %r9, (%r8)
movl $0xbf800000, (%rax)     # imm = 0xBF800000
movss 0xc0869(%rip), %xmm0    # 0x4d5490
movaps %xmm0, %xmm1
divss (%rsi), %xmm1
leaq 0x2c(%rsp), %rsi
movss %xmm1, (%rsi)
movaps %xmm0, %xmm1
divss 0x4(%r15), %xmm1
movss %xmm1, 0x4(%rsi)
divss 0x8(%r15), %xmm0
movq %rdi, %r12
movss %xmm0, 0x8(%rsi)
addq $0x8, %r8
leaq 0xc(%r14), %r9
callq 0x408878
testb %al, %al
je 0x414e82
leaq 0x2c(%rsp), %rdx
movss 0xc1ccf(%rip), %xmm0    # 0x4d6944
leaq 0x4(%rsp), %r8
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rcx
movq %rbx, %r9
callq 0x40628a
testb %al, %al
je 0x414e82
movq 0x90(%rsp), %rbp
movq $0x0, 0x8(%rsp)
movq %rbx, 0x20(%rsp)
movslq 0x4(%rsp), %rax
shlq $0x4, %rax
movq (%r14,%rax), %rcx
cmpl $0x0, 0x10(%rcx)
jle 0x414e34
xorl %r13d, %r13d
movq 0x8(%rcx), %rax
movl (%rax,%r13,4), %ebx
movl %ebx, %eax
subl 0x98(%rsp), %eax
jge 0x414d52
movslq %ebx, %rax
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
pushq 0x90(%rsp)
leaq 0x24(%rsp), %rax
pushq %rax
leaq 0x18(%rsp), %rax
pushq %rax
callq 0x40866d
addq $0x20, %rsp
testb %al, %al
je 0x414e0d
movss (%rsp), %xmm0
movq 0x70(%rsp), %rax
movss (%rax), %xmm1
ucomiss %xmm0, %xmm1
ja 0x414dce
testb $0x1, 0x8(%rsp)
je 0x414dce
jmp 0x414e0d
cltq
imulq $0x2c, %rax, %rax
movq 0xa0(%rsp), %rcx
leaq (%rcx,%rax), %rdx
addq $0x1c, %rdx
movss 0xc(%rdx), %xmm0
movq %r12, %rdi
movq %r15, %rsi
movq %rsp, %rcx
callq 0x4085bc
testb %al, %al
je 0x414e0d
movss (%rsp), %xmm0
movq 0x70(%rsp), %rax
movss (%rax), %xmm1
ucomiss %xmm0, %xmm1
ja 0x414d9d
testb $0x1, 0x8(%rsp)
jne 0x414e0d
movq %r14, %rbp
leaq 0x14(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x40766c
movq %r14, %rdi
movq %r14, %rsi
movq %rbp, %r14
movq 0x90(%rsp), %rbp
movq %r12, %rdx
callq 0x407616
movss (%rsp), %xmm0
movq 0x70(%rsp), %rax
movss %xmm0, (%rax)
movss 0x14(%rsp), %xmm0
movq 0x78(%rsp), %rax
movss %xmm0, (%rax)
movss 0x18(%rsp), %xmm0
movss %xmm0, 0x4(%rax)
movss 0x1c(%rsp), %xmm0
movss %xmm0, 0x8(%rax)
movq 0x80(%rsp), %rax
movl %ebx, (%rax)
movb $0x1, %al
movq %rax, 0x8(%rsp)
incq %r13
movslq 0x4(%rsp), %rax
shlq $0x4, %rax
movq (%r14,%rax), %rcx
movslq 0x10(%rcx), %rdx
cmpq %rdx, %r13
jl 0x414cc3
addq %r14, %rax
movq 0x20(%rsp), %rbx
jmp 0x414e37
addq %r14, %rax
movq 0x70(%rsp), %rcx
movss (%rcx), %xmm0
ucomiss 0x8(%rax), %xmm0
jbe 0x414e50
movss 0xc(%rax), %xmm1
ucomiss %xmm0, %xmm1
ja 0x414e95
movq %r12, %rdi
movq %r15, %rsi
leaq 0x2c(%rsp), %rdx
movss 0xc1ae1(%rip), %xmm0    # 0x4d6944
movq %r14, %rcx
leaq 0x4(%rsp), %r8
movq %rbx, %r9
callq 0x40628a
testb %al, %al
jne 0x414ca9
movq 0x8(%rsp), %rax
jmp 0x414e84
xorl %eax, %eax
andb $0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
jmp 0x414e84