// File: /workspace/github_c_makefile_star2_O3/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)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xa0(%rsp)
xorl %r12d, %r12d
movq %r12, 0xb0(%rsp)
movq %r12, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movq %r12, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movq %r12, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movq %r12, 0x30(%rsp)
movapd %xmm0, 0x20(%rsp)
cmpl $0x0, (%rsi)
jle 0xac2b
movsd 0x11c75(%rip), %xmm0    # 0x1c828
xorl %ebp, %ebp
leaq 0x60(%rsp), %r13
leaq 0xa0(%rsp), %r15
leaq 0x80(%rsp), %rbx
xorl %r12d, %r12d
movsd %xmm0, 0x18(%rsp)
movq %r13, (%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
leaq 0x100(%rsp), %rcx
leaq 0xe0(%rsp), %r8
movq %rbx, %r9
callq 0xaa9d
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r13, %rcx
leaq 0xc0(%rsp), %r8
callq 0xac9e
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
minsd %xmm1, %xmm0
cmoval %ebp, %r12d
incl %ebp
cmpl (%r14), %ebp
jl 0xabcd
movq 0x10(%rsp), %rax
movl %r12d, (%rax)
leaq 0x40(%rsp), %rbx
leaq 0x20(%rsp), %r15
movq 0x8(%rsp), %r13
movq %r13, %rdi
movl %r12d, %esi
movq %r14, %rdx
movq %rbx, %rcx
movq %r15, %r8
callq 0xb1bb
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 %r15, %rsi
callq 0x7e36
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