// File: 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:
subq $0xc8, %rsp
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq 0xb8(%rsp), %rax
testb $0x1, 0x49(%rax)
je 0x32cf8
movq 0xb8(%rsp), %rax
cmpl $-0x2, 0xa0(%rax)
jne 0x32ce6
movl $0xffffffff, 0xc4(%rsp) # imm = 0xFFFFFFFF
jmp 0x332ce
movq 0xb8(%rsp), %rax
movq %rax, 0xb0(%rsp)
jmp 0x32cf8
movq 0xb8(%rsp), %rax
cmpl $-0x1, 0x80(%rax)
jne 0x32d6f
movq 0xb8(%rsp), %rax
testb $0x1, 0x4a(%rax)
je 0x32d3b
movq 0xb8(%rsp), %rax
movq 0xa8(%rax), %rdi
callq 0x4370
movl $0xffffffff, 0xc4(%rsp) # imm = 0xFFFFFFFF
jmp 0x332ce
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x344a0
movl %eax, 0xac(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl 0xac(%rsp), %eax
movl %eax, 0xc4(%rsp)
jmp 0x332ce
leaq 0x10bc652(%rip), %rax   # 0x10ef3c8
movl (%rax), %eax
movl %eax, 0x8c(%rsp)
leaq 0x10bc642(%rip), %rax   # 0x10ef3c8
cmpl $0x7fff, (%rax)         # imm = 0x7FFF
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x32, %esi
callq 0xbe90
leaq 0x10bc622(%rip), %rax   # 0x10ef3c8
movslq (%rax), %rcx
leaq 0x10bc620(%rip), %rax   # 0x10ef3d0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0xa0(%rsp)
leaq 0x10bc602(%rip), %rax   # 0x10ef3c8
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x10bc5f6(%rip), %rax   # 0x10ef3c8
movl %ecx, (%rax)
movq 0xb8(%rsp), %rax
testb $0x1, 0x48(%rax)
je 0x32fd7
movq 0xb0(%rsp), %rax
movsd 0x50(%rax), %xmm0
movq 0xb8(%rsp), %rax
ucomisd 0x18(%rax), %xmm0
jbe 0x32e17
movq 0xb0(%rsp), %rax
movsd 0x50(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0x32e2a
movq 0xb8(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0xb0(%rsp), %rax
movsd 0x58(%rax), %xmm0
movq 0xb8(%rsp), %rax
ucomisd 0x20(%rax), %xmm0
jbe 0x32e67
movq 0xb0(%rsp), %rax
movsd 0x58(%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
jmp 0x32e7a
movq 0xb8(%rsp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0xb0(%rsp), %rax
movsd 0x60(%rax), %xmm0
movq 0xb8(%rsp), %rax
ucomisd 0x28(%rax), %xmm0
jbe 0x32eb7
movq 0xb0(%rsp), %rax
movsd 0x60(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x32eca
movq 0xb8(%rsp), %rax
movsd 0x28(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0xb0(%rsp), %rax
movsd 0x68(%rax), %xmm1
movq 0xb8(%rsp), %rax
movsd 0x30(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x32f0e
movq 0xb0(%rsp), %rax
movsd 0x68(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0x32f21
movq 0xb8(%rsp), %rax
movsd 0x30(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0xb0(%rsp), %rax
movsd 0x70(%rax), %xmm1
movq 0xb8(%rsp), %rax
movsd 0x38(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x32f62
movq 0xb0(%rsp), %rax
movsd 0x70(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x32f75
movq 0xb8(%rsp), %rax
movsd 0x38(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0xb0(%rsp), %rax
movsd 0x78(%rax), %xmm1
movq 0xb8(%rsp), %rax
movsd 0x40(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x32fb6
movq 0xb0(%rsp), %rax
movsd 0x78(%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x32fc9
movq 0xb8(%rsp), %rax
movsd 0x40(%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x3304c
movq 0xb8(%rsp), %rax
movsd 0x50(%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x58(%rax), %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x60(%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x68(%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x70(%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x78(%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movl $0x0, 0x4c(%rsp)
cmpl $0x3, 0x4c(%rsp)
jge 0x33196
movslq 0x4c(%rsp), %rax
cvttsd2si 0x70(%rsp,%rax,8), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x33082
movslq 0x4c(%rsp), %rax
cvttsd2si 0x70(%rsp,%rax,8), %eax
movl %eax, 0x14(%rsp)
jmp 0x3308d
movl $0x7fff, %eax           # imm = 0x7FFF
movl %eax, 0x14(%rsp)
jmp 0x3308d
movl 0x14(%rsp), %ecx
movl $0xffff8001, %eax       # imm = 0xFFFF8001
cmpl %ecx, %eax
jle 0x330a5
movl $0xffff8001, %eax       # imm = 0xFFFF8001
movl %eax, 0x10(%rsp)
jmp 0x330db
movslq 0x4c(%rsp), %rax
cvttsd2si 0x70(%rsp,%rax,8), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x330c8
movslq 0x4c(%rsp), %rax
cvttsd2si 0x70(%rsp,%rax,8), %eax
movl %eax, 0xc(%rsp)
jmp 0x330d3
movl $0x7fff, %eax           # imm = 0x7FFF
movl %eax, 0xc(%rsp)
jmp 0x330d3
movl 0xc(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movw %ax, %dx
movq 0xa0(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movw %dx, 0x8(%rax,%rcx,2)
movslq 0x4c(%rsp), %rax
cvttsd2si 0x50(%rsp,%rax,8), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x33117
movslq 0x4c(%rsp), %rax
cvttsd2si 0x50(%rsp,%rax,8), %eax
movl %eax, 0x8(%rsp)
jmp 0x33122
movl $0x7fff, %eax           # imm = 0x7FFF
movl %eax, 0x8(%rsp)
jmp 0x33122
movl 0x8(%rsp), %ecx
movl $0xffff8001, %eax       # imm = 0xFFFF8001
cmpl %ecx, %eax
jle 0x3313a
movl $0xffff8001, %eax       # imm = 0xFFFF8001
movl %eax, 0x4(%rsp)
jmp 0x3316d
movslq 0x4c(%rsp), %rax
cvttsd2si 0x50(%rsp,%rax,8), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x3315c
movslq 0x4c(%rsp), %rax
cvttsd2si 0x50(%rsp,%rax,8), %eax
movl %eax, (%rsp)
jmp 0x33166
movl $0x7fff, %eax           # imm = 0x7FFF
movl %eax, (%rsp)
jmp 0x33166
movl (%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
movw %ax, %dx
movq 0xa0(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movw %dx, 0xe(%rax,%rcx,2)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x33054
movq 0xb8(%rsp), %rax
movl 0x80(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x331ba
leaq 0x8fab(%rip), %rdi      # 0x3c15e
movb $0x0, %al
callq 0xb510
movq 0xb8(%rsp), %rax
movl 0x80(%rax), %edi
callq 0x34380
movl %eax, %ecx
movq 0xa0(%rsp), %rax
movl %ecx, (%rax)
leaq 0x1b7c1e4(%rip), %rax   # 0x1baf3c4
movl (%rax), %eax
movw %ax, %cx
movq 0xa0(%rsp), %rax
movw %cx, 0x14(%rax)
movq 0xb8(%rsp), %rax
movq 0x98(%rax), %rax
movq %rax, 0x90(%rsp)
cmpq $0x0, 0x90(%rsp)
je 0x33235
movq 0x90(%rsp), %rdi
callq 0x34b20
movq 0x90(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x90(%rsp)
jmp 0x33208
leaq 0x1b7c188(%rip), %rax   # 0x1baf3c4
movl (%rax), %eax
movq 0xa0(%rsp), %rcx
movzwl 0x14(%rcx), %ecx
subl %ecx, %eax
movw %ax, %cx
movq 0xa0(%rsp), %rax
movw %cx, 0x16(%rax)
movl $0x0, 0x9c(%rsp)
cmpl $0x2, 0x9c(%rsp)
jge 0x332c0
movq 0xb8(%rsp), %rax
movslq 0x9c(%rsp), %rcx
movq 0x88(%rax,%rcx,8), %rdi
movq 0xb0(%rsp), %rsi
callq 0x32ca0
movw %ax, %dx
movq 0xa0(%rsp), %rax
movslq 0x9c(%rsp), %rcx
movw %dx, 0x4(%rax,%rcx,2)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x33266
movl 0x8c(%rsp), %eax
movl %eax, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
addq $0xc8, %rsp
retq
nopl (%rax)