// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/qradutil.cpp
// Function: FindFacePositions(int)
void FindFacePositions (int facenum)
	// this function must be called after g_face_offset and g_face_centroids and g_edgeshare have been calculated
{
	dface_t *f;
	positionmap_t *map;
	texinfo_t *ti;
	vec3_t v;
	const vec3_t v_up = {0, 0, 1};
	vec_t density;
	vec_t texmins[2], texmaxs[2];
	int imins[2], imaxs[2];
	int is, it;
	int x;
	int k;

	f = &g_dfaces[facenum];
	map = &g_face_positions[facenum];
	map->valid = true;
	map->facenum = facenum;
	map->facewinding = NULL;
	map->facewindingwithoffset = NULL;
	map->texwinding = NULL;
	map->grid = NULL;
	
	ti = &g_texinfo[f->texinfo];
	if (ti->flags & TEX_SPECIAL)
	{
		map->valid = false;
		return;
	}

	VectorCopy (g_face_offset[facenum], map->face_offset);
	VectorCopy (g_face_centroids[facenum], map->face_centroid);
	TranslateWorldToTex (facenum, map->worldtotex);
	if (!InvertMatrix (map->worldtotex, map->textoworld))
	{
		map->valid = false;
		return;
	}
	
	map->facewinding = new Winding (*f);
	map->faceplane = *getPlaneFromFace (f);
	map->facewindingwithoffset = new Winding (map->facewinding->m_NumPoints);
	for (x = 0; x < map->facewinding->m_NumPoints; x++)
	{
		VectorAdd (map->facewinding->m_Points[x], map->face_offset, map->facewindingwithoffset->m_Points[x]);
	}
	map->faceplanewithoffset = map->faceplane;
	map->faceplanewithoffset.dist = map->faceplane.dist + DotProduct (map->face_offset, map->faceplane.normal);

	map->texwinding = new Winding (map->facewinding->m_NumPoints);
	for (x = 0; x < map->facewinding->m_NumPoints; x++)
	{
		ApplyMatrix (map->worldtotex, map->facewinding->m_Points[x], map->texwinding->m_Points[x]);
		map->texwinding->m_Points[x][2] = 0.0;
	}
	map->texwinding->RemoveColinearPoints ();
	VectorCopy (v_up, map->texplane.normal);
	if (CalcMatrixSign (map->worldtotex) < 0.0)
	{
		map->texplane.normal[2] *= -1;
	}
	map->texplane.dist = 0.0;
	if (map->texwinding->m_NumPoints == 0)
	{
		delete map->facewinding;
		map->facewinding = NULL;
		delete map->facewindingwithoffset;
		map->facewindingwithoffset = NULL;
		delete map->texwinding;
		map->texwinding = NULL;
		map->valid = false;
		return;
	}
	VectorSubtract (map->face_centroid, map->face_offset, v);
	ApplyMatrix (map->worldtotex, v, map->texcentroid);
	map->texcentroid[2] = 0.0;

	for (x = 0; x < map->texwinding->m_NumPoints; x++)
	{
		for (k = 0; k < 2; k++)
		{
			if (x == 0 || map->texwinding->m_Points[x][k] < texmins[k])
				texmins[k] = map->texwinding->m_Points[x][k];
			if (x == 0 || map->texwinding->m_Points[x][k] > texmaxs[k])
				texmaxs[k] = map->texwinding->m_Points[x][k];
		}
	}
	density = 3.0;
#ifdef HLRAD_FASTMODE
	if (g_fastmode)
	{
		density = 1.0;
	}
#endif
	map->step[0] = (vec_t)TEXTURE_STEP / density;
	map->step[1] = (vec_t)TEXTURE_STEP / density;
	map->step[2] = 1.0;
	for (k = 0; k < 2; k++)
	{
		imins[k] = (int)floor (texmins[k] / map->step[k] + 0.5 - ON_EPSILON);
		imaxs[k] = (int)ceil (texmaxs[k] / map->step[k] - 0.5 + ON_EPSILON);
	}
	map->start[0] = (imins[0] - 0.5) * map->step[0];
	map->start[1] = (imins[1] - 0.5) * map->step[1];
	map->start[2] = 0.0;
	map->w = imaxs[0] - imins[0] + 1;
	map->h = imaxs[1] - imins[1] + 1;
	if (map->w <= 0 || map->h <= 0 || (double)map->w * (double)map->h > 99999999)
	{
		delete map->facewinding;
		map->facewinding = NULL;
		delete map->facewindingwithoffset;
		map->facewindingwithoffset = NULL;
		delete map->texwinding;
		map->texwinding = NULL;
		map->valid = false;
		return;
	}

	map->grid = (position_t *)malloc (map->w * map->h * sizeof (position_t));
	hlassume (map->grid != NULL, assume_NoMemory);

	for (it = 0; it < map->h; it++)
	{
		for (is = 0; is < map->w; is++)
		{
			CalcSinglePosition (map, is, it);
		}
	}

	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movslq %edi, %rdi
imulq $0x14, %rdi, %r14
imulq $0x110, %rdi, %rax      # imm = 0x110
leaq 0x55ca9d8(%rip), %rcx   # 0x55f6be0
movb $0x1, (%rax,%rcx)
movl %edi, 0x4(%rax,%rcx)
andq $0x0, 0x80(%rax,%rcx)
leaq 0x1b811f0(%rip), %r12   # 0x1bad410
andq $0x0, 0xa0(%rax,%rcx)
leaq (%rcx,%rax), %rbx
andq $0x0, 0xc0(%rax,%rcx)
andq $0x0, 0x108(%rax,%rcx)
movswq 0xa(%r12,%r14), %rax
imulq $0x28, %rax, %rax
leaq 0x11811b0(%rip), %rcx   # 0x11ad400
testb $0x1, 0x24(%rcx,%rax)
jne 0x2c71c
imulq $0xc, %rdi, %rax
leaq 0x540879a(%rip), %rcx   # 0x5434a00
movsd 0x4(%rcx,%rax), %xmm0
leaq 0x4c146ad(%rip), %rdx   # 0x4c40920
movss (%rcx,%rax), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movss (%rdx,%rax), %xmm2
shufps $0xd4, %xmm0, %xmm2     # xmm2 = xmm2[0,1],xmm0[1,3]
shufps $0x28, %xmm2, %xmm1     # xmm1 = xmm1[0,2],xmm2[2,0]
movups %xmm1, 0x8(%rbx)
movsd 0x4(%rdx,%rax), %xmm0
movsd %xmm0, 0x18(%rbx)
leaq 0x20(%rbx), %r15
movq %r15, %rsi
callq 0x2be6e
leaq 0x50(%rbx), %rsi
movq %r15, %rdi
callq 0x2bf09
testb %al, %al
je 0x2c71c
addq %r14, %r12
pushq $0x20
popq %rdi
callq 0x42e0
movq %rax, %r14
movss 0x96f7(%rip), %xmm0     # 0x359c4
movq %rax, %rdi
movq %r12, %rsi
callq 0xadb6
movq %r14, 0x80(%rbx)
movzwl (%r12), %eax
imulq $0x14, %rax, %rax
cmpw $0x0, 0x2(%r12)
jne 0x2c2fa
leaq 0xb01108(%rip), %rcx    # 0xb2d400
jmp 0x2c301
leaq 0x552a8df(%rip), %rcx   # 0x5556be0
movl 0x10(%rcx,%rax), %edx
movl %edx, 0x98(%rbx)
movups (%rcx,%rax), %xmm0
movups %xmm0, 0x88(%rbx)
pushq $0x20
popq %rdi
callq 0x42e0
movq %rax, %r12
movl 0x8(%r14), %esi
movq %rax, %rdi
callq 0xaa74
leaq 0x88(%rbx), %rax
movq %r12, 0xa0(%rbx)
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x80(%rbx), %r12
movl 0x8(%r12), %esi
cmpq %rsi, %rdx
jae 0x2c3c9
movq 0x10(%r12), %rsi
movss (%rsi,%rcx), %xmm0
addss 0x8(%rbx), %xmm0
movq 0xa0(%rbx), %rsi
movq 0x10(%rsi), %rsi
movss %xmm0, (%rsi,%rcx)
movq 0x80(%rbx), %rsi
movq 0xa0(%rbx), %rdi
movq 0x10(%rsi), %rsi
movss 0x4(%rsi,%rcx), %xmm0
addss 0xc(%rbx), %xmm0
movq 0x10(%rdi), %rsi
movss %xmm0, 0x4(%rsi,%rcx)
movq 0x80(%rbx), %rsi
movq 0xa0(%rbx), %rdi
movq 0x10(%rsi), %rsi
movss 0x8(%rsi,%rcx), %xmm0
addss 0x10(%rbx), %xmm0
movq 0x10(%rdi), %rsi
movss %xmm0, 0x8(%rsi,%rcx)
incq %rdx
addq $0xc, %rcx
jmp 0x2c33f
movups (%rax), %xmm0
movups %xmm0, 0xa8(%rbx)
movl 0x10(%rax), %eax
movl %eax, 0xb8(%rbx)
movss 0x8(%rbx), %xmm0
movss 0xc(%rbx), %xmm1
mulss 0x8c(%rbx), %xmm1
mulss 0x88(%rbx), %xmm0
addss %xmm1, %xmm0
movss 0x10(%rbx), %xmm1
mulss 0x90(%rbx), %xmm1
addss %xmm0, %xmm1
addss 0x94(%rbx), %xmm1
movss %xmm1, 0xb4(%rbx)
pushq $0x20
popq %rdi
callq 0x42e0
movq %rax, %r14
movl 0x8(%r12), %esi
movq %rax, %rdi
callq 0xaa74
movq %r14, 0xc0(%rbx)
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x80(%rbx), %rax
movl 0x8(%rax), %ecx
cmpq %rcx, %r12
jae 0x2c486
movq 0x10(%rax), %rsi
addq %r14, %rsi
movq 0xc0(%rbx), %rax
movq 0x10(%rax), %rdx
addq %r14, %rdx
movq %r15, %rdi
callq 0x2bb9d
movq 0xc0(%rbx), %rax
movq 0x10(%rax), %rax
andl $0x0, 0x8(%rax,%r14)
incq %r12
addq $0xc, %r14
jmp 0x2c440
movq 0xc0(%rbx), %rdi
movss 0x952f(%rip), %xmm0     # 0x359c4
callq 0xae86
andq $0x0, 0xc8(%rbx)
movl $0x3f800000, 0xd0(%rbx) # imm = 0x3F800000
movq %r15, %rdi
callq 0x2be12
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x2c4c6
movl $0xbf800000, 0xd0(%rbx) # imm = 0xBF800000
andl $0x0, 0xd4(%rbx)
movq 0xc0(%rbx), %rax
cmpl $0x0, 0x8(%rax)
je 0x2c6ce
leaq 0xc8(%rbx), %rax
movq %rax, 0x30(%rsp)
movsd 0x8(%rbx), %xmm0
movsd 0x14(%rbx), %xmm1
subps %xmm0, %xmm1
leaq 0xd8(%rsp), %rsi
movlps %xmm1, (%rsi)
movss 0x1c(%rbx), %xmm0
subss 0x10(%rbx), %xmm0
movss %xmm0, 0x8(%rsi)
leaq 0xdc(%rbx), %rdx
movq %r15, %rdi
callq 0x2bb9d
andl $0x0, 0xe4(%rbx)
movq 0xc0(%rbx), %rax
movl 0x8(%rax), %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rcx, %rsi
je 0x2c595
movq 0x10(%rax), %rdi
leaq (%rdi,%rdx), %r8
xorl %r9d, %r9d
cmpq $0x2, %r9
je 0x2c58c
testq %rsi, %rsi
je 0x2c573
movss (%r8,%r9,4), %xmm0
movss 0x28(%rsp,%r9,4), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x2c569
movss %xmm0, 0x28(%rsp,%r9,4)
ucomiss 0x40(%rsp,%r9,4), %xmm0
ja 0x2c580
jmp 0x2c587
movss (%rdi,%r9,4), %xmm0
movss %xmm0, 0x28(%rsp,%r9,4)
movss %xmm0, 0x40(%rsp,%r9,4)
incq %r9
jmp 0x2c545
incq %rsi
addq $0xc, %rdx
jmp 0x2c535
leaq 0x5248444(%rip), %rax   # 0x52749e0
cmpb $0x0, (%rax)
jne 0x2c5ab
movss 0xd2ab(%rip), %xmm0     # 0x39854
jmp 0x2c5b3
movss 0x9439(%rip), %xmm0     # 0x359ec
movss %xmm0, 0xf4(%rbx)
movss %xmm0, 0xf8(%rbx)
movl $0x3f800000, 0xfc(%rbx) # imm = 0x3F800000
xorl %r14d, %r14d
movss %xmm0, 0x8(%rsp)
cmpq $0x8, %r14
je 0x2c654
movss 0x28(%rsp,%r14), %xmm0
movss 0xf4(%rbx,%r14), %xmm1
movss %xmm1, 0xc(%rsp)
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
addsd 0x8c5d(%rip), %xmm0     # 0x35260
addsd 0x8f8d(%rip), %xmm0     # 0x35598
callq 0x4590
cvttsd2si %xmm0, %eax
movl %eax, 0x58(%rsp,%r14)
movss 0x40(%rsp,%r14), %xmm0
divss 0xc(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
addsd 0x8f86(%rip), %xmm0     # 0x355b8
addsd 0x8f6e(%rip), %xmm0     # 0x355a8
callq 0x4090
cvttsd2si %xmm0, %eax
movss 0x8(%rsp), %xmm0
movl %eax, 0x50(%rsp,%r14)
addq $0x4, %r14
jmp 0x2c5d6
cvtss2sd %xmm0, %xmm0
movq 0x58(%rsp), %xmm1
cvtdq2pd %xmm1, %xmm2
addpd 0x9006(%rip), %xmm2     # 0x35670
movlhps %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm2, %xmm0
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, 0xe8(%rbx)
andl $0x0, 0xf0(%rbx)
movq 0x50(%rsp), %xmm0
psubd %xmm1, %xmm0
pcmpeqd %xmm1, %xmm1
pshufd $0x50, %xmm0, %xmm2     # xmm2 = xmm0[0,0,1,1]
psubd %xmm1, %xmm0
movq %xmm0, 0x100(%rbx)
movmskpd %xmm2, %eax
testl %eax, %eax
jne 0x2c6ce
movd %xmm0, %eax
cvtsi2sd %eax, %xmm1
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ecx
cvtsi2sd %ecx, %xmm0
mulsd %xmm1, %xmm0
ucomisd 0xd144(%rip), %xmm0     # 0x39810
jbe 0x2c731
movq 0x80(%rbx), %rdi
testq %rdi, %rdi
je 0x2c6e0
movq (%rdi), %rax
callq *0x8(%rax)
andq $0x0, 0x80(%rbx)
movq 0xa0(%rbx), %rdi
testq %rdi, %rdi
je 0x2c6fa
movq (%rdi), %rax
callq *0x8(%rax)
andq $0x0, 0xa0(%rbx)
movq 0xc0(%rbx), %rdi
testq %rdi, %rdi
je 0x2c714
movq (%rdi), %rax
callq *0x8(%rax)
andq $0x0, 0xc0(%rbx)
movb $0x0, (%rbx)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
imull %eax, %ecx
imulq $0x18, %rcx, %rdi
callq 0x4420
movq %rax, 0x108(%rbx)
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x92cb
xorl %ecx, %ecx
cmpl 0x104(%rbx), %ecx
jge 0x2c71f
cvtsi2ss %ecx, %xmm0
movss %xmm0, 0x8(%rsp)
leal 0x1(%rcx), %eax
movl %eax, 0x20(%rsp)
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x24(%rsp)
cvtsi2sd %ecx, %xmm0
addsd 0x8ada(%rip), %xmm0     # 0x35260
movapd %xmm0, 0xb0(%rsp)
xorl %ebp, %ebp
movq %rcx, 0x48(%rsp)
movl 0x100(%rbx), %eax
cmpl %eax, %ebp
jge 0x2ca8c
movq 0x108(%rbx), %r12
imull %ecx, %eax
addl %ebp, %eax
cltq
imulq $0x18, %rax, %r14
cvtsi2ss %ebp, %xmm0
movss 0xf4(%rbx), %xmm1
mulss %xmm1, %xmm0
movss 0xe8(%rbx), %xmm2
movss 0xec(%rbx), %xmm3
leal 0x1(%rbp), %eax
movl %eax, 0xc(%rsp)
cvtsi2ss %eax, %xmm4
addss %xmm2, %xmm0
mulss %xmm1, %xmm4
addss %xmm2, %xmm4
movss 0xf8(%rbx), %xmm1
movss 0x8(%rsp), %xmm2
mulss %xmm1, %xmm2
addss %xmm3, %xmm2
movss 0x5859(%rip), %xmm5     # 0x32064
movlps %xmm5, 0x60(%rsp)
andl $0x0, 0x68(%rsp)
movss %xmm0, 0x6c(%rsp)
movsd 0x936d(%rip), %xmm0     # 0x35b90
movsd %xmm0, 0x74(%rsp)
movl $0x80000000, %eax       # imm = 0x80000000
movl %eax, 0x7c(%rsp)
movaps 0x8a57(%rip), %xmm0     # 0x35290
xorps %xmm0, %xmm4
movss %xmm4, 0x80(%rsp)
movsd 0x9233(%rip), %xmm4     # 0x35a80
movsd %xmm4, 0x88(%rsp)
andl $0x0, 0x90(%rsp)
mulss 0x24(%rsp), %xmm1
addss %xmm3, %xmm1
movss %xmm2, 0x94(%rsp)
movsd 0x9327(%rip), %xmm2     # 0x35ba0
movsd %xmm2, 0x9c(%rsp)
movl %eax, 0xa4(%rsp)
xorps %xmm0, %xmm1
movss %xmm1, 0xa8(%rsp)
movb $0x1, 0x1(%r12,%r14)
pushq $0x20
popq %rdi
callq 0x42e0
movq %rax, %r13
movq %rbx, %r15
movq 0xc0(%rbx), %rsi
movq %rax, %rdi
callq 0xaab8
addq %r14, %r12
xorl %r14d, %r14d
movl 0x8(%r13), %eax
cmpq $0x50, %r14
je 0x2c8f0
testl %eax, %eax
je 0x2c97c
leaq (%rsp,%r14), %rsi
addq $0x60, %rsi
movq %r13, %rdi
xorl %edx, %edx
movss 0x90df(%rip), %xmm0     # 0x359c4
callq 0xb632
addq $0x14, %r14
jmp 0x2c8be
testl %eax, %eax
je 0x2c97c
cvtsi2sd %ebp, %xmm0
addsd 0x895c(%rip), %xmm0     # 0x35260
cvtps2pd 0xe8(%r15), %xmm1
unpcklpd 0xb0(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
cvtps2pd 0xf4(%r15), %xmm2
mulpd %xmm0, %xmm2
addpd %xmm1, %xmm2
cvtpd2ps %xmm2, %xmm0
movb $0x0, (%r12)
movapd %xmm0, 0xc0(%rsp)
movlpd %xmm0, 0x10(%rsp)
andl $0x0, 0x18(%rsp)
movq %r13, %rdi
movq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdx
callq 0x21a7f
leaq 0xc(%r12), %rdx
movq %r15, %rdi
movq %rbx, %rsi
movq %rdx, 0x38(%rsp)
callq 0x2d2b8
testb %al, %al
je 0x2c986
movw $0x1, (%r12)
jmp 0x2ca55
movb $0x0, (%r12)
jmp 0x2ca6f
movb (%r12), %al
testb $0x1, %al
jne 0x2c9ba
movq %r13, %rdi
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0xa53c
movq %r15, %rdi
movq %rbx, %rsi
movq 0x38(%rsp), %rdx
callq 0x2d2b8
testb %al, %al
jne 0x2ca50
movb (%r12), %al
testb $0x1, %al
jne 0x2ca6f
leaq 0x5248017(%rip), %rax   # 0x52749e0
cmpb $0x0, (%rax)
jne 0x2ca6f
xorl %ecx, %ecx
leaq 0xc(%rcx), %r14
cmpq $0x9c, %r14
je 0x2ca6f
movss 0xfc(%r15), %xmm0
xorps %xmm3, %xmm3
mulss %xmm3, %xmm0
leaq 0xcf54(%rip), %rax      # 0x39950
movsd (%rcx,%rax), %xmm1
movsd 0xf4(%r15), %xmm2
mulps %xmm1, %xmm2
addps 0xc0(%rsp), %xmm2
movlps %xmm2, 0x10(%rsp)
addss %xmm3, %xmm0
movss %xmm0, 0x18(%rsp)
movq %r13, %rdi
movq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rbp
movq %rbp, %rdx
callq 0x21a7f
movq %r15, %rdi
movq %rbp, %rsi
movq 0x38(%rsp), %rdx
callq 0x2d2b8
movq %r14, %rcx
testb %al, %al
je 0x2c9d4
movb $0x1, (%r12)
movss 0x10(%rsp), %xmm0
movss %xmm0, 0x4(%r12)
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x8(%r12)
movq (%r13), %rax
movq %r13, %rdi
callq *0x8(%rax)
movl 0xc(%rsp), %eax
movl %eax, %ebp
movq 0x48(%rsp), %rcx
movq %r15, %rbx
jmp 0x2c796
movl 0x20(%rsp), %eax
movl %eax, %ecx
jmp 0x2c757
jmp 0x2caa1
movq %rax, %rbx
movq %r12, %r14
jmp 0x2caa4
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r14, %rdi
jmp 0x2cab5
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r13, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x45a0