// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
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)
andq $0x0, 0x74(%rbx)
pushq $0x1
popq %r15
movl %r15d, %edi
callq 0x4ea7
pushq $0x1e
popq %rdi
callq 0x78bc
movl %eax, (%rbx)
movq %r14, %rdi
callq 0x852f
movq %rax, %r14
pushq $0x7c
popq %rbp
movq %rax, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x85e9
imull $0x14, 0xb0a022(%rip), %edx # 0xb0f414
leaq 0xb0a027(%rip), %rsi    # 0xb0f420
movl %r15d, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
imull $0x1c, 0xa2a0d6(%rip), %edx # 0xa2f4e4
leaq 0xa2a0db(%rip), %rsi    # 0xa2f4f0
pushq $0xa
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
imull $0xc, 0x1009ffa(%rip), %edx # 0x100f424
leaq 0x1009fff(%rip), %rsi   # 0x100f430
pushq $0x3
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
imull $0x18, 0x10c9fe2(%rip), %edx # 0x10cf428
leaq 0x10c9fe3(%rip), %rsi   # 0x10cf430
pushq $0x5
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
imull $0x28, 0x1189fba(%rip), %edx # 0x118f41c
leaq 0x1189fb7(%rip), %rsi   # 0x118f420
pushq $0x6
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
imull $0x14, 0x1b89fa6(%rip), %edx # 0x1b8f424
leaq 0x1b89fab(%rip), %rsi   # 0x1b8f430
pushq $0x7
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
movl 0x1cc9f87(%rip), %edx   # 0x1ccf420
shll $0x3, %edx
leaq 0x1cc9f8d(%rip), %rsi   # 0x1ccf430
pushq $0x9
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
movl 0x1e03f7d(%rip), %edx   # 0x1e09434
addl %edx, %edx
leaq 0x1e03f80(%rip), %rsi   # 0x1e09440
pushq $0xb
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
movl 0x1e23f70(%rip), %edx   # 0x1e29444
shll $0x2, %edx
leaq 0x1e23f72(%rip), %rsi   # 0x1e29450
pushq $0xd
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
movl 0x1d09f3a(%rip), %edx   # 0x1d0f42c
shll $0x2, %edx
leaq 0x1d09f34(%rip), %rsi   # 0x1d0f430
pushq $0xc
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
movl 0x21f80(%rip), %edx     # 0x27490
shll $0x6, %edx
leaq 0x21f86(%rip), %rsi     # 0x274a0
pushq $0xe
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
movq 0x829f89(%rip), %rsi    # 0x82f4b8
movl 0x829f7f(%rip), %edx    # 0x82f4b4
pushq $0x8
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
movl 0x29f5b(%rip), %edx     # 0x2f4a4
leaq 0x29f60(%rip), %rsi     # 0x2f4b0
pushq $0x4
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
movl 0x829f70(%rip), %edx    # 0x82f4d4
leaq 0x829f75(%rip), %rsi    # 0x82f4e0
xorl %edi, %edi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
movq 0x829f49(%rip), %rsi    # 0x82f4c8
movl 0x829f3f(%rip), %edx    # 0x82f4c4
pushq $0x2
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
callq 0x55c2
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4370
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x85e9
movq %r14, %rdi
callq 0x4300
addq $0x88, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq