// File: /workspace/github_c_makefile_star2_O2/archer1357[P]Offline-Raytracer/math.c
// Function: intersectRayTriangle
bool intersectRayTriangle(const float *P,const float *V,
                          const float *Q0,const float *Q1,
                          const float *Q2,
                          const float *plane,
                          float *t,float *colPt,
                          float *bcOut) {
  //ax+by+cz+d=0
  float d, NPd, NV;
  const float *N;
  float u0,u1,u2,v0,v1,v2;

  //
  d=plane[3];
  N=plane;

  //
  NPd=vec3_dot(N,P)+d;

  //origin below plane
  if(NPd <= 0.0f) {
    return false;
  }

  //
  NV=vec3_dot(N,V);

  //vector parallel or pointing away from plane
  if(NV >= 0.0f) {
    return false;
  }


  //
  (*t)=-NPd/NV;

  //
  vec3_mulScalar(colPt,V,(*t));
  vec3_add(colPt,colPt,P);

  //rest from '3D Math Primer For Graphics And Game Development'

  //
  if(fabs((double)N[0]) > fabs((double)N[1])) {
    if(fabs((double)N[0]) > fabs((double)N[2])) {
      u0 = colPt[1]-Q0[1];
      u1 = Q1[1]-Q0[1];
      u2 = Q2[1]-Q0[1];
      v0 = colPt[2]-Q0[2];
      v1 = Q1[2]-Q0[2];
      v2 = Q2[2]-Q0[2];
    } else {
      u0 = colPt[0]-Q0[0];
      u1 = Q1[0]-Q0[0];
      u2 = Q2[0]-Q0[0];
      v0 = colPt[1]-Q0[1];
      v1 = Q1[1]-Q0[1];
      v2 = Q2[1]-Q0[1];
    }
  } else {
    if(fabs((double)N[1]) > fabs((double)N[2])) {
      u0 = colPt[0]-Q0[0];
      u1 = Q1[0]-Q0[0];
      u2 = Q2[0]-Q0[0];
      v0 = colPt[2]-Q0[2];
      v1 = Q1[2]-Q0[2];
      v2 = Q2[2]-Q0[2];
    } else {
      u0 = colPt[0]-Q0[0];
      u1 = Q1[0]-Q0[0];
      u2 = Q2[0]-Q0[0];
      v0 = colPt[1]-Q0[1];
      v1 = Q1[1]-Q0[1];
      v2 = Q2[1]-Q0[1];
    }
  }

  //

  // Compute denominator, check for invalid
  float temp = u1*v2-v1*u2;

  if(!(temp != 0.0f)) {
    return false;
  }

  temp = 1.0f / temp;

  // Compute barycentric coords, checking for out-of-range
  // at each step
  float alpha = (u0 * v2-v0 * u2) * temp;

  if (!(alpha >= 0.0f)) {
    return false;
  }

  float beta = (u1 * v0-v1 * u0) * temp;

  if (!(beta >= 0.0f)) {
    return false;
  }

  float gamma = 1.0f-alpha-beta;

  if (!(gamma >= 0.0f)) {
    return false;
  }

  bcOut[0]=alpha;
  bcOut[1]=beta;
  bcOut[2]=gamma;
  //
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r12
movq %r8, %r13
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %rbp
movss 0xc(%r9), %xmm0
movss %xmm0, 0x10(%rsp)
movq %r9, %rdi
movq %rbp, %rsi
callq 0x406f00
addss 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jae 0x407fc8
movaps %xmm0, 0x10(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x406f00
ucomiss 0xca688(%rip), %xmm0    # 0x4d248c
jae 0x407fc8
movq %r13, 0x8(%rsp)
movq 0x68(%rsp), %r13
movq 0x60(%rsp), %rax
movaps 0x10(%rsp), %xmm1
xorps 0xcbe2b(%rip), %xmm1    # 0x4d3c50
divss %xmm0, %xmm1
movss %xmm1, (%rax)
movss (%rbx), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, (%r13)
movss 0x4(%rbx), %xmm0
mulss %xmm1, %xmm0
leaq 0x4(%r13), %rax
movq %rax, 0x10(%rsp)
movss %xmm0, 0x4(%r13)
mulss 0x8(%rbx), %xmm1
leaq 0x8(%r13), %rbx
movss %xmm1, 0x8(%r13)
movq %r13, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x406f8f
movq (%r12), %xmm0
movdqa 0xcbdf2(%rip), %xmm3    # 0x4d3c70
pand %xmm3, %xmm0
pshufd $0x55, %xmm0, %xmm2     # xmm2 = xmm0[1,1,1,1]
ucomiss %xmm2, %xmm0
movd 0x8(%r12), %xmm1
pand %xmm3, %xmm1
jbe 0x407ecc
movq 0x10(%rsp), %rdx
ucomiss %xmm1, %xmm0
jbe 0x407f09
movss (%rdx), %xmm0
movss 0x4(%r15), %xmm3
subss %xmm3, %xmm0
movss 0x4(%r14), %xmm1
subss %xmm3, %xmm1
movq 0x8(%rsp), %rcx
movss 0x4(%rcx), %xmm2
subss %xmm3, %xmm2
pushq $0x8
popq %rax
jmp 0x407f34
xorl %eax, %eax
ucomiss %xmm1, %xmm2
seta %al
movss (%r13), %xmm0
movss (%r15), %xmm3
subss %xmm3, %xmm0
movss (%r14), %xmm1
subss %xmm3, %xmm1
movq 0x8(%rsp), %rcx
movss (%rcx), %xmm2
subss %xmm3, %xmm2
cmovbeq 0x10(%rsp), %rbx
leaq 0x4(,%rax,4), %rax
jmp 0x407f34
movss (%r13), %xmm0
movss (%r15), %xmm3
subss %xmm3, %xmm0
movss (%r14), %xmm1
subss %xmm3, %xmm1
movq 0x8(%rsp), %rcx
movss (%rcx), %xmm2
subss %xmm3, %xmm2
pushq $0x4
popq %rax
movq %rdx, %rbx
movss (%r15,%rax), %xmm4
movss (%r14,%rax), %xmm5
subss %xmm4, %xmm5
movss (%rcx,%rax), %xmm3
subss %xmm4, %xmm3
movaps %xmm2, %xmm6
mulss %xmm5, %xmm6
movaps %xmm1, %xmm9
mulss %xmm3, %xmm9
subss %xmm6, %xmm9
xorps %xmm8, %xmm8
ucomiss %xmm8, %xmm9
jne 0x407f6e
jnp 0x407fc8
movss (%rbx), %xmm7
subss %xmm4, %xmm7
movss 0xca4e2(%rip), %xmm4    # 0x4d2460
movaps %xmm4, %xmm6
divss %xmm9, %xmm6
mulss %xmm7, %xmm2
mulss %xmm0, %xmm3
subss %xmm2, %xmm3
mulss %xmm6, %xmm3
ucomiss %xmm8, %xmm3
jb 0x407fc8
xorps 0xcbcad(%rip), %xmm5    # 0x4d3c50
mulss %xmm5, %xmm0
mulss %xmm7, %xmm1
addss %xmm0, %xmm1
mulss %xmm1, %xmm6
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm6
jb 0x407fc8
subss %xmm3, %xmm4
subss %xmm6, %xmm4
ucomiss %xmm0, %xmm4
jae 0x407fd9
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x70(%rsp), %rax
movss %xmm3, (%rax)
movss %xmm6, 0x4(%rax)
movss %xmm4, 0x8(%rax)
movb $0x1, %al
jmp 0x407fca