// File: /workspace/github_cpp_makefile_star2_O3/FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: CreateIntersectTest(dplane_t const*, int)
intersecttest_t *CreateIntersectTest (const dplane_t *p, int facenum)
{
	dface_t *f = &g_dfaces[facenum];
	intersecttest_t *t;
	t = (intersecttest_t *)malloc (sizeof (intersecttest_t));
	hlassume (t != NULL, assume_NoMemory);
	t->clipplanes = (dplane_t *)malloc (f->numedges * sizeof (dplane_t));
	hlassume (t->clipplanes != NULL, assume_NoMemory);
	t->numclipplanes = 0;
	int j;
	for (j = 0; j < f->numedges; j++)
	{
		// should we use winding instead?
		int edgenum = g_dsurfedges[f->firstedge + j];
		{
			vec3_t v0, v1;
			vec3_t dir, normal;
			if (edgenum < 0)
			{
				VectorCopy (g_dvertexes[g_dedges[-edgenum].v[1]].point, v0);
				VectorCopy (g_dvertexes[g_dedges[-edgenum].v[0]].point, v1);
			}
			else
			{
				VectorCopy (g_dvertexes[g_dedges[edgenum].v[0]].point, v0);
				VectorCopy (g_dvertexes[g_dedges[edgenum].v[1]].point, v1);
			}
			VectorAdd (v0, g_face_offset[facenum], v0);
			VectorAdd (v1, g_face_offset[facenum], v1);
			VectorSubtract (v1, v0, dir);
			CrossProduct (dir, p->normal, normal); // facing inward
			if (!VectorNormalize (normal))
			{
				continue;
			}
			VectorCopy (normal, t->clipplanes[t->numclipplanes].normal);
			t->clipplanes[t->numclipplanes].dist = DotProduct (v0, normal);
			t->numclipplanes++;
		}
	}
	return t;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r15
movslq %esi, %r12
leaq (%r12,%r12,4), %r13
leaq 0x1b9aaea(%rip), %rbp   # 0x1bb0400
movl $0x10, %edi
callq 0x4400
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa6ed
movswq 0x8(%rbp,%r13,4), %rax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rdi
callq 0x4400
movq %rax, %r14
movq %rax, 0x8(%rbx)
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa6ed
movl $0x0, (%rbx)
movswq 0x8(%rbp,%r13,4), %rax
testq %rax, %rax
jle 0x15b2f
leaq (,%r13,4), %rcx
addq %rbp, %rcx
movslq 0x4(%rcx), %rcx
leaq (%r12,%r12,2), %rdx
leaq 0x542205e(%rip), %rsi   # 0x54379f0
movss (%rsi,%rdx,4), %xmm0
movsd 0x4(%rsi,%rdx,4), %xmm1
movsd 0x4(%r15), %xmm2
movss (%r15), %xmm3
movaps %xmm3, %xmm4
shufps $0xd4, %xmm2, %xmm4     # xmm4 = xmm4[0,1],xmm2[1,3]
shufps $0xe2, %xmm2, %xmm4     # xmm4 = xmm4[2,0],xmm2[2,3]
shlq $0x2, %rcx
addq 0x315d2(%rip), %rcx     # 0x46f90
xorl %edx, %edx
leaq 0x1d1aa39(%rip), %rsi   # 0x1d30400
leaq 0x101aa32(%rip), %rdi   # 0x1030400
movsd 0x21742(%rip), %xmm5    # 0x37118
xorps %xmm6, %xmm6
xorl %r8d, %r8d
movslq (%rcx,%r8,4), %r9
movl %r9d, %r10d
negl %r10d
testq %r9, %r9
leaq (%rsi,%r10,4), %r11
leaq 0x2(%rsi,%r10,4), %r10
leaq (%rsi,%r9,4), %r15
cmovsq %r10, %r15
leaq 0x2(%rsi,%r9,4), %r9
cmovsq %r11, %r9
movzwl (%r15), %r10d
leaq (%r10,%r10,2), %r10
movzwl (%r9), %r9d
leaq (%r9,%r9,2), %r9
movss (%rdi,%r10,4), %xmm7
addss %xmm0, %xmm7
movss (%rdi,%r9,4), %xmm9
addss %xmm0, %xmm9
subss %xmm7, %xmm9
movsd 0x4(%rdi,%r10,4), %xmm8
movsd 0x4(%rdi,%r9,4), %xmm10
addps %xmm1, %xmm8
addps %xmm1, %xmm10
subps %xmm8, %xmm10
movaps %xmm9, %xmm11
shufps $0xd4, %xmm10, %xmm11   # xmm11 = xmm11[0,1],xmm10[1,3]
shufps $0x52, %xmm10, %xmm11   # xmm11 = xmm11[2,0],xmm10[1,1]
mulps %xmm2, %xmm11
movaps %xmm3, %xmm12
mulss %xmm10, %xmm12
mulps %xmm4, %xmm10
subps %xmm11, %xmm10
mulss %xmm2, %xmm9
subss %xmm12, %xmm9
movaps %xmm10, %xmm11
mulps %xmm10, %xmm11
shufps $0x55, %xmm11, %xmm11   # xmm11 = xmm11[1,1,1,1]
movaps %xmm10, %xmm12
mulss %xmm10, %xmm12
addss %xmm11, %xmm12
movaps %xmm9, %xmm11
mulss %xmm9, %xmm11
addss %xmm12, %xmm11
cvtss2sd %xmm11, %xmm11
sqrtsd %xmm11, %xmm11
ucomisd %xmm11, %xmm5
ja 0x15b23
xorps %xmm12, %xmm12
cvtsd2ss %xmm11, %xmm12
ucomiss %xmm6, %xmm12
jne 0x15abf
jnp 0x15b23
cvtss2sd %xmm9, %xmm9
divsd %xmm11, %xmm9
cvtsd2ss %xmm9, %xmm9
cvtps2pd %xmm10, %xmm10
movslq %edx, %rdx
leaq (%rdx,%rdx,4), %r9
unpcklpd %xmm11, %xmm11          # xmm11 = xmm11[0,0]
divpd %xmm11, %xmm10
cvtpd2ps %xmm10, %xmm10
movlpd %xmm10, (%r14,%r9,4)
movss %xmm9, 0x8(%r14,%r9,4)
mulss %xmm10, %xmm7
shufps $0x55, %xmm10, %xmm10   # xmm10 = xmm10[1,1,1,1]
mulss %xmm8, %xmm10
addss %xmm7, %xmm10
shufps $0x55, %xmm8, %xmm8     # xmm8 = xmm8[1,1,1,1]
mulss %xmm9, %xmm8
addss %xmm10, %xmm8
movss %xmm8, 0xc(%r14,%r9,4)
incl %edx
movl %edx, (%rbx)
incq %r8
cmpq %r8, %rax
jne 0x159dc
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq