// File: FreeSlave[P]vhlt/hlbsp/qbsp.cpp
// Function: ProcessFile(char const*)
static void     ProcessFile(const char* const filename)
{
    int             i;
    char            name[_MAX_PATH];

    // delete existing files
    safe_snprintf(g_portfilename, _MAX_PATH, "%s.prt", filename);
    unlink(g_portfilename);

    safe_snprintf(g_pointfilename, _MAX_PATH, "%s.pts", filename);
    unlink(g_pointfilename);

    safe_snprintf(g_linefilename, _MAX_PATH, "%s.lin", filename);
    unlink(g_linefilename);

#ifdef ZHLT_64BIT_FIX
	safe_snprintf (g_extentfilename, _MAX_PATH, "%s.ext", filename);
	unlink (g_extentfilename);
#endif
    // open the hull files
    for (i = 0; i < NUM_HULLS; i++)
    {
                   //mapname.p[0-3]
		sprintf(name, "%s.p%i", filename, i);
        polyfiles[i] = fopen(name, "r");

        if (!polyfiles[i])
            Error("Can't open %s", name);
#ifdef ZHLT_DETAILBRUSH
		sprintf(name, "%s.b%i", filename, i);
		brushfiles[i] = fopen(name, "r");
		if (!brushfiles[i])
			Error("Can't open %s", name);
#endif
    }
#ifdef HLCSG_HLBSP_ALLOWEMPTYENTITY
	{
		FILE			*f;
		char			name[_MAX_PATH];
		safe_snprintf (name, _MAX_PATH, "%s.hsz", filename);
		f = fopen (name, "r");
		if (!f)
		{
			Warning("Couldn't open %s", name);
		}
		else
		{
			float x1,y1,z1;
			float x2,y2,z2;
			for (i = 0; i < NUM_HULLS; i++)
			{
				int count;
				count = fscanf (f, "%f %f %f %f %f %f\n", &x1, &y1, &z1, &x2, &y2, &z2);
				if (count != 6)
				{
					Error ("Load hull size (line %i): scanf failure", i+1);
				}
				g_hull_size[i][0][0] = x1;
				g_hull_size[i][0][1] = y1;
				g_hull_size[i][0][2] = z1;
				g_hull_size[i][1][0] = x2;
				g_hull_size[i][1][1] = y2;
				g_hull_size[i][1][2] = z2;
			}
			fclose (f);
		}
	}
#endif

    // load the output of csg
    safe_snprintf(g_bspfilename, _MAX_PATH, "%s.bsp", filename);
    LoadBSPFile(g_bspfilename);
    ParseEntities();

    Settings(); // AJM: moved here due to info_compile_parameters entity

#ifdef HLCSG_HLBSP_DOUBLEPLANE
	{
		char name[_MAX_PATH];
		safe_snprintf (name, _MAX_PATH, "%s.pln", filename);
		FILE *planefile = fopen (name, "rb");
		if (!planefile)
		{
			Warning("Couldn't open %s", name);
#undef dplane_t
#undef g_dplanes
			for (i = 0; i < g_numplanes; i++)
			{
				plane_t *mp = &g_mapplanes[i];
				dplane_t *dp = &g_dplanes[i];
				VectorCopy (dp->normal, mp->normal);
				mp->dist = dp->dist;
				mp->type = dp->type;
			}
#define dplane_t plane_t
#define g_dplanes g_mapplanes
		}
		else
		{
			if (q_filelength (planefile) != g_numplanes * sizeof (dplane_t))
			{
				Error ("Invalid plane data");
			}
			SafeRead (planefile, g_dplanes, g_numplanes * sizeof (dplane_t));
			fclose (planefile);
		}
	}
#endif
    // init the tables to be shared by all models
    BeginBSPFile();

    // process each model individually
    while (ProcessModel())
        ;

    // write the updated bsp file out
    FinishBSPFile();
#ifdef HLBSP_DELETETEMPFILES

	// Because the bsp file has been updated, these polyfiles are no longer valid.
    for (i = 0; i < NUM_HULLS; i++)
    {
		sprintf (name, "%s.p%i", filename, i);
		fclose (polyfiles[i]);
		polyfiles[i] = NULL;
		unlink (name);
#ifdef ZHLT_DETAILBRUSH
		sprintf(name, "%s.b%i", filename, i);
		fclose (brushfiles[i]);
		brushfiles[i] = NULL;
		unlink (name);
#endif
    }
#ifdef HLCSG_HLBSP_ALLOWEMPTYENTITY
	safe_snprintf (name, _MAX_PATH, "%s.hsz", filename);
	unlink (name);
#endif
#ifdef HLCSG_HLBSP_DOUBLEPLANE
	safe_snprintf (name, _MAX_PATH, "%s.pln", filename);
	unlink (name);
#endif
#endif
}

// Assembly:
subq $0x3078, %rsp           # imm = 0x3078
movq %rdi, 0x3070(%rsp)
movq 0x3070(%rsp), %rcx
leaq 0x20c6db2(%rip), %rdi   # 0x20eda20
movl $0x1000, %esi           # imm = 0x1000
leaq 0x10e23(%rip), %rdx     # 0x37a9d
movb $0x0, %al
callq 0x9470
leaq 0x20c6d98(%rip), %rdi   # 0x20eda20
callq 0x4150
movq 0x3070(%rsp), %rcx
leaq 0x20c4d84(%rip), %rdi   # 0x20eba20
movl $0x1000, %esi           # imm = 0x1000
leaq 0x10dfc(%rip), %rdx     # 0x37aa4
movb $0x0, %al
callq 0x9470
leaq 0x20c4d6a(%rip), %rdi   # 0x20eba20
callq 0x4150
movq 0x3070(%rsp), %rcx
leaq 0x20c5d56(%rip), %rdi   # 0x20eca20
movl $0x1000, %esi           # imm = 0x1000
leaq 0x10dd5(%rip), %rdx     # 0x37aab
movb $0x0, %al
callq 0x9470
leaq 0x20c5d3c(%rip), %rdi   # 0x20eca20
callq 0x4150
movq 0x3070(%rsp), %rcx
leaq 0x20c7d28(%rip), %rdi   # 0x20eea20
movl $0x1000, %esi           # imm = 0x1000
leaq 0x10ddb(%rip), %rdx     # 0x37adf
movb $0x0, %al
callq 0x9470
leaq 0x20c7d0e(%rip), %rdi   # 0x20eea20
callq 0x4150
movl $0x0, 0x306c(%rsp)
cmpl $0x4, 0x306c(%rsp)
jge 0x26e3e
leaq 0x2060(%rsp), %rdi
movq 0x3070(%rsp), %rdx
movl 0x306c(%rsp), %ecx
leaq 0x143a7(%rip), %rsi     # 0x3b0f5
movb $0x0, %al
callq 0x40b0
leaq 0x2060(%rsp), %rdi
leaq 0x12359(%rip), %rsi     # 0x390bd
callq 0x4360
movq %rax, %rdx
movslq 0x306c(%rsp), %rcx
leaq 0x30c8cb5(%rip), %rax   # 0x30efa30
movq %rdx, (%rax,%rcx,8)
movslq 0x306c(%rsp), %rcx
leaq 0x30c8ca2(%rip), %rax   # 0x30efa30
cmpq $0x0, (%rax,%rcx,8)
jne 0x26dab
leaq 0x2060(%rsp), %rsi
leaq 0x14358(%rip), %rdi     # 0x3b0fc
movb $0x0, %al
callq 0xb510
leaq 0x2060(%rsp), %rdi
movq 0x3070(%rsp), %rdx
movl 0x306c(%rsp), %ecx
leaq 0x14341(%rip), %rsi     # 0x3b10a
movb $0x0, %al
callq 0x40b0
leaq 0x2060(%rsp), %rdi
leaq 0x122de(%rip), %rsi     # 0x390bd
callq 0x4360
movq %rax, %rdx
movslq 0x306c(%rsp), %rcx
leaq 0x30c8c5a(%rip), %rax   # 0x30efa50
movq %rdx, (%rax,%rcx,8)
movslq 0x306c(%rsp), %rcx
leaq 0x30c8c47(%rip), %rax   # 0x30efa50
cmpq $0x0, (%rax,%rcx,8)
jne 0x26e26
leaq 0x2060(%rsp), %rsi
leaq 0x142dd(%rip), %rdi     # 0x3b0fc
movb $0x0, %al
callq 0xb510
jmp 0x26e28
movl 0x306c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x306c(%rsp)
jmp 0x26d22
leaq 0x1050(%rsp), %rdi
movq 0x3070(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x10c58(%rip), %rdx     # 0x37ab2
movb $0x0, %al
callq 0x9470
leaq 0x1050(%rsp), %rdi
leaq 0x1224d(%rip), %rsi     # 0x390bd
callq 0x4360
movq %rax, 0x2058(%rsp)
cmpq $0x0, 0x2058(%rsp)
jne 0x26ea3
leaq 0x1050(%rsp), %rsi
leaq 0x136a7(%rip), %rdi     # 0x3a53e
movb $0x0, %al
callq 0xb980
jmp 0x27046
movl $0x0, 0x306c(%rsp)
cmpl $0x4, 0x306c(%rsp)
jge 0x27039
movq 0x2058(%rsp), %rdi
leaq 0x14246(%rip), %rsi     # 0x3b111
leaq 0x104c(%rsp), %rdx
leaq 0x1048(%rsp), %rcx
leaq 0x1044(%rsp), %r8
leaq 0x1040(%rsp), %r9
leaq 0x103c(%rsp), %r10
leaq 0x1038(%rsp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x4570
movl %eax, 0x1034(%rsp)
cmpl $0x6, 0x1034(%rsp)
je 0x26f34
movl 0x306c(%rsp), %esi
addl $0x1, %esi
leaq 0x141f7(%rip), %rdi     # 0x3b124
movb $0x0, %al
callq 0xb510
movss 0x104c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movslq 0x306c(%rsp), %rcx
leaq 0x203e0(%rip), %rax     # 0x47330
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, (%rax)
movss 0x1048(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movslq 0x306c(%rsp), %rcx
leaq 0x203b9(%rip), %rax     # 0x47330
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x8(%rax)
movss 0x1044(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movslq 0x306c(%rsp), %rcx
leaq 0x20391(%rip), %rax     # 0x47330
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x10(%rax)
movss 0x1040(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movslq 0x306c(%rsp), %rcx
leaq 0x20369(%rip), %rax     # 0x47330
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x18(%rax)
movss 0x103c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movslq 0x306c(%rsp), %rcx
leaq 0x20341(%rip), %rax     # 0x47330
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x20(%rax)
movss 0x1038(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movslq 0x306c(%rsp), %rcx
leaq 0x20319(%rip), %rax     # 0x47330
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x28(%rax)
movl 0x306c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x306c(%rsp)
jmp 0x26eae
movq 0x2058(%rsp), %rdi
callq 0x42e0
movq 0x3070(%rsp), %rcx
leaq 0x20c39cb(%rip), %rdi   # 0x20eaa20
movl $0x1000, %esi           # imm = 0x1000
leaq 0x10a16(%rip), %rdx     # 0x37a77
movb $0x0, %al
callq 0x9470
leaq 0x20c39b1(%rip), %rdi   # 0x20eaa20
callq 0x4ab0
callq 0x7f20
callq 0x27380
leaq 0x30(%rsp), %rdi
movq 0x3070(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x10a22(%rip), %rdx     # 0x37ab9
movb $0x0, %al
callq 0x9470
leaq 0x30(%rsp), %rdi
leaq 0x10829(%rip), %rsi     # 0x378d3
callq 0x4360
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x271b0
leaq 0x30(%rsp), %rsi
leaq 0x13472(%rip), %rdi     # 0x3a53e
movb $0x0, %al
callq 0xb980
movl $0x0, 0x306c(%rsp)
movl 0x306c(%rsp), %eax
leaq 0xb082c8(%rip), %rcx    # 0xb2f3b4
cmpl (%rcx), %eax
jge 0x271ae
movslq 0x306c(%rsp), %rcx
leaq 0x20c892d(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movslq 0x306c(%rsp), %rcx
leaq 0xb082a2(%rip), %rax    # 0xb2f3c0
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x20(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x18(%rsp), %rax
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x20(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movq 0x18(%rsp), %rax
movss 0x8(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x20(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movq 0x18(%rsp), %rax
movss 0xc(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x20(%rsp), %rax
movsd %xmm0, 0x30(%rax)
movq 0x18(%rsp), %rax
movl 0x10(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x38(%rax)
movl 0x306c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x306c(%rsp)
jmp 0x270de
jmp 0x27208
movq 0x28(%rsp), %rdi
callq 0xaa00
cltq
leaq 0xb081f1(%rip), %rcx    # 0xb2f3b4
movslq (%rcx), %rcx
shlq $0x6, %rcx
cmpq %rcx, %rax
je 0x271dd
leaq 0x13f76(%rip), %rdi     # 0x3b14c
movb $0x0, %al
callq 0xb510
movq 0x28(%rsp), %rdi
leaq 0xb081cb(%rip), %rax    # 0xb2f3b4
movslq (%rax), %rax
shlq $0x6, %rax
movl %eax, %edx
leaq 0x20c8837(%rip), %rsi   # 0x20efa30
callq 0xab70
movq 0x28(%rsp), %rdi
callq 0x42e0
callq 0x332e0
callq 0x27760
testb $0x1, %al
jne 0x27218
jmp 0x2721a
jmp 0x2720d
callq 0x333a0
movl $0x0, 0x306c(%rsp)
cmpl $0x4, 0x306c(%rsp)
jge 0x27310
leaq 0x2060(%rsp), %rdi
movq 0x3070(%rsp), %rdx
movl 0x306c(%rsp), %ecx
leaq 0x13e9f(%rip), %rsi     # 0x3b0f5
movb $0x0, %al
callq 0x40b0
movslq 0x306c(%rsp), %rcx
leaq 0x30c87c4(%rip), %rax   # 0x30efa30
movq (%rax,%rcx,8), %rdi
callq 0x42e0
movslq 0x306c(%rsp), %rcx
leaq 0x30c87ac(%rip), %rax   # 0x30efa30
movq $0x0, (%rax,%rcx,8)
leaq 0x2060(%rsp), %rdi
callq 0x4150
leaq 0x2060(%rsp), %rdi
movq 0x3070(%rsp), %rdx
movl 0x306c(%rsp), %ecx
leaq 0x13e53(%rip), %rsi     # 0x3b10a
movb $0x0, %al
callq 0x40b0
movslq 0x306c(%rsp), %rcx
leaq 0x30c8783(%rip), %rax   # 0x30efa50
movq (%rax,%rcx,8), %rdi
callq 0x42e0
movslq 0x306c(%rsp), %rcx
leaq 0x30c876b(%rip), %rax   # 0x30efa50
movq $0x0, (%rax,%rcx,8)
leaq 0x2060(%rsp), %rdi
callq 0x4150
movl 0x306c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x306c(%rsp)
jmp 0x2722a
leaq 0x2060(%rsp), %rdi
movq 0x3070(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x10786(%rip), %rdx     # 0x37ab2
movb $0x0, %al
callq 0x9470
leaq 0x2060(%rsp), %rdi
callq 0x4150
leaq 0x2060(%rsp), %rdi
movq 0x3070(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x1075d(%rip), %rdx     # 0x37ab9
movb $0x0, %al
callq 0x9470
leaq 0x2060(%rsp), %rdi
callq 0x4150
addq $0x3078, %rsp           # imm = 0x3078
retq
nopl (%rax,%rax)