// File: FreeSlave[P]vhlt/common/bspfile.cpp
// Function: FindWadValue()
char *FindWadValue ()
	// return NULL for syntax error
	// this function needs to be as stable as possible because it might be called from ripent
{
	int linestart, lineend;
	bool inentity = false;
	for (linestart = 0; linestart < g_entdatasize; )
	{
		for (lineend = linestart; lineend < g_entdatasize; lineend++)
			if (g_dentdata[lineend] == '\r' || g_dentdata[lineend] == '\n')
				break;
		if (lineend == linestart + 1)
		{
			if (g_dentdata[linestart] == '{')
			{
				if (inentity)
					return NULL;
				inentity = true;
			}
			else if (g_dentdata[linestart] == '}')
			{
				if (!inentity)
					return NULL;
				inentity = false;
				return _strdup (""); // only parse the first entity
			}
			else
				return NULL;
		}
		else
		{
			if (!inentity)
				return NULL;
			int quotes[4];
			int i, j;
			for (i = 0, j = linestart; i < 4; i++, j++)
			{
				for (; j < lineend; j++)
					if (g_dentdata[j] == '\"')
						break;
				if (j >= lineend)
					break;
				quotes[i] = j;
			}
			if (i != 4 || quotes[0] != linestart || quotes[3] != lineend - 1)
			{
				return NULL;
			}
			if (quotes[1] - (quotes[0] + 1) == (int)strlen ("wad") && !strncmp (&g_dentdata[quotes[0] + 1], "wad", strlen ("wad")))
			{
				int len = quotes[3] - (quotes[2] + 1);
				char *value = (char *)malloc (len + 1);
				hlassume (value != NULL, assume_NoMemory);
				memcpy (value, &g_dentdata[quotes[2] + 1], len);
				value[len] = '\0';
				return value;
			}
		}
		for (linestart = lineend; linestart < g_entdatasize; linestart++)
			if (g_dentdata[linestart] != '\r' && g_dentdata[linestart] != '\n')
				break;
	}
	return NULL;
}

// Assembly:
subq $0x48, %rsp
movb $0x0, 0x37(%rsp)
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x838779(%rip), %eax    # 0x83f524
jge 0x7068
movl 0x3c(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x838761(%rip), %eax    # 0x83f524
jge 0x6e00
movslq 0x38(%rsp), %rcx
leaq 0x83875f(%rip), %rax    # 0x83f530
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
je 0x6def
movslq 0x38(%rsp), %rcx
leaq 0x83874a(%rip), %rax    # 0x83f530
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x6df1
jmp 0x6e00
jmp 0x6df3
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x6db9
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x6e9c
movslq 0x3c(%rsp), %rcx
leaq 0x838711(%rip), %rax    # 0x83f530
movsbl (%rax,%rcx), %eax
cmpl $0x7b, %eax
jne 0x6e44
testb $0x1, 0x37(%rsp)
je 0x6e3d
movq $0x0, 0x40(%rsp)
jmp 0x7071
movb $0x1, 0x37(%rsp)
jmp 0x6e97
movslq 0x3c(%rsp), %rcx
leaq 0x8386e0(%rip), %rax    # 0x83f530
movsbl (%rax,%rcx), %eax
cmpl $0x7d, %eax
jne 0x6e89
testb $0x1, 0x37(%rsp)
jne 0x6e6e
movq $0x0, 0x40(%rsp)
jmp 0x7071
movb $0x0, 0x37(%rsp)
leaq 0x24ebf(%rip), %rdi     # 0x2bd39
callq 0x46b0
movq %rax, 0x40(%rsp)
jmp 0x7071
movq $0x0, 0x40(%rsp)
jmp 0x7071
jmp 0x7014
testb $0x1, 0x37(%rsp)
jne 0x6eb1
movq $0x0, 0x40(%rsp)
jmp 0x7071
movl $0x0, 0x1c(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x18(%rsp)
cmpl $0x4, 0x1c(%rsp)
jge 0x6f2b
jmp 0x6eca
movl 0x18(%rsp), %eax
cmpl 0x38(%rsp), %eax
jge 0x6efa
movslq 0x18(%rsp), %rcx
leaq 0x838650(%rip), %rax    # 0x83f530
movsbl (%rax,%rcx), %eax
cmpl $0x22, %eax
jne 0x6eeb
jmp 0x6efa
jmp 0x6eed
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x6eca
movl 0x18(%rsp), %eax
cmpl 0x38(%rsp), %eax
jl 0x6f06
jmp 0x6f2b
movl 0x18(%rsp), %ecx
movslq 0x1c(%rsp), %rax
movl %ecx, 0x20(%rsp,%rax,4)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x6ec1
cmpl $0x4, 0x1c(%rsp)
jne 0x6f4b
movl 0x20(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jne 0x6f4b
movl 0x2c(%rsp), %eax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x6f59
movq $0x0, 0x40(%rsp)
jmp 0x7071
movl 0x24(%rsp), %eax
movl 0x20(%rsp), %ecx
addl $0x1, %ecx
subl %ecx, %eax
cmpl $0x3, %eax
jne 0x7012
movl 0x20(%rsp), %eax
addl $0x1, %eax
cltq
leaq 0x8385b1(%rip), %rdi    # 0x83f530
addq %rax, %rdi
leaq 0x27aa0(%rip), %rsi     # 0x2ea29
movl $0x3, %edx
callq 0x41f0
cmpl $0x0, %eax
jne 0x7012
movl 0x2c(%rsp), %eax
movl 0x28(%rsp), %ecx
addl $0x1, %ecx
subl %ecx, %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x4500
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xc400
movq 0x8(%rsp), %rdi
movl 0x28(%rsp), %eax
addl $0x1, %eax
cltq
leaq 0x838545(%rip), %rsi    # 0x83f530
addq %rax, %rsi
movslq 0x14(%rsp), %rdx
callq 0x4310
movq 0x8(%rsp), %rax
movslq 0x14(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x8(%rsp), %rax
movq %rax, 0x40(%rsp)
jmp 0x7071
jmp 0x7014
movl 0x38(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x8384fe(%rip), %eax    # 0x83f524
jge 0x7063
movslq 0x3c(%rsp), %rcx
leaq 0x8384fc(%rip), %rax    # 0x83f530
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
je 0x7054
movslq 0x3c(%rsp), %rcx
leaq 0x8384e7(%rip), %rax    # 0x83f530
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
je 0x7054
jmp 0x7063
jmp 0x7056
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x701c
jmp 0x6da1
movq $0x0, 0x40(%rsp)
movq 0x40(%rsp), %rax
addq $0x48, %rsp
retq
nopl (%rax,%rax)