// File: FreeSlave[P]vhlt/hlrad/qradutil.cpp
// Function: IsPositionValid(positionmap_t*, float const (&) [3], float (&) [3], bool, bool, int, float)
static bool IsPositionValid (positionmap_t *map, const vec3_t &pos_st, vec3_t &pos_out, bool usephongnormal = true, bool doedgetest = true, int hunt_size = 2, vec_t hunt_scale = 0.2)
{
	vec3_t pos;
	vec3_t pos_normal;
	vec_t hunt_offset;

	ApplyMatrix (map->textoworld, pos_st, pos);
	VectorAdd (pos, map->face_offset, pos);
	if (usephongnormal)
	{
		GetPhongNormal (map->facenum, pos, pos_normal);
	}
	else
	{
		VectorCopy (map->faceplanewithoffset.normal, pos_normal);
	}
	VectorMA (pos, DEFAULT_HUNT_OFFSET, pos_normal, pos);

	hunt_offset = DotProduct (pos, map->faceplanewithoffset.normal) - map->faceplanewithoffset.dist; // might be smaller than DEFAULT_HUNT_OFFSET

	// push the point 0.2 units around to avoid walls
	if (!HuntForWorld (pos, vec3_origin, &map->faceplanewithoffset, hunt_size, hunt_scale, hunt_offset))
	{
		return false;
	}

	if (doedgetest && !point_in_winding_noedge (*map->facewindingwithoffset, map->faceplanewithoffset, pos, DEFAULT_EDGE_WIDTH))
	{
		// if the sample has gone beyond face boundaries, be careful that it hasn't passed a wall
		vec3_t test;
#ifdef HLRAD_HULLU
		vec3_t transparency;
#endif
#ifdef HLRAD_OPAQUE_STYLE
		int opaquestyle;
#endif

		VectorCopy (pos, test);
		snap_to_winding_noedge (*map->facewindingwithoffset, map->faceplanewithoffset, test, DEFAULT_EDGE_WIDTH, 4 * DEFAULT_EDGE_WIDTH);

		if (!HuntForWorld (test, vec3_origin, &map->faceplanewithoffset, hunt_size, hunt_scale, hunt_offset))
		{
			return false;
		}

		if (TestLine (pos, test) != CONTENTS_EMPTY)
		{
			return false;
		}

		if (TestSegmentAgainstOpaqueList (pos, test
#ifdef HLRAD_HULLU
				, transparency
#endif
#ifdef HLRAD_OPAQUE_STYLE
				, opaquestyle
#endif
				) == true
#ifdef HLRAD_OPAQUE_STYLE
			|| opaquestyle != -1
#endif
			)
		{
			return false;
		}
	}

	VectorCopy (pos, pos_out);
	return true;
}

// Assembly:
subq $0x68, %rsp
movb %r8b, %al
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
andb $0x1, %cl
movb %cl, 0x47(%rsp)
andb $0x1, %al
movb %al, 0x46(%rsp)
movl %r9d, 0x40(%rsp)
movss %xmm0, 0x3c(%rsp)
movq 0x58(%rsp), %rdi
addq $0x50, %rdi
movq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x5d920
movss 0x30(%rsp), %xmm0
movq 0x58(%rsp), %rax
addss 0x8(%rax), %xmm0
movss %xmm0, 0x30(%rsp)
movss 0x34(%rsp), %xmm0
movq 0x58(%rsp), %rax
addss 0xc(%rax), %xmm0
movss %xmm0, 0x34(%rsp)
movss 0x38(%rsp), %xmm0
movq 0x58(%rsp), %rax
addss 0x10(%rax), %xmm0
movss %xmm0, 0x38(%rsp)
testb $0x1, 0x47(%rsp)
je 0x61138
movq 0x58(%rsp), %rax
movl 0x4(%rax), %edi
leaq 0x30(%rsp), %rsi
leaq 0x24(%rsp), %rdx
callq 0x329b0
jmp 0x61171
movq 0x58(%rsp), %rax
movss 0xa8(%rax), %xmm0
movss %xmm0, 0x24(%rsp)
movq 0x58(%rsp), %rax
movss 0xac(%rax), %xmm0
movss %xmm0, 0x28(%rsp)
movq 0x58(%rsp), %rax
movss 0xb0(%rax), %xmm0
movss %xmm0, 0x2c(%rsp)
movss 0x30(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movss 0x24(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xc363(%rip), %xmm2     # 0x6d4f0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x30(%rsp)
movss 0x34(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movss 0x28(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xc335(%rip), %xmm2     # 0x6d4f0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x34(%rsp)
movss 0x38(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movss 0x2c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xc307(%rip), %xmm2     # 0x6d4f0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x38(%rsp)
movss 0x30(%rsp), %xmm1
movq 0x58(%rsp), %rax
movss 0xa8(%rax), %xmm2
movss 0x34(%rsp), %xmm0
movq 0x58(%rsp), %rax
mulss 0xac(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x38(%rsp), %xmm0
movq 0x58(%rsp), %rax
movss 0xb0(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x58(%rsp), %rax
subss 0xb4(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rdx
addq $0xa8, %rdx
movl 0x40(%rsp), %ecx
movss 0x3c(%rsp), %xmm0
movss 0x20(%rsp), %xmm1
leaq 0xabd1(%rip), %rsi      # 0x6be50
callq 0x5d2d0
cmpq $0x0, %rax
jne 0x61294
movb $0x0, 0x67(%rsp)
jmp 0x613ea
testb $0x1, 0x46(%rsp)
je 0x613b6
movq 0x58(%rsp), %rax
movq 0xa0(%rax), %rdi
movq 0x58(%rsp), %rsi
addq $0xa8, %rsi
leaq 0x30(%rsp), %rdx
movss 0xc504(%rip), %xmm0     # 0x6d7c8
callq 0x4a180
testb $0x1, %al
jne 0x613b6
movss 0x30(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x34(%rsp), %xmm0
movss %xmm0, 0x18(%rsp)
movss 0x38(%rsp), %xmm0
movss %xmm0, 0x1c(%rsp)
movq 0x58(%rsp), %rax
movq 0xa0(%rax), %rdi
movq 0x58(%rsp), %rsi
addq $0xa8, %rsi
leaq 0x14(%rsp), %rdx
movss 0xc4ae(%rip), %xmm0     # 0x6d7c8
movss 0xc4aa(%rip), %xmm1     # 0x6d7cc
callq 0x4ac00
leaq 0x14(%rsp), %rdi
movq 0x58(%rsp), %rdx
addq $0xa8, %rdx
movl 0x40(%rsp), %ecx
movss 0x3c(%rsp), %xmm0
movss 0x20(%rsp), %xmm1
leaq 0xab01(%rip), %rsi      # 0x6be50
callq 0x5d2d0
cmpq $0x0, %rax
jne 0x61364
movb $0x0, 0x67(%rsp)
jmp 0x613ea
leaq 0x30(%rsp), %rdi
leaq 0x14(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x62b70
cmpl $-0x1, %eax
je 0x61383
movb $0x0, 0x67(%rsp)
jmp 0x613ea
leaq 0x30(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq 0x4ba90
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
je 0x613ad
cmpl $-0x1, 0x4(%rsp)
je 0x613b4
movb $0x0, 0x67(%rsp)
jmp 0x613ea
jmp 0x613b6
movss 0x30(%rsp), %xmm0
movq 0x48(%rsp), %rax
movss %xmm0, (%rax)
movss 0x34(%rsp), %xmm0
movq 0x48(%rsp), %rax
movss %xmm0, 0x4(%rax)
movss 0x38(%rsp), %xmm0
movq 0x48(%rsp), %rax
movss %xmm0, 0x8(%rax)
movb $0x1, 0x67(%rsp)
movb 0x67(%rsp), %al
andb $0x1, %al
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)
nop