// File: FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: CreateSampleFrag(int, float, float, float const (*) [2], int)
static samplefraginfo_t *CreateSampleFrag (int facenum, vec_t s, vec_t t, 
#ifdef HLRAD_BLUR_MINIMALSQUARE
	const vec_t square[2][2],
#else
	vec_t reach, 
#endif
	int maxsize)
{
	samplefraginfo_t *info;
	const vec3_t v_s = {1, 0, 0};
	const vec3_t v_t = {0, 1, 0};

	info = (samplefraginfo_t *)malloc (sizeof (samplefraginfo_t));
	hlassume (info != NULL, assume_NoMemory);
	info->maxsize = maxsize;
	info->size = 1;
	info->head = (samplefrag_t *)malloc (sizeof (samplefrag_t));
	hlassume (info->head != NULL, assume_NoMemory);

	info->head->next = NULL;
	info->head->parentfrag = NULL;
	info->head->parentedge = NULL;
	info->head->facenum = facenum;
	
	info->head->flippedangle = 0.0;
	info->head->noseam = true;

	MatrixForScale (vec3_origin, 1.0, info->head->coordtomycoord);
	MatrixForScale (vec3_origin, 1.0, info->head->mycoordtocoord);

	info->head->origin[0] = s;
	info->head->origin[1] = t;
	info->head->origin[2] = 0.0;
	VectorCopy (info->head->origin, info->head->myorigin);

#ifdef HLRAD_BLUR_MINIMALSQUARE
	VectorScale (v_s,  1, info->head->rect.planes[0].normal); info->head->rect.planes[0].dist =  square[0][0]; // smin
	VectorScale (v_s, -1, info->head->rect.planes[1].normal); info->head->rect.planes[1].dist = -square[1][0]; // smax
	VectorScale (v_t,  1, info->head->rect.planes[2].normal); info->head->rect.planes[2].dist =  square[0][1]; // tmin
	VectorScale (v_t, -1, info->head->rect.planes[3].normal); info->head->rect.planes[3].dist = -square[1][1]; // tmax
#else
	VectorScale (v_s,  1, info->head->rect.planes[0].normal); info->head->rect.planes[0].dist =  (s - reach);
	VectorScale (v_s, -1, info->head->rect.planes[1].normal); info->head->rect.planes[1].dist = -(s + reach);
	VectorScale (v_t,  1, info->head->rect.planes[2].normal); info->head->rect.planes[2].dist =  (t - reach);
	VectorScale (v_t, -1, info->head->rect.planes[3].normal); info->head->rect.planes[3].dist = -(t + reach);
#endif
	info->head->myrect = info->head->rect;

	ChopFrag (info->head);

	if (info->head->winding->m_NumPoints == 0 || info->head->mywinding->m_NumPoints == 0)
	{
		// empty
		delete info->head->mywinding;
		delete info->head->winding;
		free (info->head->edges);
		free (info->head);
		info->head = NULL;
		info->size = 0;
	}
	else
	{
		// prune edges
		for (samplefragedge_t *e = info->head->edges; e < info->head->edges + info->head->numedges; e++)
		{
			if (e->nextfacenum == info->head->facenum)
			{
				e->tried = true;
			}
		}
	}

	while (info->size < info->maxsize)
	{
		samplefrag_t *bestfrag;
		samplefragedge_t *bestedge;
		samplefrag_t *newfrag;

		if (!FindBestEdge (info, bestfrag, bestedge))
		{
			break;
		}

		newfrag = GrowSingleFrag (info, bestfrag, bestedge);
		bestedge->tried = true;

		if (newfrag)
		{
			newfrag->next = info->head;
			info->head = newfrag;
			info->size++;

			for (samplefrag_t *f = info->head; f; f = f->next)
			{
				for (samplefragedge_t *e = newfrag->edges; e < newfrag->edges + newfrag->numedges; e++)
				{
					if (e->nextfacenum == f->facenum)
					{
						e->tried = true;
					}
				}
			}
			for (samplefrag_t *f = info->head; f; f = f->next)
			{
				for (samplefragedge_t *e = f->edges; e < f->edges + f->numedges; e++)
				{
					if (e->nextfacenum == newfrag->facenum)
					{
						e->tried = true;
					}
				}
			}
		}
	}

	return info;
}

// Assembly:
subq $0x98, %rsp
movl %edi, 0x94(%rsp)
movss %xmm0, 0x90(%rsp)
movss %xmm1, 0x8c(%rsp)
movq %rsi, 0x80(%rsp)
movl %edx, 0x7c(%rsp)
movq 0x2b67d(%rip), %rax     # 0x6dd90
movq %rax, 0x64(%rsp)
movl 0x2b67a(%rip), %eax     # 0x6dd98
movl %eax, 0x6c(%rsp)
movq 0x2b673(%rip), %rax     # 0x6dd9c
movq %rax, 0x58(%rsp)
movl 0x2b670(%rip), %eax     # 0x6dda4
movl %eax, 0x60(%rsp)
movl $0x10, %edi
callq 0x4430
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movl 0x7c(%rsp), %ecx
movq 0x70(%rsp), %rax
movl %ecx, (%rax)
movq 0x70(%rsp), %rax
movl $0x1, 0x4(%rax)
movl $0x188, %edi            # imm = 0x188
callq 0x4430
movq %rax, %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x70(%rsp), %rax
cmpq $0x0, 0x8(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq $0x0, (%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq $0x0, 0x8(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq $0x0, 0x10(%rax)
movl 0x94(%rsp), %ecx
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movl %ecx, 0x18(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x1c(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movb $0x1, 0x20(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rsi
addq $0x24, %rsi
leaq 0x2962f(%rip), %rdi     # 0x6be50
movss 0x27823(%rip), %xmm0    # 0x6a04c
callq 0x5dd80
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rsi
addq $0x54, %rsi
leaq 0x2960e(%rip), %rdi     # 0x6be50
movss 0x27802(%rip), %xmm0    # 0x6a04c
callq 0x5dd80
movss 0x90(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0x84(%rax)
movss 0x8c(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0x88(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x8c(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss 0x84(%rax), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0x90(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss 0x88(%rax), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0x94(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss 0x8c(%rax), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0x98(%rax)
movss 0x27747(%rip), %xmm0    # 0x6a04c
mulss 0x64(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0x9c(%rax)
movss 0x27728(%rip), %xmm0    # 0x6a04c
mulss 0x68(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xa0(%rax)
movss 0x27709(%rip), %xmm0    # 0x6a04c
mulss 0x6c(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xa4(%rax)
movq 0x80(%rsp), %rax
movss (%rax), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xa8(%rax)
movss 0x2ab95(%rip), %xmm0    # 0x6d514
mulss 0x64(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xb0(%rax)
movss 0x2ab76(%rip), %xmm0    # 0x6d514
mulss 0x68(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xb4(%rax)
movss 0x2ab57(%rip), %xmm0    # 0x6d514
mulss 0x6c(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xb8(%rax)
movq 0x80(%rsp), %rax
movss 0x8(%rax), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xbc(%rax)
movss 0x27645(%rip), %xmm0    # 0x6a04c
mulss 0x58(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xc4(%rax)
movss 0x27626(%rip), %xmm0    # 0x6a04c
mulss 0x5c(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xc8(%rax)
movss 0x27607(%rip), %xmm0    # 0x6a04c
mulss 0x60(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xcc(%rax)
movq 0x80(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xd0(%rax)
movss 0x2aa92(%rip), %xmm0    # 0x6d514
mulss 0x58(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xd8(%rax)
movss 0x2aa73(%rip), %xmm0    # 0x6d514
mulss 0x5c(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xdc(%rax)
movss 0x2aa54(%rip), %xmm0    # 0x6d514
mulss 0x60(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xe0(%rax)
movq 0x80(%rsp), %rax
movss 0xc(%rax), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movss %xmm0, 0xe4(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rsi
addq $0x9c, %rsi
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rdi
addq $0xec, %rdi
movl $0x50, %edx
callq 0x4280
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x2d120
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x140(%rax), %rax
cmpl $0x0, 0x8(%rax)
je 0x42b6a
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x160(%rax), %rax
cmpl $0x0, 0x8(%rax)
jne 0x42bf4
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x160(%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x42b90
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x140(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x42bb6
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x180(%rax), %rdi
callq 0x43a0
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x43a0
movq 0x70(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x70(%rsp), %rax
movl $0x0, 0x4(%rax)
jmp 0x42c72
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x180(%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq 0x70(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq 0x180(%rcx), %rcx
movq 0x70(%rsp), %rdx
movq 0x8(%rdx), %rdx
movslq 0x17c(%rdx), %rdx
imulq $0xa8, %rdx, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0x42c70
movq 0x50(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x70(%rsp), %rcx
movq 0x8(%rcx), %rcx
cmpl 0x18(%rcx), %eax
jne 0x42c5c
movq 0x50(%rsp), %rax
movb $0x1, 0xc(%rax)
jmp 0x42c5e
movq 0x50(%rsp), %rax
addq $0xa8, %rax
movq %rax, 0x50(%rsp)
jmp 0x42c09
jmp 0x42c72
jmp 0x42c74
movq 0x70(%rsp), %rax
movl 0x4(%rax), %eax
movq 0x70(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x42e3c
movq 0x70(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x43030
testb $0x1, %al
jne 0x42ca6
jmp 0x42e3c
movq 0x70(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x432e0
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rax
movb $0x1, 0xc(%rax)
cmpq $0x0, 0x38(%rsp)
je 0x42e37
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x38(%rsp), %rax
movq %rcx, (%rax)
movq 0x38(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x70(%rsp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
je 0x42d9b
movq 0x38(%rsp), %rax
movq 0x180(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq 0x38(%rsp), %rcx
movq 0x180(%rcx), %rcx
movq 0x38(%rsp), %rdx
movslq 0x17c(%rdx), %rdx
imulq $0xa8, %rdx, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0x42d87
movq 0x28(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x30(%rsp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x42d73
movq 0x28(%rsp), %rax
movb $0x1, 0xc(%rax)
jmp 0x42d75
movq 0x28(%rsp), %rax
addq $0xa8, %rax
movq %rax, 0x28(%rsp)
jmp 0x42d2c
jmp 0x42d89
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
jmp 0x42d0f
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x42e35
movq 0x20(%rsp), %rax
movq 0x180(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movq 0x180(%rcx), %rcx
movq 0x20(%rsp), %rdx
movslq 0x17c(%rdx), %rdx
imulq $0xa8, %rdx, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0x42e21
movq 0x18(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x38(%rsp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x42e0d
movq 0x18(%rsp), %rax
movb $0x1, 0xc(%rax)
jmp 0x42e0f
movq 0x18(%rsp), %rax
addq $0xa8, %rax
movq %rax, 0x18(%rsp)
jmp 0x42dc6
jmp 0x42e23
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x42da9
jmp 0x42e37
jmp 0x42c74
movq 0x70(%rsp), %rax
addq $0x98, %rsp
retq
nopl (%rax)