// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlbsp/brink.cpp
// Function: CalculateCircle(bbrink_t*, bcircle_t*)
bool CalculateCircle (bbrink_t *b, bcircle_t *c)
{
	VectorCopy (b->direction, c->axis);
	if (!VectorNormalize (c->axis))
	{
		return false;
	}
	VectorCopy ((*b->nodes)[0].plane->normal, c->basenormal);

	int side, i;
	for (side = 0; side < 2; side++)
	{
		vec3_t facing;
		CrossProduct (c->basenormal, c->axis, facing);
		VectorScale (facing, side? -1: 1, facing);
		if (VectorNormalize (facing) < 1 - 0.01)
		{
			return false;
		}

		// sort the wedges
		c->numwedges[side] = 1;
		c->wedges[side][0].nodenum = (*b->nodes)[0].children[side];
		c->surfaces[side][0].nodenum = 0;
		c->surfaces[side][0].nodeside = !side;
		while (1)
		{
			for (i = 0; i < c->numwedges[side]; i++)
			{
				int nodenum = c->wedges[side][i].nodenum;
				bbrinknode_t *node = &(*b->nodes)[nodenum];
				if (!node->isleaf)
				{
					memmove (&c->wedges[side][i + 1], &c->wedges[side][i], (c->numwedges[side] - i) * sizeof (bwedge_t));
					memmove (&c->surfaces[side][i + 2], &c->surfaces[side][i + 1], (c->numwedges[side] - 1 - i) * sizeof (bsurface_t));
					c->numwedges[side]++;
					bool flipnode = (DotProduct (node->plane->normal, facing) < 0);
					c->wedges[side][i].nodenum = node->children[flipnode];
					c->wedges[side][i + 1].nodenum = node->children[!flipnode];
					c->surfaces[side][i + 1].nodenum = nodenum;
					c->surfaces[side][i + 1].nodeside = flipnode;
					break;
				}
			}
			if (i == c->numwedges[side])
			{
				break;
			}
		}
	}
	if ((c->numwedges[0] + c->numwedges[1]) * 2 - 1 != b->numnodes)
	{
		PrintOnce ("CalculateCircle: internal error 1");
		hlassume (false, assume_first);
	}

	// fill in other information
	for (side = 0; side < 2; side++)
	{
		for (i = 0; i < c->numwedges[side]; i++)
		{
			bwedge_t *w = &c->wedges[side][i];
			bbrinknode_t *node = &(*b->nodes)[w->nodenum];
			if (!node->clipnode->isleaf)
			{
				PrintOnce ("CalculateCircle: internal error: not leaf");
				hlassume (false, assume_first);
			}
			w->content = node->content;
			w->prev = &c->surfaces[side][i];
			w->next = (i == c->numwedges[side] - 1)? &c->surfaces[!side][0]: &c->surfaces[side][i + 1];
			w->prev->next = w;
			w->next->prev = w;
		}
		for (i = 0; i < c->numwedges[side]; i++)
		{
			bsurface_t *s = &c->surfaces[side][i];
			bbrinknode_t *node = &(*b->nodes)[s->nodenum];
			VectorScale (node->plane->normal, s->nodeside? -1: 1, s->normal);
		}
	}

	// check the normals
	for (side = 0; side < 2; side++)
	{
		for (i = 0; i < c->numwedges[side]; i++)
		{
			bwedge_t *w = &c->wedges[side][i];
			if (i == 0 && i == c->numwedges[side] - 1) // 180 degrees
			{
				continue;
			}
			vec3_t v;
			CrossProduct (w->prev->normal, w->next->normal, v);
			if (!VectorNormalize (v) ||
				DotProduct (v, c->axis) < 1 - 0.01)
			{
				return false;
			}
		}
	}
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movsd 0x30(%rdi), %xmm0
movsd %xmm0, (%rsi)
movsd 0x38(%rdi), %xmm0
movsd %xmm0, 0x8(%rsi)
movsd 0x40(%rdi), %xmm0
movsd %xmm0, 0x10(%rsi)
movq %rsi, %rdi
callq 0xc073
xorl %r15d, %r15d
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xe3c8
jnp 0xe924
movq 0x50(%r14), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rbx)
movq 0x50(%r14), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x20(%rbx)
movq 0x50(%r14), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x28(%rbx)
leaq 0x38(%rbx), %r12
leaq 0xc38(%rbx), %rax
movq %rax, 0x48(%rsp)
leaq 0xc98(%rbx), %rax
movq %rax, 0x8(%rsp)
movq %r12, 0x50(%rsp)
movq %rbx, %r8
movq %rbx, (%rsp)
movq %r14, %r9
movq %r14, 0x18(%rsp)
cmpq $0x2, %r15
je 0xe667
movsd (%r8), %xmm1
movsd 0x18(%r8), %xmm0
xorl %eax, %eax
testq %r15, %r15
sete %al
leaq 0xff76(%rip), %rcx      # 0x1e3d0
movsd (%rcx,%rax,8), %xmm2
movupd 0x20(%r8), %xmm3
movupd 0x8(%r8), %xmm4
movapd %xmm3, %xmm5
shufpd $0x1, %xmm0, %xmm5      # xmm5 = xmm5[1],xmm0[0]
mulpd %xmm4, %xmm5
mulsd %xmm4, %xmm0
shufpd $0x1, %xmm1, %xmm4      # xmm4 = xmm4[1],xmm1[0]
mulpd %xmm3, %xmm4
subpd %xmm5, %xmm4
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm4, %xmm2
movapd %xmm2, 0x20(%rsp)
movsd %xmm0, 0x30(%rsp)
leaq 0x20(%rsp), %rdi
movq %r8, %rbx
callq 0xc073
movsd 0xff32(%rip), %xmm1     # 0x1e3f0
ucomisd %xmm0, %xmm1
ja 0xe921
movl $0x1, 0x30(%rbx,%r15,4)
movq 0x18(%rsp), %r9
movq 0x50(%r9), %rax
movq (%rax), %rax
movl 0x10(%rax,%r15,4), %eax
imulq $0x600, %r15, %rcx      # imm = 0x600
movq 0x50(%rsp), %rdx
movl %eax, 0x4(%rdx,%rcx)
imulq $0xc00, %r15, %rax      # imm = 0xC00
movq 0x48(%rsp), %rdx
andl $0x0, 0x18(%rdx,%rax)
movl %r15d, %ecx
xorb $0x1, %cl
movb %cl, 0x1c(%rdx,%rax)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movq %r15, 0x10(%rsp)
movl 0x30(%rbx,%r15,4), %eax
movq %rbx, %r8
testl %eax, %eax
movl $0x0, %r15d
cmovgl %eax, %r15d
imulq $0x18, %r15, %rcx
pushq $0x1
popq %rdx
movl %edx, %ebp
xorl %r14d, %r14d
cmpq %r14, %rcx
je 0xe641
movslq 0x4(%r12,%r14), %rbx
movq 0x50(%r9), %rdx
movq (%rdx), %r13
imulq $0x28, %rbx, %rdx
cmpb $0x0, (%r13,%rdx)
je 0xe57f
addq $0x18, %r14
incl %ebp
jmp 0xe556
addq %rdx, %r13
leaq (%r12,%r14), %rsi
leal -0x1(%rbp), %r15d
leaq (%r12,%r14), %rdi
addq $0x18, %rdi
movq %rdi, 0x40(%rsp)
cltq
imulq $0x18, %rax, %rdx
subq %r14, %rdx
callq 0x4500
movq 0x8(%rsp), %rax
leaq (%rax,%r14,2), %rdi
leaq (%rax,%r14,2), %rsi
addq $-0x30, %rsi
movq (%rsp), %rax
movq 0x10(%rsp), %r14
movl 0x30(%rax,%r14,4), %eax
subl %ebp, %eax
cltq
imulq $0x30, %rax, %rdx
movq %rsi, %rbp
callq 0x4500
movq 0x18(%rsp), %r9
movq (%rsp), %r8
incl 0x30(%r8,%r14,4)
movq 0x8(%r13), %rax
movsd 0x8(%rax), %xmm0
mulsd 0x60(%rsp), %xmm0
movsd (%rax), %xmm1
mulsd 0x38(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x10(%rax), %xmm0
mulsd 0x58(%rsp), %xmm0
addsd %xmm1, %xmm0
xorl %eax, %eax
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
setbe %al
seta %cl
movl 0x10(%r13,%rcx,4), %ecx
movq 0x40(%rsp), %rdx
movl %ecx, -0x14(%rdx)
movl 0x10(%r13,%rax,4), %eax
movl %eax, 0x4(%rdx)
movl %ebx, 0x18(%rbp)
seta 0x1c(%rbp)
movl 0x30(%r8,%r14,4), %eax
cmpl %eax, %r15d
jne 0xe53e
movq 0x10(%rsp), %r15
incq %r15
addq $0xc00, 0x8(%rsp)       # imm = 0xC00
addq $0x600, %r12            # imm = 0x600
jmp 0xe436
movl 0x34(%r8), %eax
addl 0x30(%r8), %eax
leal -0x1(,%rax,2), %eax
cmpl 0x48(%r9), %eax
je 0xe69c
leaq 0x103d7(%rip), %rdi     # 0x1ea5a
xorl %eax, %eax
callq 0x8d93
xorl %edi, %edi
xorl %esi, %esi
callq 0x92bb
movq 0x18(%rsp), %r9
movq (%rsp), %r8
leaq 0xc54(%r8), %r10
pushq $0x38
popq %r13
movapd 0xfb51(%rip), %xmm1     # 0x1e200
xorl %ebp, %ebp
xorl %r12d, %r12d
cmpq $0x2, %r12
je 0xe827
movq %r10, 0x10(%rsp)
movl %r12d, %eax
xorq $0x1, %rax
imulq $0xc00, %rax, %rsi      # imm = 0xC00
addq 0x48(%rsp), %rsi
movq %rbp, 0x38(%rsp)
movq %r13, 0x8(%rsp)
xorl %r15d, %r15d
movq %rsi, 0x40(%rsp)
movslq 0x30(%r8,%r12,4), %rdx
cmpq %rdx, %r15
jge 0xe793
movq 0x50(%r9), %rax
movslq 0x4(%r8,%r13), %rcx
movq (%rax), %r14
imulq $0x28, %rcx, %rbx
movq 0x20(%r14,%rbx), %rax
cmpb $0x0, (%rax)
jne 0xe73d
leaq 0x10365(%rip), %rdi     # 0x1ea7c
xorl %eax, %eax
callq 0x8d93
xorl %edi, %edi
xorl %esi, %esi
callq 0x92bb
movq 0x40(%rsp), %rsi
movapd 0xfacc(%rip), %xmm1     # 0x1e200
movq 0x18(%rsp), %r9
movq (%rsp), %r8
leaq (%r8,%r13), %rax
addq %rbx, %r14
movl 0x18(%r14), %ecx
movl %ecx, (%r8,%r13)
leaq (%r8,%rbp), %rcx
addq $0xc38, %rcx            # imm = 0xC38
movq %rcx, 0x8(%r8,%r13)
movl 0x30(%r8,%r12,4), %ecx
decl %ecx
cmpq %rcx, %r15
leaq 0x1(%r15), %r15
leaq 0xc68(%r8,%rbp), %rcx
movq %rcx, %rdx
cmoveq %rsi, %rdx
movq %rdx, 0x10(%r8,%r13)
movq %rax, -0x8(%rcx)
movq %rax, 0x20(%rdx)
addq $0x18, %r13
addq $0x30, %rbp
jmp 0xe6e8
movq 0x10(%rsp), %r10
movq %r10, %rax
xorl %ecx, %ecx
movq 0x8(%rsp), %r13
movq 0x38(%rsp), %rbp
movslq %edx, %rdx
cmpq %rdx, %rcx
jge 0xe80d
movq 0x50(%r9), %rdx
movslq -0x4(%rax), %rsi
movq (%rdx), %rdx
imulq $0x28, %rsi, %rsi
movq 0x8(%rdx,%rsi), %rdi
movsd (%rdi), %xmm0
cmpb $0x0, (%rax)
je 0xe7d0
xorpd %xmm1, %xmm0
movsd %xmm0, -0x1c(%rax)
movq 0x8(%rdx,%rsi), %rdi
movsd 0x8(%rdi), %xmm0
je 0xe7e5
xorpd %xmm1, %xmm0
movsd %xmm0, -0x14(%rax)
movq 0x8(%rdx,%rsi), %rdx
movsd 0x10(%rdx), %xmm0
je 0xe7fa
xorpd %xmm1, %xmm0
movsd %xmm0, -0xc(%rax)
incq %rcx
movl 0x30(%r8,%r12,4), %edx
addq $0x30, %rax
jmp 0xe7a7
incq %r12
addq $0x600, %r13            # imm = 0x600
movl $0xc00, %eax            # imm = 0xC00
addq %rax, %rbp
addq %rax, %r10
jmp 0xe6b4
leaq 0x48(%r8), %r15
xorl %r12d, %r12d
leaq 0x20(%rsp), %r14
cmpq $0x2, %r12
je 0xe936
movq %r15, %r13
xorl %ebp, %ebp
movslq 0x30(%r8,%r12,4), %rax
cmpq %rax, %rbp
jge 0xe912
cmpl $0x1, %eax
je 0xe906
movq -0x8(%r13), %rax
movq (%r13), %rcx
movsd 0x10(%rcx), %xmm0
movsd 0x10(%rax), %xmm1
movsd 0x8(%rcx), %xmm2
mulsd %xmm1, %xmm2
movsd 0x8(%rax), %xmm3
mulsd %xmm0, %xmm3
subsd %xmm2, %xmm3
movsd %xmm3, 0x20(%rsp)
mulsd (%rax), %xmm0
mulsd (%rcx), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
movsd (%rax), %xmm0
movsd (%rcx), %xmm1
mulsd 0x8(%rax), %xmm1
mulsd 0x8(%rcx), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movq %r14, %rdi
callq 0xc073
movq (%rsp), %r8
ucomisd 0xfb2f(%rip), %xmm0     # 0x1e3f8
jne 0xe8cd
jnp 0xe921
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
mulsd 0x8(%r8), %xmm1
mulsd (%r8), %xmm0
movsd 0x30(%rsp), %xmm2
mulsd 0x10(%r8), %xmm2
addsd %xmm1, %xmm0
addsd %xmm0, %xmm2
movsd 0xfaf0(%rip), %xmm0     # 0x1e3f0
ucomisd %xmm2, %xmm0
ja 0xe921
incq %rbp
addq $0x18, %r13
jmp 0xe842
incq %r12
addq $0x600, %r15            # imm = 0x600
jmp 0xe833
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %r15b
jmp 0xe924