// File: /workspace/github_c_makefile_star2_O2/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 $0x828, %rsp            # imm = 0x828
leaq 0x420(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x188e0
leaq 0x20(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x188e0
leaq 0x220(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x188e0
movq 0x45f9c(%rip), %rax     # 0x66eb0
movq (%rax), %rax
movq 0x8(%rax), %rsi
leaq 0x620(%rsp), %rbx
movq %rbx, %rdi
callq 0x182c0
movq %rbx, %rdi
callq 0x186c0
movw $0x2f, 0x620(%rsp,%rax)
movq 0x45754(%rip), %rax     # 0x66698
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x19800
leaq 0x2e60e(%rip), %rsi     # 0x4f568
movq %rbx, %rdi
callq 0x19620
testq %rax, %rax
je 0x21259
movq %rax, %rbx
pushq $-0x1
popq %r14
movq %rbx, %rdi
callq 0x18960
incq %r14
cmpl $-0x1, %eax
jne 0x20f72
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x19270
movq 0x4566b(%rip), %rbp     # 0x66600
leal 0x2(%r14), %edi
movl $0x2fd, %esi            # imm = 0x2FD
callq *0x108(%rbp)
testq %rax, %rax
je 0x2120b
movq %rax, %r13
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x18420
movq %rax, %r14
movq %rbx, %rdi
callq 0x185e0
testq %r14, %r14
je 0x2124d
movb $0x0, (%r13,%r14)
xorps %xmm0, %xmm0
movq 0x45593(%rip), %rcx     # 0x66578
movaps %xmm0, (%rcx)
movaps %xmm0, 0x1000(%rcx)
movq 0x45a12(%rip), %rax     # 0x66a08
movl $0x1, (%rax)
leaq 0x420(%rsp), %r12
leaq 0x1000(%rcx), %rax
movq %rax, 0x18(%rsp)
movq %r13, %rbx
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movq %rbx, %rax
subq %r13, %rax
cmpq %r14, %rax
jge 0x211aa
movq %rbx, %rdi
leaq 0x2fd25(%rip), %rsi     # 0x50d69
movq %r12, %rdx
xorl %eax, %eax
callq 0x19110
testl %eax, %eax
je 0x211aa
movq %r12, %rdi
callq 0x186c0
addq %rax, %rbx
decq %rbx
cmpb $0xa, 0x1(%rbx)
leaq 0x1(%rbx), %rbx
je 0x21064
movzbl 0x420(%rsp), %r15d
cmpq $0x3b, %r15
ja 0x2108d
movabsq $0x800800800000000, %rax # imm = 0x800800800000000
btq %r15, %rax
jb 0x2102b
movq %r12, %rdi
leaq 0x2e8b4(%rip), %rsi     # 0x4f94b
pushq $0x6
popq %rdx
callq 0x191e0
testl %eax, %eax
movl 0x4(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0x4(%rsp)
movq %r12, %rdi
leaq 0x2e89b(%rip), %rsi     # 0x4f951
pushq $0x7
popq %rdx
callq 0x191e0
testl %eax, %eax
pushq $0x1
popq %rax
movl 0x4(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0x4(%rsp)
testb %r15b, %r15b
je 0x2102b
movq %r12, %rdi
leaq 0x2e877(%rip), %rsi     # 0x4f958
leaq 0x220(%rsp), %rdx
leaq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0x19110
cmpl $0x2, %eax
jne 0x2102b
leaq 0x220(%rsp), %rdi
callq 0x18300
cmpl $0x0, 0x4(%rsp)
je 0x2115a
movslq 0xc(%rsp), %r15
movq %r15, 0x10(%rsp)
shlq $0x4, %r15
movq 0x45451(%rip), %rcx     # 0x66578
movq %rax, 0x1000(%rcx,%r15)
leaq 0x20(%rsp), %rdi
callq 0x18300
movq 0x45438(%rip), %rcx     # 0x66578
movq %rax, 0x1008(%rcx,%r15)
movq 0x10(%rsp), %rdx
incl %edx
movq 0x18(%rsp), %rax
movl %edx, 0xc(%rsp)
jmp 0x21197
movslq 0x8(%rsp), %r15
movq %r15, 0x10(%rsp)
shlq $0x4, %r15
movq 0x45409(%rip), %rcx     # 0x66578
movq %rax, (%rcx,%r15)
leaq 0x20(%rsp), %rdi
callq 0x18300
movq 0x453f4(%rip), %rcx     # 0x66578
movq %rax, 0x8(%rcx,%r15)
movq 0x10(%rsp), %rdx
incl %edx
movq %rcx, %rax
movl %edx, 0x8(%rsp)
movslq %edx, %rcx
shlq $0x4, %rcx
xorps %xmm0, %xmm0
movaps %xmm0, (%rax,%rcx)
jmp 0x2102b
movq %r13, %rdi
callq *0x110(%rbp)
movq 0x4544e(%rip), %rax     # 0x66608
movq (%rax), %rax
movss 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x211d0
jnp 0x21259
leaq 0x2e78a(%rip), %rdi     # 0x4f961
xorl %eax, %eax
callq *0x8(%rbp)
movq 0x45395(%rip), %r14     # 0x66578
addq $0x8, %r14
leaq 0x2e77a(%rip), %rbx     # 0x4f968
movq -0x8(%r14), %rsi
movq 0x8(%rbp), %rcx
testq %rsi, %rsi
je 0x21214
movq (%r14), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq *%rcx
addq $0x10, %r14
jmp 0x211ee
leaq 0x2e709(%rip), %rdi     # 0x4f91b
jmp 0x21254
leaq 0x2e766(%rip), %rdi     # 0x4f981
xorl %eax, %eax
callq *%rcx
movl $0x1008, %r14d          # imm = 0x1008
addq 0x4534c(%rip), %r14     # 0x66578
leaq 0x2e735(%rip), %rbx     # 0x4f968
movq -0x8(%r14), %rsi
testq %rsi, %rsi
je 0x21259
movq (%r14), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq *0x8(%rbp)
addq $0x10, %r14
jmp 0x21233
leaq 0x2e6ad(%rip), %rdi     # 0x4f901
xorl %eax, %eax
callq *0x8(%rbp)
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq