// File: FreeSlave[P]vhlt/hlbsp/writebsp.cpp
// Function: WriteDrawLeaf(node_s*, node_s const*)
static void     WriteDrawLeaf(const node_t* const node)
#endif
{
    face_t**        fp;
    face_t*         f;
    dleaf_t*        leaf_p;
#ifdef ZHLT_DETAILBRUSH
	int				leafnum = g_numleafs;
#endif

    // emit a leaf
#ifdef ZHLT_MAX_MAP_LEAFS
	hlassume (g_numleafs < MAX_MAP_LEAFS, assume_MAX_MAP_LEAFS);
#endif
    leaf_p = &g_dleafs[g_numleafs];
    g_numleafs++;

#ifdef ZHLT_DETAILBRUSH
	leaf_p->contents = portalleaf->contents;
#else
    leaf_p->contents = node->contents;
#endif

    //
    // write bounding box info
    //
#ifdef ZHLT_DETAILBRUSH
#ifdef HLBSP_DETAILBRUSH_CULL
	vec3_t mins, maxs;
#if 0
	printf ("leaf 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)
	{
		// 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 (node->mins, mins);
		VectorCopy (node->maxs, maxs);
	}
#ifdef ZHLT_LARGERANGE
	for (int k = 0; k < 3; k++)
	{
		leaf_p->mins[k] = (short)qmax (-32767, qmin ((int)mins[k], 32767));
		leaf_p->maxs[k] = (short)qmax (-32767, qmin ((int)maxs[k], 32767));
	}
#else
	VectorCopy (mins, leaf_p->mins);
	VectorCopy (maxs, leaf_p->maxs);
#endif
#else
#ifdef ZHLT_LARGERANGE
	for (int k = 0; k < 3; k++)
	{
		leaf_p->mins[k] = (short)qmax (-32767, qmin ((int)portalleaf->mins[k], 32767));
		leaf_p->maxs[k] = (short)qmax (-32767, qmin ((int)portalleaf->maxs[k], 32767));
	}
#else
	VectorCopy (portalleaf->mins, leaf_p->mins);
	VectorCopy (portalleaf->maxs, leaf_p->maxs);
#endif
#endif
#else
#ifdef ZHLT_LARGERANGE
	for (int k = 0; k < 3; k++)
	{
		leaf_p->mins[k] = (short)qmax (-32767, qmin ((int)node->mins[k], 32767));
		leaf_p->maxs[k] = (short)qmax (-32767, qmin ((int)node->maxs[k], 32767));
	}
#else
    VectorCopy(node->mins, leaf_p->mins);
    VectorCopy(node->maxs, leaf_p->maxs);
#endif
#endif

    leaf_p->visofs = -1;                                   // no vis info yet

    //
    // write the marksurfaces
    //
    leaf_p->firstmarksurface = g_nummarksurfaces;

    hlassume(node->markfaces != NULL, assume_EmptySolid);

    for (fp = node->markfaces; *fp; fp++)
    {
        // emit a marksurface
        f = *fp;
        do
        {
#ifdef HLBSP_NULLFACEOUTPUT_FIX
			// fix face 0 being seen everywhere
			if (f->outputnumber == -1)
			{
				f = f->original;
				continue;
			}
#endif
#if defined(HLBSP_HIDDENFACE) || defined(ZHLT_HIDDENSOUNDTEXTURE)
			bool ishidden = false;
			{
				const char *name = GetTextureByNumber (f->texturenum);
				if (strlen (name) >= 7 && !strcasecmp (&name[strlen (name) - 7], "_HIDDEN"))
				{
					ishidden = true;
				}
#ifdef ZHLT_HIDDENSOUNDTEXTURE
				if (f->texturenum >= 0 && (g_texinfo[f->texturenum].flags & TEX_SHOULDHIDE))
				{
					ishidden = true;
				}
#endif
			}
			if (ishidden)
			{
				f = f->original;
				continue;
			}
#endif
            g_dmarksurfaces[g_nummarksurfaces] = f->outputnumber;
            hlassume(g_nummarksurfaces < MAX_MAP_MARKSURFACES, assume_MAX_MAP_MARKSURFACES);
            g_nummarksurfaces++;
            f = f->original;                               // grab tjunction split faces
        }
        while (f);
    }
    free(node->markfaces);

    leaf_p->nummarksurfaces = g_nummarksurfaces - leaf_p->firstmarksurface;
#ifdef ZHLT_DETAILBRUSH
	return leafnum;
#endif
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
leaq 0xa1afc6(%rip), %rax    # 0xa4f484
movl (%rax), %eax
movl %eax, 0x9c(%rsp)
leaq 0xa1afb6(%rip), %rax    # 0xa4f484
cmpl $0x7ff8, (%rax)         # imm = 0x7FF8
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x35, %esi
callq 0xbe90
leaq 0xa1af96(%rip), %rax    # 0xa4f484
movslq (%rax), %rcx
leaq 0xa1af98(%rip), %rax    # 0xa4f490
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0xa0(%rsp)
leaq 0xa1af76(%rip), %rax    # 0xa4f484
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0xa1af6a(%rip), %rax    # 0xa4f484
movl %ecx, (%rax)
movq 0xb8(%rsp), %rax
movl 0xa0(%rax), %ecx
movq 0xa0(%rsp), %rax
movl %ecx, (%rax)
movq 0xc0(%rsp), %rax
testb $0x1, 0x48(%rax)
je 0x3473d
movq 0xb8(%rsp), %rax
movsd 0x50(%rax), %xmm0
movq 0xc0(%rsp), %rax
ucomisd 0x18(%rax), %xmm0
jbe 0x34577
movq 0xb8(%rsp), %rax
movsd 0x50(%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x3458a
movq 0xc0(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x58(%rax), %xmm0
movq 0xc0(%rsp), %rax
ucomisd 0x20(%rax), %xmm0
jbe 0x345ca
movq 0xb8(%rsp), %rax
movsd 0x58(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0x345dd
movq 0xc0(%rsp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x60(%rax), %xmm0
movq 0xc0(%rsp), %rax
ucomisd 0x28(%rax), %xmm0
jbe 0x3461d
movq 0xb8(%rsp), %rax
movsd 0x60(%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
jmp 0x34630
movq 0xc0(%rsp), %rax
movsd 0x28(%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x68(%rax), %xmm1
movq 0xc0(%rsp), %rax
movsd 0x30(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x34674
movq 0xb8(%rsp), %rax
movsd 0x68(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x34687
movq 0xc0(%rsp), %rax
movsd 0x30(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x70(%rax), %xmm1
movq 0xc0(%rsp), %rax
movsd 0x38(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x346c8
movq 0xb8(%rsp), %rax
movsd 0x70(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0x346db
movq 0xc0(%rsp), %rax
movsd 0x38(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x78(%rax), %xmm1
movq 0xc0(%rsp), %rax
movsd 0x40(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3471c
movq 0xb8(%rsp), %rax
movsd 0x78(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x3472f
movq 0xc0(%rsp), %rax
movsd 0x40(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
jmp 0x347b8
movq 0xc0(%rsp), %rax
movsd 0x50(%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0xc0(%rsp), %rax
movsd 0x58(%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
movq 0xc0(%rsp), %rax
movsd 0x60(%rax), %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0xc0(%rsp), %rax
movsd 0x68(%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0xc0(%rsp), %rax
movsd 0x70(%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0xc0(%rsp), %rax
movsd 0x78(%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movl $0x0, 0x5c(%rsp)
cmpl $0x3, 0x5c(%rsp)
jge 0x34911
movslq 0x5c(%rsp), %rax
cvttsd2si 0x80(%rsp,%rax,8), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x347f4
movslq 0x5c(%rsp), %rax
cvttsd2si 0x80(%rsp,%rax,8), %eax
movl %eax, 0x1c(%rsp)
jmp 0x347ff
movl $0x7fff, %eax           # imm = 0x7FFF
movl %eax, 0x1c(%rsp)
jmp 0x347ff
movl 0x1c(%rsp), %ecx
movl $0xffff8001, %eax       # imm = 0xFFFF8001
cmpl %ecx, %eax
jle 0x34817
movl $0xffff8001, %eax       # imm = 0xFFFF8001
movl %eax, 0x18(%rsp)
jmp 0x34853
movslq 0x5c(%rsp), %rax
cvttsd2si 0x80(%rsp,%rax,8), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x34840
movslq 0x5c(%rsp), %rax
cvttsd2si 0x80(%rsp,%rax,8), %eax
movl %eax, 0x14(%rsp)
jmp 0x3484b
movl $0x7fff, %eax           # imm = 0x7FFF
movl %eax, 0x14(%rsp)
jmp 0x3484b
movl 0x14(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
movw %ax, %dx
movq 0xa0(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movw %dx, 0x8(%rax,%rcx,2)
movslq 0x5c(%rsp), %rax
cvttsd2si 0x60(%rsp,%rax,8), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x3488f
movslq 0x5c(%rsp), %rax
cvttsd2si 0x60(%rsp,%rax,8), %eax
movl %eax, 0x10(%rsp)
jmp 0x3489a
movl $0x7fff, %eax           # imm = 0x7FFF
movl %eax, 0x10(%rsp)
jmp 0x3489a
movl 0x10(%rsp), %ecx
movl $0xffff8001, %eax       # imm = 0xFFFF8001
cmpl %ecx, %eax
jle 0x348b2
movl $0xffff8001, %eax       # imm = 0xFFFF8001
movl %eax, 0xc(%rsp)
jmp 0x348e8
movslq 0x5c(%rsp), %rax
cvttsd2si 0x60(%rsp,%rax,8), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x348d5
movslq 0x5c(%rsp), %rax
cvttsd2si 0x60(%rsp,%rax,8), %eax
movl %eax, 0x8(%rsp)
jmp 0x348e0
movl $0x7fff, %eax           # imm = 0x7FFF
movl %eax, 0x8(%rsp)
jmp 0x348e0
movl 0x8(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movw %ax, %dx
movq 0xa0(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movw %dx, 0xe(%rax,%rcx,2)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x347c0
movq 0xa0(%rsp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
leaq 0x1df4aad(%rip), %rax   # 0x1e293d4
movl (%rax), %eax
movw %ax, %cx
movq 0xa0(%rsp), %rax
movw %cx, 0x14(%rax)
movq 0xc0(%rsp), %rax
cmpq $0x0, 0xa8(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x16, %esi
callq 0xbe90
movq 0xc0(%rsp), %rax
movq 0xa8(%rax), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
cmpq $0x0, (%rax)
je 0x34acf
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
cmpl $-0x1, 0x28(%rax)
jne 0x349be
movq 0xa8(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0xa8(%rsp)
jmp 0x34aa5
movb $0x0, 0x5b(%rsp)
movq 0xa8(%rsp), %rax
movl 0xc(%rax), %edi
callq 0x6950
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
callq 0x4140
cmpq $0x7, %rax
jb 0x34a1c
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq 0x50(%rsp), %rdi
callq 0x4140
movq (%rsp), %rdi
subq $0x7, %rax
addq %rax, %rdi
leaq 0x7770(%rip), %rsi      # 0x3c17d
callq 0x4320
cmpl $0x0, %eax
jne 0x34a1c
movb $0x1, 0x5b(%rsp)
testb $0x1, 0x5b(%rsp)
je 0x34a39
movq 0xa8(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0xa8(%rsp)
jmp 0x34aa5
movq 0xa8(%rsp), %rax
movl 0x28(%rax), %eax
movw %ax, %dx
leaq 0x1df4986(%rip), %rax   # 0x1e293d4
movslq (%rax), %rcx
leaq 0x1df4988(%rip), %rax   # 0x1e293e0
movw %dx, (%rax,%rcx,2)
leaq 0x1df4971(%rip), %rax   # 0x1e293d4
cmpl $0xffff, (%rax)         # imm = 0xFFFF
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x2f, %esi
callq 0xbe90
leaq 0x1df4951(%rip), %rax   # 0x1e293d4
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x1df4945(%rip), %rax   # 0x1e293d4
movl %ecx, (%rax)
movq 0xa8(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0xa8(%rsp)
cmpq $0x0, 0xa8(%rsp)
jne 0x34997
jmp 0x34ab6
movq 0xb0(%rsp), %rax
addq $0x8, %rax
movq %rax, 0xb0(%rsp)
jmp 0x34972
movq 0xc0(%rsp), %rax
movq 0xa8(%rax), %rdi
callq 0x4370
leaq 0x1df48ea(%rip), %rax   # 0x1e293d4
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 0x9c(%rsp), %eax
addq $0xc8, %rsp
retq
nopl (%rax,%rax)