// File: FreeSlave[P]vhlt/hlbsp/surfaces.cpp
// Function: GetVertex(double const*, int)
static int      GetVertex(const vec3_t in, const int planenum)
{
    int             h;
    int             i;
    hashvert_t*     hv;
    vec3_t          vert;
#ifdef HLBSP_HASH_FIX
	int				num_hashneighbors;
	int				hashneighbors[MAX_HASH_NEIGHBORS];
#endif

    for (i = 0; i < 3; i++)
    {
        if (fabs(in[i] - VectorRound(in[i])) < 0.001)
        {
            vert[i] = VectorRound(in[i]);
        }
        else
        {
            vert[i] = in[i];
        }
    }

#ifdef HLBSP_HASH_FIX
	h = HashVec(vert, &num_hashneighbors, hashneighbors);
#else
    h = HashVec(vert);
#endif

#ifdef HLBSP_HASH_FIX
  for (i = 0; i < num_hashneighbors; i++)
	for (hv = hashverts[hashneighbors[i]]; hv; hv = hv->next)
#else
    for (hv = hashverts[h]; hv; hv = hv->next)
#endif
    {
        if (fabs(hv->point[0] - vert[0]) < POINT_EPSILON
            && fabs(hv->point[1] - vert[1]) < POINT_EPSILON && fabs(hv->point[2] - vert[2]) < POINT_EPSILON)
        {
            hv->numedges++;
            if (hv->numplanes == 3)
            {
                return hv->num;                            // allready known to be a corner
            }
            for (i = 0; i < hv->numplanes; i++)
            {
                if (hv->planenums[i] == planenum)
                {
                    return hv->num;                        // allready know this plane
                }
            }
            if (hv->numplanes != 2)
            {
                hv->planenums[hv->numplanes] = planenum;
            }
            hv->numplanes++;
            return hv->num;
        }
    }

    hv = hvert_p;
    hv->numedges = 1;
    hv->numplanes = 1;
    hv->planenums[0] = planenum;
    hv->next = hashverts[h];
    hashverts[h] = hv;
    VectorCopy(vert, hv->point);
    hv->num = g_numvertexes;
    hlassume(hv->num != MAX_MAP_VERTS, assume_MAX_MAP_VERTS);
    hvert_p++;

    // emit a vertex
    hlassume(g_numvertexes < MAX_MAP_VERTS, assume_MAX_MAP_VERTS);

    g_dvertexes[g_numvertexes].point[0] = vert[0];
    g_dvertexes[g_numvertexes].point[1] = vert[1];
    g_dvertexes[g_numvertexes].point[2] = vert[2];
    g_numvertexes++;

    return hv->num;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movl %esi, 0x64(%rsp)
movl $0x0, 0x5c(%rsp)
cmpl $0x3, 0x5c(%rsp)
jge 0x30159
movq 0x68(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x68(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movsd 0x9361(%rip), %xmm0     # 0x39438
addsd (%rax,%rcx,8), %xmm0
callq 0x4510
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movaps 0x937e(%rip), %xmm0     # 0x39470
pand %xmm0, %xmm1
movsd 0xbc4a(%rip), %xmm0     # 0x3bd48
ucomisd %xmm1, %xmm0
jbe 0x3012d
movq 0x68(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movsd 0x9322(%rip), %xmm0     # 0x39438
addsd (%rax,%rcx,8), %xmm0
callq 0x4510
movslq 0x5c(%rsp), %rax
movsd %xmm0, 0x30(%rsp,%rax,8)
jmp 0x30147
movq 0x68(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movslq 0x5c(%rsp), %rax
movsd %xmm0, 0x30(%rsp,%rax,8)
jmp 0x30149
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x300a5
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rdx
leaq 0x2c(%rsp), %rsi
callq 0x30620
movl %eax, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x3030e
movslq 0x5c(%rsp), %rax
movslq 0x10(%rsp,%rax,4), %rcx
leaq 0x36a7908(%rip), %rax   # 0x36d7aa0
movq (%rax,%rcx,8), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, 0x50(%rsp)
je 0x302fc
movq 0x50(%rsp), %rax
movsd 0x8(%rax), %xmm1
subsd 0x30(%rsp), %xmm1
movaps 0x92ac(%rip), %xmm0     # 0x39470
pand %xmm0, %xmm1
movsd 0xbb70(%rip), %xmm0     # 0x3bd40
ucomisd %xmm1, %xmm0
jbe 0x302e8
movq 0x50(%rsp), %rax
movsd 0x10(%rax), %xmm1
subsd 0x38(%rsp), %xmm1
movaps 0x927f(%rip), %xmm0     # 0x39470
pand %xmm0, %xmm1
movsd 0xbb43(%rip), %xmm0     # 0x3bd40
ucomisd %xmm1, %xmm0
jbe 0x302e8
movq 0x50(%rsp), %rax
movsd 0x18(%rax), %xmm1
subsd 0x40(%rsp), %xmm1
movaps 0x9252(%rip), %xmm0     # 0x39470
pand %xmm0, %xmm1
movsd 0xbb16(%rip), %xmm0     # 0x3bd40
ucomisd %xmm1, %xmm0
jbe 0x302e8
movq 0x50(%rsp), %rax
movl 0x30(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x30(%rax)
movq 0x50(%rsp), %rax
cmpl $0x3, 0x24(%rax)
jne 0x3025e
movq 0x50(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x74(%rsp)
jmp 0x30492
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x24(%rcx), %eax
jge 0x302a8
movq 0x50(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movl 0x28(%rax,%rcx,4), %eax
cmpl 0x64(%rsp), %eax
jne 0x30299
movq 0x50(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x74(%rsp)
jmp 0x30492
jmp 0x3029b
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x30266
movq 0x50(%rsp), %rax
cmpl $0x2, 0x24(%rax)
je 0x302c9
movl 0x64(%rsp), %edx
movq 0x50(%rsp), %rax
movq 0x50(%rsp), %rcx
movslq 0x24(%rcx), %rcx
movl %edx, 0x28(%rax,%rcx,4)
movq 0x50(%rsp), %rax
movl 0x24(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x24(%rax)
movq 0x50(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x74(%rsp)
jmp 0x30492
jmp 0x302ea
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x50(%rsp)
jmp 0x301a1
jmp 0x302fe
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x30179
movq 0x36af78b(%rip), %rax   # 0x36dfaa0
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movl $0x1, 0x30(%rax)
movq 0x50(%rsp), %rax
movl $0x1, 0x24(%rax)
movl 0x64(%rsp), %ecx
movq 0x50(%rsp), %rax
movl %ecx, 0x28(%rax)
movslq 0x60(%rsp), %rcx
leaq 0x36a7756(%rip), %rax   # 0x36d7aa0
movq (%rax,%rcx,8), %rcx
movq 0x50(%rsp), %rax
movq %rcx, (%rax)
movq 0x50(%rsp), %rdx
movslq 0x60(%rsp), %rcx
leaq 0x36a7739(%rip), %rax   # 0x36d7aa0
movq %rdx, (%rax,%rcx,8)
movsd 0x30(%rsp), %xmm0
movq 0x50(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movsd 0x38(%rsp), %xmm0
movq 0x50(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movsd 0x40(%rsp), %xmm0
movq 0x50(%rsp), %rax
movsd %xmm0, 0x18(%rax)
leaq 0xfff022(%rip), %rax    # 0x102f3c4
movl (%rax), %ecx
movq 0x50(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x50(%rsp), %rax
cmpl $0xffff, 0x20(%rax)     # imm = 0xFFFF
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x2c, %esi
callq 0xbe90
movq 0x36af6ce(%rip), %rax   # 0x36dfaa0
addq $0x38, %rax
movq %rax, 0x36af6c3(%rip)   # 0x36dfaa0
leaq 0xffefe0(%rip), %rax    # 0x102f3c4
cmpl $0xffff, (%rax)         # imm = 0xFFFF
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x2c, %esi
callq 0xbe90
movsd 0x30(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
leaq 0xffefb6(%rip), %rax    # 0x102f3c4
movslq (%rax), %rcx
leaq 0xffefb8(%rip), %rax    # 0x102f3d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movsd 0x38(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
leaq 0xffef90(%rip), %rax    # 0x102f3c4
movslq (%rax), %rcx
leaq 0xffef92(%rip), %rax    # 0x102f3d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movsd 0x40(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
leaq 0xffef69(%rip), %rax    # 0x102f3c4
movslq (%rax), %rcx
leaq 0xffef6b(%rip), %rax    # 0x102f3d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
leaq 0xffef4c(%rip), %rax    # 0x102f3c4
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0xffef40(%rip), %rax    # 0x102f3c4
movl %ecx, (%rax)
movq 0x50(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopl (%rax,%rax)