// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlvis/flow.cpp
// Function: ClipToSeperators(winding_t const*, winding_t const*, winding_t*, bool, pstack_s*)
inline static winding_t* ClipToSeperators(
    const winding_t* const source,
    const winding_t* const pass, 
    winding_t* const a_target,
    const bool flipclip, 
    pstack_t* const stack)
{
    int             i, j, k, l;
    plane_t         plane;
    vec3_t          v1, v2;
    float           d;
    int             counts[3];
    bool            fliptest;
    winding_t*      target = a_target;

    const unsigned int numpoints = source->numpoints;

    // check all combinations       
    for (i=0, l=1; i < numpoints; i++, l++)
    {
        if (l == numpoints)
        {
            l = 0;
        }

        VectorSubtract(source->points[l], source->points[i], v1);

        // fing a vertex of pass that makes a plane that puts all of the
        // vertexes of pass on the front side and all of the vertexes of
        // source on the back side
        for (j = 0; j < pass->numpoints; j++)
        {
            VectorSubtract(pass->points[j], source->points[i], v2);
            CrossProduct(v1, v2, plane.normal);
            if (VectorNormalize(plane.normal) < ON_EPSILON)
            {
                continue;
            }
            plane.dist = DotProduct(pass->points[j], plane.normal);

            // find out which side of the generated seperating plane has the
            // source portal
            fliptest = false;
            for (k = 0; k < numpoints; k++)
            {
                if ((k == i) | (k == l)) // | instead of || for branch optimization
                {
                    continue;
                }
                d = DotProduct(source->points[k], plane.normal) - plane.dist;
                if (d < -ON_EPSILON)
                {                                          // source is on the negative side, so we want all
                    // pass and target on the positive side
                    fliptest = false;
                    break;
                }
                else if (d > ON_EPSILON)
                {                                          // source is on the positive side, so we want all
                    // pass and target on the negative side
                    fliptest = true;
                    break;
                }
            }
            if (k == numpoints)
            {
                continue;                                  // planar with source portal
            }

            // flip the normal if the source portal is backwards
            if (fliptest)
            {
                VectorSubtract(vec3_origin, plane.normal, plane.normal);
                plane.dist = -plane.dist;
            }

            // if all of the pass portal points are now on the positive side,
            // this is the seperating plane
            counts[0] = counts[1] = counts[2] = 0;
            for (k = 0; k < pass->numpoints; k++)
            {
                if (k == j)
                {
                    continue;
                }
                d = DotProduct(pass->points[k], plane.normal) - plane.dist;
                if (d < -ON_EPSILON)
                {
                    break;
                }
                else if (d > ON_EPSILON)
                {
                    counts[0]++;
                }
                else
                {
                    counts[2]++;
                }
            }
            if (k != pass->numpoints)
            {
                continue;                                  // points on negative side, not a seperating plane
            }

            if (!counts[0])
            {
                continue;                                  // planar with seperating plane
            }

            // flip the normal if we want the back side
            if (flipclip)
            {
                VectorSubtract(vec3_origin, plane.normal, plane.normal);
                plane.dist = -plane.dist;
            }

	    if (target != NULL)
	    {
            // clip target by the seperating plane
            target = ChopWinding(target, stack, &plane);
            if (!target)
            {
                return NULL;                               // target is not visible
            }
	    }
	    else
	    {
        	AddPlane(stack, &plane);
	    }

#ifdef RVIS_LEVEL_1
            break; /* Antony was here */
#endif
        }
    }

    return target;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r8, 0x18(%rsp)
movl %ecx, 0x24(%rsp)
movq %rdx, 0x10(%rsp)
movl 0x4(%rdi), %ebp
leaq 0x8(%rdi), %rax
movq %rax, 0x38(%rsp)
leaq 0x8(%rsi), %rax
movq %rax, 0x30(%rsp)
leaq 0x54cd(%rip), %rax      # 0x11d60
movsd (%rax), %xmm0
movaps %xmm0, 0x50(%rsp)
movss 0x8(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
addq $0x10, %rdi
movq %rdi, 0x48(%rsp)
movq %rsi, 0x28(%rsp)
leaq 0x10(%rsi), %rax
movq %rax, 0x40(%rsp)
pushq $0x1
popq %r13
xorl %r15d, %r15d
cmpq %rbp, %r15
je 0xcc37
cmpl %ebp, %r13d
movl $0x0, %eax
cmovel %eax, %r13d
movslq %r13d, %rax
imulq $0xc, %rax, %rax
movq 0x38(%rsp), %rdx
movss (%rdx,%rax), %xmm1
imulq $0xc, %r15, %rcx
leaq (%rdx,%rcx), %r12
subss (%rdx,%rcx), %xmm1
movsd 0x4(%rdx,%rax), %xmm2
movsd 0x4(%rdx,%rcx), %xmm0
subps %xmm0, %xmm2
movaps %xmm1, 0x90(%rsp)
shufps $0xd4, %xmm2, %xmm1     # xmm1 = xmm1[0,1],xmm2[1,3]
shufps $0x52, %xmm2, %xmm1     # xmm1 = xmm1[2,0],xmm2[1,1]
movaps 0x68f2(%rip), %xmm0     # 0x13210
xorps %xmm0, %xmm1
movaps %xmm1, 0x70(%rsp)
movaps %xmm2, 0x80(%rsp)
xorps %xmm0, %xmm2
movaps %xmm2, 0x60(%rsp)
xorl %r14d, %r14d
movq 0x28(%rsp), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %r14
jge 0xcc2c
imulq $0xc, %r14, %rbx
movq 0x30(%rsp), %rax
movss 0x4(%rax,%rbx), %xmm0
subss 0x4(%r12), %xmm0
movss (%rax,%rbx), %xmm1
movss 0x8(%rax,%rbx), %xmm2
unpcklps %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
movss (%r12), %xmm1
movss 0x8(%r12), %xmm3
unpcklps %xmm1, %xmm3            # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subps %xmm3, %xmm2
movaps 0x90(%rsp), %xmm1
mulss %xmm0, %xmm1
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
mulps 0x70(%rsp), %xmm0
movaps 0x80(%rsp), %xmm3
mulps %xmm2, %xmm3
addps %xmm0, %xmm3
movlps %xmm3, (%rsp)
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
mulss 0x60(%rsp), %xmm2
addss %xmm1, %xmm2
movss %xmm2, 0x8(%rsp)
movq %rsp, %rdi
callq 0xae51
movsd 0x6a04(%rip), %xmm7     # 0x133d0
movsd 0x69f4(%rip), %xmm6     # 0x133c8
cvtss2sd %xmm0, %xmm0
ucomisd %xmm0, %xmm6
ja 0xcb31
addq 0x30(%rsp), %rbx
movsd (%rsp), %xmm1
movaps %xmm1, %xmm3
shufps $0x55, %xmm1, %xmm3     # xmm3 = xmm3[1,1],xmm1[1,1]
movaps %xmm3, %xmm0
mulss 0x4(%rbx), %xmm0
movss (%rbx), %xmm4
mulss %xmm1, %xmm4
addss %xmm0, %xmm4
movss 0x8(%rsp), %xmm2
movss 0x8(%rbx), %xmm0
mulss %xmm2, %xmm0
addss %xmm4, %xmm0
movss %xmm0, 0xc(%rsp)
movq 0x48(%rsp), %rcx
xorl %eax, %eax
cmpq %rax, %rbp
je 0xcb31
cmpq %rax, %r15
je 0xca70
cmpq %rax, %r13
je 0xca70
movss -0x4(%rcx), %xmm4
mulss %xmm3, %xmm4
movss -0x8(%rcx), %xmm5
mulss %xmm1, %xmm5
addss %xmm4, %xmm5
movss (%rcx), %xmm4
mulss %xmm2, %xmm4
addss %xmm5, %xmm4
subss %xmm0, %xmm4
cvtss2sd %xmm4, %xmm4
ucomisd %xmm4, %xmm7
ja 0xca79
ucomisd %xmm6, %xmm4
ja 0xca82
incq %rax
addq $0xc, %rcx
jmp 0xca27
cmpl %eax, %ebp
jne 0xcab1
jmp 0xcb31
movaps 0x50(%rsp), %xmm3
subps %xmm1, %xmm3
movlps %xmm3, (%rsp)
movss 0x20(%rsp), %xmm1
subss %xmm2, %xmm1
movss %xmm1, 0x8(%rsp)
xorps 0x676b(%rip), %xmm0     # 0x13210
movss %xmm0, 0xc(%rsp)
movaps %xmm1, %xmm2
movaps %xmm3, %xmm1
movq 0x28(%rsp), %rax
movl 0x4(%rax), %eax
xorl %edx, %edx
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movaps %xmm1, %xmm3
shufps $0x55, %xmm1, %xmm3     # xmm3 = xmm3[1,1],xmm1[1,1]
movq 0x40(%rsp), %rdi
xorl %esi, %esi
cmpq %rdx, %rcx
je 0xcb20
cmpq %rdx, %r14
je 0xcb15
movss -0x4(%rdi), %xmm4
mulss %xmm3, %xmm4
movss -0x8(%rdi), %xmm5
mulss %xmm1, %xmm5
addss %xmm4, %xmm5
movss (%rdi), %xmm4
mulss %xmm2, %xmm4
addss %xmm5, %xmm4
subss %xmm0, %xmm4
cvtss2sd %xmm4, %xmm4
ucomisd %xmm4, %xmm7
ja 0xcb1e
ucomisd %xmm6, %xmm4
jbe 0xcb15
incl %esi
incq %rdx
addq $0xc, %rdi
jmp 0xcad3
movl %edx, %ecx
cmpl %eax, %ecx
sete %al
testl %esi, %esi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
je 0xcb39
incq %r14
jmp 0xc939
cmpb $0x0, 0x24(%rsp)
je 0xcb69
movaps 0x50(%rsp), %xmm3
subps %xmm1, %xmm3
movlps %xmm3, (%rsp)
movss 0x20(%rsp), %xmm1
subss %xmm2, %xmm1
movss %xmm1, 0x8(%rsp)
xorps 0x66ad(%rip), %xmm0     # 0x13210
movss %xmm0, 0xc(%rsp)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xcb93
movq 0x18(%rsp), %rsi
movq %rsp, %rdx
callq 0xcce1
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0xcc2c
jmp 0xcc4e
movq 0x18(%rsp), %rax
movl 0x14d0(%rax), %eax
testl %eax, %eax
je 0xcc03
xorl %ebx, %ebx
xorl %r14d, %r14d
movslq %eax, %rcx
cmpq %rcx, %r14
jge 0xcc05
movq 0x18(%rsp), %rcx
movq 0x14d8(%rcx), %rdi
movss 0xc(%rdi,%rbx), %xmm0
subss 0xc(%rsp), %xmm0
andps 0x6652(%rip), %xmm0     # 0x13220
cvtss2sd %xmm0, %xmm0
movsd 0x6806(%rip), %xmm1     # 0x133e0
ucomisd %xmm0, %xmm1
jb 0xcbfa
addq %rbx, %rdi
movq %rsp, %rsi
callq 0xcca4
testb %al, %al
jne 0xcc23
movq 0x18(%rsp), %rax
movl 0x14d0(%rax), %eax
incq %r14
addq $0x10, %rbx
jmp 0xcba7
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
movq 0x14d8(%rdx), %rax
shlq $0x4, %rcx
movups (%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
incl 0x14d0(%rdx)
movq $0x0, 0x10(%rsp)
incq %r15
incl %r13d
jmp 0xc8c5
movq 0x10(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0xcc3c