// File: FreeSlave[P]vhlt/hlbsp/writebsp.cpp
// Function: WriteFace(face_s*)
static void     WriteFace(face_t* f)
{
    dface_t*        df;
    int             i;
    int             e;

    if (    CheckFaceForHint(f)
        ||  CheckFaceForSkip(f)
#ifdef ZHLT_NULLTEX
        ||  CheckFaceForNull(f)  // AJM
#endif
#ifdef HLCSG_HLBSP_SOLIDHINT
		|| CheckFaceForDiscardable (f)
#endif
#ifdef HLCSG_HLBSP_VOIDTEXINFO
		|| f->texturenum == -1
#endif
#ifdef HLBSP_REMOVECOVEREDFACES
		|| f->referenced == 0 // this face is not referenced by any nonsolid leaf because it is completely covered by func_details
#endif

// =====================================================================================
//Cpt_Andrew - Env_Sky Check
// =====================================================================================
       ||  CheckFaceForEnv_Sky(f)
// =====================================================================================

       )
    {
#ifdef HLBSP_NULLFACEOUTPUT_FIX
		f->outputnumber = -1;
#endif
        return;
    }

    f->outputnumber = g_numfaces;

    df = &g_dfaces[g_numfaces];
    hlassume(g_numfaces < MAX_MAP_FACES, assume_MAX_MAP_FACES);
    g_numfaces++;

	df->planenum = WritePlane(f->planenum);
	df->side = f->planenum & 1;
    df->firstedge = g_numsurfedges;
    df->numedges = f->numpoints;
#ifdef HLCSG_HLBSP_REDUCETEXTURE
	df->texinfo = WriteTexinfo (f->texturenum);
#else
    df->texinfo = f->texturenum;
#endif
    for (i = 0; i < f->numpoints; i++)
    {
#ifdef ZHLT_DETAILBRUSH
		e = f->outputedges[i];
#else
        e = GetEdge(f->pts[i], f->pts[(i + 1) % f->numpoints], f);
#endif
        hlassume(g_numsurfedges < MAX_MAP_SURFEDGES, assume_MAX_MAP_SURFEDGES);
        g_dsurfedges[g_numsurfedges] = e;
        g_numsurfedges++;
    }
#ifdef ZHLT_DETAILBRUSH
	free (f->outputedges);
	f->outputedges = NULL;
#endif
}

// Assembly:
subq $0x18, %rsp
movq %rdi, 0x10(%rsp)
movq 0x10(%rsp), %rdi
callq 0x25d50
testb $0x1, %al
jne 0x34b87
movq 0x10(%rsp), %rdi
callq 0x25da0
testb $0x1, %al
jne 0x34b87
movq 0x10(%rsp), %rdi
callq 0x25c60
testb $0x1, %al
jne 0x34b87
movq 0x10(%rsp), %rdi
callq 0x25df0
testb $0x1, %al
jne 0x34b87
movq 0x10(%rsp), %rax
cmpl $-0x1, 0xc(%rax)
je 0x34b87
movq 0x10(%rsp), %rax
cmpl $0x0, 0x34(%rax)
je 0x34b87
movq 0x10(%rsp), %rdi
callq 0x25d00
testb $0x1, %al
jne 0x34b87
jmp 0x34b98
movq 0x10(%rsp), %rax
movl $0xffffffff, 0x28(%rax) # imm = 0xFFFFFFFF
jmp 0x34d06
leaq 0x1b7a825(%rip), %rax   # 0x1baf3c4
movl (%rax), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x28(%rax)
leaq 0x1b7a814(%rip), %rax   # 0x1baf3c4
movslq (%rax), %rcx
leaq 0x1b7a816(%rip), %rax   # 0x1baf3d0
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x8(%rsp)
leaq 0x1b7a7f7(%rip), %rax   # 0x1baf3c4
cmpl $0xffff, (%rax)         # imm = 0xFFFF
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x30, %esi
callq 0xbe90
leaq 0x1b7a7d7(%rip), %rax   # 0x1baf3c4
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x1b7a7cb(%rip), %rax   # 0x1baf3c4
movl %ecx, (%rax)
movq 0x10(%rsp), %rax
movl 0x8(%rax), %edi
callq 0x34380
movw %ax, %cx
movq 0x8(%rsp), %rax
movw %cx, (%rax)
movq 0x10(%rsp), %rax
movl 0x8(%rax), %eax
andl $0x1, %eax
movw %ax, %cx
movq 0x8(%rsp), %rax
movw %cx, 0x2(%rax)
leaq 0x1e147b3(%rip), %rax   # 0x1e493e4
movl (%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x10(%rsp), %rax
movl 0x2c(%rax), %eax
movw %ax, %cx
movq 0x8(%rsp), %rax
movw %cx, 0x8(%rax)
movq 0x10(%rsp), %rax
movl 0xc(%rax), %edi
callq 0x34d10
movw %ax, %cx
movq 0x8(%rsp), %rax
movw %cx, 0xa(%rax)
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
movq 0x10(%rsp), %rcx
cmpl 0x2c(%rcx), %eax
jge 0x34ceb
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movslq 0x4(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, (%rsp)
leaq 0x1e1474b(%rip), %rax   # 0x1e493e4
cmpl $0x7d000, (%rax)        # imm = 0x7D000
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x31, %esi
callq 0xbe90
movl (%rsp), %edx
leaq 0x1e14728(%rip), %rax   # 0x1e493e4
movslq (%rax), %rcx
leaq 0x1e1472a(%rip), %rax   # 0x1e493f0
movl %edx, (%rax,%rcx,4)
leaq 0x1e14714(%rip), %rax   # 0x1e493e4
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x1e14708(%rip), %rax   # 0x1e493e4
movl %ecx, (%rax)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x34c70
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x4370
movq 0x10(%rsp), %rax
movq $0x0, 0x18(%rax)
addq $0x18, %rsp
retq
nopl (%rax,%rax)