// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/trace.cpp
// Function: TestLine_r(int, float const*, float const*, int&, float*)
int             TestLine_r(const int node, const vec3_t start, const vec3_t stop
#ifdef HLRAD_WATERBLOCKLIGHT
						   , int &linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
						   , vec_t *skyhit
#endif
						   )
{
    tnode_t*        tnode;
    float           front, back;
    vec3_t          mid;
    float           frac;
    int             side;
    int             r;

#ifdef HLRAD_WATERBLOCKLIGHT
	if (node < 0)
	{
		if (node == linecontent)
			return CONTENTS_EMPTY;
#ifdef HLRAD_OPAQUEINSKY_FIX
		if (node == CONTENTS_SOLID)
		{
			return CONTENTS_SOLID;
		}
		if (node == CONTENTS_SKY)
		{
			if (skyhit)
			{
				VectorCopy (start, skyhit);
			}
			return CONTENTS_SKY;
		}
#else
		if (node == CONTENTS_SOLID || node == CONTENTS_SKY)
			return node;
#endif
		if (linecontent)
		{
			return CONTENTS_SOLID;
		}
		linecontent = node;
		return CONTENTS_EMPTY;
	}
#else
#ifdef HLRAD_OPAQUEINSKY_FIX
	if (node == CONTENTS_SKY)
	{
		VectorCopy (start, skyhit);
	}
#endif
	if (   (node == CONTENTS_SOLID) 
        || (node == CONTENTS_SKY  ) 
      /*|| (node == CONTENTS_NULL ) */
       )
		return node;

    if (node < 0)
        return CONTENTS_EMPTY; 
#endif

    tnode = &tnodes[node];
    switch (tnode->type)
    {
    case plane_x:
        front = start[0] - tnode->dist;
        back = stop[0] - tnode->dist;
        break;
    case plane_y:
        front = start[1] - tnode->dist;
        back = stop[1] - tnode->dist;
        break;
    case plane_z:
        front = start[2] - tnode->dist;
        back = stop[2] - tnode->dist;
        break;
    default:
        front = (start[0] * tnode->normal[0] + start[1] * tnode->normal[1] + start[2] * tnode->normal[2]) - tnode->dist;
        back = (stop[0] * tnode->normal[0] + stop[1] * tnode->normal[1] + stop[2] * tnode->normal[2]) - tnode->dist;
        break;
    }

#ifdef HLRAD_TestLine_EDGE_FIX
	if (front > ON_EPSILON/2 && back > ON_EPSILON/2)
	{
		return TestLine_r(tnode->children[0], start, stop
#ifdef HLRAD_WATERBLOCKLIGHT
			, linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
			, skyhit
#endif
			);
	}
	if (front < -ON_EPSILON/2 && back < -ON_EPSILON/2)
	{
		return TestLine_r(tnode->children[1], start, stop
#ifdef HLRAD_WATERBLOCKLIGHT
			, linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
			, skyhit
#endif
			);
	}
	if (fabs(front) <= ON_EPSILON && fabs(back) <= ON_EPSILON)
	{
		int r1 = TestLine_r(tnode->children[0], start, stop
#ifdef HLRAD_WATERBLOCKLIGHT
			, linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
			, skyhit
#endif
			);
		if (r1 == CONTENTS_SOLID)
			return CONTENTS_SOLID;
		int r2 = TestLine_r(tnode->children[1], start, stop
#ifdef HLRAD_WATERBLOCKLIGHT
			, linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
			, skyhit
#endif
			);
		if (r2 == CONTENTS_SOLID)
			return CONTENTS_SOLID;
		if (r1 == CONTENTS_SKY || r2 == CONTENTS_SKY)
			return CONTENTS_SKY;
		return CONTENTS_EMPTY;
	}
	side = (front - back) < 0;
	frac = front / (front - back);
	if (frac < 0) frac = 0;
	if (frac > 1) frac = 1;
	mid[0] = start[0] + (stop[0] - start[0]) * frac;
	mid[1] = start[1] + (stop[1] - start[1]) * frac;
	mid[2] = start[2] + (stop[2] - start[2]) * frac;
	r = TestLine_r(tnode->children[side], start, mid
#ifdef HLRAD_WATERBLOCKLIGHT
		, linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
		, skyhit
#endif
		);
	if (r != CONTENTS_EMPTY)
		return r;
	return TestLine_r(tnode->children[!side], mid, stop
#ifdef HLRAD_WATERBLOCKLIGHT
		, linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
		, skyhit
#endif
		);
#else //bug: light can go through edges of solid brushes
    if (front >= -ON_EPSILON && back >= -ON_EPSILON)
        return TestLine_r(tnode->children[0], start, stop
#ifdef HLRAD_WATERBLOCKLIGHT
		, linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
		, skyhit
#endif
		);

    if (front < ON_EPSILON && back < ON_EPSILON)
        return TestLine_r(tnode->children[1], start, stop
#ifdef HLRAD_WATERBLOCKLIGHT
		, linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
		, skyhit
#endif
		);

    side = front < 0;

    frac = front / (front - back);

    mid[0] = start[0] + (stop[0] - start[0]) * frac;
    mid[1] = start[1] + (stop[1] - start[1]) * frac;
    mid[2] = start[2] + (stop[2] - start[2]) * frac;

    r = TestLine_r(tnode->children[side], start, mid
#ifdef HLRAD_WATERBLOCKLIGHT
		, linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
		, skyhit
#endif
		);
    if (r != CONTENTS_EMPTY)
        return r;
    return TestLine_r(tnode->children[!side], mid, stop
#ifdef HLRAD_WATERBLOCKLIGHT
		, linecontent
#endif
#ifdef HLRAD_OPAQUEINSKY_FIX
		, skyhit
#endif
		);
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %rbx
movq %rcx, %r14
movq %rsi, %r12
testl %edi, %edi
js 0x2e02d
movq %rdx, %r15
movq 0x66c8ad7(%rip), %rax   # 0x66f6ad8
movl %edi, %ecx
shlq $0x5, %rcx
leaq (%rax,%rcx), %r13
movl (%rax,%rcx), %eax
cmpl $0x2, %eax
je 0x2e08f
cmpl $0x1, %eax
je 0x2e080
testl %eax, %eax
jne 0x2e0ae
movss (%r12), %xmm1
movss (%r15), %xmm0
jmp 0x2e09c
movl (%r14), %ecx
pushq $-0x1
popq %rax
cmpl %edi, %ecx
je 0x2e2b6
cmpl $-0x2, %edi
je 0x2e2a6
cmpl $-0x6, %edi
jne 0x2e2aa
pushq $-0x6
popq %rax
testq %rbx, %rbx
je 0x2e2b6
movss (%r12), %xmm0
movss %xmm0, (%rbx)
movss 0x4(%r12), %xmm0
movss %xmm0, 0x4(%rbx)
movss 0x8(%r12), %xmm0
movss %xmm0, 0x8(%rbx)
jmp 0x2e2b6
movss 0x4(%r12), %xmm1
movss 0x4(%r15), %xmm0
jmp 0x2e09c
movss 0x8(%r12), %xmm1
movss 0x8(%r15), %xmm0
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movss 0x10(%r13), %xmm1
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
subps %xmm1, %xmm0
jmp 0x2e116
movss (%r12), %xmm0
movss 0x4(%r12), %xmm1
movss 0x8(%r12), %xmm2
movss (%r15), %xmm3
unpcklps %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1]
movss 0x4(%r15), %xmm4
unpcklps %xmm1, %xmm4            # xmm4 = xmm4[0],xmm1[0],xmm4[1],xmm1[1]
movss 0x8(%r15), %xmm1
unpcklps %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movss 0x4(%r13), %xmm2
movss 0x8(%r13), %xmm5
movss 0xc(%r13), %xmm0
movss 0x10(%r13), %xmm6
shufps $0x0, %xmm5, %xmm5      # xmm5 = xmm5[0,0,0,0]
mulps %xmm4, %xmm5
shufps $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
mulps %xmm3, %xmm2
addps %xmm5, %xmm2
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
mulps %xmm1, %xmm0
addps %xmm2, %xmm0
shufps $0x0, %xmm6, %xmm6      # xmm6 = xmm6[0,0,0,0]
subps %xmm6, %xmm0
cvtps2pd %xmm0, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
ucomisd 0xb6f7(%rip), %xmm2     # 0x39820
jbe 0x2e13b
ucomisd 0xb6ed(%rip), %xmm1     # 0x39820
jbe 0x2e13b
movl 0x14(%r13), %edi
jmp 0x2e151
cmpltpd 0xba1c(%rip), %xmm1     # 0x39b60
movmskpd %xmm1, %eax
cmpl $0x3, %eax
jne 0x2e170
movl 0x18(%r13), %edi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %rbx, %r8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2dfdc
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
movaps 0x7122(%rip), %xmm2     # 0x352a0
movaps %xmm1, %xmm3
andps %xmm2, %xmm3
cvtss2sd %xmm3, %xmm4
movsd 0x7418(%rip), %xmm3     # 0x355a8
ucomisd %xmm4, %xmm3
jb 0x2e1fb
andps %xmm0, %xmm2
cvtss2sd %xmm2, %xmm2
ucomisd %xmm2, %xmm3
jb 0x2e1fb
movl 0x14(%r13), %edi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %rbx, %r8
callq 0x2dfdc
cmpl $-0x2, %eax
je 0x2e2ae
movl %eax, %ebp
movl 0x18(%r13), %edi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %rbx, %r8
callq 0x2dfdc
movl %eax, %ecx
cmpl $-0x6, %ebp
setne %al
cmpl $-0x6, %ecx
setne %dl
andb %al, %dl
movzbl %dl, %eax
negl %eax
orl $-0x6, %eax
cmpl $-0x2, %ecx
cmovel %ecx, %eax
jmp 0x2e2b6
movaps %xmm1, %xmm2
subss %xmm0, %xmm2
xorps %xmm0, %xmm0
xorl %eax, %eax
ucomiss %xmm2, %xmm0
seta %al
movss %xmm2, 0x4(%rsp)
divss %xmm2, %xmm1
maxss %xmm1, %xmm0
movss 0x3e41(%rip), %xmm1     # 0x32064
minss %xmm0, %xmm1
movsd (%r12), %xmm0
movsd (%r15), %xmm2
subps %xmm0, %xmm2
movaps %xmm1, %xmm3
unpcklps %xmm1, %xmm3            # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
mulps %xmm2, %xmm3
addps %xmm0, %xmm3
leaq 0x8(%rsp), %rdx
movlps %xmm3, (%rdx)
movss 0x8(%r12), %xmm0
movss 0x8(%r15), %xmm2
subss %xmm0, %xmm2
mulss %xmm1, %xmm2
addss %xmm0, %xmm2
movss %xmm2, 0x8(%rdx)
movl 0x14(%r13,%rax,4), %edi
movq %r12, %rsi
movq %r14, %rcx
movq %rbx, %r8
callq 0x2dfdc
cmpl $-0x1, %eax
jne 0x2e2b6
xorl %eax, %eax
xorps %xmm0, %xmm0
ucomiss 0x4(%rsp), %xmm0
setbe %al
movl 0x14(%r13,%rax,4), %edi
leaq 0x8(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %rbx, %r8
callq 0x2dfdc
jmp 0x2e2b6
movl %edi, %eax
jmp 0x2e2b6
testl %ecx, %ecx
je 0x2e2b3
pushq $-0x2
popq %rax
jmp 0x2e2b6
movl %edi, (%r14)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq