// File: /workspace/github_cpp_makefile_star2_O2/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)
leaq 0x22607(%rip), %r14     # 0x274c4
leaq 0x225f4(%rip), %r15     # 0x274b8
xorl %ebx, %ebx
pushq $-0xc
popq %rbp
movslq 0x225c0(%rip), %rax     # 0x27490
cmpq %rax, %rbx
jge 0x4f87
movq %rbx, %r12
shlq $0x6, %r12
leaq 0x225b9(%rip), %rax     # 0x274a0
addq %rax, %r12
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x4f05
movl (%r14,%r13,4), %edi
callq 0x78bc
movl %eax, (%r14,%r13,4)
incq %r13
jmp 0x4eed
movl 0x34(%r12), %edi
callq 0x78bc
movl %eax, 0x34(%r12)
movl 0x38(%r12), %edi
callq 0x78bc
movl %eax, 0x38(%r12)
movl 0x3c(%r12), %edi
callq 0x78bc
movl %eax, 0x3c(%r12)
movq %rbp, %r12
testq %r12, %r12
je 0x4f77
movss -0xc(%r15,%r12), %xmm0
callq 0x78ca
movss %xmm0, -0xc(%r15,%r12)
movss (%r15,%r12), %xmm0
callq 0x78ca
movss %xmm0, (%r15,%r12)
movss 0xc(%r15,%r12), %xmm0
callq 0x78ca
movss %xmm0, 0xc(%r15,%r12)
addq $0x4, %r12
jmp 0x4f35
incq %rbx
addq $0x40, %r14
addq $0x40, %r15
jmp 0x4ec9
leaq 0x100a4a2(%rip), %rbx   # 0x100f430
xorl %r14d, %r14d
movslq 0x100a48c(%rip), %rax   # 0x100f424
cmpq %rax, %r14
jge 0x4fc5
xorl %r15d, %r15d
cmpq $0x3, %r15
je 0x4fbc
movss (%rbx,%r15,4), %xmm0
callq 0x78ca
movss %xmm0, (%rbx,%r15,4)
incq %r15
jmp 0x4fa0
incq %r14
addq $0xc, %rbx
jmp 0x4f91
leaq 0xb0a454(%rip), %r14    # 0xb0f420
xorl %r15d, %r15d
movq %r14, %r12
movslq 0xb0a43b(%rip), %rax    # 0xb0f414
cmpq %rax, %r15
jge 0x5027
imulq $0x14, %r15, %rbx
addq %r14, %rbx
xorl %r13d, %r13d
cmpq $0x3, %r13
je 0x5004
movss (%r12,%r13,4), %xmm0
callq 0x78ca
movss %xmm0, (%r12,%r13,4)
incq %r13
jmp 0x4fe8
movss 0xc(%rbx), %xmm0
callq 0x78ca
movss %xmm0, 0xc(%rbx)
movl 0x10(%rbx), %edi
callq 0x78bc
movl %eax, 0x10(%rbx)
incq %r15
addq $0x14, %r12
jmp 0x4fd2
leaq 0x118a3f2(%rip), %r14   # 0x118f420
xorl %r15d, %r15d
movq %r14, %r12
movslq 0x118a3e1(%rip), %rax   # 0x118f41c
cmpq %rax, %r15
jge 0x5085
imulq $0x28, %r15, %rbx
addq %r14, %rbx
xorl %r13d, %r13d
cmpq $0x8, %r13
je 0x5066
movss (%r12,%r13,4), %xmm0
callq 0x78ca
movss %xmm0, (%r12,%r13,4)
incq %r13
jmp 0x504a
movl 0x20(%rbx), %edi
callq 0x78bc
movl %eax, 0x20(%rbx)
movl 0x24(%rbx), %edi
callq 0x78bc
movl %eax, 0x24(%rbx)
incq %r15
addq $0x28, %r12
jmp 0x5034
leaq 0x1b8a3a4(%rip), %r14   # 0x1b8f430
xorl %r15d, %r15d
movslq 0x1b8a38e(%rip), %rax   # 0x1b8f424
cmpq %rax, %r15
jge 0x50f8
movswl 0xa(%r14), %edi
callq 0x78b4
movw %ax, 0xa(%r14)
movswl (%r14), %edi
callq 0x78b4
movw %ax, (%r14)
movswl 0x2(%r14), %edi
callq 0x78b4
movw %ax, 0x2(%r14)
movl 0x10(%r14), %edi
callq 0x78bc
movl %eax, 0x10(%r14)
movl 0x4(%r14), %edi
callq 0x78bc
movl %eax, 0x4(%r14)
movswl 0x8(%r14), %edi
callq 0x78b4
movw %ax, 0x8(%r14)
incq %r15
addq $0x14, %r14
jmp 0x508f
leaq 0x10ca33f(%rip), %r14   # 0x10cf43e
xorl %r15d, %r15d
leaq 0x10ca327(%rip), %r12   # 0x10cf430
pushq $-0x6
popq %r13
movslq 0x10ca314(%rip), %rax   # 0x10cf428
cmpq %rax, %r15
jge 0x51a0
imulq $0x18, %r15, %rbx
leaq (%r12,%rbx), %rbp
movl (%rbx,%r12), %edi
callq 0x78bc
movl %eax, (%rbx,%r12)
movq %r13, %rbx
testq %rbx, %rbx
je 0x5160
movswl (%r14,%rbx), %edi
callq 0x78b4
movw %ax, (%r14,%rbx)
movswl 0x6(%r14,%rbx), %edi
callq 0x78b4
movw %ax, 0x6(%r14,%rbx)
addq $0x2, %rbx
jmp 0x5135
movswl 0x4(%rbp), %edi
callq 0x78b4
movw %ax, 0x4(%rbp)
movswl 0x6(%rbp), %edi
callq 0x78b4
movw %ax, 0x6(%rbp)
movswl 0x14(%rbp), %edi
callq 0x78b4
movw %ax, 0x14(%rbp)
movswl 0x16(%rbp), %edi
callq 0x78b4
movw %ax, 0x16(%rbp)
incq %r15
addq $0x18, %r14
jmp 0x510d
leaq 0xa2a357(%rip), %r14    # 0xa2f4fe
xorl %r15d, %r15d
leaq 0xa2a33f(%rip), %r12    # 0xa2f4f0
pushq $-0x6
popq %r13
movslq 0xa2a328(%rip), %rax    # 0xa2f4e4
cmpq %rax, %r15
jge 0x5232
imulq $0x1c, %r15, %rbx
leaq (%r12,%rbx), %rbp
movl (%rbx,%r12), %edi
callq 0x78bc
movl %eax, (%rbx,%r12)
movq %r13, %rbx
testq %rbx, %rbx
je 0x5204
movswl (%r14,%rbx), %edi
callq 0x78b4
movw %ax, (%r14,%rbx)
movswl 0x6(%r14,%rbx), %edi
callq 0x78b4
movw %ax, 0x6(%r14,%rbx)
addq $0x2, %rbx
jmp 0x51d9
movswl 0x14(%rbp), %edi
callq 0x78b4
movw %ax, 0x14(%rbp)
movswl 0x16(%rbp), %edi
callq 0x78b4
movw %ax, 0x16(%rbp)
movl 0x4(%rbp), %edi
callq 0x78bc
movl %eax, 0x4(%rbp)
incq %r15
addq $0x1c, %r14
jmp 0x51b5
xorl %ebx, %ebx
leaq 0x1cca1f5(%rip), %r14   # 0x1ccf430
movslq 0x1cca1de(%rip), %rax   # 0x1ccf420
cmpq %rax, %rbx
jge 0x527b
movl (%r14,%rbx,8), %edi
callq 0x78bc
movl %eax, (%r14,%rbx,8)
movswl 0x4(%r14,%rbx,8), %edi
callq 0x78b4
movw %ax, 0x4(%r14,%rbx,8)
movswl 0x6(%r14,%rbx,8), %edi
callq 0x78b4
movw %ax, 0x6(%r14,%rbx,8)
incq %rbx
jmp 0x523b
cmpl $0x0, 0x82a242(%rip)    # 0x82f4c4
je 0x52cd
movq 0x82a23d(%rip), %r14    # 0x82f4c8
movl (%r14), %edi
cmpb $0x0, 0x4(%rsp)
je 0x5299
movl %edi, %ebx
jmp 0x52a3
callq 0x78bc
movl %eax, %ebx
movl (%r14), %edi
callq 0x78bc
movl %eax, (%r14)
xorl %r15d, %r15d
testl %ebx, %ebx
cmovlel %r15d, %ebx
cmpq %r15, %rbx
je 0x52cd
movl 0x4(%r14,%r15,4), %edi
callq 0x78bc
movl %eax, 0x4(%r14,%r15,4)
incq %r15
jmp 0x52b4
leaq 0x1e0416c(%rip), %rbx   # 0x1e09440
xorl %r14d, %r14d
movslq 0x1e04156(%rip), %rax   # 0x1e09434
cmpq %rax, %r14
jge 0x52f7
movswl (%rbx), %edi
callq 0x78b4
movw %ax, (%rbx)
incq %r14
addq $0x2, %rbx
jmp 0x52d7
leaq 0x1e24152(%rip), %rbx   # 0x1e29450
xorl %r14d, %r14d
movslq 0x1e2413c(%rip), %rax   # 0x1e29444
cmpq %rax, %r14
jge 0x531f
movl (%rbx), %edi
callq 0x78bc
movl %eax, (%rbx)
incq %r14
addq $0x4, %rbx
jmp 0x5301
xorl %ebx, %ebx
leaq 0x1d0a108(%rip), %r14   # 0x1d0f430
movslq 0x1d0a0fd(%rip), %rax   # 0x1d0f42c
cmpq %rax, %rbx
jge 0x5359
movswl (%r14,%rbx,4), %edi
callq 0x78b4
movw %ax, (%r14,%rbx,4)
movswl 0x2(%r14,%rbx,4), %edi
callq 0x78b4
movw %ax, 0x2(%r14,%rbx,4)
incq %rbx
jmp 0x5328
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq