// File: 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:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movl %esi, 0x4c(%rsp)
movslq 0x4c(%rsp), %rcx
leaq 0x1bbc357(%rip), %rax   # 0x1be6410
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movl $0x10, %edi
callq 0x4430
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movq 0x40(%rsp), %rax
movswq 0x8(%rax), %rax
imulq $0x14, %rax, %rdi
callq 0x4430
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x38(%rsp), %rax
cmpq $0x0, 0x8(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movq 0x38(%rsp), %rax
movl $0x0, (%rax)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
movq 0x40(%rsp), %rcx
movswl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x2a640
movq 0x40(%rsp), %rax
movl 0x4(%rax), %eax
addl 0x34(%rsp), %eax
movslq %eax, %rcx
leaq 0x1e562c9(%rip), %rax   # 0x1e80430
movl (%rax,%rcx,4), %eax
movl %eax, 0x30(%rsp)
cmpl $0x0, 0x30(%rsp)
jge 0x2a299
xorl %eax, %eax
subl 0x30(%rsp), %eax
movslq %eax, %rcx
leaq 0x1d3c287(%rip), %rax   # 0x1d66410
movzwl 0x2(%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c279(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movss %xmm0, 0x24(%rsp)
xorl %eax, %eax
subl 0x30(%rsp), %eax
movslq %eax, %rcx
leaq 0x1d3c258(%rip), %rax   # 0x1d66410
movzwl 0x2(%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c24a(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movss %xmm0, 0x28(%rsp)
xorl %eax, %eax
subl 0x30(%rsp), %eax
movslq %eax, %rcx
leaq 0x1d3c228(%rip), %rax   # 0x1d66410
movzwl 0x2(%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c21a(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movss %xmm0, 0x2c(%rsp)
xorl %eax, %eax
subl 0x30(%rsp), %eax
movslq %eax, %rcx
leaq 0x1d3c1f8(%rip), %rax   # 0x1d66410
movzwl (%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c1eb(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movss %xmm0, 0x18(%rsp)
xorl %eax, %eax
subl 0x30(%rsp), %eax
movslq %eax, %rcx
leaq 0x1d3c1ca(%rip), %rax   # 0x1d66410
movzwl (%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c1bd(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movss %xmm0, 0x1c(%rsp)
xorl %eax, %eax
subl 0x30(%rsp), %eax
movslq %eax, %rcx
leaq 0x1d3c19b(%rip), %rax   # 0x1d66410
movzwl (%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c18e(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
jmp 0x2a39c
movslq 0x30(%rsp), %rcx
leaq 0x1d3c16b(%rip), %rax   # 0x1d66410
movzwl (%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c15e(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movss %xmm0, 0x24(%rsp)
movslq 0x30(%rsp), %rcx
leaq 0x1d3c141(%rip), %rax   # 0x1d66410
movzwl (%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c134(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movss %xmm0, 0x28(%rsp)
movslq 0x30(%rsp), %rcx
leaq 0x1d3c116(%rip), %rax   # 0x1d66410
movzwl (%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c109(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movss %xmm0, 0x2c(%rsp)
movslq 0x30(%rsp), %rcx
leaq 0x1d3c0eb(%rip), %rax   # 0x1d66410
movzwl 0x2(%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c0dd(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movss %xmm0, 0x18(%rsp)
movslq 0x30(%rsp), %rcx
leaq 0x1d3c0c0(%rip), %rax   # 0x1d66410
movzwl 0x2(%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c0b2(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movss %xmm0, 0x1c(%rsp)
movslq 0x30(%rsp), %rcx
leaq 0x1d3c094(%rip), %rax   # 0x1d66410
movzwl 0x2(%rax,%rcx,4), %eax
movl %eax, %ecx
leaq 0x103c086(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movss 0x24(%rsp), %xmm0
movslq 0x4c(%rsp), %rcx
leaq 0x5443662(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss (%rax), %xmm0
movss %xmm0, 0x24(%rsp)
movss 0x28(%rsp), %xmm0
movslq 0x4c(%rsp), %rcx
leaq 0x544363f(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss 0x4(%rax), %xmm0
movss %xmm0, 0x28(%rsp)
movss 0x2c(%rsp), %xmm0
movslq 0x4c(%rsp), %rcx
leaq 0x544361b(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss 0x8(%rax), %xmm0
movss %xmm0, 0x2c(%rsp)
movss 0x18(%rsp), %xmm0
movslq 0x4c(%rsp), %rcx
leaq 0x54435f7(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss (%rax), %xmm0
movss %xmm0, 0x18(%rsp)
movss 0x1c(%rsp), %xmm0
movslq 0x4c(%rsp), %rcx
leaq 0x54435d4(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss 0x4(%rax), %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x20(%rsp), %xmm0
movslq 0x4c(%rsp), %rcx
leaq 0x54435b0(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss 0x8(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movss 0x18(%rsp), %xmm0
subss 0x24(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movss 0x1c(%rsp), %xmm0
subss 0x28(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0x20(%rsp), %xmm0
subss 0x2c(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x10(%rsp), %xmm0
movq 0x50(%rsp), %rax
movss 0x8(%rax), %xmm2
movss 0x14(%rsp), %xmm1
movq 0x50(%rsp), %rax
mulss 0x4(%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rsp)
movss 0x14(%rsp), %xmm0
movq 0x50(%rsp), %rax
movss (%rax), %xmm2
movss 0xc(%rsp), %xmm1
movq 0x50(%rsp), %rax
mulss 0x8(%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x4(%rsp)
movss 0xc(%rsp), %xmm0
movq 0x50(%rsp), %rax
movss 0x4(%rax), %xmm2
movss 0x10(%rsp), %xmm1
movq 0x50(%rsp), %rax
mulss (%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x8(%rsp)
movq %rsp, %rdi
callq 0x11980
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x2a56d
jp 0x2a56d
jmp 0x2a630
movss (%rsp), %xmm0
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rsp), %rcx
movslq (%rcx), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movss 0x4(%rsp), %xmm0
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rsp), %rcx
movslq (%rcx), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movss 0x8(%rsp), %xmm0
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rsp), %rcx
movslq (%rcx), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movss 0x24(%rsp), %xmm1
movss (%rsp), %xmm2
movss 0x28(%rsp), %xmm0
mulss 0x4(%rsp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x2c(%rsp), %xmm0
movss 0x8(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rsp), %rcx
movslq (%rcx), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0xc(%rax)
movq 0x38(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x2a13c
movq 0x38(%rsp), %rax
addq $0x58, %rsp
retq
nopw (%rax,%rax)