// File: /workspace/github_c_makefile_star2_O3/QwazyWabbitWOS[P]lmctf60/g_skins.c
// Function: SkinsReadFile
void SkinsReadFile()
{
	FILE	*fp;

	char	name[MAX_INFO_STRING], *newb = NULL;
	size_t	size;
	char    *tempbuf;
	char	line[MAX_INFO_STRING] = { 0 };
	int		colorindex;
	char	skin[MAX_INFO_STRING] = { 0 };
	char	model[MAX_INFO_STRING] = { 0 };
	int		red, blue;
	int		count, ch;

	strcpy(name, gamedir->string);
	strcat(name, "/");
	strcat(name, skin_file->string);

	
	// Open the skins file
	fp = fopen (name, "rt");
	if (!fp)
		return;

	// Prescan to get size we need.
	for (count = 0; (ch = fgetc(fp)) != EOF; count++);
	fseek(fp, 0, SEEK_SET);  //rewind

	// Read the whole skins file and place it in a spot of memory
	
	tempbuf = (char *) gi.TagMalloc(count + 2, TAG_GAME);
	if (tempbuf)
	{
		size = fread(tempbuf, 1, count, fp);
		fclose(fp);

		if (size)
		{
			newb = tempbuf;
			newb[size] = 0;
		}
		else
		{
			if (size >= (size_t)count + 1)
				gi.dprintf("Error: skins file truncated.\n");
			else
				gi.dprintf("Error: skins file empty.\n");
			return;
		}

	}
	else
	{
		gi.dprintf("Error: unable to allocate memory for entities.\n");
		return;
	}

	// Parse our input
	// One list of red skins, one of blue
	// Each list contains model and skin name
	
	red = blue = 0;
	skinlist[SKIN_RED][red][SKIN_MODELNAME] = NULL;
	skinlist[SKIN_RED][red][SKIN_SKINNAME] = NULL;
	skinlist[SKIN_BLUE][blue][SKIN_MODELNAME] = NULL;
	skinlist[SKIN_BLUE][blue][SKIN_SKINNAME] = NULL;

	if (!newb)
		return; // No valid file

	skinlistinuse = true;
	colorindex = SKIN_RED;

	while (tempbuf - newb < (ptrdiff_t)size && sscanf(tempbuf,"%[^\n]", line))
	{
		tempbuf += strlen(line);
		
		// Step over newline
		while (*tempbuf == '\n')
			tempbuf++;

		// Ignore lines with # or / or ;
		if (line[0] == '#' || line[0] == '/' || line[0] == ';')
			continue;

		// Determine which skinset we are dealing with
		if (!strcmp(line, "[red]"))
			colorindex = SKIN_RED;
		if (!strcmp(line, "[blue]"))
			colorindex = SKIN_BLUE;

		// If it is a skin, add it to the list
		if (strlen(line) && sscanf(line, "%[^/]/%s", model, skin) == 2)
		{
			// Add to the red list?
			if (colorindex == SKIN_RED)
			{
				skinlist[SKIN_RED][red][SKIN_MODELNAME] = CopyString(model);
				skinlist[SKIN_RED][red][SKIN_SKINNAME] = CopyString(skin);
				red++;
				skinlist[SKIN_RED][red][SKIN_MODELNAME] = NULL;
				skinlist[SKIN_RED][red][SKIN_SKINNAME] = NULL;
			}
			else if (colorindex == SKIN_BLUE)
			{
				skinlist[SKIN_BLUE][blue][SKIN_MODELNAME] = CopyString(model);
				skinlist[SKIN_BLUE][blue][SKIN_SKINNAME] = CopyString(skin);
				blue++;
				skinlist[SKIN_BLUE][blue][SKIN_MODELNAME] = NULL;
				skinlist[SKIN_BLUE][blue][SKIN_SKINNAME] = NULL;
			}

		}
	}

	// Free the memory
	gi.TagFree(newb);

	if (skin_debug->value)
	{
		red = 0;
		gi.dprintf("[red]\n");
		while (skinlist[SKIN_RED][red][SKIN_MODELNAME])
		{
			gi.dprintf("MODEL: [%s]  SKIN: [%s]\n",
				skinlist[SKIN_RED][red][SKIN_MODELNAME],
				skinlist[SKIN_RED][red][SKIN_SKINNAME]);
			red++;
		}
		red = 0;
		gi.dprintf("\n[blue]\n");
		while (skinlist[SKIN_BLUE][red][SKIN_MODELNAME])
		{
			gi.dprintf("MODEL: [%s]  SKIN: [%s]\n",
				skinlist[SKIN_BLUE][red][SKIN_MODELNAME],
				skinlist[SKIN_BLUE][red][SKIN_SKINNAME]);
			red++;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x848, %rsp            # imm = 0x848
leaq 0x240(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x18880
leaq 0x40(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x18880
leaq 0x440(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x18880
movq 0x464b4(%rip), %rax     # 0x68eb0
movq (%rax), %rax
movq 0x8(%rax), %rsi
leaq 0x640(%rsp), %rbx
movq %rbx, %rdi
callq 0x182a0
movq %rbx, %rdi
callq 0x18680
movw $0x2f, 0x640(%rsp,%rax)
movq 0x45c6c(%rip), %rax     # 0x68698
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x19640
leaq 0x30b26(%rip), %rsi     # 0x53568
movq %rbx, %rdi
callq 0x19470
testq %rax, %rax
je 0x22e26
movq %rax, %rbx
movq $-0x1, %r14
movq %rbx, %rdi
callq 0x18900
incq %r14
cmpl $-0x1, %eax
jne 0x22a5d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x19100
movq 0x45b80(%rip), %r13     # 0x68600
leal 0x2(%r14), %edi
movl $0x2fd, %esi            # imm = 0x2FD
callq *0x108(%r13)
testq %rax, %rax
je 0x22e10
movq %rax, %r15
movl $0x1, %esi
movq %rax, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x183f0
movq %rax, %r14
movq %rbx, %rdi
callq 0x185a0
testq %r14, %r14
je 0x22e19
movq %r15, 0x30(%rsp)
movb $0x0, (%r15,%r14)
xorps %xmm0, %xmm0
movq 0x45aa1(%rip), %rax     # 0x68578
movaps %xmm0, (%rax)
movaps %xmm0, 0x1000(%rax)
movq 0x45f20(%rip), %rax     # 0x68a08
movl $0x1, (%rax)
jle 0x22d5f
movq 0x30(%rsp), %r15
movq %r15, %rbp
notq %rbp
leaq 0x240(%rsp), %r12
movl $0x0, 0x28(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movq %r15, %rdi
leaq 0x32260(%rip), %rsi     # 0x54d89
movq %r12, %rdx
xorl %eax, %eax
callq 0x18fe0
testl %eax, %eax
je 0x22d5f
movq %r12, %rdi
callq 0x18680
leaq (%r15,%rax), %rcx
decq %rcx
addq %rbp, %r15
addq %rax, %r15
movq %r15, %rbx
movq %rcx, %r15
incq %rbx
cmpb $0xa, 0x1(%r15)
leaq 0x1(%r15), %r15
je 0x22b56
movzbl 0x240(%rsp), %eax
cmpq $0x3b, %rax
ja 0x22b8c
movabsq $0x800800800000000, %rcx # imm = 0x800800800000000
btq %rax, %rcx
jae 0x22b8c
cmpq %r14, %rbx
jl 0x22b1f
jmp 0x22d5f
movl 0x240(%rsp), %ecx
movl %ecx, %edx
xorl $0x6465725b, %edx       # imm = 0x6465725B
movzwl 0x244(%rsp), %esi
xorl $0x5d, %esi
orl %edx, %esi
movl $0x0, %edx
movl 0x14(%rsp), %edi
cmovel %edx, %edi
xorl $0x756c625b, %ecx       # imm = 0x756C625B
movl 0x243(%rsp), %edx
movl $0x5d6575, %esi         # imm = 0x5D6575
xorl %esi, %edx
orl %ecx, %edx
movl $0x1, %ecx
cmovel %ecx, %edi
movl %edi, 0x14(%rsp)
testb %al, %al
je 0x22b82
movq %r12, %rdi
leaq 0x30d74(%rip), %rsi     # 0x53958
leaq 0x440(%rsp), %rdx
leaq 0x40(%rsp), %rcx
xorl %eax, %eax
callq 0x18fe0
cmpl $0x2, %eax
jne 0x22b82
movq 0x108(%r13), %rax
movq %rax, 0x18(%rsp)
leaq 0x440(%rsp), %rdi
callq 0x18680
leal 0x1(%rax), %edi
movl $0x2fd, %esi            # imm = 0x2FD
callq *0x18(%rsp)
movq %rax, 0x8(%rsp)
movq %rax, %rdi
leaq 0x440(%rsp), %rsi
callq 0x182a0
cmpl $0x0, 0x14(%rsp)
je 0x22cd4
movslq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq 0x45925(%rip), %rax     # 0x68578
leaq 0x1000(%rax), %rdx
movq %rdx, 0x38(%rsp)
shlq $0x4, %rcx
movq %rcx, 0x28(%rsp)
movq 0x8(%rsp), %rdx
movq %rdx, 0x1000(%rax,%rcx)
movq 0x108(%r13), %rax
movq %rax, 0x8(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x18680
leal 0x1(%rax), %edi
movl $0x2fd, %esi            # imm = 0x2FD
callq *0x8(%rsp)
movq %rax, 0x8(%rsp)
movq %rax, %rdi
leaq 0x40(%rsp), %rsi
callq 0x182a0
movq 0x38(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %rax
movq 0x458b9(%rip), %rsi     # 0x68578
movq 0x8(%rsp), %rdi
movq %rdi, 0x1008(%rsi,%rax)
incl %ecx
movl %ecx, 0x28(%rsp)
jmp 0x22d4c
movslq 0x20(%rsp), %rcx
movq %rcx, 0x18(%rsp)
shlq $0x4, %rcx
movq %rcx, 0x20(%rsp)
movq 0x4588a(%rip), %rax     # 0x68578
movq 0x8(%rsp), %rdx
movq %rdx, (%rax,%rcx)
movq 0x108(%r13), %rax
movq %rax, 0x8(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x18680
leal 0x1(%rax), %edi
movl $0x2fd, %esi            # imm = 0x2FD
callq *0x8(%rsp)
movq %rax, 0x8(%rsp)
movq %rax, %rdi
leaq 0x40(%rsp), %rsi
callq 0x182a0
movq 0x18(%rsp), %rcx
movq 0x45841(%rip), %rdx     # 0x68578
movq 0x20(%rsp), %rax
movq 0x8(%rsp), %rsi
movq %rsi, 0x8(%rdx,%rax)
incl %ecx
movl %ecx, 0x20(%rsp)
movslq %ecx, %rax
shlq $0x4, %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rdx,%rax)
jmp 0x22b82
movq 0x30(%rsp), %rdi
callq *0x110(%r13)
movq 0x45896(%rip), %rax     # 0x68608
movq (%rax), %rax
movss 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x22d88
jnp 0x22e26
leaq 0x30bd2(%rip), %rdi     # 0x53961
xorl %eax, %eax
callq *0x8(%r13)
movq 0x457dc(%rip), %r15     # 0x68578
movq (%r15), %rsi
testq %rsi, %rsi
je 0x22dc8
leaq 0x10(%r15), %r14
leaq 0x30bb9(%rip), %rbx     # 0x53968
movq -0x8(%r14), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq *0x8(%r13)
movq (%r14), %rsi
addq $0x10, %r14
testq %rsi, %rsi
jne 0x22daf
leaq 0x30bb2(%rip), %rdi     # 0x53981
xorl %eax, %eax
callq *0x8(%r13)
movq 0x1000(%r15), %rsi
testq %rsi, %rsi
je 0x22e26
movl $0x1010, %r14d          # imm = 0x1010
addq 0x4578a(%rip), %r14     # 0x68578
leaq 0x30b73(%rip), %rbx     # 0x53968
movq -0x8(%r14), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq *0x8(%r13)
movq (%r14), %rsi
addq $0x10, %r14
testq %rsi, %rsi
jne 0x22df5
jmp 0x22e26
leaq 0x30b04(%rip), %rdi     # 0x5391b
jmp 0x22e20
leaq 0x30ae1(%rip), %rdi     # 0x53901
xorl %eax, %eax
callq *0x8(%r13)
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq