// File: /workspace/github_cpp_makefile_star2_O3/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
movsd 0x30(%rdi), %xmm0
movsd %xmm0, (%rsi)
movsd 0x38(%rdi), %xmm2
movsd %xmm2, 0x8(%rsi)
movsd 0x40(%rdi), %xmm1
movapd %xmm2, %xmm3
mulsd %xmm2, %xmm3
movapd %xmm0, %xmm4
mulsd %xmm0, %xmm4
addsd %xmm3, %xmm4
movapd %xmm1, %xmm3
mulsd %xmm1, %xmm3
addsd %xmm4, %xmm3
sqrtsd %xmm3, %xmm3
movsd 0xd0ef(%rip), %xmm4     # 0x1d118
ucomisd %xmm3, %xmm4
jbe 0x10050
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
unpcklpd %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0]
divsd %xmm3, %xmm1
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
divpd %xmm3, %xmm0
movsd %xmm1, 0x10(%rsi)
movupd %xmm0, (%rsi)
movq 0x50(%rdi), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsi)
movq 0x50(%rdi), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x20(%rsi)
movq 0x50(%rdi), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x28(%rsi)
leaq 0x38(%rsi), %rax
movq %rax, 0x50(%rsp)
leaq 0xc38(%rsi), %rbx
leaq 0xc98(%rsi), %rax
movq %rax, 0x48(%rsp)
movb $0x1, %r9b
xorl %ebp, %ebp
movq %rsi, 0x8(%rsp)
movq %rdi, 0x30(%rsp)
movq %rbx, 0x40(%rsp)
movsd 0xffe9(%rip), %xmm0     # 0x200c8
testb $0x1, %r9b
jne 0x100ed
movsd 0x1018b(%rip), %xmm0    # 0x20278
movsd 0x20(%rsi), %xmm1
movsd 0x10(%rsi), %xmm2
movsd 0x28(%rsi), %xmm5
movsd (%rsi), %xmm3
movsd 0x8(%rsi), %xmm4
movapd %xmm1, %xmm6
mulsd %xmm2, %xmm6
movsd 0x18(%rsi), %xmm7
mulsd %xmm7, %xmm2
mulsd %xmm4, %xmm7
mulsd %xmm5, %xmm4
subsd %xmm4, %xmm6
mulsd %xmm3, %xmm5
subsd %xmm2, %xmm5
mulsd %xmm1, %xmm3
subsd %xmm3, %xmm7
mulsd %xmm0, %xmm6
mulsd %xmm0, %xmm5
mulsd %xmm0, %xmm7
movapd %xmm5, %xmm0
mulsd %xmm5, %xmm0
movapd %xmm6, %xmm1
mulsd %xmm6, %xmm1
addsd %xmm0, %xmm1
movapd %xmm7, %xmm0
mulsd %xmm7, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
movsd 0xcfae(%rip), %xmm1     # 0x1d118
ucomisd %xmm0, %xmm1
ja 0x1003f
movsd 0x10314(%rip), %xmm1    # 0x20490
ucomisd %xmm0, %xmm1
ja 0x1003f
divsd %xmm0, %xmm6
divsd %xmm0, %xmm5
divsd %xmm0, %xmm7
movl $0x1, 0x30(%rsi,%rbp,4)
movq 0x50(%rdi), %rax
movq (%rax), %rax
movl 0x10(%rax,%rbp,4), %eax
movq %rbp, %rcx
shlq $0x9, %rcx
leaq (%rcx,%rcx,2), %r13
movq 0x50(%rsp), %rcx
movl %eax, 0x4(%rcx,%r13)
movq %rbp, %rax
shlq $0xa, %rax
leaq (%rax,%rax,2), %rdx
movl $0x0, 0x18(%rbx,%rdx)
movl %ebp, %eax
xorb $0x1, %al
movb %al, 0x1c(%rbx,%rdx)
movl 0x30(%rsi,%rbp,4), %eax
addq 0x48(%rsp), %rdx
movq %rdx, 0x28(%rsp)
addq %rcx, %r13
testl %eax, %eax
jle 0x10225
movq 0x50(%rdi), %rcx
movq (%rcx), %rcx
movl %eax, %edx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
movl $0x1, %r14d
xorl %r12d, %r12d
movslq 0x4(%r13,%r12), %r15
leaq (%r15,%r15,4), %r8
cmpb $0x0, (%rcx,%r8,8)
je 0x1022c
addq $0x18, %r12
incl %r14d
cmpq %r12, %rdx
jne 0x10204
jmp 0x10339
xorl %ecx, %ecx
jmp 0x10331
leaq (%rcx,%r8,8), %rcx
movq %rcx, 0x58(%rsp)
leaq (%r12,%r13), %rsi
leal -0x1(%r14), %ecx
movl %ecx, 0x3c(%rsp)
leaq (%r12,%r13), %rdi
addq $0x18, %rdi
movq %rdi, 0x60(%rsp)
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdx
subq %r12, %rdx
movl %r9d, %ebx
movsd %xmm5, 0x20(%rsp)
movsd %xmm6, 0x18(%rsp)
movsd %xmm7, 0x10(%rsp)
callq 0x44f0
movq 0x28(%rsp), %rax
leaq (%rax,%r12,2), %rdi
leaq (%rax,%r12,2), %r12
addq $-0x30, %r12
movq 0x8(%rsp), %rax
movl 0x30(%rax,%rbp,4), %eax
subl %r14d, %eax
cltq
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
movq %r12, %rsi
callq 0x44f0
movsd 0x10(%rsp), %xmm7
movsd 0x18(%rsp), %xmm6
movsd 0x20(%rsp), %xmm5
movl %ebx, %r9d
movq 0x40(%rsp), %rbx
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
incl 0x30(%rsi,%rbp,4)
movq 0x58(%rsp), %rdx
movq 0x8(%rdx), %rax
movsd 0x8(%rax), %xmm0
mulsd %xmm5, %xmm0
movsd (%rax), %xmm1
mulsd %xmm6, %xmm1
addsd %xmm0, %xmm1
movsd 0x10(%rax), %xmm0
mulsd %xmm7, %xmm0
addsd %xmm1, %xmm0
xorl %eax, %eax
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
setbe %al
seta %cl
movl 0x10(%rdx,%rcx,4), %ecx
movq 0x60(%rsp), %r8
movl %ecx, -0x14(%r8)
movl 0x3c(%rsp), %ecx
movl 0x10(%rdx,%rax,4), %eax
movl %eax, 0x4(%r8)
movl %r15d, 0x18(%r12)
seta 0x1c(%r12)
movl 0x30(%rsi,%rbp,4), %eax
cmpl %eax, %ecx
jne 0x101e6
movl $0x1, %ebp
testb $0x1, %r9b
movl $0x0, %r9d
jne 0x100d7
movl 0x34(%rsi), %eax
addl 0x30(%rsi), %eax
leal -0x1(,%rax,2), %eax
cmpl 0x48(%rdi), %eax
je 0x10381
leaq 0x10793(%rip), %rdi     # 0x20afa
xorl %eax, %eax
callq 0xa10c
xorl %edi, %edi
xorl %esi, %esi
callq 0xa655
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
leaq 0xc54(%rsi), %rax
movq %rax, 0x10(%rsp)
movb $0x1, %r9b
xorl %r10d, %r10d
cmpl $0x0, 0x30(%rsi,%r10,4)
jle 0x10513
movl %r9d, 0x18(%rsp)
movq %r10, %rax
xorq $0x1, %rax
leaq (%rax,%rax,2), %r9
shlq $0xa, %r9
addq %rbx, %r9
movq %r10, %rax
shlq $0x9, %rax
leaq (%rax,%rax,2), %r15
addq $0x38, %r15
movq %r10, %rax
shlq $0xa, %rax
leaq (%rax,%rax,2), %r14
movq %r14, %r12
xorl %ebp, %ebp
movq %r10, 0x28(%rsp)
movq %r9, 0x20(%rsp)
movq 0x50(%rdi), %rax
movslq 0x4(%rsi,%r15), %rcx
movq (%rax), %r13
leaq (%rcx,%rcx,4), %rbx
movq 0x20(%r13,%rbx,8), %rax
cmpb $0x0, (%rax)
jne 0x10424
leaq 0x1071c(%rip), %rdi     # 0x20b1c
xorl %eax, %eax
callq 0xa10c
xorl %edi, %edi
xorl %esi, %esi
callq 0xa655
movq 0x20(%rsp), %r9
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
leaq (%rsi,%r15), %rax
leaq (,%rbx,8), %rcx
addq %r13, %rcx
movl 0x18(%rcx), %ecx
movl %ecx, (%rsi,%r15)
leaq (%rsi,%r12), %rcx
addq $0xc38, %rcx            # imm = 0xC38
movq %rcx, 0x8(%rsi,%r15)
movl 0x30(%rsi,%r10,4), %ecx
decl %ecx
cmpq %rcx, %rbp
leaq 0x1(%rbp), %rcx
leaq 0xc68(%rsi,%r12), %rdx
movq %rdx, %r8
cmoveq %r9, %r8
movq %r8, 0x10(%rsi,%r15)
movq %rax, -0x8(%rdx)
movq %rax, 0x20(%r8)
movslq 0x30(%rsi,%r10,4), %rax
addq $0x18, %r15
addq $0x30, %r12
movq %rcx, %rbp
cmpq %rax, %rcx
jl 0x103df
testl %eax, %eax
movq 0x40(%rsp), %rbx
movl 0x18(%rsp), %r9d
jle 0x10513
addq 0x10(%rsp), %r14
xorl %eax, %eax
movq 0x50(%rdi), %rcx
movslq -0x4(%r14), %rdx
movq (%rcx), %rcx
leaq (%rdx,%rdx,4), %rdx
movq 0x8(%rcx,%rdx,8), %r8
movsd (%r8), %xmm0
cmpb $0x0, (%r14)
je 0x104c8
xorps 0xfde8(%rip), %xmm0     # 0x202b0
movsd %xmm0, -0x1c(%r14)
movq 0x8(%rcx,%rdx,8), %r8
movsd 0x8(%r8), %xmm0
je 0x104e2
xorps 0xfdce(%rip), %xmm0     # 0x202b0
movsd %xmm0, -0x14(%r14)
movq 0x8(%rcx,%rdx,8), %rcx
movsd 0x10(%rcx), %xmm0
je 0x104fc
xorpd 0xfdb4(%rip), %xmm0     # 0x202b0
movsd %xmm0, -0xc(%r14)
incq %rax
movslq 0x30(%rsi,%r10,4), %rcx
addq $0x30, %r14
cmpq %rcx, %rax
jl 0x104a2
movl $0x1, %r10d
testb $0x1, %r9b
movl $0x0, %r9d
jne 0x10393
leaq 0x48(%rsi), %rcx
movb $0x1, %dl
xorl %edi, %edi
movslq 0x30(%rsi,%rdi,4), %rax
testq %rax, %rax
jle 0x1062c
leaq (%rdi,%rdi,2), %r11
shlq $0x9, %r11
addq %rcx, %r11
leaq (,%rax,8), %rdi
leaq (%rdi,%rdi,2), %rdi
xorl %r8d, %r8d
cmpl $0x1, %eax
je 0x1061f
movq -0x8(%r11,%r8), %r9
movq (%r11,%r8), %r10
movsd (%r9), %xmm0
movsd 0x8(%r9), %xmm3
movsd 0x10(%r10), %xmm4
movsd 0x10(%r9), %xmm1
movsd (%r10), %xmm5
movsd 0x8(%r10), %xmm6
movapd %xmm3, %xmm2
mulsd %xmm4, %xmm2
mulsd %xmm0, %xmm4
mulsd %xmm6, %xmm0
mulsd %xmm1, %xmm6
subsd %xmm6, %xmm2
mulsd %xmm5, %xmm1
subsd %xmm4, %xmm1
mulsd %xmm3, %xmm5
subsd %xmm5, %xmm0
movapd %xmm1, %xmm3
mulsd %xmm1, %xmm3
movapd %xmm2, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
movapd %xmm0, %xmm3
mulsd %xmm0, %xmm3
addsd %xmm4, %xmm3
sqrtsd %xmm3, %xmm3
movsd 0xcb37(%rip), %xmm4     # 0x1d118
ucomisd %xmm3, %xmm4
ja 0x1003f
divsd %xmm3, %xmm0
divsd %xmm3, %xmm1
divsd %xmm3, %xmm2
mulsd 0x8(%rsi), %xmm1
mulsd (%rsi), %xmm2
mulsd 0x10(%rsi), %xmm0
addsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movsd 0xfe7b(%rip), %xmm1     # 0x20490
ucomisd %xmm0, %xmm1
ja 0x1003f
addq $0x18, %r8
cmpq %r8, %rdi
jne 0x10559
movb $0x1, %al
movl $0x1, %edi
testb $0x1, %dl
movl $0x0, %edx
jne 0x10531
jmp 0x10041