// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x48(%rbp), %eax
movq 0x40(%rbp), %rax
movl 0x38(%rbp), %eax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movb $0x0, -0x49(%rbp)
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq 0x10(%rbp), %rax
movss 0xc2d21(%rip), %xmm0    # 0x4e4c04
movss %xmm0, (%rax)
movq -0x18(%rbp), %rax
movss 0xc1591(%rip), %xmm0    # 0x4e3484
divss (%rax), %xmm0
movss %xmm0, -0x48(%rbp)
movq -0x18(%rbp), %rax
movss 0xc157c(%rip), %xmm0    # 0x4e3484
divss 0x4(%rax), %xmm0
movss %xmm0, -0x44(%rbp)
movq -0x18(%rbp), %rax
movss 0xc1566(%rip), %xmm0    # 0x4e3484
divss 0x8(%rax), %xmm0
movss %xmm0, -0x40(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x8, %r8
movq -0x30(%rbp), %r9
addq $0xc, %r9
callq 0x40e720
testb $0x1, %al
jne 0x421f5a
movb $0x0, -0x1(%rbp)
jmp 0x4221eb
jmp 0x421f5c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x48(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r9
movss 0xc2998(%rip), %xmm0    # 0x4e4910
leaq -0x3c(%rbp), %r8
callq 0x40a960
movb %al, %cl
andb $0x1, %cl
movb %cl, -0x4a(%rbp)
testb $0x1, %al
jne 0x421f92
jmp 0x4221e3
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x30(%rbp), %rcx
movslq -0x3c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
cmpl 0x10(%rcx), %eax
jge 0x42219a
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movslq -0x50(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl 0x38(%rbp), %eax
jge 0x4220be
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq 0x30(%rbp), %rdx
movslq -0x54(%rbp), %rax
imulq $0x74, %rax, %rax
addq %rax, %rdx
addq $0x1c, %rdx
movq 0x30(%rbp), %rcx
movslq -0x54(%rbp), %rax
imulq $0x74, %rax, %rax
addq %rax, %rcx
addq $0x28, %rcx
movq 0x30(%rbp), %r8
movslq -0x54(%rbp), %rax
imulq $0x74, %rax, %rax
addq %rax, %r8
addq $0x34, %r8
movq 0x30(%rbp), %r9
movslq -0x54(%rbp), %rax
imulq $0x74, %rax, %rax
addq %rax, %r9
addq $0x64, %r9
leaq -0x60(%rbp), %r10
movq 0x28(%rbp), %rax
leaq -0x64(%rbp), %r11
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x40e220
testb $0x1, %al
jne 0x42205c
jmp 0x4220b9
movss -0x64(%rbp), %xmm1
movq 0x10(%rbp), %rax
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
ja 0x422074
testb $0x1, -0x49(%rbp)
jne 0x4220b7
movss -0x64(%rbp), %xmm0
movq 0x10(%rbp), %rax
movss %xmm0, (%rax)
movss -0x60(%rbp), %xmm0
movq 0x18(%rbp), %rax
movss %xmm0, (%rax)
movss -0x5c(%rbp), %xmm0
movq 0x18(%rbp), %rax
movss %xmm0, 0x4(%rax)
movss -0x58(%rbp), %xmm0
movq 0x18(%rbp), %rax
movss %xmm0, 0x8(%rax)
movl -0x54(%rbp), %ecx
movq 0x20(%rbp), %rax
movl %ecx, (%rax)
movb $0x1, -0x49(%rbp)
jmp 0x4220b9
jmp 0x42218a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq 0x40(%rbp), %rdx
movl -0x54(%rbp), %eax
subl 0x38(%rbp), %eax
cltq
imulq $0x2c, %rax, %rax
addq %rax, %rdx
addq $0x1c, %rdx
movq 0x40(%rbp), %rax
movl -0x54(%rbp), %ecx
subl 0x38(%rbp), %ecx
movslq %ecx, %rcx
imulq $0x2c, %rcx, %rcx
addq %rcx, %rax
movss 0x28(%rax), %xmm0
leaq -0x64(%rbp), %rcx
callq 0x40e100
testb $0x1, %al
jne 0x422108
jmp 0x422188
movss -0x64(%rbp), %xmm1
movq 0x10(%rbp), %rax
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
ja 0x422120
testb $0x1, -0x49(%rbp)
jne 0x422186
leaq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
movss -0x64(%rbp), %xmm0
callq 0x40cb40
leaq -0x60(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movq -0x10(%rbp), %rdx
callq 0x40ca80
movss -0x64(%rbp), %xmm0
movq 0x10(%rbp), %rax
movss %xmm0, (%rax)
movss -0x60(%rbp), %xmm0
movq 0x18(%rbp), %rax
movss %xmm0, (%rax)
movss -0x5c(%rbp), %xmm0
movq 0x18(%rbp), %rax
movss %xmm0, 0x4(%rax)
movss -0x58(%rbp), %xmm0
movq 0x18(%rbp), %rax
movss %xmm0, 0x8(%rax)
movl -0x54(%rbp), %ecx
movq 0x20(%rbp), %rax
movl %ecx, (%rax)
movb $0x1, -0x49(%rbp)
jmp 0x422188
jmp 0x42218a
jmp 0x42218c
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x421f99
movq 0x10(%rbp), %rax
movss (%rax), %xmm0
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
ucomiss 0x8(%rax), %xmm0
jbe 0x4221de
movq 0x10(%rbp), %rax
movss (%rax), %xmm1
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movss 0xc(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x4221de
movb $0x1, -0x1(%rbp)
jmp 0x4221eb
jmp 0x421f5c
movb -0x49(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)