// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/common/bspfile.cpp
// Function: UnparseEntities()
void            UnparseEntities()
{
    char*           buf;
    char*           end;
    epair_t*        ep;
    char            line[MAXTOKEN];
    int             i;

    buf = g_dentdata;
    end = buf;
    *end = 0;

#ifdef ZHLT_ENTITY_INFOSUNLIGHT
	for (i = 0; i < g_numentities; i++)
	{
		entity_t *mapent = &g_entities[i];
		if (!strcmp (ValueForKey (mapent, "classname"), "info_sunlight") ||
			!strcmp (ValueForKey (mapent, "classname"), "light_environment") )
		{
			float vec[3] = {0,0,0};
			{
				sscanf (ValueForKey (mapent, "angles"), "%f %f %f", &vec[0], &vec[1], &vec[2]);
				float pitch = FloatForKey(mapent, "pitch");
				if (pitch)
					vec[0] = pitch;

				const char *target = ValueForKey (mapent, "target");
				if (target[0])
				{
					entity_t *targetent = FindTargetEntity (target);
					if (targetent)
					{
						float origin1[3] = {0,0,0}, origin2[3] = {0,0,0}, normal[3];
						sscanf (ValueForKey (mapent, "origin"), "%f %f %f", &origin1[0], &origin1[1], &origin1[2]);
						sscanf (ValueForKey (targetent, "origin"), "%f %f %f", &origin2[0], &origin2[1], &origin2[2]);
						VectorSubtract (origin2, origin1, normal);
						anglesforvector (vec, normal);
					}
				}
			}
			char stmp[1024];
			safe_snprintf (stmp, 1024, "%g %g %g", vec[0], vec[1], vec[2]);
			SetKeyValue (mapent, "angles", stmp);
			DeleteKey (mapent, "pitch");

			if (!strcmp (ValueForKey (mapent, "classname"), "info_sunlight"))
			{
				if (g_numentities == MAX_MAP_ENTITIES)
				{
					Error("g_numentities == MAX_MAP_ENTITIES");
				}
				entity_t *newent = &g_entities[g_numentities++];
				newent->epairs = mapent->epairs;
				SetKeyValue (newent, "classname", "light_environment");
				SetKeyValue (newent, "_fake", "1");
				mapent->epairs = NULL;
			}
		}
	}
#endif
#ifdef ZHLT_ENTITY_LIGHTSHADOW
    for (i = 0; i < g_numentities; i++)
	{
		entity_t *mapent = &g_entities[i];
		if (!strcmp (ValueForKey (mapent, "classname"), "light_shadow")
	#ifdef ZHLT_ENTITY_LIGHTBOUNCE
			|| !strcmp (ValueForKey (mapent, "classname"), "light_bounce")
	#endif
			)
		{
			SetKeyValue (mapent, "convertfrom", ValueForKey (mapent, "classname"));
			SetKeyValue (mapent, "classname", (*ValueForKey (mapent, "convertto")? ValueForKey (mapent, "convertto"): "light"));
			SetKeyValue (mapent, "convertto", "");
		}
	}
#endif
#ifdef ZHLT_ENTITY_LIGHTSURFACE
	// ugly code
	for (i = 0; i < g_numentities; i++)
	{
		entity_t *mapent = &g_entities[i];
		if (!strcmp (ValueForKey (mapent, "classname"), "light_surface"))
		{
			if (!*ValueForKey (mapent, "_tex"))
			{
				SetKeyValue (mapent, "_tex", "                ");
			}
			const char *newclassname = ValueForKey (mapent, "convertto");
			if (!*newclassname)
			{
				SetKeyValue (mapent, "classname", "light");
			}
			else if (strncmp (newclassname, "light", 5))
			{
				Error ("New classname for 'light_surface' should begin with 'light' not '%s'.\n", newclassname);
			}
			else
			{
				SetKeyValue (mapent, "classname", newclassname);
			}
			SetKeyValue (mapent, "convertto", "");
		}
	}
#endif
#ifdef HLCSG_OPTIMIZELIGHTENTITY
#ifdef HLCSG
	extern bool g_nolightopt;
	if (!g_nolightopt)
	{
		int i, j;
		int count = 0;
		bool *lightneedcompare = (bool *)malloc (g_numentities * sizeof (bool));
		hlassume (lightneedcompare != NULL, assume_NoMemory);
		memset (lightneedcompare, 0, g_numentities * sizeof(bool));
		for (i = g_numentities - 1; i > -1; i--)
		{
			entity_t *ent = &g_entities[i];
			const char *classname = ValueForKey (ent, "classname");
			const char *targetname = ValueForKey (ent, "targetname");
			int style = IntForKey (ent, "style");
			if (!targetname[0] || strcmp (classname, "light") && strcmp (classname, "light_spot") && strcmp (classname, "light_environment"))
				continue;
			for (j = i + 1; j < g_numentities; j++)
			{
				if (!lightneedcompare[j])
					continue;
				entity_t *ent2 = &g_entities[j];
				const char *targetname2 = ValueForKey (ent2, "targetname");
				int style2 = IntForKey (ent2, "style");
				if (style == style2 && !strcmp (targetname, targetname2))
					break;
			}
			if (j < g_numentities)
			{
				DeleteKey (ent, "targetname");
				count++;
			}
			else
			{
				lightneedcompare[i] = true;
			}
		}
		if (count > 0)
		{
			Log ("%d redundant named lights optimized.\n", count);
		}
		free (lightneedcompare);
	}
#endif
#endif
    for (i = 0; i < g_numentities; i++)
    {
        ep = g_entities[i].epairs;
        if (!ep)
        {
            continue;                                      // ent got removed
        }

        strcat(end, "{\n");
        end += 2;

        for (ep = g_entities[i].epairs; ep; ep = ep->next)
        {
            sprintf(line, "\"%s\" \"%s\"\n", ep->key, ep->value);
            strcat(end, line);
            end += strlen(line);
        }
        strcat(end, "}\n");
        end += 2;

        if (end > buf + MAX_MAP_ENTSTRING)
        {
            Error("Entity text too long");
        }
    }
    g_entdatasize = end - buf + 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1068, %rsp           # imm = 0x1068
movb $0x0, 0x828868(%rip)    # 0x82f4e0
leaq 0x20167e1(%rip), %rbp   # 0x201d460
leaq 0x128e4(%rip), %r15     # 0x1956a
leaq 0x12999(%rip), %rbx     # 0x19626
xorl %r14d, %r14d
movslq 0x20167bd(%rip), %r12   # 0x201d454
cmpq %r12, %r14
jge 0x6ef0
movq %rbp, %rdi
movq %r15, %rsi
callq 0x69ab
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
testl %eax, %eax
je 0x6cd4
movq %r13, %rdi
leaq 0x1294d(%rip), %rsi     # 0x19614
callq 0x4480
testl %eax, %eax
jne 0x6ee4
andl $0x0, 0x8(%rsp)
andq $0x0, (%rsp)
movq %rbp, %rdi
leaq 0x12952(%rip), %rsi     # 0x1963a
callq 0x69ab
movq %rax, %rdi
leaq 0x1294a(%rip), %rsi     # 0x19641
movq %rsp, %rdx
leaq 0x4(%rsp), %rcx
leaq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x42f0
movq %rbp, %rdi
leaq 0x12935(%rip), %rsi     # 0x1964a
callq 0x7375
cvtsd2ss %xmm0, %xmm0
ucomiss 0x1234b(%rip), %xmm0    # 0x19070
jne 0x6d29
jnp 0x6d2e
movss %xmm0, (%rsp)
movq %rbp, %rdi
leaq 0x12918(%rip), %rsi     # 0x19650
callq 0x69ab
cmpb $0x0, (%rax)
je 0x6e0c
movq %rax, %rdi
callq 0x7384
testq %rax, %rax
je 0x6e0c
movq %rax, %r13
andl $0x0, 0x68(%rsp)
andq $0x0, 0x60(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
movq %rbp, %rdi
movq %rbx, %r15
leaq 0x17c90(%rip), %rbx     # 0x1ea0d
movq %rbx, %rsi
callq 0x69ab
movq %rax, %rdi
leaq 0x128b2(%rip), %r12     # 0x19641
movq %r12, %rsi
leaq 0x60(%rsp), %rdx
leaq 0x64(%rsp), %rcx
leaq 0x68(%rsp), %r8
xorl %eax, %eax
callq 0x42f0
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rbx
leaq 0x127b2(%rip), %r15     # 0x1956a
callq 0x69ab
movq %rax, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
leaq 0x24(%rsp), %rcx
leaq 0x28(%rsp), %r8
xorl %eax, %eax
callq 0x42f0
movsd 0x20(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
subps %xmm1, %xmm0
movlps %xmm0, 0x50(%rsp)
movss 0x28(%rsp), %xmm0
subss 0x68(%rsp), %xmm0
movss %xmm0, 0x58(%rsp)
movq %rsp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x6b13
cvtss2sd (%rsp), %xmm0
cvtss2sd 0x4(%rsp), %xmm1
cvtss2sd 0x8(%rsp), %xmm2
movl $0x400, %esi            # imm = 0x400
leaq 0x60(%rsp), %r12
movq %r12, %rdi
leaq 0x12826(%rip), %rdx     # 0x19657
movb $0x3, %al
callq 0x78cb
movq %rbp, %rdi
leaq 0x127f8(%rip), %rsi     # 0x1963a
movq %r12, %rdx
callq 0x69e6
movq %rbp, %rdi
leaq 0x127f6(%rip), %rsi     # 0x1964a
callq 0x6a8a
movq %rbp, %rdi
movq %r15, %rsi
callq 0x69ab
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
testl %eax, %eax
jne 0x6ee4
movl 0x20165db(%rip), %eax   # 0x201d454
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x6e94
leaq 0x1269a(%rip), %rdi     # 0x19521
xorl %eax, %eax
callq 0x8d7b
movl 0x20165c0(%rip), %eax   # 0x201d454
leal 0x1(%rax), %ecx
movl %ecx, 0x20165b7(%rip)   # 0x201d454
cltq
imulq $0x28, %rax, %rax
leaq 0x20165b6(%rip), %rdx   # 0x201d460
leaq (%rdx,%rax), %r13
movq 0x20(%rbp), %rcx
movq %rcx, 0x20(%rax,%rdx)
movq %r13, %rdi
movq %r15, %rsi
leaq 0x12750(%rip), %rdx     # 0x19614
callq 0x69e6
movq %r13, %rdi
leaq 0x12761(%rip), %rsi     # 0x19634
leaq 0x13b10(%rip), %rdx     # 0x1a9ea
callq 0x69e6
andq $0x0, 0x20(%rbp)
incq %r14
addq $0x28, %rbp
jmp 0x6c90
leaq 0x2016569(%rip), %rbx   # 0x201d460
xorl %r15d, %r15d
leaq 0x12669(%rip), %r14     # 0x1956a
leaq 0x126ce(%rip), %r13     # 0x195d6
movslq %r12d, %rax
cmpq %rax, %r15
jge 0x6fa4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x69ab
movq %rax, %rbp
movq %rax, %rdi
leaq 0x126ce(%rip), %rsi     # 0x195fa
callq 0x4480
testl %eax, %eax
je 0x6f48
movq %rbp, %rdi
leaq 0x126c8(%rip), %rsi     # 0x19607
callq 0x4480
testl %eax, %eax
jne 0x6f98
movq %rbx, %rdi
leaq 0x1269c(%rip), %rsi     # 0x195ee
movq %rbp, %rdx
callq 0x69e6
movq %rbx, %rdi
movq %r13, %rsi
callq 0x69ab
cmpb $0x0, (%rax)
leaq 0x126bf(%rip), %rdx     # 0x1962e
je 0x6f74
movq %rax, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x69e6
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x13c8d(%rip), %rdx     # 0x1ac19
callq 0x69e6
movl 0x20164bc(%rip), %r12d  # 0x201d454
incq %r15
addq $0x28, %rbx
jmp 0x6f08
leaq 0x20164b5(%rip), %rbx   # 0x201d460
leaq 0x125b8(%rip), %r14     # 0x1956a
leaq 0x12627(%rip), %r15     # 0x195e0
leaq 0x12616(%rip), %r13     # 0x195d6
xorl %ebp, %ebp
movslq %r12d, %rdi
cmpq %rdi, %rbp
jge 0x709a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x69ab
movq %rax, %rdi
movq %r15, %rsi
callq 0x4480
testl %eax, %eax
jne 0x708e
movq %rbx, %rdi
leaq 0x125db(%rip), %rsi     # 0x195d1
callq 0x69ab
cmpb $0x0, (%rax)
jne 0x7016
movq %rbx, %rdi
leaq 0x125c7(%rip), %rsi     # 0x195d1
leaq 0x1264f(%rip), %rdx     # 0x19660
callq 0x69e6
movq %rbx, %rdi
movq %r13, %r12
movq %r13, %rsi
callq 0x69ab
cmpb $0x0, (%rax)
je 0x7055
movq %rax, %r13
movq %rax, %rdi
leaq 0x125f8(%rip), %rsi     # 0x1962e
pushq $0x5
popq %rdx
callq 0x41c0
testl %eax, %eax
je 0x7064
leaq 0x12628(%rip), %rdi     # 0x19671
movq %r13, %rsi
xorl %eax, %eax
callq 0x8d7b
jmp 0x7072
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x125cc(%rip), %rdx     # 0x1962e
jmp 0x706d
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x69e6
movq %rbx, %rdi
movq %r12, %r13
movq %r12, %rsi
leaq 0x13b97(%rip), %rdx     # 0x1ac19
callq 0x69e6
movl 0x20163c6(%rip), %r12d  # 0x201d454
incq %rbp
addq $0x28, %rbx
jmp 0x6fc2
leaq 0x61c45c1(%rip), %rax   # 0x61cb662
cmpb $0x0, (%rax)
jne 0x7273
callq 0x4450
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x9517
movl 0x201638b(%rip), %ecx   # 0x201d454
movq %rcx, 0x10(%rsp)
movslq %ecx, %rdx
movl $0x0, 0xc(%rsp)
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41e0
leaq 0x2016371(%rip), %r13   # 0x201d460
movl 0x201635e(%rip), %r12d  # 0x201d454
movq 0x10(%rsp), %r15
imulq $0x28, %r15, %rax
addq %r13, %rax
movq %rax, 0x40(%rsp)
movq 0x30(%rsp), %rax
addq %r15, %rax
movq %rax, 0x38(%rsp)
testl %r15d, %r15d
jle 0x7253
movq %r15, %rax
decq %rax
movq %rax, 0x10(%rsp)
imulq $0x28, %rax, %r14
addq %r13, %r14
movq %r14, %rdi
leaq 0x12431(%rip), %rsi     # 0x1956a
callq 0x69ab
movq %rax, %rbp
movq %r14, %rdi
leaq 0x1256d(%rip), %rsi     # 0x196b8
callq 0x69ab
movq %r13, %rbx
movq %rax, %r13
movq %r14, 0x48(%rsp)
movq %r14, %rdi
leaq 0x16af9(%rip), %rsi     # 0x1dc5e
callq 0x6adf
movq %r13, 0x18(%rsp)
cmpb $0x0, (%r13)
movq %rbx, %r13
je 0x7224
movl %eax, %r14d
movq %rbp, %rdi
leaq 0x124a4(%rip), %rsi     # 0x1962e
callq 0x4480
testl %eax, %eax
je 0x71b9
movq %rbp, %rdi
leaq 0x12526(%rip), %rsi     # 0x196c3
callq 0x4480
testl %eax, %eax
je 0x71b9
movq %rbp, %rdi
leaq 0x12464(%rip), %rsi     # 0x19614
callq 0x4480
testl %eax, %eax
jne 0x7224
movq 0x38(%rsp), %r13
movq 0x40(%rsp), %rbx
cmpl %r15d, %r12d
jle 0x720f
cmpb $0x1, (%r13)
jne 0x7203
movq %rbx, %rdi
leaq 0x16a85(%rip), %rsi     # 0x1dc5e
callq 0x6adf
cmpl %eax, %r14d
jne 0x7203
movq %rbx, %rdi
leaq 0x124cb(%rip), %rsi     # 0x196b8
callq 0x69ab
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x4480
testl %eax, %eax
je 0x7239
addq $0x28, %rbx
incq %r13
incl %r15d
jmp 0x71c3
movq 0x30(%rsp), %rax
movq 0x10(%rsp), %rcx
movb $0x1, (%rax,%rcx)
leaq 0x201623c(%rip), %r13   # 0x201d460
addq $-0x28, 0x40(%rsp)
decq 0x38(%rsp)
movq 0x10(%rsp), %r15
jmp 0x7114
movq 0x48(%rsp), %rdi
leaq 0x12473(%rip), %rsi     # 0x196b8
callq 0x6a8a
incl 0xc(%rsp)
jmp 0x70e8
movl 0xc(%rsp), %esi
testl %esi, %esi
je 0x7269
leaq 0x1246c(%rip), %rdi     # 0x196ce
xorl %eax, %eax
callq 0x89dc
movq 0x30(%rsp), %rdi
callq 0x43b0
leaq 0x828266(%rip), %r13    # 0x82f4e0
leaq 0x12476(%rip), %r14     # 0x196f7
leaq 0x60(%rsp), %r15
xorl %edx, %edx
xorl %ebx, %ebx
leaq (%rbx,%r13), %r12
movslq 0x20161bf(%rip), %rax   # 0x201d454
cmpq %rax, %rdx
jge 0x734f
imulq $0x28, %rdx, %rax
leaq 0x20161b7(%rip), %rcx   # 0x201d460
movq 0x20(%rax,%rcx), %rbp
testq %rbp, %rbp
je 0x7347
movq %rdx, 0x18(%rsp)
movq %r12, %rdi
callq 0x4190
movw $0xa7b, (%r12,%rax)     # imm = 0xA7B
movb $0x0, 0x2(%r12,%rax)
addq $0x2, %rbx
leaq (%rbx,%r13), %r12
testq %rbp, %rbp
je 0x730f
movq 0x8(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq %r15, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x40c0
movq %r12, %rdi
movq %r15, %rsi
callq 0x45a0
movq %r15, %rdi
callq 0x4190
addq %rax, %rbx
movq (%rbp), %rbp
jmp 0x72d5
movq %r12, %rdi
callq 0x4190
movw $0xa7d, (%r12,%rax)     # imm = 0xA7D
movb $0x0, 0x2(%r12,%rax)
leaq 0x2(%rbx), %r12
cmpq $0x1fffff, %rbx         # imm = 0x1FFFFF
jl 0x733f
leaq 0x123cd(%rip), %rdi     # 0x19705
xorl %eax, %eax
callq 0x8d7b
movq 0x18(%rsp), %rdx
movq %r12, %rbx
incq %rdx
jmp 0x728a
leaq 0x82818a(%rip), %rax    # 0x82f4e0
subl %eax, %r12d
incl %r12d
movl %r12d, 0x828171(%rip)   # 0x82f4d4
addq $0x1068, %rsp           # imm = 0x1068
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq