// File: /workspace/github_cpp_makefile_star2_O3/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 0x8a38
movl %eax, (%r14,%rbx,4)
incq %rbx
cmpq $0x1f, %rbx
jne 0x4bf9
movl (%r14), %esi
cmpl $0x1e, %esi
je 0x4c2a
leaq 0x17522(%rip), %rdi     # 0x1c140
movl $0x1e, %edx
xorl %eax, %eax
callq 0xa164
movslq 0x78(%r14), %rbx
movslq 0x74(%r14), %r15
testb $0x3f, %bl
je 0x4c45
leaq 0x17b78(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
addq %r14, %r15
leaq 0x24841(%rip), %rdi     # 0x29490
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4260
leal 0x3f(%rbx), %eax
testl %ebx, %ebx
cmovnsl %ebx, %eax
sarl $0x6, %eax
movl %eax, 0x24815(%rip)     # 0x29480
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 0x4cab
leaq 0x17b12(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
addq %r14, %r15
leaq 0x100c76b(%rip), %rdi   # 0x1011420
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4260
movl %r12d, 0x100c74d(%rip)  # 0x1011414
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 0x4d07
leaq 0x17ab6(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
addq %r14, %r15
leaq 0xb0c6ff(%rip), %rdi    # 0xb11410
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4260
movl %r12d, 0xb0c6e1(%rip)   # 0xb11404
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 0x4d69
leaq 0x17a54(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
addq %r14, %r15
leaq 0xa2c76d(%rip), %rdi    # 0xa314e0
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4260
movl %r12d, 0xa2c74f(%rip)   # 0xa314d4
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 0x4dc5
leaq 0x179f8(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
addq %r14, %r12
leaq 0x10cc651(%rip), %rdi   # 0x10d1420
movq %r12, %rsi
movq %r15, %rdx
callq 0x4260
movl %ebx, 0x10cc638(%rip)   # 0x10d1418
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 0x4e20
leaq 0x1799d(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
addq %r14, %r13
leaq 0x118c5e6(%rip), %r15   # 0x1191410
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x4260
movl %ebx, 0x118c5ce(%rip)   # 0x119140c
movslq 0x50(%r14), %r12
movslq 0x4c(%r14), %rbx
testb $0x7, %r12b
je 0x4e5a
leaq 0x17963(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
addq %r14, %rbx
leaq 0x1ccc5bc(%rip), %rdi   # 0x1cd1420
movq %rbx, %rsi
movq %r12, %rdx
callq 0x4260
leal 0x7(%r12), %eax
testl %r12d, %r12d
cmovnsl %r12d, %eax
sarl $0x3, %eax
movl %eax, 0x1ccc58c(%rip)   # 0x1cd1410
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 0x4ec4
leaq 0x178f9(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
addq %r14, %rbp
leaq 0x1b8c552(%rip), %r12   # 0x1b91420
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x4260
movl %ebx, 0x1b8c532(%rip)   # 0x1b91414
movslq 0x60(%r14), %r13
movslq 0x5c(%r14), %rbx
testb $0x1, %r13b
je 0x4efe
leaq 0x178bf(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
addq %r14, %rbx
leaq 0x1e06528(%rip), %rdi   # 0x1e0b430
movq %rbx, %rsi
movq %r13, %rdx
callq 0x4260
movl %r13d, %eax
shrl $0x1f, %eax
addl %r13d, %eax
sarl %eax
movl %eax, 0x1e06500(%rip)   # 0x1e0b424
movslq 0x70(%r14), %r13
movslq 0x6c(%r14), %rbx
testb $0x3, %r13b
je 0x4f40
leaq 0x1787d(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
addq %r14, %rbx
leaq 0x1e264f6(%rip), %rdi   # 0x1e2b440
movq %rbx, %rsi
movq %r13, %rdx
callq 0x4260
leal 0x3(%r13), %eax
testl %r13d, %r13d
cmovnsl %r13d, %eax
sarl $0x2, %eax
movl %eax, 0x1e264cb(%rip)   # 0x1e2b434
movslq 0x68(%r14), %rbp
movslq 0x64(%r14), %rbx
testb $0x3, %bpl
je 0x4f85
leaq 0x17838(%rip), %rdi     # 0x1c7b6
xorl %eax, %eax
callq 0xa164
movq 0x82c52c(%rip), %r13    # 0x8314b8
addq %r14, %rbx
leaq 0x1d0c48a(%rip), %rdi   # 0x1d11420
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x4260
leal 0x3(%rbp), %eax
testl %ebp, %ebp
cmovnsl %ebp, %eax
sarl $0x2, %eax
movl %eax, 0x1d0c46a(%rip)   # 0x1d1141c
movslq 0x18(%r14), %rbx
movslq 0x14(%r14), %rbp
xorl %edi, %edi
cmpl %ebx, 0x2336e(%rip)     # 0x28330
setg %dil
movl $0x25, %esi
callq 0xa91d
addq %r14, %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x4260
movl %ebx, 0x82c4cd(%rip)    # 0x8314b4
movslq 0x28(%r14), %rbx
movslq 0x24(%r14), %rsi
movq 0x82c4b2(%rip), %r13    # 0x8314a8
addq %r14, %rsi
leaq 0x2c4a0(%rip), %rdi     # 0x314a0
movq %rbx, %rdx
callq 0x4260
movl %ebx, 0x2c486(%rip)     # 0x31494
movslq 0x48(%r14), %rbx
movslq 0x44(%r14), %rbp
xorl %edi, %edi
cmpl %ebx, 0x23316(%rip)     # 0x28334
setg %dil
movl $0x38, %esi
callq 0xa91d
addq %r14, %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x4260
movl %ebx, 0x82c461(%rip)    # 0x8314a4
movslq 0x8(%r14), %rbx
movslq 0x4(%r14), %rsi
addq %r14, %rsi
leaq 0x82c47b(%rip), %r13    # 0x8314d0
movq %r13, %rdi
movq %rbx, %rdx
callq 0x4260
movl %ebx, 0x82c45e(%rip)    # 0x8314c4
movq %r14, %rdi
callq 0x4979
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x5379
movl 0x24403(%rip), %eax     # 0x29480
shll $0x6, %eax
movl $0x0, %ecx
leaq 0x24404(%rip), %rdi     # 0x29490
je 0x50a4
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 0x5094
movl %ecx, 0x2c3e6(%rip)     # 0x31490
movl 0x100c364(%rip), %eax   # 0x1011414
shll $0x2, %eax
leal (%rax,%rax,2), %eax
testl %eax, %eax
leaq 0x100c361(%rip), %rsi   # 0x1011420
leaq 0xb0c34a(%rip), %r8     # 0xb11410
leaq 0x10cc353(%rip), %r10   # 0x10d1420
je 0x50e5
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 0x50d5
movl %ebx, 0x10cc329(%rip)   # 0x10d1414
movl 0xb0c313(%rip), %eax    # 0xb11404
shll $0x2, %eax
leal (%rax,%rax,4), %ecx
xorl %eax, %eax
movl $0x0, %edx
testl %ecx, %ecx
leaq 0x1d0c319(%rip), %r9    # 0x1d11420
je 0x5120
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 0x510f
movl %edx, 0x100c2ea(%rip)   # 0x1011410
movl 0xa2c3a8(%rip), %edx    # 0xa314d4
leal (%rdx,%rdx,8), %ecx
leal (%rcx,%rcx,2), %ecx
addl %edx, %ecx
leaq 0xa2c3a5(%rip), %rdi    # 0xa314e0
je 0x5153
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 0x5143
movl %eax, 0xb0c2a7(%rip)    # 0xb11400
movl 0x10cc2b9(%rip), %eax   # 0x10d1418
shll $0x3, %eax
leal (%rax,%rax,2), %ecx
xorl %eax, %eax
movl $0x0, %edx
testl %ecx, %ecx
leaq 0x1ccc2ab(%rip), %r8    # 0x1cd1420
je 0x518e
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 0x517d
movl %edx, 0x118c274(%rip)   # 0x1191408
movl 0x118c272(%rip), %ecx   # 0x119140c
shll $0x3, %ecx
leal (%rcx,%rcx,4), %ecx
testl %ecx, %ecx
je 0x51bb
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 0x51aa
movl %eax, 0x1b8c24f(%rip)   # 0x1b91410
movl 0x1ccc249(%rip), %ecx   # 0x1cd1410
shll $0x3, %ecx
xorl %eax, %eax
movl $0x0, %edx
testl %ecx, %ecx
je 0x51ec
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 0x51db
movl %edx, 0x1d0c226(%rip)   # 0x1d11418
movl 0x1b8c21c(%rip), %ecx   # 0x1b91414
shll $0x2, %ecx
leal (%rcx,%rcx,4), %ecx
testl %ecx, %ecx
je 0x5219
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 0x5208
movl %eax, 0x1ccc1ed(%rip)   # 0x1cd140c
movl 0x1e061ff(%rip), %ecx   # 0x1e0b424
xorl %eax, %eax
movl $0x0, %edx
addl %ecx, %ecx
leaq 0x1e061fb(%rip), %r8    # 0x1e0b430
je 0x524e
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 0x523d
movl %edx, 0x1e261dc(%rip)   # 0x1e2b430
movl 0x1e261da(%rip), %ecx   # 0x1e2b434
shll $0x2, %ecx
testl %ecx, %ecx
leaq 0x1e261da(%rip), %rdi   # 0x1e2b440
je 0x527e
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 0x526e
movl %eax, 0x201a1bc(%rip)   # 0x201f440
movl 0x1d0c192(%rip), %eax   # 0x1d1141c
leal (,%rax,4), %edx
xorl %ecx, %ecx
movl $0x0, %esi
testl %edx, %edx
je 0x52b4
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 0x52a2
movl %esi, 0x1e06166(%rip)   # 0x1e0b420
testq %rax, %rax
je 0x52da
xorl %edx, %edx
movq 0x82c1f0(%rip), %rsi    # 0x8314b8
xorl %ecx, %ecx
roll $0x4, %ecx
movsbl (%rsi,%rdx), %edi
xorl %edi, %ecx
incq %rdx
cmpl %edx, %eax
jne 0x52ca
movl %ecx, 0x82c1e0(%rip)    # 0x8314c0
movl 0x2c1ae(%rip), %ecx     # 0x31494
xorl %eax, %eax
movl $0x0, %edx
testq %rcx, %rcx
leaq 0x2c1a9(%rip), %r8      # 0x314a0
je 0x530e
xorl %esi, %esi
xorl %edx, %edx
roll $0x4, %edx
movsbl (%rsi,%r8), %edi
xorl %edi, %edx
incq %rsi
cmpl %esi, %ecx
jne 0x52fd
movl %edx, 0x82c18c(%rip)    # 0x8314a0
movl 0x82c18a(%rip), %ecx    # 0x8314a4
testq %rcx, %rcx
je 0x533a
xorl %edx, %edx
movq 0x82c180(%rip), %rsi    # 0x8314a8
xorl %eax, %eax
roll $0x4, %eax
movsbl (%rsi,%rdx), %edi
xorl %edi, %eax
incq %rdx
cmpl %edx, %ecx
jne 0x532a
movl %eax, 0x82c170(%rip)    # 0x8314b0
movl 0x82c17e(%rip), %eax    # 0x8314c4
testq %rax, %rax
je 0x5362
xorl %edx, %edx
xorl %ecx, %ecx
roll $0x4, %ecx
movsbl (%rdx,%r13), %esi
xorl %esi, %ecx
incq %rdx
cmpl %edx, %eax
jne 0x534f
jmp 0x5364
xorl %ecx, %ecx
movl %ecx, 0xa2c166(%rip)    # 0xa314d0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq