// File: /workspace/github_c_makefile_star2_O2/QwazyWabbitWOS[P]lmctf60/g_replace.c
// Function: string_replace
void string_replace(edict_t *person, char *inmsg, char *outmsg, int outsize)
{
	char * result;
	char * msgptr;
	char * subptr;
	char * tmpstr;
	char * substr;
	
	
	strcpy(outmsg,inmsg); //in case anything fails, you'll say something
	substr = (char *)gi.TagMalloc(outsize * 2, TAG_LEVEL);
	result = (char *)gi.TagMalloc(outsize * 2, TAG_LEVEL);
	tmpstr = (char *)gi.TagMalloc(outsize * 2, TAG_LEVEL);
	
	//initialize strings empty
	tmpstr[0] = 0;
	result[0] = 0;
	substr[0] = 0;
	
	if ( (substr != NULL) && (result != NULL) && (tmpstr != NULL) )
	{
		msgptr = &inmsg[0]; //point to beginning of inmsg
		while (*msgptr != 0) //while not at end of the inmsg
		{
			if (*msgptr == '%')
			{
				//we are dealing with a substr
				msgptr++; //advance to start with character after %
				subptr = &substr[0]; //point to beginning of string
				do
				{
					*subptr = *msgptr;
					subptr++;
					msgptr++;
				}
				while ( (*msgptr != 0) && (*msgptr != '%') && (*msgptr != ' ') );

				msgptr--; //back off because we went past the end
				
				//now in theory we have the block of chars after %
				*subptr = 0; //should already be pointing to end of substr
				
				if (strlen(substr) == 1)
				{
					switch(tolower(substr[0])) //switch on the single character
					{
					case 'l' :
						replace_location(person, tmpstr, false, true);
						break;
					case 'a' :
						replace_armor(person, tmpstr);
						break;
					case 'h' :
						replace_health(person, tmpstr);
						break;
					case 't' :
						replace_artifact(person, tmpstr);
						break;
					case 'w' :
						replace_weapon(person, tmpstr);
						break;
					case 'n' :
						replace_team(person, tmpstr);
						break;
					case 'p' :
						replace_location(person, tmpstr, true, false);
						break;
					case 'f' :
						replace_flaginfo(person, tmpstr);
						break;
					case 'v' :
						replace_viewinfo(person, tmpstr);
						break;
					case 'c' :
						replace_carrierinfo(person, tmpstr);
						break;
					default:
						tmpstr[0] = substr[0];
						tmpstr[1] = 0;
						break;
					}
				}
				else
				{
					//use infovalueforkey feature
					strcpy(tmpstr, Info_ValueForKey(person->client->pers.userinfo, substr));
				}
				
			}
			else
			{
				tmpstr[0] = *msgptr;
				tmpstr[1] = 0;
				//above should cause us to strcat the single char
			}
			strcat(result, tmpstr);
			msgptr++;
		}
		result[outsize-2] = 0;
		strcpy(outmsg, result); //copy the resulting string
	}

	gi.TagFree(tmpstr);
	gi.TagFree(result);
	gi.TagFree(substr);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebx
movq %rsi, %r12
movq %rdi, (%rsp)
movq %rdx, 0x10(%rsp)
movq %rdx, %rdi
callq 0x182c0
movq 0x49b4d(%rip), %r14     # 0x66600
movq %rbx, 0x8(%rsp)
addl %ebx, %ebx
movl %ebx, %edi
movl $0x2fe, %esi            # imm = 0x2FE
callq *0x108(%r14)
movq %rax, %r15
movl %ebx, %edi
movl $0x2fe, %esi            # imm = 0x2FE
callq *0x108(%r14)
movq %rax, %rbp
movl %ebx, %edi
movl $0x2fe, %esi            # imm = 0x2FE
callq *0x108(%r14)
movq %rax, %rbx
xorl %eax, %eax
movb %al, (%rbx)
movb %al, (%rbp)
movb %al, (%r15)
movabsq $0x2100000001, %r14     # imm = 0x2100000001
movzbl (%r12), %eax
cmpl $0x25, %eax
je 0x1cb1e
testl %eax, %eax
je 0x1cc57
movb %al, (%rbx)
movb $0x0, 0x1(%rbx)
jmp 0x1cbb1
movb 0x1(%r12), %al
xorl %r13d, %r13d
movb %al, (%r15,%r13)
movzbl 0x2(%r12,%r13), %eax
incq %r13
cmpq $0x25, %rax
ja 0x1cb26
btq %rax, %r14
jae 0x1cb26
movb $0x0, (%r15,%r13)
movq %r15, %rdi
callq 0x186c0
addq %r13, %r12
cmpq $0x1, %rax
jne 0x1cb8e
callq 0x19c00
movq (%rax), %rcx
movsbq (%r15), %rax
movl (%rcx,%rax,4), %ecx
addl $-0x61, %ecx
cmpl $0x16, %ecx
ja 0x1cc0a
leaq 0x3231d(%rip), %rdx     # 0x4ee94
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x19520
jmp 0x1cbb1
movq (%rsp), %rax
movq 0x58(%rax), %rdi
movl $0xc0, %eax
addq %rax, %rdi
movq %r15, %rsi
callq 0x18c70
movq %rbx, %rdi
movq %rax, %rsi
callq 0x182c0
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x19800
incq %r12
jmp 0x1cb01
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x197e0
jmp 0x1cbb1
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x19230
jmp 0x1cbb1
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x19830
jmp 0x1cbb1
movq (%rsp), %rdi
movq %rbx, %rsi
pushq $0x1
popq %rdx
xorl %ecx, %ecx
jmp 0x1cc2b
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x18450
jmp 0x1cbb1
movb %al, (%rbx)
jmp 0x1cb15
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x18860
jmp 0x1cbb1
movq (%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x18e40
jmp 0x1cbb1
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x189a0
jmp 0x1cbb1
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x18590
jmp 0x1cbb1
movslq 0x8(%rsp), %rax
movb $0x0, -0x2(%rbp,%rax)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x182c0
movq %rbx, %rdi
movq 0x49988(%rip), %rbx     # 0x66600
callq *0x110(%rbx)
movq %rbp, %rdi
callq *0x110(%rbx)
movq %r15, %rdi
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *0x110(%rax)