// File: FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: AddFaceForVertexNormal(int, int&, int, int&, dface_t*, dface_t*&, float&, float (&) [3])
int AddFaceForVertexNormal (const int edgeabs, int &edgeabsnext, const int edgeend, int &edgeendnext, dface_t *const f, dface_t *&fnext, vec_t &angle, vec3_t &normal)
// Must guarantee these faces will form a loop or a chain, otherwise will result in endless loop.
//
//   e[end]/enext[endnext]
//  *
//  |\.
//  |a\ fnext
//  |  \,
//  | f \.
//  |    \.
//  e   enext
//
{
	VectorCopy(getPlaneFromFace(f)->normal, normal);
	int vnum = g_dedges[edgeabs].v[edgeend];
	int iedge, iedgenext, edge, edgenext;
	int i, e, count1, count2;
	vec_t dot;
	for (count1 = count2 = 0, i = 0; i < f->numedges; i++)
	{
		e = g_dsurfedges[f->firstedge + i];
		if (g_dedges[abs(e)].v[0] == g_dedges[abs(e)].v[1])
			continue;
		if (abs(e) == edgeabs)
		{
			iedge = i;
			edge = e;
			count1 ++;
		}
		else if (g_dedges[abs(e)].v[0] == vnum || g_dedges[abs(e)].v[1] == vnum)
		{
			iedgenext = i;
			edgenext = e;
			count2 ++;
		}
	}
	if (count1 != 1 || count2 != 1)
	{
		AddFaceForVertexNormal_printerror (edgeabs, edgeend, f);
		return -1;
	}
	int vnum11, vnum12, vnum21, vnum22;
	vec3_t vec1, vec2;
	vnum11 = g_dedges[abs(edge)].v[edge>0?0:1];
	vnum12 = g_dedges[abs(edge)].v[edge>0?1:0];
	vnum21 = g_dedges[abs(edgenext)].v[edgenext>0?0:1];
	vnum22 = g_dedges[abs(edgenext)].v[edgenext>0?1:0];
	if (vnum == vnum12 && vnum == vnum21 && vnum != vnum11 && vnum != vnum22)
	{
		VectorSubtract(g_dvertexes[vnum11].point, g_dvertexes[vnum].point, vec1);
		VectorSubtract(g_dvertexes[vnum22].point, g_dvertexes[vnum].point, vec2);
		edgeabsnext = abs(edgenext);
		edgeendnext = edgenext>0?0:1;
	}
	else if (vnum == vnum11 && vnum == vnum22 && vnum != vnum12 && vnum != vnum21)
	{
		VectorSubtract(g_dvertexes[vnum12].point, g_dvertexes[vnum].point, vec1);
		VectorSubtract(g_dvertexes[vnum21].point, g_dvertexes[vnum].point, vec2);
		edgeabsnext = abs(edgenext);
		edgeendnext = edgenext>0?1:0;
	}
	else
	{
		AddFaceForVertexNormal_printerror (edgeabs, edgeend, f);
		return -1;
	}
	VectorNormalize(vec1);
	VectorNormalize(vec2);
	dot = DotProduct(vec1,vec2);
	dot = dot>1? 1: dot<-1? -1: dot;
	angle = acos(dot);
	edgeshare_t *es = &g_edgeshare[edgeabsnext];
	if (!(es->faces[0] && es->faces[1]))
		return 1;
	if (es->faces[0] == f && es->faces[1] != f)
		fnext = es->faces[1];
	else if (es->faces[1] == f && es->faces[0] != f)
		fnext = es->faces[0];
	else
	{
		AddFaceForVertexNormal_printerror (edgeabs, edgeend, f);
		return -1;
	}
	return 0;
}

// Assembly:
subq $0x98, %rsp
movq 0xa8(%rsp), %rax
movq 0xa0(%rsp), %rax
movl %edi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movl %edx, 0x84(%rsp)
movq %rcx, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movq %r9, 0x68(%rsp)
movq 0x70(%rsp), %rdi
callq 0x5cfa0
movss (%rax), %xmm0
movq 0xa8(%rsp), %rax
movss %xmm0, (%rax)
movq 0x70(%rsp), %rdi
callq 0x5cfa0
movss 0x4(%rax), %xmm0
movq 0xa8(%rsp), %rax
movss %xmm0, 0x4(%rax)
movq 0x70(%rsp), %rdi
callq 0x5cfa0
movss 0x8(%rax), %xmm0
movq 0xa8(%rsp), %rax
movss %xmm0, 0x8(%rax)
movslq 0x90(%rsp), %rcx
leaq 0x1d3ba43(%rip), %rax   # 0x1d66410
shlq $0x2, %rcx
addq %rcx, %rax
movslq 0x84(%rsp), %rcx
movzwl (%rax,%rcx,2), %eax
movl %eax, 0x64(%rsp)
movl $0x0, 0x44(%rsp)
movl $0x0, 0x48(%rsp)
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
movq 0x70(%rsp), %rcx
movswl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x2ab09
movq 0x70(%rsp), %rax
movl 0x4(%rax), %eax
addl 0x50(%rsp), %eax
movslq %eax, %rcx
leaq 0x1e55a09(%rip), %rax   # 0x1e80430
movl (%rax,%rcx,4), %eax
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movslq %eax, %rcx
leaq 0x1d3b9cd(%rip), %rax   # 0x1d66410
movzwl (%rax,%rcx,4), %eax
movl 0x4c(%rsp), %ecx
movl %ecx, %edx
negl %edx
cmovnsl %edx, %ecx
movslq %ecx, %rdx
leaq 0x1d3b9b4(%rip), %rcx   # 0x1d66410
movzwl 0x2(%rcx,%rdx,4), %ecx
cmpl %ecx, %eax
jne 0x2aa6a
jmp 0x2aaf9
movl 0x4c(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cmpl 0x90(%rsp), %eax
jne 0x2aa9b
movl 0x50(%rsp), %eax
movl %eax, 0x60(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0x58(%rsp)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x2aaf7
movl 0x4c(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movslq %eax, %rcx
leaq 0x1d3b960(%rip), %rax   # 0x1d66410
movzwl (%rax,%rcx,4), %eax
cmpl 0x64(%rsp), %eax
je 0x2aada
movl 0x4c(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movslq %eax, %rcx
leaq 0x1d3b941(%rip), %rax   # 0x1d66410
movzwl 0x2(%rax,%rcx,4), %eax
cmpl 0x64(%rsp), %eax
jne 0x2aaf5
movl 0x50(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x2aaf7
jmp 0x2aaf9
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x2a9fc
cmpl $0x1, 0x48(%rsp)
jne 0x2ab17
cmpl $0x1, 0x44(%rsp)
je 0x2ab3f
movl 0x90(%rsp), %edi
movl 0x84(%rsp), %esi
movq 0x70(%rsp), %rdx
callq 0x2a670
movl $0xffffffff, 0x94(%rsp) # imm = 0xFFFFFFFF
jmp 0x2b142
movl 0x58(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movslq %eax, %rcx
leaq 0x1d3b8bc(%rip), %rax   # 0x1d66410
shlq $0x2, %rcx
addq %rcx, %rax
movl 0x58(%rsp), %esi
movl $0x1, %ecx
xorl %edx, %edx
cmpl $0x0, %esi
cmovgl %edx, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
movl %eax, 0x3c(%rsp)
movl 0x58(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movslq %eax, %rcx
leaq 0x1d3b884(%rip), %rax   # 0x1d66410
shlq $0x2, %rcx
addq %rcx, %rax
movl 0x58(%rsp), %esi
xorl %ecx, %ecx
movl $0x1, %edx
cmpl $0x0, %esi
cmovgl %edx, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
movl %eax, 0x38(%rsp)
movl 0x54(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movslq %eax, %rcx
leaq 0x1d3b84c(%rip), %rax   # 0x1d66410
shlq $0x2, %rcx
addq %rcx, %rax
movl 0x54(%rsp), %esi
movl $0x1, %ecx
xorl %edx, %edx
cmpl $0x0, %esi
cmovgl %edx, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
movl %eax, 0x34(%rsp)
movl 0x54(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movslq %eax, %rcx
leaq 0x1d3b814(%rip), %rax   # 0x1d66410
shlq $0x2, %rcx
addq %rcx, %rax
movl 0x54(%rsp), %esi
xorl %ecx, %ecx
movl $0x1, %edx
cmpl $0x0, %esi
cmovgl %edx, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
movl %eax, 0x30(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x38(%rsp), %eax
jne 0x2adc9
movl 0x64(%rsp), %eax
cmpl 0x34(%rsp), %eax
jne 0x2adc9
movl 0x64(%rsp), %eax
cmpl 0x3c(%rsp), %eax
je 0x2adc9
movl 0x64(%rsp), %eax
cmpl 0x30(%rsp), %eax
je 0x2adc9
movslq 0x3c(%rsp), %rcx
leaq 0x103b7ad(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b796(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss (%rax), %xmm0
movss %xmm0, 0x24(%rsp)
movslq 0x3c(%rsp), %rcx
leaq 0x103b779(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b761(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x28(%rsp)
movslq 0x3c(%rsp), %rcx
leaq 0x103b743(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b72b(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x2c(%rsp)
movslq 0x30(%rsp), %rcx
leaq 0x103b70d(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b6f6(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss (%rax), %xmm0
movss %xmm0, 0x18(%rsp)
movslq 0x30(%rsp), %rcx
leaq 0x103b6d9(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b6c1(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x1c(%rsp)
movslq 0x30(%rsp), %rcx
leaq 0x103b6a3(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b68b(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movl 0x54(%rsp), %ecx
movl %ecx, %eax
negl %eax
cmovnsl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, (%rax)
movl 0x54(%rsp), %edx
movl $0x1, %ecx
xorl %eax, %eax
cmpl $0x0, %edx
cmovgl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, (%rax)
jmp 0x2af9a
movl 0x64(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jne 0x2af70
movl 0x64(%rsp), %eax
cmpl 0x30(%rsp), %eax
jne 0x2af70
movl 0x64(%rsp), %eax
cmpl 0x38(%rsp), %eax
je 0x2af70
movl 0x64(%rsp), %eax
cmpl 0x34(%rsp), %eax
je 0x2af70
movslq 0x38(%rsp), %rcx
leaq 0x103b603(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b5ec(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss (%rax), %xmm0
movss %xmm0, 0x24(%rsp)
movslq 0x38(%rsp), %rcx
leaq 0x103b5cf(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b5b7(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x28(%rsp)
movslq 0x38(%rsp), %rcx
leaq 0x103b599(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b581(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x2c(%rsp)
movslq 0x34(%rsp), %rcx
leaq 0x103b563(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b54c(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss (%rax), %xmm0
movss %xmm0, 0x18(%rsp)
movslq 0x34(%rsp), %rcx
leaq 0x103b52f(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b517(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x1c(%rsp)
movslq 0x34(%rsp), %rcx
leaq 0x103b4f9(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movslq 0x64(%rsp), %rcx
leaq 0x103b4e1(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movl 0x54(%rsp), %ecx
movl %ecx, %eax
negl %eax
cmovnsl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, (%rax)
movl 0x54(%rsp), %edx
xorl %ecx, %ecx
movl $0x1, %eax
cmpl $0x0, %edx
cmovgl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, (%rax)
jmp 0x2af98
movl 0x90(%rsp), %edi
movl 0x84(%rsp), %esi
movq 0x70(%rsp), %rdx
callq 0x2a670
movl $0xffffffff, 0x94(%rsp) # imm = 0xFFFFFFFF
jmp 0x2b142
jmp 0x2af9a
leaq 0x24(%rsp), %rdi
callq 0x11980
leaq 0x18(%rsp), %rdi
callq 0x11980
movss 0x24(%rsp), %xmm1
movss 0x18(%rsp), %xmm2
movss 0x28(%rsp), %xmm0
mulss 0x1c(%rsp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x2c(%rsp), %xmm0
movss 0x20(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x40(%rsp)
movss 0x40(%rsp), %xmm0
movss 0x3f056(%rip), %xmm1    # 0x6a04c
ucomiss %xmm1, %xmm0
jbe 0x2b00b
movss 0x3f049(%rip), %xmm0    # 0x6a04c
movss %xmm0, 0xc(%rsp)
jmp 0x2b042
movss 0x42501(%rip), %xmm0    # 0x6d514
ucomiss 0x40(%rsp), %xmm0
jbe 0x2b02a
movss 0x424f2(%rip), %xmm0    # 0x6d514
movss %xmm0, 0x8(%rsp)
jmp 0x2b036
movss 0x40(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x8(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x40(%rsp)
movss 0x40(%rsp), %xmm0
callq 0x440a0
movq 0xa0(%rsp), %rax
movss %xmm0, (%rax)
movq 0x88(%rsp), %rax
movslq (%rax), %rcx
leaq 0x21568b9(%rip), %rax   # 0x2181930
imulq $0xb0, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpq $0x0, (%rax)
je 0x2b09d
movq 0x10(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x2b0ad
movl $0x1, 0x94(%rsp)
jmp 0x2b142
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpq 0x70(%rsp), %rax
jne 0x2b0df
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
cmpq 0x70(%rsp), %rax
je 0x2b0df
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x68(%rsp), %rax
movq %rcx, (%rax)
jmp 0x2b137
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
cmpq 0x70(%rsp), %rax
jne 0x2b110
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpq 0x70(%rsp), %rax
je 0x2b110
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq 0x68(%rsp), %rax
movq %rcx, (%rax)
jmp 0x2b135
movl 0x90(%rsp), %edi
movl 0x84(%rsp), %esi
movq 0x70(%rsp), %rdx
callq 0x2a670
movl $0xffffffff, 0x94(%rsp) # imm = 0xFFFFFFFF
jmp 0x2b142
jmp 0x2b137
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)