// File: FreeSlave[P]vhlt/common/bspfile.cpp
// Function: WriteBSPFile(char const*)
void            WriteBSPFile(const char* const filename)
{
    dheader_t       outheader;
    dheader_t*      header;
    FILE*           bspfile;

    header = &outheader;
    memset(header, 0, sizeof(dheader_t));

    SwapBSPFile(true);

    header->version = LittleLong(BSPVERSION);

    bspfile = SafeOpenWrite(filename);
    SafeWrite(bspfile, header, sizeof(dheader_t));         // overwritten later

    //      LUMP TYPE       DATA            LENGTH                              HEADER  BSPFILE   
    AddLump(LUMP_PLANES,    g_dplanes,      g_numplanes * sizeof(dplane_t),     header, bspfile);
    AddLump(LUMP_LEAFS,     g_dleafs,       g_numleafs * sizeof(dleaf_t),       header, bspfile);
    AddLump(LUMP_VERTEXES,  g_dvertexes,    g_numvertexes * sizeof(dvertex_t),  header, bspfile);
    AddLump(LUMP_NODES,     g_dnodes,       g_numnodes * sizeof(dnode_t),       header, bspfile);
    AddLump(LUMP_TEXINFO,   g_texinfo,      g_numtexinfo * sizeof(texinfo_t),   header, bspfile);
    AddLump(LUMP_FACES,     g_dfaces,       g_numfaces * sizeof(dface_t),       header, bspfile);
#ifdef ZHLT_XASH2
	for (int hull = 1; hull < MAX_MAP_HULLS; hull++)
	{
		int lump;
		switch (hull)
		{
		case 1: lump = LUMP_CLIPNODES; break;
		case 2: lump = LUMP_CLIPNODES2; break;
		case 3: lump = LUMP_CLIPNODES3; break;
		default:
			Error ("bad hull number %d", hull);
			break;
		}
		AddLump(lump, g_dclipnodes[hull - 1],   g_numclipnodes[hull - 1] * sizeof(dclipnode_t), header, bspfile);
	}
#else
    AddLump(LUMP_CLIPNODES, g_dclipnodes,   g_numclipnodes * sizeof(dclipnode_t), header, bspfile);
#endif

    AddLump(LUMP_MARKSURFACES, g_dmarksurfaces, g_nummarksurfaces * sizeof(g_dmarksurfaces[0]), header, bspfile);
    AddLump(LUMP_SURFEDGES, g_dsurfedges,   g_numsurfedges * sizeof(g_dsurfedges[0]), header, bspfile);
    AddLump(LUMP_EDGES,     g_dedges,       g_numedges * sizeof(dedge_t),       header, bspfile);
    AddLump(LUMP_MODELS,    g_dmodels,      g_nummodels * sizeof(dmodel_t),     header, bspfile);

    AddLump(LUMP_LIGHTING,  g_dlightdata,   g_lightdatasize,                    header, bspfile);
    AddLump(LUMP_VISIBILITY,g_dvisdata,     g_visdatasize,                      header, bspfile);
    AddLump(LUMP_ENTITIES,  g_dentdata,     g_entdatasize,                      header, bspfile);
    AddLump(LUMP_TEXTURES,  g_dtexdata,     g_texdatasize,                      header, bspfile);

    fseek(bspfile, 0, SEEK_SET);
    SafeWrite(bspfile, header, sizeof(dheader_t));

    fclose(bspfile);
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
leaq 0x14(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movl $0x7c, %edx
callq 0x4210
movl $0x1, %edi
callq 0x5330
movl $0x1e, %edi
callq 0x9980
movl %eax, %ecx
movq 0x8(%rsp), %rax
movl %ecx, (%rax)
movq 0x90(%rsp), %rdi
callq 0xb020
movq %rax, (%rsp)
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movl $0x7c, %edx
callq 0xb130
movslq 0xb19582(%rip), %rax    # 0xb1f464
imulq $0x14, %rax, %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0x1, %edi
leaq 0xb19573(%rip), %rsi    # 0xb1f470
callq 0x6140
movslq 0xa3962b(%rip), %rax    # 0xa3f534
imulq $0x1c, %rax, %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0xa, %edi
leaq 0xa3961c(%rip), %rsi    # 0xa3f540
callq 0x6140
movslq 0x1019544(%rip), %rax   # 0x101f474
imulq $0xc, %rax, %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0x3, %edi
leaq 0x1019535(%rip), %rsi   # 0x101f480
callq 0x6140
movslq 0x10d9521(%rip), %rax   # 0x10df478
imulq $0x18, %rax, %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0x5, %edi
leaq 0x10d950e(%rip), %rsi   # 0x10df480
callq 0x6140
movslq 0x11994ee(%rip), %rax   # 0x119f46c
imulq $0x28, %rax, %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0x6, %edi
leaq 0x11994d7(%rip), %rsi   # 0x119f470
callq 0x6140
movslq 0x1b994cf(%rip), %rax   # 0x1b9f474
imulq $0x14, %rax, %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0x7, %edi
leaq 0x1b994c0(%rip), %rsi   # 0x1b9f480
callq 0x6140
movslq 0x1cd94a4(%rip), %rax   # 0x1cdf470
shlq $0x3, %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0x9, %edi
leaq 0x1cd9499(%rip), %rsi   # 0x1cdf480
callq 0x6140
movslq 0x1e13491(%rip), %rax   # 0x1e19484
shlq %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0xb, %edi
leaq 0x1e13483(%rip), %rsi   # 0x1e19490
callq 0x6140
movslq 0x1e3347b(%rip), %rax   # 0x1e39494
shlq $0x2, %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0xd, %edi
leaq 0x1e3346c(%rip), %rsi   # 0x1e394a0
callq 0x6140
movslq 0x1d1943c(%rip), %rax   # 0x1d1f47c
shlq $0x2, %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0xc, %edi
leaq 0x1d19425(%rip), %rsi   # 0x1d1f480
callq 0x6140
movslq 0x31479(%rip), %rax     # 0x374e0
shlq $0x6, %rax
movl %eax, %edx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0xe, %edi
leaq 0x3146e(%rip), %rsi     # 0x374f0
callq 0x6140
movq 0x83947a(%rip), %rsi    # 0x83f508
movl 0x839470(%rip), %edx    # 0x83f504
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0x8, %edi
callq 0x6140
movl 0x39447(%rip), %edx     # 0x3f4f4
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0x4, %edi
leaq 0x3943e(%rip), %rsi     # 0x3f500
callq 0x6140
movl 0x839457(%rip), %edx    # 0x83f524
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
xorl %edi, %edi
leaq 0x839451(%rip), %rsi    # 0x83f530
callq 0x6140
movq 0x83942d(%rip), %rsi    # 0x83f518
movl 0x839423(%rip), %edx    # 0x83f514
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0x2, %edi
callq 0x6140
movq (%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x4440
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movl $0x7c, %edx
callq 0xb130
movq (%rsp), %rdi
callq 0x43a0
addq $0x98, %rsp
retq
nopw (%rax,%rax)