// File: /workspace/github_cpp_makefile_star2_O1/FreeSlave[P]vhlt/hlbsp/writebsp.cpp
// Function: WriteDrawNodes_r(node_s*, node_s const*)
static void     WriteDrawNodes_r(const node_t* const node)
#endif
{
#ifdef ZHLT_DETAILBRUSH
	if (node->isportalleaf)
	{
		if (node->contents == CONTENTS_SOLID)
		{
			return -1;
		}
		else
		{
			portalleaf = node;
			// Warning: make sure parent data have not been freed when writing children.
		}
	}
	if (node->planenum == -1)
	{
		if (node->iscontentsdetail)
		{
			free(node->markfaces);
			return -1;
		}
		else
		{
			int leafnum = WriteDrawLeaf (node, portalleaf);
			return -1 - leafnum;
		}
	}
#endif
    dnode_t*        n;
    int             i;
    face_t*         f;
#ifdef ZHLT_DETAILBRUSH
	int nodenum = g_numnodes;
#endif

    // emit a node
    hlassume(g_numnodes < MAX_MAP_NODES, assume_MAX_MAP_NODES);
    n = &g_dnodes[g_numnodes];
    g_numnodes++;

#ifdef ZHLT_DETAILBRUSH
	vec3_t mins, maxs;
#if 0
	if (node->isdetail || node->isportalleaf)
		printf ("node isdetail = %d loosebound = (%f,%f,%f)-(%f,%f,%f) portalleaf = (%f,%f,%f)-(%f,%f,%f)\n", node->isdetail,
			node->loosemins[0], node->loosemins[1], node->loosemins[2], node->loosemaxs[0], node->loosemaxs[1], node->loosemaxs[2],
			portalleaf->mins[0], portalleaf->mins[1], portalleaf->mins[2], portalleaf->maxs[0], portalleaf->maxs[1], portalleaf->maxs[2]);
#endif
	if (node->isdetail)
	{
#ifdef HLBSP_DETAILBRUSH_CULL
		// intersect its loose bounds with the strict bounds of its parent portalleaf
		VectorCompareMaximum (portalleaf->mins, node->loosemins, mins);
		VectorCompareMinimum (portalleaf->maxs, node->loosemaxs, maxs);
#else
		VectorCopy (portalleaf->mins, mins);
		VectorCopy (portalleaf->maxs, maxs);
#endif
	}
	else
	{
		VectorCopy (node->mins, mins);
		VectorCopy (node->maxs, maxs);
	}
#ifdef ZHLT_LARGERANGE
	for (int k = 0; k < 3; k++)
	{
		n->mins[k] = (short)qmax (-32767, qmin ((int)mins[k], 32767));
		n->maxs[k] = (short)qmax (-32767, qmin ((int)maxs[k], 32767));
	}
#else
	VectorCopy (mins, n->mins);
	VectorCopy (maxs, n->maxs);
#endif
#else
#ifdef ZHLT_LARGERANGE
	for (int k = 0; k < 3; k++)
	{
		n->mins[k] = (short)qmax (-32767, qmin ((int)node->mins[k], 32767));
		n->maxs[k] = (short)qmax (-32767, qmin ((int)node->maxs[k], 32767));
	}
#else
    VectorCopy(node->mins, n->mins);
    VectorCopy(node->maxs, n->maxs);
#endif
#endif

    if (node->planenum & 1)
    {
        Error("WriteDrawNodes_r: odd planenum");
    }
    n->planenum = WritePlane(node->planenum);
    n->firstface = g_numfaces;

    for (f = node->faces; f; f = f->next)
    {
        WriteFace(f);
    }

    n->numfaces = g_numfaces - n->firstface;

    //
    // recursively output the other nodes
    //
    for (i = 0; i < 2; i++)
    {
#ifdef ZHLT_DETAILBRUSH
		n->children[i] = WriteDrawNodes_r (node->children[i], portalleaf);
#else
        if (node->children[i]->planenum == -1)
        {
            if (node->children[i]->contents == CONTENTS_SOLID)
            {
                n->children[i] = -1;
            }
            else
            {
                n->children[i] = -(g_numleafs + 1);
                WriteDrawLeaf(node->children[i]);
            }
        }
        else
        {
            n->children[i] = g_numnodes;
            WriteDrawNodes_r(node->children[i]);
        }
#endif
    }
#ifdef ZHLT_DETAILBRUSH
	return nodenum;
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
movq %rdi, %r13
cmpb $0x1, 0x49(%rdi)
jne 0x1c009
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $-0x2, 0xa0(%r13)
movq %r13, %r14
je 0x1c623
cmpl $-0x1, 0x80(%r13)
je 0x1c09b
leaq 0x10b83aa(%rip), %rbx   # 0x10d43c8
movl (%rbx), %eax
xorl %edi, %edi
movl %eax, 0xc(%rsp)
cmpl $0x7fff, %eax           # imm = 0x7FFF
setl %dil
movl $0x32, %esi
callq 0xa731
movslq (%rbx), %rax
leaq (%rax,%rax,2), %r12
shlq $0x3, %r12
incl %eax
movl %eax, (%rbx)
cmpb $0x1, 0x48(%r13)
jne 0x1c0bc
movupd 0x50(%r14), %xmm0
movupd 0x18(%r13), %xmm1
maxpd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
movsd 0x60(%r14), %xmm0
maxsd 0x28(%r13), %xmm0
movsd %xmm0, 0x30(%rsp)
movupd 0x68(%r14), %xmm0
movupd 0x30(%r13), %xmm1
minpd %xmm1, %xmm0
movapd %xmm0, 0x40(%rsp)
movsd 0x78(%r14), %xmm0
minsd 0x40(%r13), %xmm0
jmp 0x1c0e2
cmpb $0x1, 0x4a(%r13)
jne 0x1c41e
movq 0xa8(%r13), %rdi
callq 0x4380
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1c623
movups 0x50(%r13), %xmm0
movaps %xmm0, 0x20(%rsp)
movsd 0x60(%r13), %xmm0
movsd %xmm0, 0x30(%rsp)
movups 0x68(%r13), %xmm0
movaps %xmm0, 0x40(%rsp)
movsd 0x78(%r13), %xmm0
addq 0xeedf(%rip), %r12      # 0x2afc8
movsd %xmm0, 0x50(%rsp)
xorl %eax, %eax
movl $0x7fff, %ecx           # imm = 0x7FFF
movl $0xffff8002, %edx       # imm = 0xFFFF8002
cvttsd2si 0x20(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x1c10f
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0x8(%r12,%rax,2)
cvttsd2si 0x40(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x1c129
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0xe(%r12,%rax,2)
incq %rax
cmpq $0x3, %rax
jne 0x1c0fb
testb $0x1, 0x80(%r13)
je 0x1c150
leaq 0x7f3d(%rip), %rdi      # 0x24086
xorl %eax, %eax
callq 0x9f84
movl 0x80(%r13), %edi
callq 0x1cf4b
movl %eax, (%r12)
leaq 0x1b7825d(%rip), %rbx   # 0x1b943c4
movzwl (%rbx), %eax
movw %ax, 0x14(%r12)
movq 0x98(%r13), %r15
testq %r15, %r15
je 0x1c3e5
leaq 0x1e1225d(%rip), %rbp   # 0x1e2e3e4
movq %r15, %rdi
callq 0x1587f
testb %al, %al
jne 0x1c1d1
movq %r15, %rdi
callq 0x158a3
testb %al, %al
jne 0x1c1d1
movq %r15, %rdi
callq 0x157fd
testb %al, %al
jne 0x1c1d1
movq %r15, %rdi
callq 0x158c7
testb %al, %al
jne 0x1c1d1
cmpl $-0x1, 0xc(%r15)
je 0x1c1d1
cmpl $0x0, 0x34(%r15)
je 0x1c1d1
movq %r15, %rdi
callq 0x1585b
testb %al, %al
je 0x1c1e6
movl $0xffffffff, 0x28(%r15) # imm = 0xFFFFFFFF
movq (%r15), %r15
testq %r15, %r15
jne 0x1c187
jmp 0x1c3e5
movq %r13, 0x10(%rsp)
movslq (%rbx), %rax
xorl %edi, %edi
cmpq $0xffff, %rax           # imm = 0xFFFF
setl %dil
movl %eax, 0x28(%r15)
leaq (%rax,%rax,4), %r13
movl $0x30, %esi
callq 0xa731
incl (%rbx)
movl 0x8(%r15), %edi
callq 0x1cf4b
leaq 0x1b781b2(%rip), %rcx   # 0x1b943d0
movw %ax, (%rcx,%r13,4)
movzwl 0x8(%r15), %eax
andl $0x1, %eax
movw %ax, 0x2(%rcx,%r13,4)
movl (%rbp), %eax
movl %eax, 0x4(%rcx,%r13,4)
movzwl 0x2c(%r15), %eax
movq %r13, 0x18(%rsp)
movw %ax, 0x8(%rcx,%r13,4)
movslq 0xc(%r15), %r13
testq %r13, %r13
js 0x1c25e
leaq 0x1178163(%rip), %rax   # 0x11943bc
cmpl %r13d, (%rax)
jg 0x1c26f
leaq 0x7e48(%rip), %rdi      # 0x240ad
movl %r13d, %esi
xorl %eax, %eax
callq 0x9f84
leaq 0x20b879a(%rip), %rax   # 0x20d4a10
cmpb $0x0, (%rax)
movq %r14, 0x60(%rsp)
je 0x1c288
movq %rbp, %rbx
jmp 0x1c35e
movq 0x4a348d1(%rip), %rcx   # 0x4a50b60
leaq 0x4a348c2(%rip), %rsi   # 0x4a50b58
movq %rsi, %rax
testq %rcx, %rcx
je 0x1c2b8
movq %rsi, %rax
xorl %edx, %edx
cmpl %r13d, 0x20(%rcx)
setl %dl
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x1c2a1
movq %rsi, %rcx
cmpq %rsi, %rax
je 0x1c2cb
cmpl %r13d, 0x20(%rax)
cmovgq %rsi, %rax
movq %rax, %rcx
movq %rbp, %rbx
cmpq %rsi, %rcx
je 0x1c2dc
movl 0x24(%rcx), %r13d
jmp 0x1c35e
xorl %edi, %edi
cmpl $0x7fff, 0x4a348a0(%rip) # imm = 0x7FFF
setl %dil
movl $0x26, %esi
callq 0xa731
movslq 0x4a3488b(%rip), %rbp   # 0x4a50b88
leaq (,%r13,4), %rax
addq %r13, %rax
leaq (%rbp,%rbp,4), %rcx
leaq 0x11780ac(%rip), %rsi   # 0x11943c0
movq 0x20(%rsi,%rax,8), %rdx
leaq 0x4a34870(%rip), %rdi   # 0x4a50b90
movq %rdx, 0x20(%rdi,%rcx,8)
movupd (%rsi,%rax,8), %xmm0
movupd 0x10(%rsi,%rax,8), %xmm1
movupd %xmm1, 0x10(%rdi,%rcx,8)
movupd %xmm0, (%rdi,%rcx,8)
movl %r13d, 0x38(%rsp)
movl %ebp, 0x3c(%rsp)
leaq 0x4a34805(%rip), %rdi   # 0x4a50b50
leaq 0x38(%rsp), %rsi
callq 0x1d0a0
incl 0x4a3482d(%rip)         # 0x4a50b88
movl %ebp, %r13d
leaq 0x1b7806b(%rip), %rax   # 0x1b943d0
movq 0x18(%rsp), %rcx
leaq (%rax,%rcx,4), %rax
movw %r13w, 0xa(%rax)
cmpl $0x0, 0x2c(%r15)
leaq 0x1e12071(%rip), %r14   # 0x1e2e3f0
movq %rbx, %rbp
jle 0x1c3be
xorl %ebx, %ebx
movq 0x18(%r15), %rax
movl (%rax,%rbx,4), %r13d
xorl %edi, %edi
cmpl $0x7d000, (%rbp)        # imm = 0x7D000
setl %dil
movl $0x31, %esi
callq 0xa731
movslq (%rbp), %rax
movl %r13d, (%r14,%rax,4)
incl %eax
movl %eax, (%rbp)
incq %rbx
movslq 0x2c(%r15), %rax
cmpq %rax, %rbx
jl 0x1c386
movq 0x18(%r15), %rdi
callq 0x4380
movq $0x0, 0x18(%r15)
movq 0x10(%rsp), %r13
leaq 0x1b77fe9(%rip), %rbx   # 0x1b943c4
movq 0x60(%rsp), %r14
jmp 0x1c1d9
movzwl (%rbx), %eax
subw 0x14(%r12), %ax
movw %ax, 0x16(%r12)
xorl %ebx, %ebx
movq 0x88(%r13,%rbx,8), %rdi
movq %r14, %rsi
callq 0x1bfd9
movw %ax, 0x4(%r12,%rbx,2)
incq %rbx
cmpq $0x1, %rbx
je 0x1c3f6
movl 0xc(%rsp), %eax
jmp 0x1c623
leaq 0xa1805f(%rip), %rbx    # 0xa34484
movl (%rbx), %ebp
xorl %edi, %edi
cmpl $0x7ff8, %ebp           # imm = 0x7FF8
setl %dil
movl $0x35, %esi
callq 0xa731
movslq (%rbx), %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
leaq 0xa1803e(%rip), %rdi    # 0xa34490
incl %ecx
movl %ecx, (%rbx)
movl 0xa0(%r14), %ecx
movl %ecx, (%rdi,%rax)
cmpb $0x1, 0x48(%r13)
movl %ebp, 0xc(%rsp)
jne 0x1c4b7
movupd 0x50(%r14), %xmm0
movupd 0x18(%r13), %xmm1
maxpd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
movsd 0x60(%r14), %xmm0
maxsd 0x28(%r13), %xmm0
movsd %xmm0, 0x30(%rsp)
movupd 0x68(%r14), %xmm0
movupd 0x30(%r13), %xmm1
minpd %xmm1, %xmm0
movapd %xmm0, 0x40(%rsp)
movsd 0x78(%r14), %xmm0
minsd 0x40(%r13), %xmm0
jmp 0x1c4dd
movups 0x50(%r13), %xmm0
movaps %xmm0, 0x20(%rsp)
movsd 0x60(%r13), %xmm0
movsd %xmm0, 0x30(%rsp)
movups 0x68(%r13), %xmm0
movaps %xmm0, 0x40(%rsp)
movsd 0x78(%r13), %xmm0
movq %r13, 0x10(%rsp)
addq %rax, %rdi
movsd %xmm0, 0x50(%rsp)
xorl %eax, %eax
movl $0x7fff, %ecx           # imm = 0x7FFF
movl $0xffff8002, %edx       # imm = 0xFFFF8002
cvttsd2si 0x20(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x1c50b
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0x8(%rdi,%rax,2)
cvttsd2si 0x40(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x1c524
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0xe(%rdi,%rax,2)
incq %rax
cmpq $0x3, %rax
jne 0x1c4f7
movl $0xffffffff, 0x4(%rdi)  # imm = 0xFFFFFFFF
leaq 0x1df1e94(%rip), %r13   # 0x1e0e3d4
movzwl (%r13), %eax
movq %rdi, 0x18(%rsp)
movw %ax, 0x14(%rdi)
xorl %edi, %edi
movq 0x10(%rsp), %rbx
cmpq $0x0, 0xa8(%rbx)
setne %dil
movl $0x16, %esi
callq 0xa731
movq 0xa8(%rbx), %rbp
movq (%rbp), %rbx
testq %rbx, %rbx
je 0x1c5fa
leaq 0x7b23(%rip), %r14      # 0x240a5
leaq 0x1df1e57(%rip), %r12   # 0x1e0e3e0
cmpl $-0x1, 0x28(%rbx)
je 0x1c5e4
movl 0xc(%rbx), %edi
callq 0x62cc
movq %rax, %r15
movq %rax, %rdi
callq 0x4140
cmpq $0x7, %rax
jb 0x1c5bc
leaq (%r15,%rax), %rdi
addq $-0x7, %rdi
movq %r14, %rsi
callq 0x4320
testl %eax, %eax
je 0x1c5e4
movzwl 0x28(%rbx), %eax
movslq (%r13), %rcx
xorl %edi, %edi
cmpq $0xffff, %rcx           # imm = 0xFFFF
setl %dil
movw %ax, (%r12,%rcx,2)
movl $0x2f, %esi
callq 0xa731
incl (%r13)
movq 0x20(%rbx), %rbx
testq %rbx, %rbx
jne 0x1c589
movq 0x8(%rbp), %rbx
addq $0x8, %rbp
testq %rbx, %rbx
jne 0x1c589
movq 0x10(%rsp), %rax
movq 0xa8(%rax), %rdi
callq 0x4380
movzwl (%r13), %eax
movq 0x18(%rsp), %rcx
subw 0x14(%rcx), %ax
movw %ax, 0x16(%rcx)
movl 0xc(%rsp), %eax
notl %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq