// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movsd 0x30(%rdi), %xmm1
movsd %xmm1, (%rsi)
movsd 0x38(%rdi), %xmm2
movsd %xmm2, 0x8(%rsi)
movsd 0x40(%rdi), %xmm0
movsd %xmm0, 0x10(%rsi)
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq %rsi, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
jb 0x101a1
sqrtsd %xmm0, %xmm0
jmp 0x101b0
callq 0x4240
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movsd 0xdf58(%rip), %xmm1     # 0x1e110
ucomisd %xmm0, %xmm1
ja 0x101e2
movupd (%rsi), %xmm1
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
divpd %xmm2, %xmm1
movupd %xmm1, (%rsi)
movsd 0x10(%rsi), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsi)
jmp 0x101f2
xorpd %xmm1, %xmm1
movupd %xmm1, (%rsi)
movq $0x0, 0x10(%rsi)
movsd 0xdf16(%rip), %xmm1     # 0x1e110
ucomisd %xmm0, %xmm1
ja 0x10859
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), %rbp
leaq 0xc38(%rsi), %rax
movq %rax, 0x50(%rsp)
leaq 0xc98(%rsi), %rax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
movq %rbp, 0x58(%rsp)
movsd (%rsi), %xmm0
movsd 0x18(%rsi), %xmm4
xorl %eax, %eax
testq %r15, %r15
sete %al
leaq 0x11202(%rip), %rcx     # 0x21480
movsd (%rcx,%rax,8), %xmm5
movupd 0x20(%rsi), %xmm1
movupd 0x8(%rsi), %xmm2
movapd %xmm1, %xmm3
shufpd $0x1, %xmm4, %xmm3      # xmm3 = xmm3[1],xmm4[0]
mulpd %xmm2, %xmm3
mulsd %xmm2, %xmm4
shufpd $0x1, %xmm0, %xmm2      # xmm2 = xmm2[1],xmm0[0]
mulpd %xmm1, %xmm2
subpd %xmm3, %xmm2
mulsd %xmm1, %xmm0
subsd %xmm0, %xmm4
mulsd %xmm5, %xmm4
unpcklpd %xmm5, %xmm5            # xmm5 = xmm5[0,0]
mulpd %xmm2, %xmm5
movapd %xmm5, %xmm0
mulpd %xmm5, %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm5, %xmm1
mulsd %xmm5, %xmm1
addsd %xmm0, %xmm1
movapd %xmm4, %xmm0
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
ucomisd 0x10f95(%rip), %xmm0    # 0x21280
jb 0x102f6
xorps %xmm3, %xmm3
sqrtsd %xmm0, %xmm3
jmp 0x10321
movapd %xmm4, 0x30(%rsp)
movapd %xmm5, 0x20(%rsp)
callq 0x4240
movapd 0x20(%rsp), %xmm5
movapd 0x30(%rsp), %xmm4
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movapd %xmm0, %xmm3
movsd 0xdde7(%rip), %xmm0     # 0x1e110
ucomisd %xmm3, %xmm0
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
ja 0x1034b
movapd %xmm3, %xmm0
unpcklpd %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0]
divpd %xmm0, %xmm5
movapd %xmm5, 0x40(%rsp)
movapd %xmm3, %xmm0
movsd 0x11149(%rip), %xmm1    # 0x214a0
cmpnltpd %xmm1, %xmm0
movapd %xmm3, %xmm1
movsd 0xdda8(%rip), %xmm2     # 0x1e110
cmpnltpd %xmm2, %xmm1
andpd %xmm0, %xmm1
movd %xmm1, %eax
testb $0x1, %al
je 0x10859
divsd %xmm3, %xmm4
cmpltsd %xmm2, %xmm3
andnpd %xmm4, %xmm3
movl $0x1, 0x30(%rsi,%r15,4)
movq 0x50(%rdi), %rax
movq (%rax), %rax
movl 0x10(%rax,%r15,4), %eax
leaq (%r15,%r15,2), %rcx
movq %rcx, %rdx
shlq $0x9, %rdx
movq 0x58(%rsp), %r8
movl %eax, 0x4(%r8,%rdx)
shlq $0xa, %rcx
movq 0x50(%rsp), %rdx
movl $0x0, 0x18(%rdx,%rcx)
movl %r15d, %eax
xorb $0x1, %al
movb %al, 0x1c(%rdx,%rcx)
movapd 0x40(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm0, 0x60(%rsp)
movapd %xmm3, 0x70(%rsp)
movslq 0x30(%rsi,%r15,4), %r14
testq %r14, %r14
jle 0x1042e
movq 0x50(%rdi), %rax
movq (%rax), %rax
movq %r14, %rcx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rdx
movl $0x1, %ebx
xorl %r12d, %r12d
movslq 0x4(%rbp,%r12), %r13
leaq (,%r13,4), %rcx
addq %r13, %rcx
cmpb $0x0, (%rax,%rcx,8)
je 0x10436
addq $0x18, %r12
incl %ebx
cmpq %r12, %rdx
jne 0x10408
jmp 0x10501
xorl %r14d, %r14d
jmp 0x10501
leaq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
leal -0x1(%rbx), %r14d
leaq (%r12,%rbp), %rsi
leaq (%r12,%rbp), %rdi
addq $0x18, %rdi
movq %rdi, 0x30(%rsp)
subq %r12, %rdx
callq 0x44f0
movq 0x18(%rsp), %rax
leaq (%rax,%r12,2), %rdi
leaq (%rax,%r12,2), %r12
addq $-0x30, %r12
movq 0x10(%rsp), %rax
movl 0x30(%rax,%r15,4), %eax
subl %ebx, %eax
cltq
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
movq %r12, %rsi
callq 0x44f0
movapd 0x70(%rsp), %xmm3
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
incl 0x30(%rsi,%r15,4)
movq 0x20(%rsp), %rdx
movq 0x8(%rdx), %rax
movsd 0x8(%rax), %xmm0
mulsd 0x60(%rsp), %xmm0
movsd (%rax), %xmm1
mulsd 0x40(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x10(%rax), %xmm0
mulsd %xmm3, %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 0x30(%rsp), %r8
movl %ecx, -0x14(%r8)
movl 0x10(%rdx,%rax,4), %eax
movl %eax, 0x4(%r8)
movl %r13d, 0x18(%r12)
seta 0x1c(%r12)
cmpl 0x30(%rsi,%r15,4), %r14d
jne 0x103e4
leaq 0x1(%r15), %rax
addq $0xc00, 0x18(%rsp)      # imm = 0xC00
addq $0x600, %rbp            # imm = 0x600
testq %r15, %r15
movq %rax, %r15
je 0x10266
movl 0x34(%rsi), %eax
addl 0x30(%rsi), %eax
leal -0x1(,%rax,2), %eax
cmpl 0x48(%rdi), %eax
je 0x1055f
leaq 0x115bd(%rip), %rdi     # 0x21b02
xorl %eax, %eax
callq 0xa1f4
xorl %edi, %edi
xorl %esi, %esi
callq 0xa731
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0xc54(%rsi), %r10
movl $0x38, %eax
movq %rax, 0x40(%rsp)
movapd 0x10d58(%rip), %xmm1    # 0x212d0
movq $0x0, 0x18(%rsp)
xorl %r15d, %r15d
movq %r10, 0x20(%rsp)
cmpl $0x0, 0x30(%rsi,%r15,4)
jle 0x10664
movl %r15d, %eax
xorq $0x1, %rax
leaq (%rax,%rax,2), %r9
shlq $0xa, %r9
addq 0x50(%rsp), %r9
movq 0x18(%rsp), %rbp
movq 0x40(%rsp), %r13
xorl %ebx, %ebx
movq %r9, 0x30(%rsp)
movq 0x50(%rdi), %rax
movslq 0x4(%rsi,%r13), %rcx
movq (%rax), %r12
leaq (%rcx,%rcx,4), %r14
movq 0x20(%r12,%r14,8), %rax
cmpb $0x0, (%rax)
jne 0x10602
leaq 0x11549(%rip), %rdi     # 0x21b24
xorl %eax, %eax
callq 0xa1f4
xorl %edi, %edi
xorl %esi, %esi
callq 0xa731
movq 0x30(%rsp), %r9
movapd 0x10cd8(%rip), %xmm1    # 0x212d0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq (%rsi,%r13), %rax
leaq (%r12,%r14,8), %rcx
movl 0x18(%rcx), %ecx
movl %ecx, (%rsi,%r13)
leaq (%rsi,%rbp), %rcx
addq $0xc38, %rcx            # imm = 0xC38
movq %rcx, 0x8(%rsi,%r13)
movl 0x30(%rsi,%r15,4), %ecx
decl %ecx
cmpq %rcx, %rbx
leaq 0x1(%rbx), %rcx
leaq 0xc68(%rsi,%rbp), %rdx
movq %rdx, %r8
cmoveq %r9, %r8
movq %r8, 0x10(%rsi,%r13)
movq %rax, -0x8(%rdx)
movq %rax, 0x20(%r8)
movslq 0x30(%rsi,%r15,4), %rax
addq $0x18, %r13
addq $0x30, %rbp
movq %rcx, %rbx
cmpq %rax, %rcx
jl 0x105ba
cmpl $0x0, 0x30(%rsi,%r15,4)
movq 0x20(%rsp), %r10
jle 0x106d9
movq %r10, %rax
xorl %ecx, %ecx
movq 0x50(%rdi), %rdx
movslq -0x4(%rax), %r8
movq (%rdx), %rdx
leaq (%r8,%r8,4), %r8
movq 0x8(%rdx,%r8,8), %r9
movsd (%r9), %xmm0
cmpb $0x0, (%rax)
je 0x10698
xorpd %xmm1, %xmm0
movsd %xmm0, -0x1c(%rax)
movq 0x8(%rdx,%r8,8), %r9
movsd 0x8(%r9), %xmm0
je 0x106ae
xorpd %xmm1, %xmm0
movsd %xmm0, -0x14(%rax)
movq 0x8(%rdx,%r8,8), %rdx
movsd 0x10(%rdx), %xmm0
je 0x106c3
xorpd %xmm1, %xmm0
movsd %xmm0, -0xc(%rax)
incq %rcx
movslq 0x30(%rsi,%r15,4), %rdx
addq $0x30, %rax
cmpq %rdx, %rcx
jl 0x10676
leaq 0x1(%r15), %rax
addq $0x600, 0x40(%rsp)      # imm = 0x600
movl $0xc00, %ecx            # imm = 0xC00
addq %rcx, 0x18(%rsp)
addq %rcx, %r10
testq %r15, %r15
movq %rax, %r15
je 0x10584
leaq 0x48(%rsi), %r14
xorl %r15d, %r15d
xorpd %xmm4, %xmm4
movl 0x30(%rsi,%r15,4), %ecx
testl %ecx, %ecx
jle 0x1083e
movq %r14, %r12
xorl %r13d, %r13d
testq %r13, %r13
jne 0x10730
movl $0x18, %eax
cmpl $0x1, %ecx
je 0x10820
movq -0x8(%r12), %rax
movq (%r12), %rcx
movsd (%rax), %xmm5
movsd 0x8(%rax), %xmm0
movsd 0x10(%rcx), %xmm1
movsd 0x10(%rax), %xmm6
movsd (%rcx), %xmm2
movsd 0x8(%rcx), %xmm3
movapd %xmm0, %xmm7
mulsd %xmm1, %xmm7
mulsd %xmm5, %xmm1
mulsd %xmm3, %xmm5
mulsd %xmm6, %xmm3
subsd %xmm3, %xmm7
mulsd %xmm2, %xmm6
subsd %xmm1, %xmm6
mulsd %xmm0, %xmm2
subsd %xmm2, %xmm5
movapd %xmm6, %xmm0
mulsd %xmm6, %xmm0
movapd %xmm7, %xmm1
mulsd %xmm7, %xmm1
addsd %xmm0, %xmm1
movapd %xmm5, %xmm0
mulsd %xmm5, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm4, %xmm0
jb 0x107a9
sqrtsd %xmm0, %xmm0
jmp 0x107db
movsd %xmm5, 0x8(%rsp)
movsd %xmm6, 0x30(%rsp)
movsd %xmm7, 0x20(%rsp)
callq 0x4240
movsd 0x20(%rsp), %xmm7
movsd 0x30(%rsp), %xmm6
movsd 0x8(%rsp), %xmm5
xorpd %xmm4, %xmm4
movq 0x10(%rsp), %rsi
movl $0x1, %eax
movsd 0xd928(%rip), %xmm1     # 0x1e110
ucomisd %xmm0, %xmm1
ja 0x10820
divsd %xmm0, %xmm5
divsd %xmm0, %xmm6
divsd %xmm0, %xmm7
mulsd 0x8(%rsi), %xmm6
mulsd (%rsi), %xmm7
mulsd 0x10(%rsi), %xmm5
addsd %xmm6, %xmm7
addsd %xmm7, %xmm5
movsd 0x10c88(%rip), %xmm0    # 0x214a0
ucomisd %xmm5, %xmm0
ja 0x10820
xorl %eax, %eax
cmpl $0x18, %eax
je 0x10829
testl %eax, %eax
jne 0x10859
incq %r13
movslq 0x30(%rsi,%r15,4), %rcx
addq $0x18, %r12
cmpq %rcx, %r13
jl 0x1071d
leaq 0x1(%r15), %rax
addq $0x600, %r14            # imm = 0x600
testq %r15, %r15
movq %rax, %r15
je 0x1070a
movb $0x1, %al
jmp 0x1085b
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq