// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
xorl %r14d, %r14d
cmpq $0x1f, %r14
je 0x4aa1
movl (%rbx,%r14,4), %edi
callq 0x78bc
movl %eax, (%rbx,%r14,4)
incq %r14
jmp 0x4a89
movl (%rbx), %esi
cmpl $0x1e, %esi
je 0x4ab9
leaq 0x145f9(%rip), %rdi     # 0x190a8
pushq $0x1e
popq %rdx
xorl %eax, %eax
callq 0x8d7b
leaq 0x229e0(%rip), %rsi     # 0x274a0
pushq $0xe
popq %rdi
pushq $0x40
popq %rdx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x229bc(%rip)     # 0x27490
leaq 0x100a955(%rip), %rsi   # 0x100f430
pushq $0x3
popq %rdi
pushq $0xc
popq %r15
movl %r15d, %edx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x100a931(%rip)   # 0x100f424
leaq 0xb0a926(%rip), %rsi    # 0xb0f420
pushq $0x1
popq %r12
pushq $0x14
popq %rbp
movl %r12d, %edi
movl %ebp, %edx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0xb0a900(%rip)    # 0xb0f414
leaq 0xa2a9d5(%rip), %rsi    # 0xa2f4f0
pushq $0xa
popq %rdi
pushq $0x1c
popq %rdx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0xa2a9b5(%rip)    # 0xa2f4e4
leaq 0x10ca8fa(%rip), %rsi   # 0x10cf430
pushq $0x5
popq %rdi
pushq $0x18
popq %rdx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x10ca8de(%rip)   # 0x10cf428
leaq 0x118a8cf(%rip), %rsi   # 0x118f420
pushq $0x6
popq %rdi
pushq $0x28
popq %rdx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x118a8b7(%rip)   # 0x118f41c
leaq 0x1cca8c4(%rip), %rsi   # 0x1ccf430
pushq $0x9
popq %rdi
pushq $0x8
popq %r13
movl %r13d, %edx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x1cca89c(%rip)   # 0x1ccf420
leaq 0x1b8a8a5(%rip), %rsi   # 0x1b8f430
pushq $0x7
popq %rdi
movl %ebp, %edx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x1b8a886(%rip)   # 0x1b8f424
leaq 0x1e0489b(%rip), %rsi   # 0x1e09440
pushq $0xb
popq %rdi
pushq $0x2
popq %rbp
movl %ebp, %edx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x1e04879(%rip)   # 0x1e09434
leaq 0x1e2488e(%rip), %rsi   # 0x1e29450
pushq $0xd
popq %rdi
pushq $0x4
popq %r14
movl %r14d, %edx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x1e2486a(%rip)   # 0x1e29444
leaq 0x1d0a84f(%rip), %rsi   # 0x1d0f430
movl %r15d, %edi
movl %r14d, %edx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x1d0a837(%rip)   # 0x1d0f42c
movq 0x82a8cc(%rip), %rsi    # 0x82f4c8
movl %ebp, %edi
movl %r12d, %edx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x82a8b5(%rip)    # 0x82f4c4
movslq 0x28(%rbx), %r14
movslq 0x24(%rbx), %rsi
movq 0x82a89a(%rip), %rbp    # 0x82f4b8
addq %rbx, %rsi
leaq 0x2a888(%rip), %r15     # 0x2f4b0
movq %r15, %rdi
movq %r14, %rdx
callq 0x4280
movl %r14d, 0x2a86a(%rip)    # 0x2f4a4
movl %r13d, %edi
movq %rbp, %rsi
movl %r12d, %edx
movq %rbx, %rcx
callq 0x4e07
movl %eax, 0x82a863(%rip)    # 0x82f4b4
movslq 0x8(%rbx), %r14
movslq 0x4(%rbx), %rsi
addq %rbx, %rsi
leaq 0x82a87d(%rip), %r12    # 0x82f4e0
movq %r12, %rdi
movq %r14, %rdx
callq 0x4280
movl %r14d, 0x82a85f(%rip)   # 0x82f4d4
movq %rbx, %rdi
callq 0x4847
xorl %edi, %edi
callq 0x4ea7
movl 0x22806(%rip), %esi     # 0x27490
shll $0x6, %esi
leaq 0x2280c(%rip), %rdi     # 0x274a0
callq 0x5368
movl %eax, 0x2a801(%rip)     # 0x2f4a0
imull $0xc, 0x100a77e(%rip), %esi # 0x100f424
leaq 0x100a783(%rip), %rdi   # 0x100f430
callq 0x5368
movl %eax, 0x10ca76c(%rip)   # 0x10cf424
imull $0x14, 0xb0a755(%rip), %esi # 0xb0f414
leaq 0xb0a75a(%rip), %rdi    # 0xb0f420
callq 0x5368
movl %eax, 0x100a74f(%rip)   # 0x100f420
imull $0x1c, 0xa2a80c(%rip), %esi # 0xa2f4e4
leaq 0xa2a811(%rip), %rdi    # 0xa2f4f0
callq 0x5368
movl %eax, 0xb0a726(%rip)    # 0xb0f410
imull $0x18, 0x10ca737(%rip), %esi # 0x10cf428
leaq 0x10ca738(%rip), %rdi   # 0x10cf430
callq 0x5368
movl %eax, 0x118a715(%rip)   # 0x118f418
imull $0x28, 0x118a712(%rip), %esi # 0x118f41c
leaq 0x118a70f(%rip), %rdi   # 0x118f420
callq 0x5368
movl %eax, 0x1b8a704(%rip)   # 0x1b8f420
movl 0x1cca6fe(%rip), %esi   # 0x1ccf420
shll $0x3, %esi
leaq 0x1cca704(%rip), %rdi   # 0x1ccf430
callq 0x5368
movl %eax, 0x1d0a6f1(%rip)   # 0x1d0f428
imull $0x14, 0x1b8a6e6(%rip), %esi # 0x1b8f424
leaq 0x1b8a6eb(%rip), %rdi   # 0x1b8f430
callq 0x5368
movl %eax, 0x1cca6cc(%rip)   # 0x1ccf41c
movl 0x1e046de(%rip), %esi   # 0x1e09434
addl %esi, %esi
leaq 0x1e046e1(%rip), %rdi   # 0x1e09440
callq 0x5368
movl %eax, 0x1e246d6(%rip)   # 0x1e29440
movl 0x1e246d4(%rip), %esi   # 0x1e29444
shll $0x2, %esi
leaq 0x1e246d6(%rip), %rdi   # 0x1e29450
callq 0x5368
movl %eax, 0x20186cb(%rip)   # 0x201d450
movl 0x1d0a6a1(%rip), %ebx   # 0x1d0f42c
leal (,%rbx,4), %esi
leaq 0x1d0a697(%rip), %rdi   # 0x1d0f430
callq 0x5368
movl %eax, 0x1e0468c(%rip)   # 0x1e09430
movq 0x82a71d(%rip), %rdi    # 0x82f4c8
movl %ebx, %esi
callq 0x5368
movl %eax, 0x82a718(%rip)    # 0x82f4d0
movl 0x2a6e6(%rip), %esi     # 0x2f4a4
movq %r15, %rdi
callq 0x5368
movl %eax, 0x82a6e4(%rip)    # 0x82f4b0
movq 0x82a6e5(%rip), %rdi    # 0x82f4b8
movl 0x82a6db(%rip), %esi    # 0x82f4b4
callq 0x5368
movl %eax, 0x82a6dc(%rip)    # 0x82f4c0
movl 0x82a6ea(%rip), %esi    # 0x82f4d4
movq %r12, %rdi
callq 0x5368
movl %eax, 0xa2a6e8(%rip)    # 0xa2f4e0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq