// File: FreeSlave[P]vhlt/hlbsp/tjunc.cpp
// Function: FindEdge(double const*, double const*, double*, double*)
static wedge_t *FindEdge(const vec3_t p1, const vec3_t p2, vec_t* t1, vec_t* t2)
{
    vec3_t          origin;
    vec3_t          dir;
    wedge_t*        w;
    vec_t           temp;
    int             h;
#ifdef HLBSP_HASH_FIX
	int				num_hashneighbors;
	int				hashneighbors[MAX_HASH_NEIGHBORS];
#endif

    VectorSubtract(p2, p1, dir);
    if (!CanonicalVector(dir))
    {
#if _DEBUG
        Warning("CanonicalVector: degenerate @ (%4.3f %4.3f %4.3f )\n", p1[0], p1[1], p1[2]);
#endif
    }

    *t1 = DotProduct(p1, dir);
    *t2 = DotProduct(p2, dir);

    VectorMA(p1, -*t1, dir, origin);

    if (*t1 > *t2)
    {
        temp = *t1;
        *t1 = *t2;
        *t2 = temp;
    }

#ifdef HLBSP_HASH_FIX
	h = HashVec(origin, &num_hashneighbors, hashneighbors);
#else
    h = HashVec(origin);
#endif

#ifdef HLBSP_HASH_FIX
  for (int i = 0; i < num_hashneighbors; ++i)
	for (w = wedge_hash[hashneighbors[i]]; w; w = w->next)
#else
    for (w = wedge_hash[h]; w; w = w->next)
#endif
    {
#ifdef HLBSP_TJUNC_PRECISION_FIX
		if (fabs (w->origin[0] - origin[0]) > EQUAL_EPSILON ||
			fabs (w->origin[1] - origin[1]) > EQUAL_EPSILON ||
			fabs (w->origin[2] - origin[2]) > EQUAL_EPSILON )
		{
			continue;
		}
		if (fabs (w->dir[0] - dir[0]) > NORMAL_EPSILON ||
			fabs (w->dir[1] - dir[1]) > NORMAL_EPSILON ||
			fabs (w->dir[2] - dir[2]) > NORMAL_EPSILON )
		{
			continue;
		}
#else
        temp = w->origin[0] - origin[0];
        if (temp < -EQUAL_EPSILON || temp > EQUAL_EPSILON)
        {
            continue;
        }
        temp = w->origin[1] - origin[1];
        if (temp < -EQUAL_EPSILON || temp > EQUAL_EPSILON)
        {
            continue;
        }
        temp = w->origin[2] - origin[2];
        if (temp < -EQUAL_EPSILON || temp > EQUAL_EPSILON)
        {
            continue;
        }

        temp = w->dir[0] - dir[0];
        if (temp < -EQUAL_EPSILON || temp > EQUAL_EPSILON)
        {
            continue;
        }
        temp = w->dir[1] - dir[1];
        if (temp < -EQUAL_EPSILON || temp > EQUAL_EPSILON)
        {
            continue;
        }
        temp = w->dir[2] - dir[2];
        if (temp < -EQUAL_EPSILON || temp > EQUAL_EPSILON)
        {
            continue;
        }
#endif

        return w;
    }

    hlassume(numwedges < MAX_WEDGES, assume_MAX_WEDGES);
    w = &wedges[numwedges];
    numwedges++;

    w->next = wedge_hash[h];
    wedge_hash[h] = w;

    VectorCopy(origin, w->origin);
    VectorCopy(dir, w->dir);
    w->head.next = w->head.prev = &w->head;
    w->head.t = 99999;
    return w;
}

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movq %rdx, 0x88(%rsp)
movq %rcx, 0x80(%rsp)
movq 0x90(%rsp), %rax
movsd (%rax), %xmm0
movq 0x98(%rsp), %rax
subsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x90(%rsp), %rax
movsd 0x8(%rax), %xmm0
movq 0x98(%rsp), %rax
subsd 0x8(%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x90(%rsp), %rax
movsd 0x10(%rax), %xmm0
movq 0x98(%rsp), %rax
subsd 0x10(%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x31420
testb $0x1, %al
jne 0x30e95
jmp 0x30e95
movq 0x98(%rsp), %rax
movsd (%rax), %xmm1
movsd 0x40(%rsp), %xmm2
movq 0x98(%rsp), %rax
movsd 0x8(%rax), %xmm0
mulsd 0x48(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movq 0x98(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd 0x50(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x88(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x90(%rsp), %rax
movsd (%rax), %xmm1
movsd 0x40(%rsp), %xmm2
movq 0x90(%rsp), %rax
movsd 0x8(%rax), %xmm0
mulsd 0x48(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movq 0x90(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd 0x50(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x80(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x98(%rsp), %rax
movsd (%rax), %xmm1
movq 0x88(%rsp), %rax
movsd (%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x40(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x98(%rsp), %rax
movsd 0x8(%rax), %xmm1
movq 0x88(%rsp), %rax
movsd (%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x48(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x98(%rsp), %rax
movsd 0x10(%rax), %xmm1
movq 0x88(%rsp), %rax
movsd (%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x50(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x88(%rsp), %rax
movsd (%rax), %xmm0
movq 0x80(%rsp), %rax
ucomisd (%rax), %xmm0
jbe 0x3105e
movq 0x88(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x80(%rsp), %rax
movsd (%rax), %xmm0
movq 0x88(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x30(%rsp), %xmm0
movq 0x80(%rsp), %rax
movsd %xmm0, (%rax)
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rdx
leaq 0x28(%rsp), %rsi
callq 0x31660
movl %eax, 0x2c(%rsp)
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x28(%rsp), %eax
jge 0x311e5
movslq 0xc(%rsp), %rax
movslq 0x10(%rsp,%rax,4), %rcx
leaq 0x3a2ea33(%rip), %rax   # 0x3a5fad0
movq (%rax,%rcx,8), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
je 0x311d3
movq 0x38(%rsp), %rax
movsd 0x20(%rax), %xmm0
subsd 0x60(%rsp), %xmm0
movaps 0x83a7(%rip), %xmm1     # 0x39470
pand %xmm1, %xmm0
movsd 0xad1b(%rip), %xmm1     # 0x3bdf0
ucomisd %xmm1, %xmm0
ja 0x3112d
movq 0x38(%rsp), %rax
movsd 0x28(%rax), %xmm0
subsd 0x68(%rsp), %xmm0
movaps 0x837e(%rip), %xmm1     # 0x39470
pand %xmm1, %xmm0
movsd 0xacf2(%rip), %xmm1     # 0x3bdf0
ucomisd %xmm1, %xmm0
ja 0x3112d
movq 0x38(%rsp), %rax
movsd 0x30(%rax), %xmm0
subsd 0x70(%rsp), %xmm0
movaps 0x8355(%rip), %xmm1     # 0x39470
pand %xmm1, %xmm0
movsd 0xacc9(%rip), %xmm1     # 0x3bdf0
ucomisd %xmm1, %xmm0
jbe 0x31132
jmp 0x311c1
movq 0x38(%rsp), %rax
movsd 0x8(%rax), %xmm0
subsd 0x40(%rsp), %xmm0
movaps 0x8327(%rip), %xmm1     # 0x39470
pand %xmm1, %xmm0
movsd 0x4f13(%rip), %xmm1     # 0x36068
ucomisd %xmm1, %xmm0
ja 0x311ad
movq 0x38(%rsp), %rax
movsd 0x10(%rax), %xmm0
subsd 0x48(%rsp), %xmm0
movaps 0x82fe(%rip), %xmm1     # 0x39470
pand %xmm1, %xmm0
movsd 0x4eea(%rip), %xmm1     # 0x36068
ucomisd %xmm1, %xmm0
ja 0x311ad
movq 0x38(%rsp), %rax
movsd 0x18(%rax), %xmm0
subsd 0x50(%rsp), %xmm0
movaps 0x82d5(%rip), %xmm1     # 0x39470
pand %xmm1, %xmm0
movsd 0x4ec1(%rip), %xmm1     # 0x36068
ucomisd %xmm1, %xmm0
jbe 0x311af
jmp 0x311c1
movq 0x38(%rsp), %rax
movq %rax, 0xa0(%rsp)
jmp 0x312f2
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x38(%rsp)
jmp 0x310a6
jmp 0x311d5
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x3107e
cmpl $0x20000, 0x3a368e5(%rip) # imm = 0x20000
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x13, %esi
callq 0xbe90
movslq 0x3a368cb(%rip), %rcx   # 0x3a67ad4
leaq 0x3a36920(%rip), %rax   # 0x3a67b30
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movl 0x3a368b2(%rip), %eax   # 0x3a67ad4
addl $0x1, %eax
movl %eax, 0x3a368a9(%rip)   # 0x3a67ad4
movslq 0x2c(%rsp), %rcx
leaq 0x3a2e899(%rip), %rax   # 0x3a5fad0
movq (%rax,%rcx,8), %rcx
movq 0x38(%rsp), %rax
movq %rcx, (%rax)
movq 0x38(%rsp), %rdx
movslq 0x2c(%rsp), %rcx
leaq 0x3a2e87c(%rip), %rax   # 0x3a5fad0
movq %rdx, (%rax,%rcx,8)
movsd 0x60(%rsp), %xmm0
movq 0x38(%rsp), %rax
movsd %xmm0, 0x20(%rax)
movsd 0x68(%rsp), %xmm0
movq 0x38(%rsp), %rax
movsd %xmm0, 0x28(%rax)
movsd 0x70(%rsp), %xmm0
movq 0x38(%rsp), %rax
movsd %xmm0, 0x30(%rax)
movsd 0x40(%rsp), %xmm0
movq 0x38(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movsd 0x48(%rsp), %xmm0
movq 0x38(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movsd 0x50(%rsp), %xmm0
movq 0x38(%rsp), %rax
movsd %xmm0, 0x18(%rax)
movq 0x38(%rsp), %rcx
addq $0x38, %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x38(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0x38(%rsp), %rax
movsd 0x9340(%rip), %xmm0     # 0x3a620
movsd %xmm0, 0x38(%rax)
movq 0x38(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
addq $0xa8, %rsp
retq
nopw %cs:(%rax,%rax)