// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %r14
pushq %rbx
subq $0x88, %rsp
movq %rdi, %r14
xorps %xmm0, %xmm0
leaq 0xc(%rsp), %rbx
movups %xmm0, 0x64(%rbx)
movups %xmm0, 0x54(%rbx)
movups %xmm0, 0x44(%rbx)
movups %xmm0, 0x34(%rbx)
movups %xmm0, 0x24(%rbx)
movups %xmm0, 0x14(%rbx)
movups %xmm0, 0x4(%rbx)
movq $0x0, 0x74(%rbx)
movl $0x1, %edi
callq 0x5379
movl $0x1e, %edi
callq 0x8a38
movl %eax, (%rbx)
movq %r14, %rdi
callq 0x99c0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
movl $0x7c, %edx
callq 0x9a7c
movl 0xb0bb0c(%rip), %eax    # 0xb11404
shll $0x2, %eax
leal (%rax,%rax,4), %edx
leaq 0xb0bb0b(%rip), %rsi    # 0xb11410
movl $0x1, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0xa2bbb9(%rip), %eax    # 0xa314d4
leal (%rax,%rax,8), %ecx
leal (%rcx,%rcx,2), %edx
addl %eax, %edx
leaq 0xa2bbb6(%rip), %rsi    # 0xa314e0
movl $0xa, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x100bad4(%rip), %eax   # 0x1011414
shll $0x2, %eax
leal (%rax,%rax,2), %edx
leaq 0x100bad3(%rip), %rsi   # 0x1011420
movl $0x3, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x10cbab5(%rip), %eax   # 0x10d1418
shll $0x3, %eax
leal (%rax,%rax,2), %edx
leaq 0x10cbab0(%rip), %rsi   # 0x10d1420
movl $0x5, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x118ba86(%rip), %eax   # 0x119140c
shll $0x3, %eax
leal (%rax,%rax,4), %edx
leaq 0x118ba7d(%rip), %rsi   # 0x1191410
movl $0x6, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x1b8ba6b(%rip), %eax   # 0x1b91414
shll $0x2, %eax
leal (%rax,%rax,4), %edx
leaq 0x1b8ba6a(%rip), %rsi   # 0x1b91420
movl $0x7, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x1ccba44(%rip), %edx   # 0x1cd1410
shll $0x3, %edx
leaq 0x1ccba4a(%rip), %rsi   # 0x1cd1420
movl $0x9, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x1e05a38(%rip), %edx   # 0x1e0b424
addl %edx, %edx
leaq 0x1e05a3b(%rip), %rsi   # 0x1e0b430
movl $0xb, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x1e25a29(%rip), %edx   # 0x1e2b434
shll $0x2, %edx
leaq 0x1e25a2b(%rip), %rsi   # 0x1e2b440
movl $0xd, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x1d0b9f1(%rip), %edx   # 0x1d1141c
shll $0x2, %edx
leaq 0x1d0b9eb(%rip), %rsi   # 0x1d11420
movl $0xc, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x23a35(%rip), %edx     # 0x29480
shll $0x6, %edx
leaq 0x23a3b(%rip), %rsi     # 0x29490
movl $0xe, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movq 0x82ba3c(%rip), %rsi    # 0x8314a8
movl 0x82ba32(%rip), %edx    # 0x8314a4
movl $0x8, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x2ba0c(%rip), %edx     # 0x31494
leaq 0x2ba11(%rip), %rsi     # 0x314a0
movl $0x4, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movl 0x82ba1f(%rip), %edx    # 0x8314c4
leaq 0x82ba24(%rip), %rsi    # 0x8314d0
xorl %edi, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movq 0x82b9f8(%rip), %rsi    # 0x8314b8
movl 0x82b9ee(%rip), %edx    # 0x8314b4
movl $0x2, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b05
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4350
movq %r14, %rdi
movq %rbx, %rsi
movl $0x7c, %edx
callq 0x9a7c
movq %r14, %rdi
callq 0x42f0
addq $0x88, %rsp
popq %rbx
popq %r14
retq