// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
xorl %ebx, %ebx
movl (%r14,%rbx,4), %edi
callq 0x8b70
movl %eax, (%r14,%rbx,4)
incq %rbx
cmpq $0x1f, %rbx
jne 0x4c17
movl (%r14), %esi
cmpl $0x1e, %esi
je 0x4c48
leaq 0x1750c(%rip), %rdi     # 0x1c148
movl $0x1e, %edx
xorl %eax, %eax
callq 0xa2a0
movslq 0x78(%r14), %rbx
movslq 0x74(%r14), %r15
testb $0x3f, %bl
je 0x4c63
leaq 0x17b62(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
addq %r14, %r15
leaq 0x24833(%rip), %rdi     # 0x294a0
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4270
leal 0x3f(%rbx), %eax
testl %ebx, %ebx
cmovnsl %ebx, %eax
sarl $0x6, %eax
movl %eax, 0x24807(%rip)     # 0x29490
movslq 0x20(%r14), %rbx
movslq 0x1c(%r14), %r15
imulq $0x2aaaaaab, %rbx, %r12 # imm = 0x2AAAAAAB
movq %r12, %rax
shrq $0x3f, %rax
sarq $0x21, %r12
addl %eax, %r12d
imull $0xaaaaaaab, %ebx, %eax # imm = 0xAAAAAAAB
addl $0x2aaaaaa8, %eax       # imm = 0x2AAAAAA8
rorl $0x2, %eax
cmpl $0x15555555, %eax       # imm = 0x15555555
jb 0x4cc9
leaq 0x17afc(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
addq %r14, %r15
leaq 0x100c75d(%rip), %rdi   # 0x1011430
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4270
movl %r12d, 0x100c73f(%rip)  # 0x1011424
movslq 0x10(%r14), %rbx
movslq 0xc(%r14), %r15
imulq $0x66666667, %rbx, %r12 # imm = 0x66666667
movq %r12, %rax
shrq $0x3f, %rax
sarq $0x23, %r12
addl %eax, %r12d
imull $0xcccccccd, %ebx, %eax # imm = 0xCCCCCCCD
addl $0x19999998, %eax       # imm = 0x19999998
rorl $0x2, %eax
cmpl $0xccccccd, %eax        # imm = 0xCCCCCCD
jb 0x4d25
leaq 0x17aa0(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
addq %r14, %r15
leaq 0xb0c6f1(%rip), %rdi    # 0xb11420
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4270
movl %r12d, 0xb0c6d3(%rip)   # 0xb11414
movslq 0x58(%r14), %rbx
movslq 0x54(%r14), %r15
imulq $-0x6db6db6d, %rbx, %r12 # imm = 0x92492493
shrq $0x20, %r12
addl %ebx, %r12d
movl %r12d, %eax
shrl $0x1f, %eax
sarl $0x4, %r12d
addl %eax, %r12d
imull $0xb6db6db7, %ebx, %eax # imm = 0xB6DB6DB7
addl $0x12492490, %eax       # imm = 0x12492490
rorl $0x2, %eax
cmpl $0x9249249, %eax        # imm = 0x9249249
jb 0x4d87
leaq 0x17a3e(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
addq %r14, %r15
leaq 0xa2c75f(%rip), %rdi    # 0xa314f0
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4270
movl %r12d, 0xa2c741(%rip)   # 0xa314e4
movslq 0x30(%r14), %r15
movslq 0x2c(%r14), %r12
imulq $0x2aaaaaab, %r15, %rbx # imm = 0x2AAAAAAB
movq %rbx, %rax
shrq $0x3f, %rax
sarq $0x22, %rbx
addl %eax, %ebx
imull $0xaaaaaaab, %r15d, %eax # imm = 0xAAAAAAAB
addl $0x2aaaaaa8, %eax       # imm = 0x2AAAAAA8
rorl $0x3, %eax
cmpl $0xaaaaaab, %eax        # imm = 0xAAAAAAB
jb 0x4de3
leaq 0x179e2(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
addq %r14, %r12
leaq 0x10cc643(%rip), %rdi   # 0x10d1430
movq %r12, %rsi
movq %r15, %rdx
callq 0x4270
movl %ebx, 0x10cc62a(%rip)   # 0x10d1428
movslq 0x38(%r14), %r12
movslq 0x34(%r14), %r13
imulq $0x66666667, %r12, %rbx # imm = 0x66666667
movq %rbx, %rax
shrq $0x3f, %rax
sarq $0x24, %rbx
addl %eax, %ebx
imull $0xcccccccd, %r12d, %eax # imm = 0xCCCCCCCD
addl $0x19999998, %eax       # imm = 0x19999998
rorl $0x3, %eax
cmpl $0x6666667, %eax        # imm = 0x6666667
jb 0x4e3e
leaq 0x17987(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
addq %r14, %r13
leaq 0x118c5d8(%rip), %r15   # 0x1191420
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x4270
movl %ebx, 0x118c5c0(%rip)   # 0x119141c
movslq 0x50(%r14), %r12
movslq 0x4c(%r14), %rbx
testb $0x7, %r12b
je 0x4e78
leaq 0x1794d(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
addq %r14, %rbx
leaq 0x1ccc5ae(%rip), %rdi   # 0x1cd1430
movq %rbx, %rsi
movq %r12, %rdx
callq 0x4270
leal 0x7(%r12), %eax
testl %r12d, %r12d
cmovnsl %r12d, %eax
sarl $0x3, %eax
movl %eax, 0x1ccc57e(%rip)   # 0x1cd1420
movslq 0x40(%r14), %r13
movslq 0x3c(%r14), %rbp
imulq $0x66666667, %r13, %rbx # imm = 0x66666667
movq %rbx, %rax
shrq $0x3f, %rax
sarq $0x23, %rbx
addl %eax, %ebx
imull $0xcccccccd, %r13d, %eax # imm = 0xCCCCCCCD
addl $0x19999998, %eax       # imm = 0x19999998
rorl $0x2, %eax
cmpl $0xccccccd, %eax        # imm = 0xCCCCCCD
jb 0x4ee2
leaq 0x178e3(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
addq %r14, %rbp
leaq 0x1b8c544(%rip), %r12   # 0x1b91430
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x4270
movl %ebx, 0x1b8c524(%rip)   # 0x1b91424
movslq 0x60(%r14), %r13
movslq 0x5c(%r14), %rbx
testb $0x1, %r13b
je 0x4f1c
leaq 0x178a9(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
addq %r14, %rbx
leaq 0x1e0651a(%rip), %rdi   # 0x1e0b440
movq %rbx, %rsi
movq %r13, %rdx
callq 0x4270
movl %r13d, %eax
shrl $0x1f, %eax
addl %r13d, %eax
sarl %eax
movl %eax, 0x1e064f2(%rip)   # 0x1e0b434
movslq 0x70(%r14), %r13
movslq 0x6c(%r14), %rbx
testb $0x3, %r13b
je 0x4f5e
leaq 0x17867(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
addq %r14, %rbx
leaq 0x1e264e8(%rip), %rdi   # 0x1e2b450
movq %rbx, %rsi
movq %r13, %rdx
callq 0x4270
leal 0x3(%r13), %eax
testl %r13d, %r13d
cmovnsl %r13d, %eax
sarl $0x2, %eax
movl %eax, 0x1e264bd(%rip)   # 0x1e2b444
movslq 0x68(%r14), %rbp
movslq 0x64(%r14), %rbx
testb $0x3, %bpl
je 0x4fa3
leaq 0x17822(%rip), %rdi     # 0x1c7be
xorl %eax, %eax
callq 0xa2a0
movq 0x82c51e(%rip), %r13    # 0x8314c8
addq %r14, %rbx
leaq 0x1d0c47c(%rip), %rdi   # 0x1d11430
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x4270
leal 0x3(%rbp), %eax
testl %ebp, %ebp
cmovnsl %ebp, %eax
sarl $0x2, %eax
movl %eax, 0x1d0c45c(%rip)   # 0x1d1142c
movslq 0x18(%r14), %rbx
movslq 0x14(%r14), %rbp
xorl %edi, %edi
cmpl %ebx, 0x23360(%rip)     # 0x28340
setg %dil
movl $0x25, %esi
callq 0xaa4d
addq %r14, %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x4270
movl %ebx, 0x82c4bf(%rip)    # 0x8314c4
movslq 0x28(%r14), %rbx
movslq 0x24(%r14), %rsi
movq 0x82c4a4(%rip), %r13    # 0x8314b8
addq %r14, %rsi
leaq 0x2c492(%rip), %rdi     # 0x314b0
movq %rbx, %rdx
callq 0x4270
movl %ebx, 0x2c478(%rip)     # 0x314a4
movslq 0x48(%r14), %rbx
movslq 0x44(%r14), %rbp
xorl %edi, %edi
cmpl %ebx, 0x23308(%rip)     # 0x28344
setg %dil
movl $0x38, %esi
callq 0xaa4d
addq %r14, %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x4270
movl %ebx, 0x82c453(%rip)    # 0x8314b4
movslq 0x8(%r14), %rbx
movslq 0x4(%r14), %rsi
addq %r14, %rsi
leaq 0x82c46d(%rip), %r13    # 0x8314e0
movq %r13, %rdi
movq %rbx, %rdx
callq 0x4270
movl %ebx, 0x82c450(%rip)    # 0x8314d4
movq %r14, %rdi
callq 0x4999
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x5397
movl 0x243f5(%rip), %eax     # 0x29490
shll $0x6, %eax
movl $0x0, %ecx
leaq 0x243f6(%rip), %rdi     # 0x294a0
je 0x50c2
movl %eax, %eax
xorl %edx, %edx
xorl %ecx, %ecx
roll $0x4, %ecx
movsbl (%rdx,%rdi), %esi
xorl %esi, %ecx
incq %rdx
cmpl %edx, %eax
jne 0x50b2
movl %ecx, 0x2c3d8(%rip)     # 0x314a0
movl 0x100c356(%rip), %eax   # 0x1011424
shll $0x2, %eax
leal (%rax,%rax,2), %eax
testl %eax, %eax
leaq 0x100c353(%rip), %rsi   # 0x1011430
leaq 0xb0c33c(%rip), %r8     # 0xb11420
leaq 0x10cc345(%rip), %r10   # 0x10d1430
je 0x5103
movl %eax, %eax
xorl %ecx, %ecx
xorl %ebx, %ebx
roll $0x4, %ebx
movsbl (%rcx,%rsi), %edx
xorl %edx, %ebx
incq %rcx
cmpl %ecx, %eax
jne 0x50f3
movl %ebx, 0x10cc31b(%rip)   # 0x10d1424
movl 0xb0c305(%rip), %eax    # 0xb11414
shll $0x2, %eax
leal (%rax,%rax,4), %ecx
xorl %eax, %eax
movl $0x0, %edx
testl %ecx, %ecx
leaq 0x1d0c30b(%rip), %r9    # 0x1d11430
je 0x513e
movl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
roll $0x4, %edx
movsbl (%rsi,%r8), %edi
xorl %edi, %edx
incq %rsi
cmpl %esi, %ecx
jne 0x512d
movl %edx, 0x100c2dc(%rip)   # 0x1011420
movl 0xa2c39a(%rip), %edx    # 0xa314e4
leal (%rdx,%rdx,8), %ecx
leal (%rcx,%rcx,2), %ecx
addl %edx, %ecx
leaq 0xa2c397(%rip), %rdi    # 0xa314f0
je 0x5171
movl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
roll $0x4, %eax
movsbl (%rdx,%rdi), %esi
xorl %esi, %eax
incq %rdx
cmpl %edx, %ecx
jne 0x5161
movl %eax, 0xb0c299(%rip)    # 0xb11410
movl 0x10cc2ab(%rip), %eax   # 0x10d1428
shll $0x3, %eax
leal (%rax,%rax,2), %ecx
xorl %eax, %eax
movl $0x0, %edx
testl %ecx, %ecx
leaq 0x1ccc29d(%rip), %r8    # 0x1cd1430
je 0x51ac
movl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
roll $0x4, %edx
movsbl (%rsi,%r10), %edi
xorl %edi, %edx
incq %rsi
cmpl %esi, %ecx
jne 0x519b
movl %edx, 0x118c266(%rip)   # 0x1191418
movl 0x118c264(%rip), %ecx   # 0x119141c
shll $0x3, %ecx
leal (%rcx,%rcx,4), %ecx
testl %ecx, %ecx
je 0x51d9
movl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
roll $0x4, %eax
movsbl (%rdx,%r15), %esi
xorl %esi, %eax
incq %rdx
cmpl %edx, %ecx
jne 0x51c8
movl %eax, 0x1b8c241(%rip)   # 0x1b91420
movl 0x1ccc23b(%rip), %ecx   # 0x1cd1420
shll $0x3, %ecx
xorl %eax, %eax
movl $0x0, %edx
testl %ecx, %ecx
je 0x520a
movl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
roll $0x4, %edx
movsbl (%rsi,%r8), %edi
xorl %edi, %edx
incq %rsi
cmpl %esi, %ecx
jne 0x51f9
movl %edx, 0x1d0c218(%rip)   # 0x1d11428
movl 0x1b8c20e(%rip), %ecx   # 0x1b91424
shll $0x2, %ecx
leal (%rcx,%rcx,4), %ecx
testl %ecx, %ecx
je 0x5237
movl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
roll $0x4, %eax
movsbl (%rdx,%r12), %esi
xorl %esi, %eax
incq %rdx
cmpl %edx, %ecx
jne 0x5226
movl %eax, 0x1ccc1df(%rip)   # 0x1cd141c
movl 0x1e061f1(%rip), %ecx   # 0x1e0b434
xorl %eax, %eax
movl $0x0, %edx
addl %ecx, %ecx
leaq 0x1e061ed(%rip), %r8    # 0x1e0b440
je 0x526c
movl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
roll $0x4, %edx
movsbl (%rsi,%r8), %edi
xorl %edi, %edx
incq %rsi
cmpl %esi, %ecx
jne 0x525b
movl %edx, 0x1e261ce(%rip)   # 0x1e2b440
movl 0x1e261cc(%rip), %ecx   # 0x1e2b444
shll $0x2, %ecx
testl %ecx, %ecx
leaq 0x1e261cc(%rip), %rdi   # 0x1e2b450
je 0x529c
movl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
roll $0x4, %eax
movsbl (%rdx,%rdi), %esi
xorl %esi, %eax
incq %rdx
cmpl %edx, %ecx
jne 0x528c
movl %eax, 0x201a1ae(%rip)   # 0x201f450
movl 0x1d0c184(%rip), %eax   # 0x1d1142c
leal (,%rax,4), %edx
xorl %ecx, %ecx
movl $0x0, %esi
testl %edx, %edx
je 0x52d2
movl %edx, %edx
xorl %edi, %edi
xorl %esi, %esi
roll $0x4, %esi
movsbl (%rdi,%r9), %r8d
xorl %r8d, %esi
incq %rdi
cmpl %edi, %edx
jne 0x52c0
movl %esi, 0x1e06158(%rip)   # 0x1e0b430
testq %rax, %rax
je 0x52f8
xorl %edx, %edx
movq 0x82c1e2(%rip), %rsi    # 0x8314c8
xorl %ecx, %ecx
roll $0x4, %ecx
movsbl (%rsi,%rdx), %edi
xorl %edi, %ecx
incq %rdx
cmpl %edx, %eax
jne 0x52e8
movl %ecx, 0x82c1d2(%rip)    # 0x8314d0
movl 0x2c1a0(%rip), %ecx     # 0x314a4
xorl %eax, %eax
movl $0x0, %edx
testq %rcx, %rcx
leaq 0x2c19b(%rip), %r8      # 0x314b0
je 0x532c
xorl %esi, %esi
xorl %edx, %edx
roll $0x4, %edx
movsbl (%rsi,%r8), %edi
xorl %edi, %edx
incq %rsi
cmpl %esi, %ecx
jne 0x531b
movl %edx, 0x82c17e(%rip)    # 0x8314b0
movl 0x82c17c(%rip), %ecx    # 0x8314b4
testq %rcx, %rcx
je 0x5358
xorl %edx, %edx
movq 0x82c172(%rip), %rsi    # 0x8314b8
xorl %eax, %eax
roll $0x4, %eax
movsbl (%rsi,%rdx), %edi
xorl %edi, %eax
incq %rdx
cmpl %edx, %ecx
jne 0x5348
movl %eax, 0x82c162(%rip)    # 0x8314c0
movl 0x82c170(%rip), %eax    # 0x8314d4
testq %rax, %rax
je 0x5380
xorl %edx, %edx
xorl %ecx, %ecx
roll $0x4, %ecx
movsbl (%rdx,%r13), %esi
xorl %esi, %ecx
incq %rdx
cmpl %edx, %eax
jne 0x536d
jmp 0x5382
xorl %ecx, %ecx
movl %ecx, 0xa2c158(%rip)    # 0xa314e0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq