// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %r9, 0x18(%rsp)
movq %r8, %rbx
movq %rcx, 0x20(%rsp)
movl %edx, 0xc(%rsp)
movq %rsi, 0x10(%rsp)
movl %edi, %r15d
movq 0x68(%rsp), %r14
movq %r8, %rdi
callq 0x2f342
movss (%rax), %xmm0
movss %xmm0, (%r14)
movq %rbx, %rdi
callq 0x2f342
movss 0x4(%rax), %xmm0
movss %xmm0, 0x4(%r14)
movq %rbx, %rdi
callq 0x2f342
movss 0x8(%rax), %xmm0
movss %xmm0, 0x8(%r14)
movswq 0x8(%rbx), %rsi
testq %rsi, %rsi
jle 0x1600a
movslq %r15d, %rax
shlq $0x2, %rax
addq 0x3129a(%rip), %rax     # 0x46fc0
movslq 0xc(%rsp), %rcx
movzwl (%rax,%rcx,2), %eax
movslq 0x4(%rbx), %r8
shlq $0x2, %r8
addq 0x31252(%rip), %r8      # 0x46f90
xorl %r9d, %r9d
leaq 0x1d1a6b8(%rip), %rcx   # 0x1d30400
xorl %r11d, %r11d
xorl %r10d, %r10d
movl (%r8,%r9,4), %edi
movl %edi, %ebp
negl %ebp
cmovsl %edi, %ebp
movzwl (%rcx,%rbp,4), %r13d
movzwl 0x2(%rcx,%rbp,4), %r12d
cmpw %r12w, %r13w
je 0x15d97
cmpl %r15d, %ebp
jne 0x15d77
incl %r11d
movl %edi, %r14d
jmp 0x15d97
cmpw %ax, %r13w
sete %bpl
cmpw %ax, %r12w
sete %r12b
orb %bpl, %r12b
movzbl %r12b, %ebp
addl %ebp, %r10d
testb %bpl, %bpl
cmovnel %edi, %edx
incq %r9
cmpq %r9, %rsi
jne 0x15d4e
cmpl $0x1, %r11d
jne 0x1600a
cmpl $0x1, %r10d
jne 0x1600a
movl %r14d, %esi
negl %esi
cmovsl %r14d, %esi
leaq (%rcx,%rsi,4), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
testl %r14d, %r14d
setg %dil
setle %r8b
movzwl (%rsi,%r8,2), %r8d
movzwl (%rsi,%rdi,2), %edi
movl %edx, %esi
negl %esi
cmovsl %edx, %esi
leaq (%rcx,%rsi,4), %r10
xorl %ecx, %ecx
xorl %r9d, %r9d
testl %edx, %edx
setg %cl
setle %r9b
movzwl (%r10,%r9,2), %edx
movzwl (%r10,%rcx,2), %r10d
cmpw %di, %ax
jne 0x15e22
cmpw %dx, %ax
jne 0x15e22
cmpw %r8w, %ax
je 0x15e22
cmpw %r10w, %ax
je 0x15e22
movl %r8d, %edi
movl %r10d, %edx
movl %r9d, %ecx
movq 0x10(%rsp), %r14
jmp 0x15e4d
cmpw %di, %ax
movq 0x10(%rsp), %r14
je 0x1600a
cmpw %r8w, %ax
jne 0x1600a
cmpw %r10w, %ax
jne 0x1600a
cmpw %dx, %ax
je 0x1600a
movzwl %di, %edi
leaq (%rdi,%rdi,2), %rdi
leaq 0x101a5a5(%rip), %r8    # 0x1030400
leaq (%rax,%rax,2), %rax
movss 0x8(%r8,%rdi,4), %xmm5
movss 0x8(%r8,%rax,4), %xmm2
subss %xmm2, %xmm5
movzwl %dx, %edx
leaq (%rdx,%rdx,2), %rdx
movsd (%r8,%rax,4), %xmm0
movsd (%r8,%rdi,4), %xmm8
subps %xmm0, %xmm8
movsd (%r8,%rdx,4), %xmm1
subps %xmm0, %xmm1
movss 0x8(%r8,%rdx,4), %xmm0
subss %xmm2, %xmm0
movl %esi, (%r14)
movzbl %cl, %eax
movq 0x20(%rsp), %rcx
movl %eax, (%rcx)
movaps %xmm8, %xmm2
mulps %xmm8, %xmm2
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
movaps %xmm8, %xmm3
mulss %xmm8, %xmm3
addss %xmm2, %xmm3
movaps %xmm5, %xmm2
mulss %xmm5, %xmm2
addss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
sqrtsd %xmm2, %xmm9
xorps %xmm3, %xmm3
xorps %xmm6, %xmm6
movsd 0x21234(%rip), %xmm7    # 0x37118
ucomisd %xmm9, %xmm7
xorps %xmm4, %xmm4
xorpd %xmm2, %xmm2
ja 0x15f18
cvtps2pd %xmm8, %xmm2
movapd %xmm9, %xmm4
unpcklpd %xmm9, %xmm4            # xmm4 = xmm4[0],xmm9[0]
divpd %xmm4, %xmm2
cvtpd2ps %xmm2, %xmm2
xorps %xmm4, %xmm4
cvtss2sd %xmm5, %xmm4
divsd %xmm9, %xmm4
cvtsd2ss %xmm4, %xmm4
movaps %xmm1, %xmm5
mulps %xmm1, %xmm5
shufps $0x55, %xmm5, %xmm5     # xmm5 = xmm5[1,1,1,1]
movaps %xmm1, %xmm8
mulss %xmm1, %xmm8
addss %xmm5, %xmm8
movaps %xmm0, %xmm5
mulss %xmm0, %xmm5
addss %xmm8, %xmm5
cvtss2sd %xmm5, %xmm5
sqrtsd %xmm5, %xmm5
ucomisd %xmm5, %xmm7
ja 0x15f6c
cvtps2pd %xmm1, %xmm1
movapd %xmm5, %xmm3
unpcklpd %xmm5, %xmm3            # xmm3 = xmm3[0],xmm5[0]
divpd %xmm3, %xmm1
cvtpd2ps %xmm1, %xmm3
cvtss2sd %xmm0, %xmm0
divsd %xmm5, %xmm0
xorps %xmm6, %xmm6
cvtsd2ss %xmm0, %xmm6
mulss %xmm6, %xmm4
movapd %xmm2, %xmm0
mulps %xmm3, %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulss %xmm3, %xmm2
addss %xmm0, %xmm2
addss %xmm4, %xmm2
movss 0x243f1(%rip), %xmm1    # 0x3a380
maxss %xmm2, %xmm1
movss 0x21109(%rip), %xmm3    # 0x370a4
movaps %xmm3, %xmm0
cmpltss %xmm2, %xmm0
movaps %xmm0, %xmm2
andnps %xmm1, %xmm2
andps %xmm3, %xmm0
orps %xmm2, %xmm0
callq 0x4300
movq 0x60(%rsp), %rax
movss %xmm0, (%rax)
movslq (%r14), %rax
imulq $0xb0, %rax, %rsi
leaq 0x2135942(%rip), %rdx   # 0x214b910
movq (%rsi,%rdx), %rcx
movl $0x1, %eax
testq %rcx, %rcx
je 0x1601e
addq %rsi, %rdx
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
je 0x1601e
cmpq %rbx, %rcx
sete %al
cmpq %rbx, %rdx
setne %sil
testb %sil, %al
jne 0x1602d
cmpq %rbx, %rdx
sete %al
cmpq %rbx, %rcx
setne %dl
testb %al, %dl
jne 0x16030
movl %r15d, %edi
movl 0xc(%rsp), %esi
movq %rbx, %rdx
callq 0x15b57
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
jmp 0x1601e