// File: /workspace/github_cpp_makefile_star2_O1/FreeSlave[P]vhlt/common/bspfile.cpp
// Function: SwapBSPFile(bool)
static void     SwapBSPFile(const bool todisk)
{
    int             i, j, c;
    dmodel_t*       d;
    dmiptexlump_t*  mtl;

    // models       
    for (i = 0; i < g_nummodels; i++)
    {
        d = &g_dmodels[i];

        for (j = 0; j < MAX_MAP_HULLS; j++)
        {
            d->headnode[j] = LittleLong(d->headnode[j]);
        }

        d->visleafs = LittleLong(d->visleafs);
        d->firstface = LittleLong(d->firstface);
        d->numfaces = LittleLong(d->numfaces);

        for (j = 0; j < 3; j++)
        {
            d->mins[j] = LittleFloat(d->mins[j]);
            d->maxs[j] = LittleFloat(d->maxs[j]);
            d->origin[j] = LittleFloat(d->origin[j]);
        }
    }

    //
    // vertexes
    //
    for (i = 0; i < g_numvertexes; i++)
    {
        for (j = 0; j < 3; j++)
        {
            g_dvertexes[i].point[j] = LittleFloat(g_dvertexes[i].point[j]);
        }
    }

    //
    // planes
    //      
    for (i = 0; i < g_numplanes; i++)
    {
        for (j = 0; j < 3; j++)
        {
            g_dplanes[i].normal[j] = LittleFloat(g_dplanes[i].normal[j]);
        }
        g_dplanes[i].dist = LittleFloat(g_dplanes[i].dist);
        g_dplanes[i].type = (planetypes)LittleLong(g_dplanes[i].type);
    }

    //
    // texinfos
    //      
    for (i = 0; i < g_numtexinfo; i++)
    {
        for (j = 0; j < 8; j++)
        {
            g_texinfo[i].vecs[0][j] = LittleFloat(g_texinfo[i].vecs[0][j]);
        }
        g_texinfo[i].miptex = LittleLong(g_texinfo[i].miptex);
        g_texinfo[i].flags = LittleLong(g_texinfo[i].flags);
    }

    //
    // faces
    //
    for (i = 0; i < g_numfaces; i++)
    {
        g_dfaces[i].texinfo = LittleShort(g_dfaces[i].texinfo);
        g_dfaces[i].planenum = LittleShort(g_dfaces[i].planenum);
        g_dfaces[i].side = LittleShort(g_dfaces[i].side);
        g_dfaces[i].lightofs = LittleLong(g_dfaces[i].lightofs);
        g_dfaces[i].firstedge = LittleLong(g_dfaces[i].firstedge);
        g_dfaces[i].numedges = LittleShort(g_dfaces[i].numedges);
    }

    //
    // nodes
    //
    for (i = 0; i < g_numnodes; i++)
    {
        g_dnodes[i].planenum = LittleLong(g_dnodes[i].planenum);
        for (j = 0; j < 3; j++)
        {
            g_dnodes[i].mins[j] = LittleShort(g_dnodes[i].mins[j]);
            g_dnodes[i].maxs[j] = LittleShort(g_dnodes[i].maxs[j]);
        }
        g_dnodes[i].children[0] = LittleShort(g_dnodes[i].children[0]);
        g_dnodes[i].children[1] = LittleShort(g_dnodes[i].children[1]);
        g_dnodes[i].firstface = LittleShort(g_dnodes[i].firstface);
        g_dnodes[i].numfaces = LittleShort(g_dnodes[i].numfaces);
    }

    //
    // leafs
    //
    for (i = 0; i < g_numleafs; i++)
    {
        g_dleafs[i].contents = LittleLong(g_dleafs[i].contents);
        for (j = 0; j < 3; j++)
        {
            g_dleafs[i].mins[j] = LittleShort(g_dleafs[i].mins[j]);
            g_dleafs[i].maxs[j] = LittleShort(g_dleafs[i].maxs[j]);
        }

        g_dleafs[i].firstmarksurface = LittleShort(g_dleafs[i].firstmarksurface);
        g_dleafs[i].nummarksurfaces = LittleShort(g_dleafs[i].nummarksurfaces);
        g_dleafs[i].visofs = LittleLong(g_dleafs[i].visofs);
    }

    //
    // clipnodes
    //
#ifdef ZHLT_XASH2
	for (int hull = 1; hull < MAX_MAP_HULLS; hull++)
	{
		for (i = 0; i < g_numclipnodes[hull - 1]; i++)
		{
			g_dclipnodes[hull - 1][i].planenum = LittleLong(g_dclipnodes[hull - 1][i].planenum);
			g_dclipnodes[hull - 1][i].children[0] = LittleShort(g_dclipnodes[hull - 1][i].children[0]);
			g_dclipnodes[hull - 1][i].children[1] = LittleShort(g_dclipnodes[hull - 1][i].children[1]);
		}
	}
#else
    for (i = 0; i < g_numclipnodes; i++)
    {
        g_dclipnodes[i].planenum = LittleLong(g_dclipnodes[i].planenum);
        g_dclipnodes[i].children[0] = LittleShort(g_dclipnodes[i].children[0]);
        g_dclipnodes[i].children[1] = LittleShort(g_dclipnodes[i].children[1]);
    }
#endif

    //
    // miptex
    //
    if (g_texdatasize)
    {
        mtl = (dmiptexlump_t*)g_dtexdata;
        if (todisk)
        {
            c = mtl->nummiptex;
        }
        else
        {
            c = LittleLong(mtl->nummiptex);
        }
        mtl->nummiptex = LittleLong(mtl->nummiptex);
        for (i = 0; i < c; i++)
        {
            mtl->dataofs[i] = LittleLong(mtl->dataofs[i]);
        }
    }

    //
    // marksurfaces
    //
    for (i = 0; i < g_nummarksurfaces; i++)
    {
        g_dmarksurfaces[i] = LittleShort(g_dmarksurfaces[i]);
    }

    //
    // surfedges
    //
    for (i = 0; i < g_numsurfedges; i++)
    {
        g_dsurfedges[i] = LittleLong(g_dsurfedges[i]);
    }

    //
    // edges
    //
    for (i = 0; i < g_numedges; i++)
    {
        g_dedges[i].v[0] = LittleShort(g_dedges[i].v[0]);
        g_dedges[i].v[1] = LittleShort(g_dedges[i].v[1]);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edi, 0x4(%rsp)
cmpl $0x0, 0x240e3(%rip)     # 0x29490
jle 0x546c
leaq 0x2410a(%rip), %r14     # 0x294c4
leaq 0x240f7(%rip), %r15     # 0x294b8
xorl %r12d, %r12d
leaq 0x240d5(%rip), %r13     # 0x294a0
movq %r12, %rbp
shlq $0x6, %rbp
addq %r13, %rbp
xorl %ebx, %ebx
movl (%r14,%rbx,4), %edi
callq 0x8b70
movl %eax, (%r14,%rbx,4)
incq %rbx
cmpq $0x4, %rbx
jne 0x53d7
movl 0x34(%rbp), %edi
callq 0x8b70
movl %eax, 0x34(%rbp)
movl 0x38(%rbp), %edi
callq 0x8b70
movl %eax, 0x38(%rbp)
movl 0x3c(%rbp), %edi
callq 0x8b70
movl %eax, 0x3c(%rbp)
movq $-0x3, %rbp
movss -0xc(%r15,%rbp,4), %xmm0
callq 0x8b7e
movss %xmm0, -0xc(%r15,%rbp,4)
movss (%r15,%rbp,4), %xmm0
callq 0x8b7e
movss %xmm0, (%r15,%rbp,4)
movss 0xc(%r15,%rbp,4), %xmm0
callq 0x8b7e
movss %xmm0, 0xc(%r15,%rbp,4)
incq %rbp
jne 0x5415
incq %r12
movslq 0x24035(%rip), %rax     # 0x29490
addq $0x40, %r14
addq $0x40, %r15
cmpq %rax, %r12
jl 0x53cb
cmpl $0x0, 0x100bfb1(%rip)   # 0x1011424
jle 0x54af
leaq 0x100bfb4(%rip), %r14   # 0x1011430
xorl %r15d, %r15d
xorl %r12d, %r12d
movss (%r14,%r12,4), %xmm0
callq 0x8b7e
movss %xmm0, (%r14,%r12,4)
incq %r12
cmpq $0x3, %r12
jne 0x5482
incq %r15
movslq 0x100bf7e(%rip), %rax   # 0x1011424
addq $0xc, %r14
cmpq %rax, %r15
jl 0x547f
cmpl $0x0, 0xb0bf5e(%rip)    # 0xb11414
jle 0x551b
leaq 0xb0bf61(%rip), %r14    # 0xb11420
xorl %r15d, %r15d
movq %r14, %r12
xorl %r13d, %r13d
movss (%r12,%r13,4), %xmm0
callq 0x8b7e
movss %xmm0, (%r12,%r13,4)
incq %r13
cmpq $0x3, %r13
jne 0x54c8
leaq (%r15,%r15,4), %rbx
movss 0xc(%r14,%rbx,4), %xmm0
callq 0x8b7e
movss %xmm0, 0xc(%r14,%rbx,4)
movl 0x10(%r14,%rbx,4), %edi
callq 0x8b70
movl %eax, 0x10(%r14,%rbx,4)
incq %r15
movslq 0xb0bf02(%rip), %rax    # 0xb11414
addq $0x14, %r12
cmpq %rax, %r15
jl 0x54c5
cmpl $0x0, 0x118befa(%rip)   # 0x119141c
jle 0x5583
leaq 0x118bef5(%rip), %r14   # 0x1191420
xorl %r15d, %r15d
movq %r14, %r12
xorl %r13d, %r13d
movss (%r12,%r13,4), %xmm0
callq 0x8b7e
movss %xmm0, (%r12,%r13,4)
incq %r13
cmpq $0x8, %r13
jne 0x5534
leaq (%r15,%r15,4), %rbx
movl 0x20(%r14,%rbx,8), %edi
callq 0x8b70
movl %eax, 0x20(%r14,%rbx,8)
movl 0x24(%r14,%rbx,8), %edi
callq 0x8b70
movl %eax, 0x24(%r14,%rbx,8)
incq %r15
movslq 0x118bea2(%rip), %rax   # 0x119141c
addq $0x28, %r12
cmpq %rax, %r15
jl 0x5531
cmpl $0x0, 0x1b8be9a(%rip)   # 0x1b91424
jle 0x55fd
leaq 0x1b8be9d(%rip), %r14   # 0x1b91430
xorl %r15d, %r15d
movswl 0xa(%r14), %edi
callq 0x8b68
movw %ax, 0xa(%r14)
movswl (%r14), %edi
callq 0x8b68
movw %ax, (%r14)
movswl 0x2(%r14), %edi
callq 0x8b68
movw %ax, 0x2(%r14)
movl 0x10(%r14), %edi
callq 0x8b70
movl %eax, 0x10(%r14)
movl 0x4(%r14), %edi
callq 0x8b70
movl %eax, 0x4(%r14)
movswl 0x8(%r14), %edi
callq 0x8b68
movw %ax, 0x8(%r14)
incq %r15
movslq 0x1b8be30(%rip), %rax   # 0x1b91424
addq $0x14, %r14
cmpq %rax, %r15
jl 0x5596
cmpl $0x0, 0x10cbe24(%rip)   # 0x10d1428
jle 0x56af
leaq 0x10cbe2d(%rip), %r14   # 0x10d143e
xorl %r15d, %r15d
leaq 0x10cbe15(%rip), %r12   # 0x10d1430
leaq (%r15,%r15,2), %rax
leaq (%r12,%rax,8), %r13
movl (%r13), %edi
callq 0x8b70
movl %eax, (%r13)
movq $-0x3, %rbp
movswl (%r14,%rbp,2), %edi
callq 0x8b68
movw %ax, (%r14,%rbp,2)
movswl 0x6(%r14,%rbp,2), %edi
callq 0x8b68
movw %ax, 0x6(%r14,%rbp,2)
incq %rbp
jne 0x5637
movswl 0x4(%r13), %edi
callq 0x8b68
movw %ax, 0x4(%r13)
movswl 0x6(%r13), %edi
callq 0x8b68
movw %ax, 0x6(%r13)
movswl 0x14(%r13), %edi
callq 0x8b68
movw %ax, 0x14(%r13)
movswl 0x16(%r13), %edi
callq 0x8b68
movw %ax, 0x16(%r13)
incq %r15
movslq 0x10cbd86(%rip), %rax   # 0x10d1428
addq $0x18, %r14
cmpq %rax, %r15
jl 0x561b
cmpl $0x0, 0xa2be2e(%rip)    # 0xa314e4
jle 0x5757
leaq 0xa2be3b(%rip), %r14    # 0xa314fe
xorl %r15d, %r15d
leaq 0xa2be23(%rip), %r12    # 0xa314f0
leaq (%r15,%r15,8), %rax
leaq (%rax,%rax,2), %rbx
addq %r15, %rbx
leaq (%r12,%rbx), %r13
movl (%r12,%rbx), %edi
callq 0x8b70
movl %eax, (%r12,%rbx)
movq $-0x3, %rbp
movswl (%r14,%rbp,2), %edi
callq 0x8b68
movw %ax, (%r14,%rbp,2)
movswl 0x6(%r14,%rbp,2), %edi
callq 0x8b68
movw %ax, 0x6(%r14,%rbp,2)
incq %rbp
jne 0x56f0
movswl 0x14(%r13), %edi
callq 0x8b68
movw %ax, 0x14(%r13)
movswl 0x16(%r13), %edi
callq 0x8b68
movw %ax, 0x16(%r13)
movl 0x4(%r13), %edi
callq 0x8b70
movl %eax, 0x4(%r13)
incq %r15
movslq 0xa2bd9a(%rip), %rax    # 0xa314e4
addq $0x1c, %r14
cmpq %rax, %r15
jl 0x56cd
cmpl $0x0, 0x1ccbcc2(%rip)   # 0x1cd1420
jle 0x57a8
xorl %r14d, %r14d
leaq 0x1ccbcc6(%rip), %r15   # 0x1cd1430
movl (%r15,%r14,8), %edi
callq 0x8b70
movl %eax, (%r15,%r14,8)
movswl 0x4(%r15,%r14,8), %edi
callq 0x8b68
movw %ax, 0x4(%r15,%r14,8)
movswl 0x6(%r15,%r14,8), %edi
callq 0x8b68
movw %ax, 0x6(%r15,%r14,8)
incq %r14
movslq 0x1ccbc7d(%rip), %rax   # 0x1cd1420
cmpq %rax, %r14
jl 0x576a
cmpl $0x0, 0x82bd15(%rip)    # 0x8314c4
je 0x57f6
movq 0x82bd10(%rip), %r14    # 0x8314c8
movl (%r14), %ebp
cmpb $0x0, 0x4(%rsp)
jne 0x57cb
movl %ebp, %edi
callq 0x8b70
movl %eax, %ebp
movl (%r14), %edi
callq 0x8b70
movl %eax, (%r14)
testl %ebp, %ebp
jle 0x57f6
movl %ebp, %ebx
xorl %r15d, %r15d
movl 0x4(%r14,%r15,4), %edi
callq 0x8b70
movl %eax, 0x4(%r14,%r15,4)
incq %r15
cmpq %r15, %rbx
jne 0x57df
cmpl $0x0, 0x1e05c37(%rip)   # 0x1e0b434
jle 0x5827
leaq 0x1e05c3a(%rip), %rbx   # 0x1e0b440
xorl %r14d, %r14d
movswl (%rbx), %edi
callq 0x8b68
movw %ax, (%rbx)
incq %r14
movslq 0x1e05c16(%rip), %rax   # 0x1e0b434
addq $0x2, %rbx
cmpq %rax, %r14
jl 0x5809
cmpl $0x0, 0x1e25c16(%rip)   # 0x1e2b444
jle 0x5856
leaq 0x1e25c19(%rip), %rbx   # 0x1e2b450
xorl %r14d, %r14d
movl (%rbx), %edi
callq 0x8b70
movl %eax, (%rbx)
incq %r14
movslq 0x1e25bf7(%rip), %rax   # 0x1e2b444
addq $0x4, %rbx
cmpq %rax, %r14
jl 0x583a
cmpl $0x0, 0x1d0bbcf(%rip)   # 0x1d1142c
jle 0x5897
xorl %ebx, %ebx
leaq 0x1d0bbc8(%rip), %r14   # 0x1d11430
movswl (%r14,%rbx,4), %edi
callq 0x8b68
movw %ax, (%r14,%rbx,4)
movswl 0x2(%r14,%rbx,4), %edi
callq 0x8b68
movw %ax, 0x2(%r14,%rbx,4)
incq %rbx
movslq 0x1d0bb9a(%rip), %rax   # 0x1d1142c
cmpq %rax, %rbx
jl 0x5868
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq