// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20d0
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20d0
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20d0
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20d0
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20d0
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20d0
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20d0
leaq -0x120(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20d0
xorps %xmm0, %xmm0
movsd %xmm0, -0x128(%rbp)
movsd 0x18a07(%rip), %xmm0    # 0x278c8
movsd %xmm0, -0x130(%rbp)
movl $0x0, -0x134(%rbp)
movl $0x0, -0x138(%rbp)
movl -0x138(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jge 0xef8a
movl -0x138(%rbp), %edi
movq -0x18(%rbp), %rsi
leaq -0x40(%rbp), %rdx
leaq -0x60(%rbp), %rcx
leaq -0x80(%rbp), %r8
leaq -0xa0(%rbp), %r9
leaq -0xc0(%rbp), %rax
movq %rax, (%rsp)
callq 0xeca0
movq -0x10(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
leaq -0xc0(%rbp), %rcx
leaq -0x120(%rbp), %r8
callq 0xf040
movsd %xmm0, -0x128(%rbp)
movsd -0x130(%rbp), %xmm0
ucomisd -0x128(%rbp), %xmm0
jbe 0xef74
movsd -0x128(%rbp), %xmm0
movsd %xmm0, -0x130(%rbp)
movl -0x138(%rbp), %eax
movl %eax, -0x134(%rbp)
jmp 0xef76
movl -0x138(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x138(%rbp)
jmp 0xeedd
movl -0x134(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
movl -0x134(%rbp), %esi
movq -0x18(%rbp), %rdx
leaq -0xe0(%rbp), %rcx
leaq -0x100(%rbp), %r8
callq 0xfcf0
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
subsd -0xe0(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
subsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
subsd -0xd0(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x9880
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xf026
movl $0x0, -0x4(%rbp)
jmp 0xf02d
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopl (%rax)