// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %rdi, %rbx
movslq %esi, %r12
leaq (%r12,%r12,4), %r14
leaq 0x1b9b3bc(%rip), %r15   # 0x1bb1400
movl $0x10, %edi
callq 0x4410
movq %rax, %r13
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa7c9
movswq 0x8(%r15,%r14,4), %rax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rdi
callq 0x4410
movq %rax, 0x8(%r13)
xorl %edi, %edi
movq %rax, 0x18(%rsp)
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa7c9
movq %r13, 0x8(%rsp)
movl $0x0, (%r13)
cmpw $0x0, 0x8(%r15,%r14,4)
jle 0x162b5
leaq (%r12,%r12,2), %r13
shlq $0x2, %r13
addq 0x31ebc(%rip), %r13     # 0x47f78
leaq (%r15,%r14,4), %r14
xorl %ebp, %ebp
leaq 0x1e35357(%rip), %rdi   # 0x1e4b420
leaq 0x1d1b330(%rip), %r15   # 0x1d31400
leaq 0x101b329(%rip), %r12   # 0x1031400
xorpd %xmm4, %xmm4
movsd 0x2202d(%rip), %xmm5    # 0x38110
movq %rbx, 0x10(%rsp)
movslq 0x4(%r14), %rax
addq %rbp, %rax
movslq (%rdi,%rax,4), %rax
movl %eax, %ecx
negl %ecx
testq %rax, %rax
leaq (%r15,%rcx,4), %rdx
leaq 0x2(%r15,%rcx,4), %rcx
leaq (%r15,%rax,4), %rsi
leaq 0x2(%r15,%rax,4), %rax
cmovsq %rcx, %rsi
cmovsq %rdx, %rax
movzwl (%rsi), %ecx
leaq (%rcx,%rcx,2), %rcx
movzwl (%rax), %eax
leaq (%rax,%rax,2), %rax
movss (%r13), %xmm8
movss (%r12,%rcx,4), %xmm6
addss %xmm8, %xmm6
addss (%r12,%rax,4), %xmm8
subss %xmm6, %xmm8
movss (%rbx), %xmm0
movsd 0x4(%r12,%rcx,4), %xmm7
movsd 0x4(%r12,%rax,4), %xmm1
movsd 0x4(%r13), %xmm2
addps %xmm2, %xmm7
addps %xmm2, %xmm1
subps %xmm7, %xmm1
movsd 0x4(%rbx), %xmm2
movaps %xmm8, %xmm3
shufps $0xd4, %xmm1, %xmm3     # xmm3 = xmm3[0,1],xmm1[1,3]
shufps $0x52, %xmm1, %xmm3     # xmm3 = xmm3[2,0],xmm1[1,1]
mulps %xmm2, %xmm3
movaps %xmm0, %xmm9
shufps $0xd4, %xmm2, %xmm9     # xmm9 = xmm9[0,1],xmm2[1,3]
shufps $0x52, %xmm2, %xmm9     # xmm9 = xmm9[2,0],xmm2[1,1]
mulps %xmm1, %xmm9
subps %xmm3, %xmm9
mulss %xmm0, %xmm1
mulss %xmm2, %xmm8
subss %xmm1, %xmm8
movaps %xmm9, %xmm0
mulps %xmm9, %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm9, %xmm1
mulss %xmm9, %xmm1
addss %xmm0, %xmm1
movaps %xmm8, %xmm0
mulss %xmm8, %xmm0
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd %xmm4, %xmm0
jb 0x161cd
sqrtsd %xmm0, %xmm0
jmp 0x16217
movq %rdi, %rbx
movss %xmm6, 0x4(%rsp)
movaps %xmm7, 0x40(%rsp)
movaps %xmm8, 0x30(%rsp)
movaps %xmm9, 0x20(%rsp)
callq 0x4240
movaps 0x20(%rsp), %xmm9
movaps 0x30(%rsp), %xmm8
movaps 0x40(%rsp), %xmm7
movss 0x4(%rsp), %xmm6
movsd 0x21f05(%rip), %xmm5    # 0x38110
xorpd %xmm4, %xmm4
movq %rbx, %rdi
movq 0x10(%rsp), %rbx
ucomisd %xmm0, %xmm5
jbe 0x16227
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
xorl %eax, %eax
jmp 0x1625f
cvtps2pd %xmm9, %xmm1
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
divpd %xmm2, %xmm1
cvtpd2ps %xmm1, %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm8, %xmm2
divsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
cvtsd2ss %xmm0, %xmm0
cmpneqss 0x21eec(%rip), %xmm0    # 0x38144
movd %xmm0, %eax
andl $0x1, %eax
testb %al, %al
je 0x162a5
movq 0x8(%rsp), %rdx
movslq (%rdx), %rax
leaq (%rax,%rax,4), %rcx
movq 0x18(%rsp), %rsi
movlpd %xmm1, (%rsi,%rcx,4)
movss %xmm2, 0x8(%rsi,%rcx,4)
mulss %xmm1, %xmm6
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
mulss %xmm7, %xmm1
addss %xmm6, %xmm1
shufps $0x55, %xmm7, %xmm7     # xmm7 = xmm7[1,1,1,1]
mulss %xmm2, %xmm7
addss %xmm1, %xmm7
movss %xmm7, 0xc(%rsi,%rcx,4)
incl %eax
movl %eax, (%rdx)
movswl 0x8(%r14), %eax
incq %rbp
cmpl %eax, %ebp
jl 0x160e8
movq 0x8(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq