// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x38(%rsp)
movq %r8, %rbx
movq %rcx, 0x20(%rsp)
movl %edx, %ebp
movq %rsi, 0x8(%rsp)
movl %edi, %r15d
movq 0xa8(%rsp), %r14
movq %r8, %rdi
callq 0x303c5
movss (%rax), %xmm0
movss %xmm0, (%r14)
movq %rbx, %rdi
callq 0x303c5
movss 0x4(%rax), %xmm0
movss %xmm0, 0x4(%r14)
movq %rbx, %rdi
callq 0x303c5
movss 0x8(%rax), %xmm0
movss %xmm0, 0x8(%r14)
movslq %r15d, %rax
shlq $0x2, %rax
addq 0x31b17(%rip), %rax     # 0x47fb8
movslq %ebp, %rcx
movzwl (%rax,%rcx,2), %eax
movswq 0x8(%rbx), %rsi
testq %rsi, %rsi
jle 0x1653e
movl %ebp, 0x10(%rsp)
movslq 0x4(%rbx), %rdi
shlq $0x2, %rdi
addq 0x31ac7(%rip), %rdi     # 0x47f90
xorl %r9d, %r9d
leaq 0x1d1af2d(%rip), %r11   # 0x1d31400
xorl %r8d, %r8d
xorl %r10d, %r10d
movl (%rdi,%r9,4), %edx
movl %edx, %ebp
negl %ebp
cmovsl %edx, %ebp
movzwl (%r11,%rbp,4), %r13d
movzwl 0x2(%r11,%rbp,4), %r12d
cmpw %r12w, %r13w
je 0x16522
cmpl %r15d, %ebp
jne 0x16502
incl %r8d
movl %edx, %r14d
jmp 0x16522
cmpw %ax, %r13w
sete %bpl
cmpw %ax, %r12w
sete %r12b
orb %bpl, %r12b
movzbl %r12b, %ebp
addl %ebp, %r10d
testb %bpl, %bpl
cmovnel %edx, %ecx
incq %r9
cmpq %r9, %rsi
jne 0x164d9
xorl $0x1, %r8d
xorl $0x1, %r10d
orl %r8d, %r10d
setne %dl
movl 0x10(%rsp), %ebp
jmp 0x16540
movb $0x1, %dl
testb %dl, %dl
je 0x16565
movl %r15d, %edi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x162df
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r14d, %edx
negl %edx
cmovsl %r14d, %edx
leaq 0x1d1ae8b(%rip), %rsi   # 0x1d31400
leaq (%rsi,%rdx,4), %rdx
xorl %edi, %edi
xorl %r8d, %r8d
testl %r14d, %r14d
setg %dil
setle %r8b
movzwl (%rdx,%r8,2), %r8d
movzwl (%rdx,%rdi,2), %edi
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
leaq (%rsi,%rdx,4), %r10
xorl %esi, %esi
xorl %r9d, %r9d
testl %ecx, %ecx
setg %sil
setle %r9b
movzwl (%r10,%r9,2), %ecx
movzwl (%r10,%rsi,2), %r10d
cmpw %di, %ax
jne 0x165dc
cmpw %cx, %ax
jne 0x165dc
cmpw %r8w, %ax
je 0x165dc
cmpw %r10w, %ax
je 0x165dc
movl %r8d, %edi
movl %r10d, %ecx
movl %r9d, %esi
movq 0x8(%rsp), %r14
jmp 0x16607
cmpw %di, %ax
movq 0x8(%rsp), %r14
je 0x16544
cmpw %r8w, %ax
jne 0x16544
cmpw %r10w, %ax
jne 0x16544
cmpw %cx, %ax
je 0x16544
movzwl %di, %edi
leaq (%rdi,%rdi,2), %rdi
leaq 0x101adeb(%rip), %r8    # 0x1031400
leaq (%rax,%rax,2), %rax
movss 0x8(%r8,%rdi,4), %xmm6
movss 0x8(%r8,%rax,4), %xmm2
subss %xmm2, %xmm6
movzwl %cx, %ecx
leaq (%rcx,%rcx,2), %rcx
movsd (%r8,%rax,4), %xmm7
movsd (%r8,%rdi,4), %xmm5
subps %xmm7, %xmm5
movsd (%r8,%rcx,4), %xmm4
movss 0x8(%r8,%rcx,4), %xmm3
movl %edx, (%r14)
movzbl %sil, %eax
movq 0x20(%rsp), %rcx
movl %eax, (%rcx)
movaps %xmm5, %xmm0
mulps %xmm5, %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm5, %xmm1
mulss %xmm5, %xmm1
addss %xmm0, %xmm1
movaps %xmm6, %xmm0
mulss %xmm6, %xmm0
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1668f
sqrtsd %xmm0, %xmm0
jmp 0x166d6
movss %xmm3, 0x8(%rsp)
movaps %xmm4, 0x20(%rsp)
movss %xmm6, 0x10(%rsp)
movss %xmm2, 0x4(%rsp)
movaps %xmm5, 0x50(%rsp)
movaps %xmm7, 0x40(%rsp)
callq 0x4240
movaps 0x40(%rsp), %xmm7
movaps 0x50(%rsp), %xmm5
movss 0x4(%rsp), %xmm2
movss 0x10(%rsp), %xmm6
movaps 0x20(%rsp), %xmm4
movss 0x8(%rsp), %xmm3
subps %xmm7, %xmm4
subss %xmm2, %xmm3
movsd 0x21a2b(%rip), %xmm7    # 0x38110
ucomisd %xmm0, %xmm7
jbe 0x166f3
xorps %xmm5, %xmm5
xorps %xmm6, %xmm6
jmp 0x16718
cvtps2pd %xmm5, %xmm1
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
divpd %xmm2, %xmm1
cvtpd2ps %xmm1, %xmm5
xorps %xmm1, %xmm1
cvtss2sd %xmm6, %xmm1
divsd %xmm0, %xmm1
xorps %xmm6, %xmm6
cvtsd2ss %xmm1, %xmm6
movaps %xmm4, %xmm0
mulps %xmm4, %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm4, %xmm1
mulss %xmm4, %xmm1
addss %xmm0, %xmm1
movaps %xmm3, %xmm0
mulss %xmm3, %xmm0
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1674b
sqrtsd %xmm0, %xmm0
jmp 0x16786
movss %xmm3, 0x8(%rsp)
movaps %xmm4, 0x20(%rsp)
movapd %xmm5, 0x10(%rsp)
movss %xmm6, 0x4(%rsp)
callq 0x4240
movsd 0x219a1(%rip), %xmm7    # 0x38110
movss 0x4(%rsp), %xmm6
movapd 0x10(%rsp), %xmm5
movaps 0x20(%rsp), %xmm4
movss 0x8(%rsp), %xmm3
movq 0xa0(%rsp), %r13
ucomisd %xmm0, %xmm7
jbe 0x1679d
xorpd %xmm1, %xmm1
xorps %xmm0, %xmm0
jmp 0x167c2
cvtps2pd %xmm4, %xmm1
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
divpd %xmm2, %xmm1
cvtpd2ps %xmm1, %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm3, %xmm2
divsd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsd2ss %xmm2, %xmm0
mulss %xmm0, %xmm6
movapd %xmm5, %xmm0
mulps %xmm1, %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulss %xmm1, %xmm5
addss %xmm0, %xmm5
addss %xmm6, %xmm5
movss 0x24b8b(%rip), %xmm1    # 0x3b370
maxss %xmm5, %xmm1
movss 0x21947(%rip), %xmm2    # 0x38138
movaps %xmm2, %xmm0
cmpltss %xmm5, %xmm0
movaps %xmm0, %xmm3
andnps %xmm1, %xmm3
andps %xmm2, %xmm0
orps %xmm3, %xmm0
callq 0x4310
movss %xmm0, (%r13)
movslq (%r14), %rax
imulq $0xb0, %rax, %rsi
leaq 0x21360ef(%rip), %rdx   # 0x214c910
movq (%rsi,%rdx), %rcx
movl $0x1, %eax
testq %rcx, %rcx
je 0x16556
addq %rsi, %rdx
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
je 0x16556
cmpq %rbx, %rcx
sete %al
cmpq %rbx, %rdx
setne %sil
testb %sil, %al
jne 0x1686b
cmpq %rbx, %rdx
sete %al
cmpq %rbx, %rcx
setne %dl
testb %al, %dl
je 0x16544
jmp 0x1686e
movq %rdx, %rcx
movq 0x38(%rsp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
jmp 0x16556