// File: /workspace/github_cpp_makefile_star2_O3/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)
jne 0x1bba7
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpl $-0x2, 0xa0(%r12)
movq %r12, %r15
je 0x1c1d8
cmpl $-0x1, 0x80(%r12)
je 0x1bc28
leaq 0x10b77ff(%rip), %rbx   # 0x10d33b8
movl (%rbx), %r14d
xorl %edi, %edi
cmpl $0x7fff, %r14d          # imm = 0x7FFF
setl %dil
movl $0x32, %esi
callq 0xa655
movslq (%rbx), %rax
leaq (%rax,%rax,2), %r13
shlq $0x3, %r13
incl %eax
movl %eax, (%rbx)
cmpb $0x1, 0x48(%r12)
jne 0x1bc4c
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 0x1bc68
cmpb $0x1, 0x4a(%r12)
jne 0x1bfcc
movq 0xa8(%r12), %rdi
callq 0x4380
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x1c1d8
movupd 0x50(%r12), %xmm0
movsd 0x60(%r12), %xmm1
movupd 0x68(%r12), %xmm2
movsd 0x78(%r12), %xmm3
addq 0xe359(%rip), %r13      # 0x29fc8
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
cvttsd2si 0x40(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x1bca7
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0x8(%r13,%rax,2)
cvttsd2si 0x20(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x1bcc1
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0xe(%r13,%rax,2)
incq %rax
cmpq $0x3, %rax
jne 0x1bc93
movl 0x80(%r12), %edi
testb $0x1, %dil
je 0x1bcf4
leaq 0x7371(%rip), %rdi      # 0x23056
xorl %eax, %eax
callq 0x9e9c
movl 0x80(%r12), %edi
callq 0x1cb5f
movl %eax, (%r13)
leaq 0x1b776b0(%rip), %rbp   # 0x1b933b4
movzwl (%rbp), %eax
movw %ax, 0x14(%r13)
movq %r12, 0x10(%rsp)
movq 0x98(%r12), %r12
movl %eax, %ecx
testq %r12, %r12
je 0x1bf93
movq %r12, %rdi
callq 0x15575
testb %al, %al
jne 0x1bd71
movq %r12, %rdi
callq 0x15599
testb %al, %al
jne 0x1bd71
movq %r12, %rdi
callq 0x154f3
testb %al, %al
jne 0x1bd71
movq %r12, %rdi
callq 0x155bd
testb %al, %al
jne 0x1bd71
cmpl $-0x1, 0xc(%r12)
je 0x1bd71
cmpl $0x0, 0x34(%r12)
je 0x1bd71
movq %r12, %rdi
callq 0x15551
testb %al, %al
je 0x1bd88
movl $0xffffffff, 0x28(%r12) # imm = 0xFFFFFFFF
movq (%r12), %r12
testq %r12, %r12
jne 0x1bd25
jmp 0x1bf8a
movslq (%rbp), %rax
xorl %edi, %edi
cmpq $0xffff, %rax           # imm = 0xFFFF
setl %dil
movl %eax, 0x28(%r12)
leaq (%rax,%rax,4), %rbx
movl $0x30, %esi
callq 0xa655
incl (%rbp)
movl 0x8(%r12), %edi
callq 0x1cb5f
leaq 0x1b77601(%rip), %rcx   # 0x1b933c0
movw %ax, (%rcx,%rbx,4)
movzwl 0x8(%r12), %eax
andl $0x1, %eax
movw %ax, 0x2(%rcx,%rbx,4)
leaq 0x1e115fc(%rip), %rax   # 0x1e2d3d4
movl (%rax), %eax
movl %eax, 0x4(%rcx,%rbx,4)
movzwl 0x2c(%r12), %eax
movq %rbx, 0x60(%rsp)
movw %ax, 0x8(%rcx,%rbx,4)
movslq 0xc(%r12), %rbp
testq %rbp, %rbp
js 0x1be03
leaq 0x11775ad(%rip), %rax   # 0x11933ac
cmpl %ebp, (%rax)
jg 0x1be13
leaq 0x7273(%rip), %rdi      # 0x2307d
movl %ebp, %esi
xorl %eax, %eax
callq 0x9e9c
leaq 0x20b7be6(%rip), %rax   # 0x20d3a00
cmpb $0x0, (%rax)
movl %r14d, 0x4(%rsp)
movq %r15, 0x8(%rsp)
je 0x1be35
leaq 0x1e115a4(%rip), %rbx   # 0x1e2d3d4
jmp 0x1befb
movq 0x4a33d14(%rip), %rcx   # 0x4a4fb50
testq %rcx, %rcx
leaq 0x1e1158e(%rip), %rbx   # 0x1e2d3d4
je 0x1be7a
leaq 0x4a33cf9(%rip), %rax   # 0x4a4fb48
xorl %edx, %edx
cmpl %ebp, 0x20(%rcx)
setl %dl
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x1be4f
leaq 0x4a33cdc(%rip), %rcx   # 0x4a4fb48
cmpq %rcx, %rax
je 0x1be7a
cmpl %ebp, 0x20(%rax)
jle 0x1bf82
xorl %edi, %edi
cmpl $0x7fff, 0x4a33cf2(%rip) # imm = 0x7FFF
setl %dil
movl $0x26, %esi
callq 0xa655
movslq 0x4a33cdd(%rip), %r14   # 0x4a4fb78
leaq (,%rbp,4), %rax
addq %rbp, %rax
leaq (%r14,%r14,4), %rcx
leaq 0x11774ff(%rip), %rsi   # 0x11933b0
movq 0x20(%rsi,%rax,8), %rdx
leaq 0x4a33cc3(%rip), %rdi   # 0x4a4fb80
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 %ebp, 0x18(%rsp)
movl %r14d, 0x1c(%rsp)
leaq 0x4a33c58(%rip), %rdi   # 0x4a4fb40
leaq 0x18(%rsp), %rsi
callq 0x1cca0
incl 0x4a33c80(%rip)         # 0x4a4fb78
movl %r14d, %ebp
leaq 0x1b774be(%rip), %rax   # 0x1b933c0
movq 0x60(%rsp), %rcx
leaq (%rax,%rcx,4), %rax
movw %bp, 0xa(%rax)
cmpl $0x0, 0x2c(%r12)
leaq 0x1e114c4(%rip), %r15   # 0x1e2d3e0
jle 0x1bf59
movl (%rbx), %eax
xorl %r14d, %r14d
movq 0x18(%r12), %rcx
movl (%rcx,%r14,4), %ebp
xorl %edi, %edi
cmpl $0x7d000, %eax          # imm = 0x7D000
setl %dil
movl $0x31, %esi
callq 0xa655
movslq (%rbx), %rax
movl %ebp, (%r15,%rax,4)
incl %eax
movl %eax, (%rbx)
incq %r14
movslq 0x2c(%r12), %rcx
cmpq %rcx, %r14
jl 0x1bf23
movq 0x18(%r12), %rdi
callq 0x4380
movq $0x0, 0x18(%r12)
movl 0x4(%rsp), %r14d
leaq 0x1b7743c(%rip), %rbp   # 0x1b933b4
movq 0x8(%rsp), %r15
jmp 0x1bd7a
movl 0x24(%rax), %ebp
jmp 0x1befb
movzwl (%rbp), %eax
movzwl 0x14(%r13), %ecx
subl %ecx, %eax
movw %ax, 0x16(%r13)
movq 0x10(%rsp), %rbx
movq 0x88(%rbx), %rdi
movq %r15, %rsi
callq 0x1bb75
movw %ax, 0x4(%r13)
movq 0x90(%rbx), %rdi
movq %r15, %rsi
callq 0x1bb75
movw %ax, 0x6(%r13)
jmp 0x1c1d8
leaq 0xa174a1(%rip), %rbx    # 0xa33474
movl (%rbx), %ebp
xorl %edi, %edi
cmpl $0x7ff8, %ebp           # imm = 0x7FF8
setl %dil
movl $0x35, %esi
callq 0xa655
movslq (%rbx), %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
leaq 0xa17480(%rip), %r13    # 0xa33480
incl %ecx
movl %ecx, (%rbx)
movl 0xa0(%r15), %ecx
movl %ecx, (%r13,%rax)
cmpb $0x1, 0x48(%r12)
jne 0x1c056
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 0x1c072
movupd 0x50(%r12), %xmm0
movsd 0x60(%r12), %xmm1
movupd 0x68(%r12), %xmm2
movsd 0x78(%r12), %xmm3
movl %ebp, 0x4(%rsp)
addq %rax, %r13
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
cvttsd2si 0x40(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x1c0b1
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0x8(%r13,%rax,2)
cvttsd2si 0x20(%rsp,%rax,8), %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
cmpl %edx, %esi
jge 0x1c0cb
movl $0xffff8001, %esi       # imm = 0xFFFF8001
movw %si, 0xe(%r13,%rax,2)
incq %rax
cmpq $0x3, %rax
jne 0x1c09d
movl $0xffffffff, 0x4(%r13)  # imm = 0xFFFFFFFF
leaq 0x1df12db(%rip), %rbp   # 0x1e0d3c4
movzwl (%rbp), %eax
movw %ax, 0x14(%r13)
xorl %edi, %edi
cmpq $0x0, 0xa8(%r12)
setne %dil
movl $0x16, %esi
callq 0xa655
movq 0xa8(%r12), %r15
movq (%r15), %r14
testq %r14, %r14
je 0x1c1ba
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
leaq 0x6f45(%rip), %r12      # 0x23075
leaq 0x1df1299(%rip), %rbx   # 0x1e0d3d0
cmpl $-0x1, 0x28(%r14)
je 0x1c193
movl 0xc(%r14), %edi
callq 0x62ce
movq %rax, %r13
movq %rax, %rdi
callq 0x4140
cmpq $0x7, %rax
jb 0x1c16c
leaq (%rax,%r13), %rdi
addq $-0x7, %rdi
movq %r12, %rsi
callq 0x4320
testl %eax, %eax
je 0x1c193
movzwl 0x28(%r14), %eax
movslq (%rbp), %rcx
xorl %edi, %edi
cmpq $0xffff, %rcx           # imm = 0xFFFF
setl %dil
movw %ax, (%rbx,%rcx,2)
movl $0x2f, %esi
callq 0xa655
incl (%rbp)
movq 0x20(%r14), %r14
testq %r14, %r14
jne 0x1c137
movq 0x8(%r15), %r14
addq $0x8, %r15
testq %r14, %r14
jne 0x1c137
movq 0x10(%rsp), %rax
movq 0xa8(%rax), %r15
movq 0x8(%rsp), %r13
movq %r15, %rdi
callq 0x4380
movzwl (%rbp), %eax
subw 0x14(%r13), %ax
movw %ax, 0x16(%r13)
movl 0x4(%rsp), %r14d
notl %r14d
movl %r14d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq