// File: /workspace/github_c_makefile_star2_O3/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 0x182a0
movq 0x4a4e5(%rip), %r14     # 0x68600
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 0x1e186
testl %eax, %eax
je 0x1e360
movb %al, (%rbx)
movb $0x0, 0x1(%rbx)
jmp 0x1e219
movb 0x1(%r12), %al
xorl %r13d, %r13d
movb %al, (%r15,%r13)
movzbl 0x2(%r12,%r13), %eax
incq %r13
cmpq $0x25, %rax
ja 0x1e18e
btq %rax, %r14
jae 0x1e18e
movb $0x0, (%r15,%r13)
movq %r15, %rdi
callq 0x18680
addq %r13, %r12
cmpq $0x1, %rax
jne 0x1e1f6
callq 0x19a20
movq (%rax), %rcx
movsbq (%r15), %rax
movl (%rcx,%rax,4), %ecx
addl $-0x61, %ecx
cmpl $0x16, %ecx
ja 0x1e299
leaq 0x34cb5(%rip), %rdx     # 0x52e94
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x19380
jmp 0x1e219
movq (%rsp), %rax
movq 0x58(%rax), %rdi
movl $0xc0, %eax
addq %rax, %rdi
movq %r15, %rsi
callq 0x18bb0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x182a0
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x19640
incq %r12
jmp 0x1e169
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x19620
jmp 0x1e219
movq (%rsp), %rax
movq 0x58(%rax), %rax
movl 0x1100(%rax), %eax
cmpl $0x2, %eax
je 0x1e339
cmpl $0x1, %eax
jne 0x1e351
movabsq $0x6d61657420646572, %rax # imm = 0x6D61657420646572
movq %rax, (%rbx)
movb $0x0, 0x8(%rbx)
jmp 0x1e219
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x19670
jmp 0x1e219
movq (%rsp), %rdi
movq %rbx, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
jmp 0x1e2bf
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x18420
jmp 0x1e219
movb %al, (%rbx)
jmp 0x1e17d
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x18800
jmp 0x1e219
movq (%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x18d40
jmp 0x1e219
movq (%rsp), %rax
movq 0x58(%rax), %rax
movq 0x730(%rax), %rax
testq %rax, %rax
je 0x1e321
movq 0x50(%rax), %rsi
movq %rbx, %rdi
jmp 0x1e214
movb $0x0, (%rbx)
movq (%rsp), %rax
movq 0x58(%rax), %rax
movq 0x9130(%rax), %rax
testq %rax, %rax
je 0x1e219
movq 0x58(%rax), %rsi
movl $0x2c0, %eax            # imm = 0x2C0
addq %rax, %rsi
movq %rbx, %rdi
callq 0x182a0
movb $0x0, 0xf(%rbx)
jmp 0x1e219
movabsq $0x6f70616577206f6e, %rax # imm = 0x6F70616577206F6E
movq %rax, (%rbx)
movw $0x6e, 0x8(%rbx)
jmp 0x1e219
movabsq $0x6165742065756c62, %rax # imm = 0x6165742065756C62
movq %rax, (%rbx)
movw $0x6d, 0x8(%rbx)
jmp 0x1e219
movups 0x34ec5(%rip), %xmm0    # 0x5321d
movups %xmm0, (%rbx)
jmp 0x1e219
movslq 0x8(%rsp), %rax
movb $0x0, -0x2(%rbp,%rax)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x182a0
movq %rbx, %rdi
movq 0x4a27f(%rip), %rbx     # 0x68600
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)