// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x48, %rsp
movq %rdi, 0x28(%rsp)
movslq %esi, %rbx
imulq $0x14, %rbx, %r14
leaq 0x1b99a64(%rip), %r15   # 0x1bad410
leaq (%r15,%r14), %r12
pushq $0x10
popq %rdi
callq 0x4420
movq %rax, %r13
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rbp
movl %ebp, %esi
callq 0x92cb
movswq 0x8(%r15,%r14), %rax
imulq $0x14, %rax, %rdi
callq 0x4420
movq %rax, 0x8(%r13)
xorl %edi, %edi
movq %rax, 0x20(%rsp)
testq %rax, %rax
setne %dil
movl %ebp, %esi
callq 0x92cb
movq %r12, %rcx
movq %r13, 0x18(%rsp)
andl $0x0, (%r13)
imulq $0xc, %rbx, %rbp
addq 0x3056a(%rip), %rbp     # 0x43f78
xorl %r12d, %r12d
leaq 0x1d199f8(%rip), %r14   # 0x1d2d410
leaq 0x10199f1(%rip), %rbx   # 0x102d410
xorl %r13d, %r13d
movswl 0x8(%rcx), %eax
cmpl %eax, %r12d
jge 0x13b6e
movq %rcx, %r15
movslq 0x4(%rcx), %rax
addq %r12, %rax
leaq 0x1e339f0(%rip), %rcx   # 0x1e47430
movslq (%rcx,%rax,4), %rax
movl %eax, %ecx
negl %ecx
testq %rax, %rax
leaq (%r14,%rcx,4), %rdx
leaq 0x2(%r14,%rcx,4), %rcx
leaq (%r14,%rax,4), %rsi
cmovsq %rcx, %rsi
leaq 0x2(%r14,%rax,4), %rax
cmovsq %rdx, %rax
movzwl (%rsi), %ecx
imulq $0xc, %rcx, %rcx
movzwl (%rax), %eax
imulq $0xc, %rax, %rax
movss (%rbp), %xmm0
movss (%rbx,%rcx), %xmm1
addss %xmm0, %xmm1
addss (%rbx,%rax), %xmm0
movss %xmm1, 0x4(%rsp)
subss %xmm1, %xmm0
movq 0x28(%rsp), %rdx
movss (%rdx), %xmm1
movsd 0x4(%rbx,%rcx), %xmm4
movsd 0x4(%rbx,%rax), %xmm2
movsd 0x4(%rbp), %xmm3
addps %xmm3, %xmm4
addps %xmm3, %xmm2
movaps %xmm4, 0x30(%rsp)
subps %xmm4, %xmm2
movsd 0x4(%rdx), %xmm3
movaps %xmm0, %xmm4
shufps $0xd4, %xmm2, %xmm4     # xmm4 = xmm4[0,1],xmm2[1,3]
shufps $0x52, %xmm2, %xmm4     # xmm4 = xmm4[2,0],xmm2[1,1]
mulps %xmm3, %xmm4
movaps %xmm1, %xmm5
shufps $0xd4, %xmm3, %xmm5     # xmm5 = xmm5[0,1],xmm3[1,3]
shufps $0x52, %xmm3, %xmm5     # xmm5 = xmm5[2,0],xmm3[1,1]
mulps %xmm2, %xmm5
subps %xmm4, %xmm5
movlps %xmm5, 0x8(%rsp)
mulss %xmm1, %xmm2
mulss %xmm3, %xmm0
subss %xmm2, %xmm0
movss %xmm0, 0x10(%rsp)
leaq 0x8(%rsp), %rdi
callq 0xc021
ucomiss 0x1e56c(%rip), %xmm0    # 0x32070
jne 0x13b08
jnp 0x13b63
movslq %r13d, %r13
imulq $0x14, %r13, %rax
movsd 0x8(%rsp), %xmm0
movq 0x20(%rsp), %rcx
movsd %xmm0, (%rcx,%rax)
movss 0x10(%rsp), %xmm1
movss %xmm1, 0x8(%rcx,%rax)
movss 0x4(%rsp), %xmm2
mulss %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps 0x30(%rsp), %xmm3
mulss %xmm3, %xmm0
addss %xmm0, %xmm2
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
mulss %xmm1, %xmm3
addss %xmm2, %xmm3
movss %xmm3, 0xc(%rcx,%rax)
incl %r13d
movq 0x18(%rsp), %rax
movl %r13d, (%rax)
incq %r12
movq %r15, %rcx
jmp 0x13a22
movq 0x18(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq