// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq %rdi, %r12
cmpb $0x1, 0x49(%rdi)
pushq $-0x1
popq %rbp
jne 0x19613
cmpl $-0x2, 0xa0(%r12)
movq %r12, %r15
je 0x19bb2
cmpl $-0x1, 0x80(%r12)
je 0x1968e
leaq 0x10b8da3(%rip), %r14   # 0x10d23c8
movl (%r14), %ebp
xorl %edi, %edi
cmpl $0x7fff, %ebp           # imm = 0x7FFF
setl %dil
pushq $0x32
popq %rsi
callq 0x92bb
movslq (%r14), %rax
imulq $0x18, %rax, %rbx
incl %eax
movl %eax, (%r14)
cmpb $0x1, 0x48(%r12)
jne 0x196ac
movupd 0x50(%r15), %xmm0
movupd 0x18(%r12), %xmm1
maxpd %xmm1, %xmm0
movupd 0x30(%r12), %xmm3
movsd 0x60(%r15), %xmm1
maxsd 0x28(%r12), %xmm1
movupd 0x68(%r15), %xmm2
minpd %xmm3, %xmm2
movsd 0x78(%r15), %xmm3
minsd 0x40(%r12), %xmm3
jmp 0x196c8
cmpb $0x1, 0x4a(%r12)
jne 0x199bf
movq 0xa8(%r12), %rdi
callq 0x4390
jmp 0x19bb2
movupd 0x50(%r12), %xmm0
movsd 0x60(%r12), %xmm1
movupd 0x68(%r12), %xmm2
movsd 0x78(%r12), %xmm3
addq 0xf8f9(%rip), %rbx      # 0x28fc8
movapd %xmm0, 0x40(%rsp)
movsd %xmm1, 0x50(%rsp)
movapd %xmm2, 0x20(%rsp)
movsd %xmm3, 0x30(%rsp)
xorl %eax, %eax
movl $0x7fff, %ecx           # imm = 0x7FFF
movl $0xffff8002, %edx       # imm = 0xFFFF8002
cmpq $0x3, %rax
je 0x19730
cvttsd2si 0x40(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x1970d
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0x8(%rbx,%rax,2)
cvttsd2si 0x20(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x19726
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0xe(%rbx,%rax,2)
incq %rax
jmp 0x196f3
movl 0x80(%r12), %edi
testb $0x1, %dil
je 0x19754
leaq 0x7871(%rip), %rdi      # 0x20fb6
xorl %eax, %eax
callq 0x8b1f
movl 0x80(%r12), %edi
callq 0x1a538
movl %eax, (%rbx)
leaq 0x1b78c62(%rip), %r14   # 0x1b923c4
movzwl (%r14), %eax
movw %ax, 0x14(%rbx)
movq %r12, 0x10(%rsp)
addq $0x98, %r12
leaq 0x1e12c67(%rip), %r13   # 0x1e2c3e4
movq (%r12), %r12
testq %r12, %r12
je 0x19986
movq %r12, %rdi
callq 0x13634
testb %al, %al
jne 0x197d6
movq %r12, %rdi
callq 0x13656
testb %al, %al
jne 0x197d6
movq %r12, %rdi
callq 0x135b8
testb %al, %al
jne 0x197d6
movq %r12, %rdi
callq 0x13678
testb %al, %al
jne 0x197d6
cmpl $-0x1, 0xc(%r12)
je 0x197d6
cmpl $0x0, 0x34(%r12)
je 0x197d6
movq %r12, %rdi
callq 0x13612
testb %al, %al
je 0x197de
orl $-0x1, 0x28(%r12)
jmp 0x1977d
movl %ebp, (%rsp)
movslq (%r14), %rax
xorl %edi, %edi
cmpq $0xffff, %rax           # imm = 0xFFFF
setl %dil
movl %eax, 0x28(%r12)
imulq $0x14, %rax, %rbp
pushq $0x30
popq %rsi
callq 0x92bb
incl (%r14)
movq %rbp, %r14
movl 0x8(%r12), %edi
callq 0x1a538
leaq 0x1b78bb8(%rip), %rcx   # 0x1b923d0
movw %ax, (%rcx,%rbp)
movzwl 0x8(%r12), %eax
andl $0x1, %eax
movw %ax, 0x2(%rcx,%rbp)
movl (%r13), %eax
movl %eax, 0x4(%rcx,%rbp)
movzwl 0x2c(%r12), %eax
movw %ax, 0x8(%rcx,%rbp)
movl 0xc(%r12), %ebp
movl %ebp, 0x4(%rsp)
testl %ebp, %ebp
js 0x19855
leaq 0x1178b6b(%rip), %rax   # 0x11923bc
cmpl %ebp, (%rax)
jg 0x19865
leaq 0x7781(%rip), %rdi      # 0x20fdd
movl %ebp, %esi
xorl %eax, %eax
callq 0x8b1f
leaq 0x20b91a4(%rip), %rax   # 0x20d2a10
cmpb $0x0, (%rax)
jne 0x1991b
movq %r14, 0x8(%rsp)
leaq 0x4a352cf(%rip), %r14   # 0x4a4eb50
movq %r14, %rdi
leaq 0x4(%rsp), %rsi
callq 0x1a662
leaq 0x4a352c3(%rip), %rcx   # 0x4a4eb58
cmpq %rcx, %rax
je 0x1989f
movl 0x24(%rax), %ebp
jmp 0x19916
xorl %edi, %edi
cmpl $0x7fff, 0x4a352dd(%rip) # imm = 0x7FFF
setl %dil
pushq $0x26
popq %rsi
callq 0x92bb
movslq 0x4a352ca(%rip), %rbp   # 0x4a4eb88
movslq 0x4(%rsp), %rax
imulq $0x28, %rax, %rcx
imulq $0x28, %rbp, %rdx
leaq 0x1178aee(%rip), %rdi   # 0x11923c0
movq 0x20(%rdi,%rcx), %rsi
leaq 0x4a352b2(%rip), %r8    # 0x4a4eb90
movq %rsi, 0x20(%rdx,%r8)
movupd (%rdi,%rcx), %xmm0
movupd 0x10(%rdi,%rcx), %xmm1
movupd %xmm1, 0x10(%rdx,%r8)
movupd %xmm0, (%rdx,%r8)
movl %eax, 0x18(%rsp)
movl %ebp, 0x1c(%rsp)
movq %r14, %rdi
leaq 0x18(%rsp), %rsi
callq 0x1a6b4
incl 0x4a35272(%rip)         # 0x4a4eb88
movq 0x8(%rsp), %r14
addq 0xf6c6(%rip), %r14      # 0x28fe8
movw %bp, 0xa(%r14)
xorl %r14d, %r14d
movslq 0x2c(%r12), %rax
movq 0x18(%r12), %rdi
cmpq %rax, %r14
jge 0x1996c
movl (%rdi,%r14,4), %ebp
xorl %edi, %edi
cmpl $0x7d000, (%r13)        # imm = 0x7D000
setl %dil
pushq $0x31
popq %rsi
callq 0x92bb
movslq (%r13), %rax
leaq 0x1e12a92(%rip), %rcx   # 0x1e2c3f0
movl %ebp, (%rcx,%rax,4)
incl %eax
movl %eax, (%r13)
incq %r14
jmp 0x1992a
callq 0x4390
andq $0x0, 0x18(%r12)
movl (%rsp), %ebp
leaq 0x1b78a43(%rip), %r14   # 0x1b923c4
jmp 0x1977d
movzwl (%r14), %eax
subw 0x14(%rbx), %ax
movw %ax, 0x16(%rbx)
xorl %r14d, %r14d
movq 0x10(%rsp), %r12
cmpq $0x2, %r14
je 0x19bb2
movq 0x88(%r12,%r14,8), %rdi
movq %r15, %rsi
callq 0x195e4
movw %ax, 0x4(%rbx,%r14,2)
incq %r14
jmp 0x1999a
leaq 0xa18abe(%rip), %rbx    # 0xa32484
movl (%rbx), %ebp
xorl %edi, %edi
cmpl $0x7ff8, %ebp           # imm = 0x7FF8
setl %dil
pushq $0x35
popq %rsi
callq 0x92bb
movslq (%rbx), %rcx
imulq $0x1c, %rcx, %rax
leaq 0xa18aa6(%rip), %rdi    # 0xa32490
incl %ecx
movl %ecx, (%rbx)
movl 0xa0(%r15), %ecx
movl %ecx, (%rdi,%rax)
cmpb $0x1, 0x48(%r12)
jne 0x19a3e
movupd 0x50(%r15), %xmm0
movupd 0x18(%r12), %xmm1
maxpd %xmm1, %xmm0
movupd 0x30(%r12), %xmm3
movsd 0x60(%r15), %xmm1
maxsd 0x28(%r12), %xmm1
movupd 0x68(%r15), %xmm2
minpd %xmm3, %xmm2
movsd 0x78(%r15), %xmm3
minsd 0x40(%r12), %xmm3
jmp 0x19a5a
movupd 0x50(%r12), %xmm0
movsd 0x60(%r12), %xmm1
movupd 0x68(%r12), %xmm2
movsd 0x78(%r12), %xmm3
addq %rax, %rdi
movapd %xmm0, 0x40(%rsp)
movsd %xmm1, 0x50(%rsp)
movapd %xmm2, 0x20(%rsp)
movsd %xmm3, 0x30(%rsp)
xorl %eax, %eax
movl $0x7fff, %ecx           # imm = 0x7FFF
movl $0xffff8002, %edx       # imm = 0xFFFF8002
cmpq $0x3, %rax
je 0x19abe
cvttsd2si 0x40(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x19a9b
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0x8(%rdi,%rax,2)
cvttsd2si 0x20(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x19ab4
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0xe(%rdi,%rax,2)
incq %rax
jmp 0x19a81
movl %ebp, (%rsp)
orl $-0x1, 0x4(%rdi)
leaq 0x1df2908(%rip), %r13   # 0x1e0c3d4
movzwl (%r13), %eax
movq %rdi, 0x60(%rsp)
movw %ax, 0x14(%rdi)
xorl %edi, %edi
cmpq $0x0, 0xa8(%r12)
setne %dil
pushq $0x16
popq %rsi
callq 0x92bb
movq %r12, 0x10(%rsp)
movq 0xa8(%r12), %rax
leaq 0x74d0(%rip), %r15      # 0x20fd5
leaq 0x1df28d4(%rip), %rbx   # 0x1e0c3e0
pushq $0x2f
popq %rbp
movq (%rax), %r14
testq %r14, %r14
je 0x19b8a
movq %rax, 0x8(%rsp)
cmpl $-0x1, 0x28(%r14)
je 0x19b76
movl 0xc(%r14), %edi
callq 0x5c19
movq %rax, %r12
movq %rax, %rdi
callq 0x4140
cmpq $0x7, %rax
jb 0x19b51
leaq (%r12,%rax), %rdi
addq $-0x7, %rdi
movq %r15, %rsi
callq 0x4330
testl %eax, %eax
je 0x19b76
movzwl 0x28(%r14), %eax
movslq (%r13), %rcx
xorl %edi, %edi
cmpq $0xffff, %rcx           # imm = 0xFFFF
setl %dil
movw %ax, (%rbx,%rcx,2)
movl %ebp, %esi
callq 0x92bb
incl (%r13)
movq 0x20(%r14), %r14
testq %r14, %r14
jne 0x19b1c
movq 0x8(%rsp), %rax
addq $0x8, %rax
jmp 0x19b0f
movq 0x10(%rsp), %rax
movq 0xa8(%rax), %rdi
callq 0x4390
movzwl (%r13), %eax
movq 0x60(%rsp), %rcx
subw 0x14(%rcx), %ax
movw %ax, 0x16(%rcx)
movl (%rsp), %ebp
notl %ebp
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq