// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rsp, %rbx
movups %xmm0, 0x6c(%rbx)
movaps %xmm0, 0x60(%rbx)
movaps %xmm0, 0x50(%rbx)
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
movl $0x1, %edi
callq 0x5397
movl $0x1e, %edi
callq 0x8b70
movl %eax, (%rbx)
movq %r14, %rdi
callq 0x9aff
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
movl $0x7c, %edx
callq 0x9bbb
movl 0xb0bb05(%rip), %eax    # 0xb11414
shll $0x2, %eax
leal (%rax,%rax,4), %edx
leaq 0xb0bb04(%rip), %rsi    # 0xb11420
movl $0x1, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0xa2bbb2(%rip), %eax    # 0xa314e4
leal (%rax,%rax,8), %ecx
leal (%rcx,%rcx,2), %edx
addl %eax, %edx
leaq 0xa2bbaf(%rip), %rsi    # 0xa314f0
movl $0xa, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x100bacd(%rip), %eax   # 0x1011424
shll $0x2, %eax
leal (%rax,%rax,2), %edx
leaq 0x100bacc(%rip), %rsi   # 0x1011430
movl $0x3, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x10cbaae(%rip), %eax   # 0x10d1428
shll $0x3, %eax
leal (%rax,%rax,2), %edx
leaq 0x10cbaa9(%rip), %rsi   # 0x10d1430
movl $0x5, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x118ba7f(%rip), %eax   # 0x119141c
shll $0x3, %eax
leal (%rax,%rax,4), %edx
leaq 0x118ba76(%rip), %rsi   # 0x1191420
movl $0x6, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x1b8ba64(%rip), %eax   # 0x1b91424
shll $0x2, %eax
leal (%rax,%rax,4), %edx
leaq 0x1b8ba63(%rip), %rsi   # 0x1b91430
movl $0x7, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x1ccba3d(%rip), %edx   # 0x1cd1420
shll $0x3, %edx
leaq 0x1ccba43(%rip), %rsi   # 0x1cd1430
movl $0x9, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x1e05a31(%rip), %edx   # 0x1e0b434
addl %edx, %edx
leaq 0x1e05a34(%rip), %rsi   # 0x1e0b440
movl $0xb, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x1e25a22(%rip), %edx   # 0x1e2b444
shll $0x2, %edx
leaq 0x1e25a24(%rip), %rsi   # 0x1e2b450
movl $0xd, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x1d0b9ea(%rip), %edx   # 0x1d1142c
shll $0x2, %edx
leaq 0x1d0b9e4(%rip), %rsi   # 0x1d11430
movl $0xc, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x23a2e(%rip), %edx     # 0x29490
shll $0x6, %edx
leaq 0x23a34(%rip), %rsi     # 0x294a0
movl $0xe, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movq 0x82ba35(%rip), %rsi    # 0x8314b8
movl 0x82ba2b(%rip), %edx    # 0x8314b4
movl $0x8, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x2ba05(%rip), %edx     # 0x314a4
leaq 0x2ba0a(%rip), %rsi     # 0x314b0
movl $0x4, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movl 0x82ba18(%rip), %edx    # 0x8314d4
leaq 0x82ba1d(%rip), %rsi    # 0x8314e0
xorl %edi, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movq 0x82b9f1(%rip), %rsi    # 0x8314c8
movl 0x82b9e7(%rip), %edx    # 0x8314c4
movl $0x2, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x5b1c
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4360
movq %r14, %rdi
movq %rbx, %rsi
movl $0x7c, %edx
callq 0x9bbb
movq %r14, %rdi
callq 0x4300
addq $0x88, %rsp
popq %rbx
popq %r14
retq