// File: /workspace/github_cpp_makefile_star2_O1/FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: CopyToSkynormals(int, int, double (*) [3], int, edge_t*, int, triangle_t*)
void CopyToSkynormals (int skylevel, int numpoints, point_t *points, int numedges, edge_t *edges, int numtriangles, triangle_t *triangles)
{
	hlassume (numpoints == (1 << (2 * skylevel)) + 2, assume_first);
	hlassume (numedges == (1 << (2 * skylevel)) * 4 - 4 , assume_first);
	hlassume (numtriangles == (1 << (2 * skylevel)) * 2, assume_first);
	g_numskynormals[skylevel] = numpoints;
	g_skynormals[skylevel] = (vec3_t *)malloc (numpoints * sizeof (vec3_t));
	g_skynormalsizes[skylevel] = (vec_t *)malloc (numpoints * sizeof (vec_t));
	hlassume (g_skynormals[skylevel] != NULL, assume_NoMemory);
	hlassume (g_skynormalsizes[skylevel] != NULL, assume_NoMemory);
	int j, k;
	for (j = 0; j < numpoints; j++)
	{
		VectorCopy (points[j], g_skynormals[skylevel][j]);
		g_skynormalsizes[skylevel][j] = 0;
	}
	double totalsize = 0;
	for (j = 0; j < numtriangles; j++)
	{
		int pt[3];
		for (k = 0; k < 3; k++)
		{
			pt[k] = edges[triangles[j].edge[k]].point[triangles[j].dir[k]];
		}
		double currentsize;
		double tmp[3];
		CrossProduct (points[pt[0]], points[pt[1]], tmp);
		currentsize = DotProduct (tmp, points[pt[2]]);
		hlassume (currentsize > 0, assume_first);
		g_skynormalsizes[skylevel][pt[0]] += currentsize / 3.0;
		g_skynormalsizes[skylevel][pt[1]] += currentsize / 3.0;
		g_skynormalsizes[skylevel][pt[2]] += currentsize / 3.0;
		totalsize += currentsize;
	}
	for (j = 0; j < numpoints; j++)
	{
		g_skynormalsizes[skylevel][j] /= totalsize;
	}
#if 0
	printf ("g_numskynormals[%i]=%i\n", skylevel, g_numskynormals[skylevel]);
	for (j = 0; j < numpoints; j += (numpoints / 20 + 1))
	{
		printf ("g_skynormals[%i][%i]=%1.3f,%1.3f,%1.3f g_skynormalsizes[%i][%i]=%f\n",
			skylevel, j, g_skynormals[skylevel][j][0], g_skynormals[skylevel][j][1], g_skynormals[skylevel][j][2],
			skylevel, j, g_skynormalsizes[skylevel][j]);
	}
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, %ebx
movq %r8, %r14
movl %ecx, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
movl %esi, %r15d
movl %edi, %r12d
leal (%r12,%r12), %r13d
movl $0x2, %ebp
movl $0x2, %eax
btsl %r13d, %eax
xorl %edi, %edi
cmpl %esi, %eax
sete %dil
xorl %esi, %esi
callq 0xa7c9
movl $0x4, %eax
movl %r13d, %ecx
shll %cl, %eax
addl $-0x4, %eax
xorl %edi, %edi
cmpl 0x8(%rsp), %eax
sete %dil
xorl %esi, %esi
callq 0xa7c9
movl %r13d, %ecx
shll %cl, %ebp
xorl %edi, %edi
movl %ebx, 0x8(%rsp)
cmpl %ebx, %ebp
sete %dil
xorl %esi, %esi
callq 0xa7c9
movslq %r12d, %rbx
leaq 0x4f589f2(%rip), %rax   # 0x4f728e0
movl %r15d, (%rax,%rbx,4)
movslq %r15d, %rax
movq %rax, 0x10(%rsp)
leaq (,%rax,4), %r13
leaq (,%r13,2), %rdi
addq %r13, %rdi
callq 0x4410
movq %rax, %r12
leaq 0x4f589f4(%rip), %rbp   # 0x4f72910
movq %rax, (%rbp,%rbx,8)
movq %r13, %rdi
callq 0x4410
leaq 0x4f58a30(%rip), %r13   # 0x4f72960
movq %rax, (%r13,%rbx,8)
xorl %edi, %edi
testq %r12, %r12
setne %dil
movl $0x1, %esi
callq 0xa7c9
xorl %edi, %edi
cmpq $0x0, (%r13,%rbx,8)
setne %dil
movl $0x1, %esi
callq 0xa7c9
movq %r13, %r8
movq 0x18(%rsp), %rdi
movl %r15d, 0x20(%rsp)
movl %r15d, %r9d
cmpl $0x0, 0x10(%rsp)
jle 0x19fd7
leaq (,%r9,4), %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
xorl %edx, %edx
xorps %xmm0, %xmm0
cvtsd2ss (%rdi,%rcx,2), %xmm0
movq (%rbp,%rbx,8), %rsi
movss %xmm0, (%rsi,%rcx)
xorps %xmm0, %xmm0
cvtsd2ss 0x8(%rdi,%rcx,2), %xmm0
movq (%rbp,%rbx,8), %rsi
movss %xmm0, 0x4(%rsi,%rcx)
xorps %xmm0, %xmm0
cvtsd2ss 0x10(%rdi,%rcx,2), %xmm0
movq (%rbp,%rbx,8), %rsi
movss %xmm0, 0x8(%rsi,%rcx)
movq (%r8,%rbx,8), %rsi
movl $0x0, (%rsi,%rdx)
addq $0x4, %rdx
addq $0xc, %rcx
cmpq %rcx, %rax
jne 0x19f85
movq %r9, 0x30(%rsp)
movl 0x8(%rsp), %eax
testl %eax, %eax
jle 0x1a16f
movq 0x80(%rsp), %rbp
movl %eax, %eax
movq %rax, 0x38(%rsp)
addq $0xc, %rbp
xorl %r12d, %r12d
xorpd %xmm1, %xmm1
movq %rbx, 0x40(%rsp)
movsd %xmm1, 0x8(%rsp)
xorl %eax, %eax
movslq -0xc(%rbp,%rax,4), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%r14,%rcx,4), %rcx
movslq (%rbp,%rax,4), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, 0x24(%rsp,%rax,4)
incq %rax
cmpq $0x3, %rax
jne 0x1a00f
movslq 0x24(%rsp), %r13
leaq (,%r13,2), %rax
addq %r13, %rax
movsd (%rdi,%rax,8), %xmm6
movsd 0x8(%rdi,%rax,8), %xmm2
movslq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rsp)
leaq (%rcx,%rcx,2), %rcx
movsd 0x10(%rdi,%rcx,8), %xmm3
movsd 0x10(%rdi,%rax,8), %xmm0
movsd (%rdi,%rcx,8), %xmm4
movsd 0x8(%rdi,%rcx,8), %xmm5
movapd %xmm2, %xmm1
mulsd %xmm3, %xmm1
mulsd %xmm6, %xmm3
mulsd %xmm5, %xmm6
mulsd %xmm0, %xmm5
subsd %xmm5, %xmm1
mulsd %xmm4, %xmm0
subsd %xmm3, %xmm0
mulsd %xmm2, %xmm4
movslq 0x2c(%rsp), %r15
leaq (%r15,%r15,2), %rax
mulsd 0x8(%rdi,%rax,8), %xmm0
subsd %xmm4, %xmm6
mulsd (%rdi,%rax,8), %xmm1
addsd %xmm0, %xmm1
mulsd 0x10(%rdi,%rax,8), %xmm6
addsd %xmm1, %xmm6
movsd %xmm6, 0x10(%rsp)
movq %rdi, %rbx
xorl %edi, %edi
ucomisd 0x2123a(%rip), %xmm6    # 0x3b308
seta %dil
xorl %esi, %esi
callq 0xa7c9
movq %rbx, %rdi
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm0
movq 0x40(%rsp), %rbx
leaq 0x4f5886e(%rip), %r8    # 0x4f72960
movq (%r8,%rbx,8), %rax
xorps %xmm1, %xmm1
cvtss2sd (%rax,%r13,4), %xmm1
divsd 0x21a11(%rip), %xmm0    # 0x3bb18
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%rax,%r13,4)
movq (%r8,%rbx,8), %rax
movq 0x18(%rsp), %rcx
xorps %xmm1, %xmm1
cvtss2sd (%rax,%rcx,4), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%rax,%rcx,4)
movq (%r8,%rbx,8), %rax
xorps %xmm1, %xmm1
cvtss2sd (%rax,%r15,4), %xmm1
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, (%rax,%r15,4)
movsd 0x8(%rsp), %xmm1
addsd %xmm2, %xmm1
incq %r12
addq $0x18, %rbp
cmpq 0x38(%rsp), %r12
jne 0x1a007
jmp 0x1a173
xorpd %xmm1, %xmm1
cmpl $0x0, 0x20(%rsp)
movq 0x30(%rsp), %rdx
jle 0x1a1a2
xorl %eax, %eax
movq (%r8,%rbx,8), %rcx
xorps %xmm0, %xmm0
cvtss2sd (%rcx,%rax,4), %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rcx,%rax,4)
incq %rax
cmpq %rax, %rdx
jne 0x1a181
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq