// File: /workspace/github_c_makefile_star2_O2/mohuangrui[P]ArtraCFD/computational_geometry.c
// Function: PointInPolyhedron
int PointInPolyhedron(const Real p[restrict], const Polyhedron *poly, int fid[restrict])
{
    const Real zero = 0.0;
    RealVec v0 = {zero}; /* vertices */
    RealVec v1 = {zero};
    RealVec v2 = {zero};
    RealVec e01 = {zero}; /* edges */
    RealVec e02 = {zero};
    RealVec pi = {zero}; /* closest point */
    RealVec N = {zero}; /* normal of the closest point */
    /*
     * Parametric equation of triangle defined plane
     * T(s,t) = v0 + s(v1-v0) + t(v2-v0) = v0 + s*e01 + t*e02
     * s, t: real numbers. v0, v1, v2: vertices. e01, e02: edge vectors.
     * A point pi = T(s,t) is in the triangle T when s>=0, t>=0, and s+t<=1.
     * Further, pi is on an edge of T if one of the conditions s=0; t=0;
     * s+t=1 is true with each condition corresponds to one edge. Each
     * s=0, t=0; s=1, t=0; s=0, t=1 corresponds to v0, v1, and v2.
     */
    RealVec para = {zero}; /* parametric coordinates */
    Real distSquare = zero; /* store computed squared distance */
    Real distSquareMin = FLT_MAX; /* store minimum squared distance */
    int cid = 0; /* closest face identifier */
    for (int n = 0; n < poly->faceN; ++n) {
        BuildTriangle(n, poly, v0, v1, v2, e01, e02);
        distSquare = PointTriangleDistance(p, v0, e01, e02, para);
        if (distSquareMin > distSquare) {
            distSquareMin = distSquare;
            cid = n;
        }
    }
    *fid = cid;
    ComputeIntersection(p, cid, poly, pi, N);
    pi[X] = p[X] - pi[X];
    pi[Y] = p[Y] - pi[Y];
    pi[Z] = p[Z] - pi[Z];
    if (zero < Dot(pi, N)) {
        /* outside polyhedron */
        return 0;
    } else {
        /* inside or on polyhedron */
        return 1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
xorps %xmm0, %xmm0
leaq 0x100(%rsp), %r12
movaps %xmm0, (%r12)
andq $0x0, 0x10(%r12)
leaq 0xa0(%rsp), %r13
andq $0x0, 0x10(%r13)
leaq 0x80(%rsp), %rbp
andq $0x0, 0x10(%rbp)
movaps %xmm0, (%r13)
andq $0x0, 0x50(%rsp)
movaps %xmm0, (%rbp)
andq $0x0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x20(%rsp)
xorl %r15d, %r15d
movsd 0x113b6(%rip), %xmm0    # 0x1b710
xorl %ebx, %ebx
cmpl (%r14), %ebx
jge 0xa3b9
movq %rbp, (%rsp)
movl %ebx, %edi
movq %r14, %rsi
movq %r12, %rdx
leaq 0xe0(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movq %r13, %r9
movsd %xmm0, 0x18(%rsp)
callq 0xa25b
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
leaq 0x60(%rsp), %r8
callq 0xa42c
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
cmoval %ebx, %r15d
minsd %xmm1, %xmm0
incl %ebx
jmp 0xa35c
movq 0x10(%rsp), %rax
movl %r15d, (%rax)
leaq 0x40(%rsp), %rbx
leaq 0x20(%rsp), %r12
movq 0x8(%rsp), %r13
movq %r13, %rdi
movl %r15d, %esi
movq %r14, %rdx
movq %rbx, %rcx
movq %r12, %r8
callq 0xa949
movupd (%r13), %xmm0
subpd (%rbx), %xmm0
movapd %xmm0, (%rbx)
movsd 0x10(%r13), %xmm0
subsd 0x10(%rbx), %xmm0
movsd %xmm0, 0x10(%rbx)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x76cc
xorpd %xmm1, %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
setbe %al
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq