// File: /workspace/github_c_makefile_star2_O1/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 0x473d0(%rip), %rax     # 0x69eb0
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 0x46b88(%rip), %rax     # 0x69698
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x19640
leaq 0x31a12(%rip), %rsi     # 0x54538
movq %rbx, %rdi
callq 0x19470
testq %rax, %rax
je 0x22f03
movq %rax, %rbx
movq $-0x1, %r14
movq %rbx, %rdi
callq 0x18900
incq %r14
cmpl $-0x1, %eax
jne 0x22b41
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x19100
movq 0x46a9c(%rip), %r13     # 0x69600
leal 0x2(%r14), %edi
movl $0x2fd, %esi            # imm = 0x2FD
callq *0x108(%r13)
testq %rax, %rax
je 0x22eed
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 0x22ef6
movq %r15, 0x30(%rsp)
movb $0x0, (%r15,%r14)
xorps %xmm0, %xmm0
movq 0x469bd(%rip), %rax     # 0x69578
movaps %xmm0, (%rax)
movaps %xmm0, 0x1000(%rax)
movq 0x46e3c(%rip), %rax     # 0x69a08
movl $0x1, (%rax)
jle 0x22e3c
movq 0x30(%rsp), %rbx
movq %rbx, %rbp
notq %rbp
leaq 0x240(%rsp), %r12
movl $0x0, 0x28(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movq %rbx, %rdi
leaq 0x3315c(%rip), %rsi     # 0x55d69
movq %r12, %rdx
xorl %eax, %eax
callq 0x18fe0
testl %eax, %eax
je 0x22e3c
movq %r12, %rdi
callq 0x18680
leaq (%rbx,%rbp), %r15
addq %rax, %r15
addq %rax, %rbx
decq %rbx
incq %r15
cmpb $0xa, 0x1(%rbx)
leaq 0x1(%rbx), %rbx
je 0x22c34
movzbl 0x240(%rsp), %eax
cmpq $0x3b, %rax
ja 0x22c69
movabsq $0x800800800000000, %rcx # imm = 0x800800800000000
btq %rax, %rcx
jae 0x22c69
cmpq %r14, %r15
jl 0x22c03
jmp 0x22e3c
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 0x22c5f
movq %r12, %rdi
leaq 0x31c67(%rip), %rsi     # 0x54928
leaq 0x440(%rsp), %rdx
leaq 0x40(%rsp), %rcx
xorl %eax, %eax
callq 0x18fe0
cmpl $0x2, %eax
jne 0x22c5f
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 0x22db1
movslq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq 0x46848(%rip), %rax     # 0x69578
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 0x467dc(%rip), %rsi     # 0x69578
movq 0x8(%rsp), %rdi
movq %rdi, 0x1008(%rsi,%rax)
incl %ecx
movl %ecx, 0x28(%rsp)
jmp 0x22e29
movslq 0x20(%rsp), %rcx
movq %rcx, 0x18(%rsp)
shlq $0x4, %rcx
movq %rcx, 0x20(%rsp)
movq 0x467ad(%rip), %rax     # 0x69578
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 0x46764(%rip), %rdx     # 0x69578
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 0x22c5f
movq 0x30(%rsp), %rdi
callq *0x110(%r13)
movq 0x467b9(%rip), %rax     # 0x69608
movq (%rax), %rax
movss 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x22e65
jnp 0x22f03
leaq 0x31ac5(%rip), %rdi     # 0x54931
xorl %eax, %eax
callq *0x8(%r13)
movq 0x466ff(%rip), %r15     # 0x69578
movq (%r15), %rsi
testq %rsi, %rsi
je 0x22ea5
leaq 0x10(%r15), %r14
leaq 0x31aac(%rip), %rbx     # 0x54938
movq -0x8(%r14), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq *0x8(%r13)
movq (%r14), %rsi
addq $0x10, %r14
testq %rsi, %rsi
jne 0x22e8c
leaq 0x31aa5(%rip), %rdi     # 0x54951
xorl %eax, %eax
callq *0x8(%r13)
movq 0x1000(%r15), %rsi
testq %rsi, %rsi
je 0x22f03
movl $0x1010, %r14d          # imm = 0x1010
addq 0x466ad(%rip), %r14     # 0x69578
leaq 0x31a66(%rip), %rbx     # 0x54938
movq -0x8(%r14), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq *0x8(%r13)
movq (%r14), %rsi
addq $0x10, %r14
testq %rsi, %rsi
jne 0x22ed2
jmp 0x22f03
leaq 0x319f7(%rip), %rdi     # 0x548eb
jmp 0x22efd
leaq 0x319d4(%rip), %rdi     # 0x548d1
xorl %eax, %eax
callq *0x8(%r13)
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq