// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %r9, 0x8(%rsp)
movq %r8, %rbx
movq %rcx, 0x10(%rsp)
movl %edx, %ebp
movq %rsi, 0x18(%rsp)
movl %edi, %r15d
movq 0x78(%rsp), %r14
movq %r8, %rdi
callq 0x2b6a1
movss (%rax), %xmm0
movss %xmm0, (%r14)
movq %rbx, %rdi
callq 0x2b6a1
movss 0x4(%rax), %xmm0
movss %xmm0, 0x4(%r14)
movq %rbx, %rdi
callq 0x2b6a1
movss 0x8(%rax), %xmm0
movss %xmm0, 0x8(%r14)
movslq %r15d, %rax
shlq $0x2, %rax
addq 0x3028a(%rip), %rax     # 0x43fc0
movl %ebp, 0x4(%rsp)
movslq %ebp, %rcx
movzwl (%rax,%rcx,2), %eax
movl 0x8(%rbx), %ecx
xorl %esi, %esi
testw %cx, %cx
cmovlel %esi, %ecx
movzwl %cx, %edi
leaq 0x1e336da(%rip), %r9    # 0x1e47430
leaq 0x1d196b3(%rip), %rcx   # 0x1d2d410
xorl %r8d, %r8d
xorl %r10d, %r10d
cmpl %esi, %edi
je 0x13dc2
movslq 0x4(%rbx), %r14
addq %rsi, %r14
movl (%r9,%r14,4), %r14d
movl %r14d, %ebp
negl %ebp
cmovsl %r14d, %ebp
movzwl (%rcx,%rbp,4), %r12d
movzwl 0x2(%rcx,%rbp,4), %r13d
cmpw %r13w, %r12w
je 0x13db7
cmpl %r15d, %ebp
jne 0x13d96
incl %r10d
jmp 0x13dba
cmpw %ax, %r12w
sete %bpl
cmpw %ax, %r13w
sete %r12b
orb %bpl, %r12b
movzbl %r12b, %ebp
addl %ebp, %r8d
testb %bpl, %bpl
cmovnel %r14d, %edx
movl %r11d, %r14d
incq %rsi
movl %r14d, %r11d
jmp 0x13d63
cmpl $0x1, %r10d
jne 0x13fd9
cmpl $0x1, %r8d
jne 0x13fd9
movl %r11d, %esi
negl %esi
cmovsl %r11d, %esi
leaq (%rcx,%rsi,4), %rsi
xorl %r8d, %r8d
xorl %edi, %edi
testl %r11d, %r11d
setg %r8b
setle %dil
movzwl (%rsi,%rdi,2), %r11d
movzwl %r11w, %edi
movzwl (%rsi,%r8,2), %r8d
movl %edx, %esi
negl %esi
cmovsl %edx, %esi
leaq (%rcx,%rsi,4), %r14
xorl %r9d, %r9d
xorl %ecx, %ecx
testl %edx, %edx
setg %r9b
setle %cl
movzwl (%r14,%rcx,2), %r10d
movzwl (%r14,%r9,2), %edx
cmpw %r8w, %ax
jne 0x13e5c
cmpw %r10w, %ax
jne 0x13e5c
cmpw %r11w, %ax
je 0x13e5c
cmpw %dx, %ax
je 0x13e5c
imulq $0xc, %rdi, %r8
leaq 0x10195ca(%rip), %r9    # 0x102d410
movss (%r9,%r8), %xmm1
imulq $0xc, %rax, %r8
movss (%r9,%r8), %xmm0
subss %xmm0, %xmm1
jmp 0x13ea8
cmpw %r8w, %ax
je 0x13fd9
cmpw %di, %ax
jne 0x13fd9
cmpw %dx, %ax
jne 0x13fd9
cmpw %r10w, %ax
je 0x13fd9
imulq $0xc, %r8, %rcx
leaq 0x1019583(%rip), %rdx   # 0x102d410
movss (%rdx,%rcx), %xmm1
imulq $0xc, %rax, %rcx
movss (%rdx,%rcx), %xmm0
subss %xmm0, %xmm1
movq %r8, %rdi
movl %r10d, %edx
movl %r9d, %ecx
leaq 0x2c(%rsp), %r14
movss %xmm1, (%r14)
imulq $0xc, %rdi, %rdi
leaq 0x1019553(%rip), %r8    # 0x102d410
imulq $0xc, %rax, %rax
movsd 0x4(%r8,%rdi), %xmm1
movsd 0x4(%r8,%rax), %xmm2
subps %xmm2, %xmm1
movlps %xmm1, 0x4(%r14)
movzwl %dx, %eax
imulq $0xc, %rax, %rax
movsd (%r8,%rax), %xmm1
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subps %xmm0, %xmm1
leaq 0x20(%rsp), %r13
movlps %xmm1, (%r13)
movss 0x8(%r8,%rax), %xmm0
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
subss %xmm2, %xmm0
movss %xmm0, 0x8(%r13)
movq 0x18(%rsp), %r12
movl %esi, (%r12)
movzbl %cl, %eax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movq %r14, %rdi
callq 0xc021
movq %r13, %rdi
callq 0xc021
movss (%r14), %xmm0
movss 0x4(%r14), %xmm1
mulss 0x4(%r13), %xmm1
mulss (%r13), %xmm0
addss %xmm1, %xmm0
movss 0x8(%r14), %xmm1
mulss 0x8(%r13), %xmm1
addss %xmm0, %xmm1
movss 0x21369(%rip), %xmm2    # 0x352c8
maxss %xmm1, %xmm2
movss 0x1e0f9(%rip), %xmm3    # 0x32064
movaps %xmm3, %xmm0
cmpltss %xmm1, %xmm0
movaps %xmm0, %xmm1
andnps %xmm2, %xmm1
andps %xmm3, %xmm0
orps %xmm1, %xmm0
callq 0x4310
movq 0x70(%rsp), %rax
movss %xmm0, (%rax)
movslq (%r12), %rax
imulq $0xb0, %rax, %rsi
leaq 0x2134981(%rip), %rdx   # 0x2148920
movq (%rsi,%rdx), %rcx
pushq $0x1
popq %rax
testq %rcx, %rcx
je 0x13feb
addq %rsi, %rdx
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
je 0x13feb
cmpq %rbx, %rcx
sete %al
cmpq %rbx, %rdx
setne %sil
testb %sil, %al
jne 0x13ffa
cmpq %rbx, %rdx
sete %al
cmpq %rbx, %rcx
setne %dl
testb %al, %dl
jne 0x13ffd
movl %r15d, %edi
movl 0x4(%rsp), %esi
movq %rbx, %rdx
callq 0x13b98
pushq $-0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %rcx
movq 0x8(%rsp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
jmp 0x13feb