// File: FreeSlave[P]vhlt/common/bspfile.cpp
// Function: LoadBSPImage(dheader_t*)
void            LoadBSPImage(dheader_t* const header)
{
    unsigned int     i;

    // swap the header
    for (i = 0; i < sizeof(dheader_t) / 4; i++)
    {
        ((int*)header)[i] = LittleLong(((int*)header)[i]);
    }

    if (header->version != BSPVERSION)
    {
        Error("BSP is version %i, not %i", header->version, BSPVERSION);
    }

    g_nummodels = CopyLump(LUMP_MODELS, g_dmodels, sizeof(dmodel_t), header);
    g_numvertexes = CopyLump(LUMP_VERTEXES, g_dvertexes, sizeof(dvertex_t), header);
    g_numplanes = CopyLump(LUMP_PLANES, g_dplanes, sizeof(dplane_t), header);
    g_numleafs = CopyLump(LUMP_LEAFS, g_dleafs, sizeof(dleaf_t), header);
    g_numnodes = CopyLump(LUMP_NODES, g_dnodes, sizeof(dnode_t), header);
    g_numtexinfo = CopyLump(LUMP_TEXINFO, g_texinfo, sizeof(texinfo_t), header);
#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;
		}
		g_numclipnodes[hull - 1] = CopyLump(lump, g_dclipnodes[hull - 1], sizeof(dclipnode_t), header);
	}
#else
    g_numclipnodes = CopyLump(LUMP_CLIPNODES, g_dclipnodes, sizeof(dclipnode_t), header);
#endif
    g_numfaces = CopyLump(LUMP_FACES, g_dfaces, sizeof(dface_t), header);
    g_nummarksurfaces = CopyLump(LUMP_MARKSURFACES, g_dmarksurfaces, sizeof(g_dmarksurfaces[0]), header);
    g_numsurfedges = CopyLump(LUMP_SURFEDGES, g_dsurfedges, sizeof(g_dsurfedges[0]), header);
    g_numedges = CopyLump(LUMP_EDGES, g_dedges, sizeof(dedge_t), header);
    g_texdatasize = CopyLump(LUMP_TEXTURES, g_dtexdata, 1, header);
    g_visdatasize = CopyLump(LUMP_VISIBILITY, g_dvisdata, 1, header);
    g_lightdatasize = CopyLump(LUMP_LIGHTING, g_dlightdata, 1, header);
    g_entdatasize = CopyLump(LUMP_ENTITIES, g_dentdata, 1, header);

    Free(header);                                          // everything has been copied out

    //
    // swap everything
    //      
    SwapBSPFile(false);

    g_dmodels_checksum = FastChecksum(g_dmodels, g_nummodels * sizeof(g_dmodels[0]));
    g_dvertexes_checksum = FastChecksum(g_dvertexes, g_numvertexes * sizeof(g_dvertexes[0]));
    g_dplanes_checksum = FastChecksum(g_dplanes, g_numplanes * sizeof(g_dplanes[0]));
    g_dleafs_checksum = FastChecksum(g_dleafs, g_numleafs * sizeof(g_dleafs[0]));
    g_dnodes_checksum = FastChecksum(g_dnodes, g_numnodes * sizeof(g_dnodes[0]));
    g_texinfo_checksum = FastChecksum(g_texinfo, g_numtexinfo * sizeof(g_texinfo[0]));
#ifdef ZHLT_XASH2
	for (int hull = 1; hull < MAX_MAP_HULLS; hull++)
	{
		g_dclipnodes_checksum[hull - 1] = FastChecksum(g_dclipnodes[hull - 1], g_numclipnodes[hull - 1] * sizeof(g_dclipnodes[hull - 1][0]));
	}
#else
    g_dclipnodes_checksum = FastChecksum(g_dclipnodes, g_numclipnodes * sizeof(g_dclipnodes[0]));
#endif
    g_dfaces_checksum = FastChecksum(g_dfaces, g_numfaces * sizeof(g_dfaces[0]));
    g_dmarksurfaces_checksum = FastChecksum(g_dmarksurfaces, g_nummarksurfaces * sizeof(g_dmarksurfaces[0]));
    g_dsurfedges_checksum = FastChecksum(g_dsurfedges, g_numsurfedges * sizeof(g_dsurfedges[0]));
    g_dedges_checksum = FastChecksum(g_dedges, g_numedges * sizeof(g_dedges[0]));
    g_dtexdata_checksum = FastChecksum(g_dtexdata, g_numedges * sizeof(g_dtexdata[0]));
    g_dvisdata_checksum = FastChecksum(g_dvisdata, g_visdatasize * sizeof(g_dvisdata[0]));
    g_dlightdata_checksum = FastChecksum(g_dlightdata, g_lightdatasize * sizeof(g_dlightdata[0]));
    g_dentdata_checksum = FastChecksum(g_dentdata, g_entdatasize * sizeof(g_dentdata[0]));
}

// Assembly:
subq $0x18, %rsp
movq %rdi, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpq $0x1f, %rax
jae 0x4e47
movq 0x10(%rsp), %rax
movl 0xc(%rsp), %ecx
movl (%rax,%rcx,4), %edi
callq 0x9980
movl %eax, %edx
movq 0x10(%rsp), %rax
movl 0xc(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x4e11
movq 0x10(%rsp), %rax
cmpl $0x1e, (%rax)
je 0x4e6b
movq 0x10(%rsp), %rax
movl (%rax), %esi
leaq 0x25231(%rip), %rdi     # 0x2a090
movl $0x1e, %edx
movb $0x0, %al
callq 0xba80
movq 0x10(%rsp), %rcx
movl $0xe, %edi
leaq 0x32674(%rip), %rsi     # 0x374f0
movl $0x40, %edx
callq 0x5240
movl %eax, 0x32654(%rip)     # 0x374e0
movq 0x10(%rsp), %rcx
movl $0x3, %edi
leaq 0x101a5e3(%rip), %rsi   # 0x101f480
movl $0xc, %edx
callq 0x5240
movl %eax, 0x101a5c7(%rip)   # 0x101f474
movq 0x10(%rsp), %rcx
movl $0x1, %edi
leaq 0xb1a5b2(%rip), %rsi    # 0xb1f470
movl $0x14, %edx
callq 0x5240
movl %eax, 0xb1a596(%rip)    # 0xb1f464
movq 0x10(%rsp), %rcx
movl $0xa, %edi
leaq 0xa3a661(%rip), %rsi    # 0xa3f540
movl $0x1c, %edx
callq 0x5240
movl %eax, 0xa3a645(%rip)    # 0xa3f534
movq 0x10(%rsp), %rcx
movl $0x5, %edi
leaq 0x10da580(%rip), %rsi   # 0x10df480
movl $0x18, %edx
callq 0x5240
movl %eax, 0x10da568(%rip)   # 0x10df478
movq 0x10(%rsp), %rcx
movl $0x6, %edi
leaq 0x119a54f(%rip), %rsi   # 0x119f470
movl $0x28, %edx
callq 0x5240
movl %eax, 0x119a53b(%rip)   # 0x119f46c
movq 0x10(%rsp), %rcx
movl $0x9, %edi
leaq 0x1cda53e(%rip), %rsi   # 0x1cdf480
movl $0x8, %edx
callq 0x5240
movl %eax, 0x1cda51e(%rip)   # 0x1cdf470
movq 0x10(%rsp), %rcx
movl $0x7, %edi
leaq 0x1b9a51d(%rip), %rsi   # 0x1b9f480
movl $0x14, %edx
callq 0x5240
movl %eax, 0x1b9a501(%rip)   # 0x1b9f474
movq 0x10(%rsp), %rcx
movl $0xb, %edi
leaq 0x1e1450c(%rip), %rsi   # 0x1e19490
movl $0x2, %edx
callq 0x5240
movl %eax, 0x1e144f0(%rip)   # 0x1e19484
movq 0x10(%rsp), %rcx
movl $0xd, %edi
leaq 0x1e344fb(%rip), %rsi   # 0x1e394a0
movl $0x4, %edx
callq 0x5240
movl %eax, 0x1e344df(%rip)   # 0x1e39494
movq 0x10(%rsp), %rcx
movl $0xc, %edi
leaq 0x1d1a4ba(%rip), %rsi   # 0x1d1f480
movl $0x4, %edx
callq 0x5240
movl %eax, 0x1d1a4a6(%rip)   # 0x1d1f47c
movq 0x83a53b(%rip), %rsi    # 0x83f518
movq 0x10(%rsp), %rcx
movl $0x2, %edi
movl $0x1, %edx
callq 0x5240
movl %eax, 0x83a51d(%rip)    # 0x83f514
movq 0x10(%rsp), %rcx
movl $0x4, %edi
leaq 0x3a4f8(%rip), %rsi     # 0x3f500
movl $0x1, %edx
callq 0x5240
movl %eax, 0x3a4dc(%rip)     # 0x3f4f4
movq 0x83a4e9(%rip), %rsi    # 0x83f508
movq 0x10(%rsp), %rcx
movl $0x8, %edi
movl $0x1, %edx
callq 0x5240
movl %eax, 0x83a4cb(%rip)    # 0x83f504
movq 0x10(%rsp), %rcx
xorl %edi, %edi
leaq 0x83a4e9(%rip), %rsi    # 0x83f530
movl $0x1, %edx
callq 0x5240
movl %eax, 0x83a4cd(%rip)    # 0x83f524
movq 0x10(%rsp), %rdi
callq 0x4ab0
xorl %edi, %edi
callq 0x5330
movslq 0x32471(%rip), %rax     # 0x374e0
shlq $0x6, %rax
movl %eax, %esi
leaq 0x32474(%rip), %rdi     # 0x374f0
callq 0x5e00
movl %eax, 0x3a469(%rip)     # 0x3f4f0
movslq 0x101a3e6(%rip), %rax   # 0x101f474
imulq $0xc, %rax, %rax
movl %eax, %esi
leaq 0x101a3e5(%rip), %rdi   # 0x101f480
callq 0x5e00
movl %eax, 0x10da3ce(%rip)   # 0x10df474
movslq 0xb1a3b7(%rip), %rax    # 0xb1f464
imulq $0x14, %rax, %rax
movl %eax, %esi
leaq 0xb1a3b6(%rip), %rdi    # 0xb1f470
callq 0x5e00
movl %eax, 0x101a3ab(%rip)   # 0x101f470
movslq 0xa3a468(%rip), %rax    # 0xa3f534
imulq $0x1c, %rax, %rax
movl %eax, %esi
leaq 0xa3a467(%rip), %rdi    # 0xa3f540
callq 0x5e00
movl %eax, 0xb1a37c(%rip)    # 0xb1f460
movslq 0x10da38d(%rip), %rax   # 0x10df478
imulq $0x18, %rax, %rax
movl %eax, %esi
leaq 0x10da388(%rip), %rdi   # 0x10df480
callq 0x5e00
movl %eax, 0x119a365(%rip)   # 0x119f468
movslq 0x119a362(%rip), %rax   # 0x119f46c
imulq $0x28, %rax, %rax
movl %eax, %esi
leaq 0x119a359(%rip), %rdi   # 0x119f470
callq 0x5e00
movl %eax, 0x1b9a34e(%rip)   # 0x1b9f470
movslq 0x1cda347(%rip), %rax   # 0x1cdf470
shlq $0x3, %rax
movl %eax, %esi
leaq 0x1cda34a(%rip), %rdi   # 0x1cdf480
callq 0x5e00
movl %eax, 0x1d1a337(%rip)   # 0x1d1f478
movslq 0x1b9a32c(%rip), %rax   # 0x1b9f474
imulq $0x14, %rax, %rax
movl %eax, %esi
leaq 0x1b9a32b(%rip), %rdi   # 0x1b9f480
callq 0x5e00
movl %eax, 0x1cda30c(%rip)   # 0x1cdf46c
movslq 0x1e1431d(%rip), %rax   # 0x1e19484
shlq %rax
movl %eax, %esi
leaq 0x1e1431d(%rip), %rdi   # 0x1e19490
callq 0x5e00
movl %eax, 0x1e34312(%rip)   # 0x1e39490
movslq 0x1e3430f(%rip), %rax   # 0x1e39494
shlq $0x2, %rax
movl %eax, %esi
leaq 0x1e3430e(%rip), %rdi   # 0x1e394a0
callq 0x5e00
movl %eax, 0x2028303(%rip)   # 0x202d4a0
movslq 0x1d1a2d8(%rip), %rax   # 0x1d1f47c
shlq $0x2, %rax
movl %eax, %esi
leaq 0x1d1a2cf(%rip), %rdi   # 0x1d1f480
callq 0x5e00
movl %eax, 0x1e142c4(%rip)   # 0x1e19480
movq 0x83a355(%rip), %rdi    # 0x83f518
movslq 0x1d1a2b2(%rip), %rax   # 0x1d1f47c
shlq $0x0, %rax
movl %eax, %esi
callq 0x5e00
movl %eax, 0x83a345(%rip)    # 0x83f520
movslq 0x3a312(%rip), %rax     # 0x3f4f4
shlq $0x0, %rax
movl %eax, %esi
leaq 0x3a311(%rip), %rdi     # 0x3f500
callq 0x5e00
movl %eax, 0x83a306(%rip)    # 0x83f500
movq 0x83a307(%rip), %rdi    # 0x83f508
movslq 0x83a2fc(%rip), %rax    # 0x83f504
shlq $0x0, %rax
movl %eax, %esi
callq 0x5e00
movl %eax, 0x83a2f7(%rip)    # 0x83f510
movslq 0x83a304(%rip), %rax    # 0x83f524
shlq $0x0, %rax
movl %eax, %esi
leaq 0x83a303(%rip), %rdi    # 0x83f530
callq 0x5e00
movl %eax, 0xa3a2f8(%rip)    # 0xa3f530
addq $0x18, %rsp
retq
nopl (%rax)