// File: /workspace/github_c_makefile_star2_O3/inflex[P]ripMIME/ripOLE/pldstr.c
// Function: PLD_strreplace_general
char *PLD_strreplace_general( struct PLD_strreplace *replace_details )
{
	char *new_buffer=NULL;
	char *source_end;
	char *segment_start, *segment_end, *segment_p;
	char *new_p;
	char *preexist_location=NULL;
	char *postexist_location=NULL;
	int replace_count=0;
	int size_required;
	int size_difference;
	int source_length;
	int searchfor_length;
	int replacewith_length;
	int segment_ok;

	if (replace_details->source == NULL) return NULL;

	source_length = strlen( replace_details->source );
	source_end = replace_details->source +source_length;
	searchfor_length = strlen(replace_details->searchfor);
	replacewith_length = strlen(replace_details->replacewith);
	size_difference = replacewith_length -searchfor_length;
	size_required = source_length;
	replace_count = replace_details->replacenumber;

	if ((replace_details->preexist != NULL)&&(strlen(replace_details->preexist) < 1)) replace_details->preexist = NULL;
	if ((replace_details->postexist != NULL)&&(strlen(replace_details->postexist) < 1)) replace_details->postexist = NULL;

	// If we have a 'pre-exist' request, then we need to check this out first
	//		because if the pre-exist string cannot be found, then there's very
	//		little point us continuing on in our search ( because without the
	//		preexist string existing, we are thus not qualified to replace anything )
	if (replace_details->preexist != NULL)
	{
		preexist_location = PLD_strstr(replace_details->source, replace_details->preexist, replace_details->insensitive);
		if (preexist_location == NULL)
		{
			return replace_details->source;
		}
	} 

	// Determine if initial POSTexist tests will pass, if we don't pick up
	//		anything here, then there's no point in continuing either
	if (replace_details->postexist != NULL)
	{
		char *p = replace_details->source;
		postexist_location = NULL;
		do {
			p = PLD_strstr(p, replace_details->postexist, replace_details->insensitive);
			if (p != NULL)
			{
				postexist_location = p;
				p = p +strlen(replace_details->postexist);
			} 
		} while (p != NULL);

		if (postexist_location == NULL)
		{
			return replace_details->source;
		}
	} 


	// Step 1 - determine the MAXIMUM number of times we might have to replace this string ( or the limit
	//		set by replacenumber
	//
	//	Note - we only need this number if the string we're going to be inserting into the 
	//	source is larger than the one we're replacing - this is so that we can ensure that
	//	we have sufficient memory available in the buffer.
	if (size_difference > 0)
	{
		if (replace_count == 0)
		{
			char *p, *q;

			p = replace_details->source;
			q = PLD_strstr(p, replace_details->searchfor, replace_details->insensitive);
			while (q != NULL)
			{
				replace_count++;
				//size_required += size_difference;
				p = q +searchfor_length;
				q = PLD_strstr(p, replace_details->searchfor, replace_details->insensitive);
			}

		}
		size_required = source_length +(size_difference *replace_count) +1;
	} else size_required = source_length +1;

	
	// Allocate the memory required to hold the new string [at least], check to see that
	//		all went well, if not, then return an error
	new_buffer = malloc( sizeof(char) *size_required);
	if (new_buffer == NULL)
	{
		LOGGER_log("%s:%d:PLD_strreplace:ERROR: Cannot allocate %d bytes of memory to perform replacement operation", FL, size_required);
		return replace_details->source;
	} 

	// Our segment must always start at the beginning of the source, 
	//		on the other hand, the segment_end can be anything from the
	//		next byte to NULL ( which is specially treated to mean to 
	//		the end of the source )
	segment_start = replace_details->source;


	// Locate the first segment 
	segment_ok = 0;
	segment_end = PLD_strstr(replace_details->source, replace_details->searchfor, replace_details->insensitive);

	// Determine if the first segment is valid in the presence of the 
	//	pre-exist and post-exist requirements
	while ((segment_end != NULL)&&(segment_ok == 0)\
			&&((replace_details->preexist != NULL)||(replace_details->postexist != NULL)))
	{
		int pre_ok = 0;
		int post_ok = 0;

		// The PREexist test assumes a couple of factors - please ensure these are
		//		relevant if you change any code prior to this point.
		//	
		//	1. preexist_location has already been computed and is not NULL
		//
		//	2. By relative position, the first preexist_location will be a valid location
		//			on which to validate for ALL replacements beyond that point, thus, we
		//			never actually have to recompute preexist_location again.
		//
		// 3. Conversely, the last computed postexist_location is valid for all 
		//			matches before it
		//
		if (preexist_location == NULL) pre_ok = 1;
		else if (preexist_location < segment_end){ pre_ok = 1;}

		if (postexist_location == NULL) post_ok = 1;
		else if (postexist_location > segment_end){ post_ok = 1;}

		if ((pre_ok == 0)||(post_ok == 0)) { segment_end = PLD_strstr(segment_end +searchfor_length, replace_details->searchfor, replace_details->insensitive); }
		else segment_ok = 1;
	} 

	segment_p = segment_start;
	new_p = new_buffer;
	while (segment_start != NULL)
	{	
		int replacewith_count;
		char *replacewith_p;

		if (segment_end == NULL) segment_end = source_end;

		replace_count--;

		// Perform the segment copy
		segment_p = segment_start;
		while ((segment_p < segment_end)&&(size_required > 0))
		{
			*new_p = *segment_p;
			new_p++;
			segment_p++;
			size_required--;
		}

		// Perform the string replacement
		if (segment_end < source_end)
		{
			replacewith_count = replacewith_length;
			replacewith_p = replace_details->replacewith;
			while ((replacewith_count--)&&(size_required > 0))
			{
				*new_p = *replacewith_p;
				new_p++;
				replacewith_p++;
				size_required--;
			}
		}

		if (size_required < 1 )
		{
			LOGGER_log("%s:%d:PLD_strreplace_general: Allocated memory ran out while replacing '%s' with '%s'",FL, replace_details->searchfor, replace_details->replacewith);
			*new_p='\0';
			break;
		}

		// Find the next segment
		segment_start = segment_end +searchfor_length;

		// If we've reached the end of the number of replacements we're supposed
		//		to do, then we prepare the termination of the while loop by setting
		//		our segment end to the end of the source.
		//
		// NOTE: Remember that the replace_count is pre-decremented at the start
		//		of the while loop, so, if the caller requested '0' replacements
		//		this will now be -1, thus, it won't get terminated from this == 0
		//		match.  Just thought you'd like to be reminded of that incase you
		//		were wondering "Huh? this would terminate an unlimited replacement"
		if (replace_count == 0)
		{
			segment_end = NULL;
		} else {
			// If our new segment to copy starts after the
			//		end of the source, then we actually have 
			//		nothing else to copy, thus, we prepare the
			//		segment_start varible to cause the while loop 
			//		to terminate.
			//
			// Otherwise, we try and locate the next segment
			//		ending point, and set the starting point to
			//		be on the 'other side' of the 'searchfor' string
			//		which we found in the last search.
			//
			if (segment_start > source_end) 
			{
				segment_start = NULL;
			} else {

				// Try find the next segment
				segment_ok = 0;
				segment_end = PLD_strstr(segment_end +searchfor_length, replace_details->searchfor, replace_details->insensitive);

				// If we have a pre/post-exist requirement, then enter into this
				//		series of tests.  NOTE - at least one of the pre or post tests
				//		must fire to give an meaningful result - else we'll end up with 
				//		a loop which simply goes to the end of the searchspace buffer
				while ((segment_end != NULL)&&(segment_ok == 0)\
						&&((replace_details->preexist != NULL)||(replace_details->postexist != NULL)))
				{
					int pre_ok = 0;
					int post_ok = 0;

					// The PREexist test assumes a couple of factors - please ensure these are
					//		relevant if you change any code prior to this point.
					//	
					//	1. preexist_location has already been computed and is not NULL
					//
					//	2. By relative position, the first preexist_location will be a valid location
					//			on which to validate for ALL replacements beyond that point, thus, we
					//			never actually have to recompute preexist_location again.
					//
					// 3. Conversely, the last computed postexist_location is valid for all 
					//			matches before it
					//
					if (preexist_location == NULL) pre_ok = 1;
					else if (preexist_location < segment_end){ pre_ok = 1;}

					if (postexist_location == NULL) post_ok = 1;
					else if (postexist_location > segment_end){ post_ok = 1;}

					if ((pre_ok == 0)||(post_ok == 0)) { segment_end = PLD_strstr(segment_end +searchfor_length, replace_details->searchfor, replace_details->insensitive); }
					else segment_ok = 1;
				} 

			} // If-else segment_start > source_end

		}

	}

	*new_p = '\0';

//	if (replace_details->source != NULL) free (replace_details->source);
//	replace_details->source = new_buffer;
	return new_buffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x41f3
movq %rdi, 0x18(%rsp)
callq 0x10b0
movq %rax, %r14
movq 0x8(%rbx), %rdi
callq 0x10b0
movq %rax, 0x8(%rsp)
movq 0x10(%rbx), %rdi
callq 0x10b0
movq %rax, 0x28(%rsp)
movq 0x18(%rbx), %rsi
testq %rsi, %rsi
je 0x41af
cmpb $0x0, (%rsi)
jne 0x41b1
movq $0x0, 0x18(%rbx)
xorl %esi, %esi
movq 0x20(%rbx), %rax
testq %rax, %rax
movq 0x18(%rsp), %rdi
je 0x41cc
cmpb $0x0, (%rax)
jne 0x41ce
movq $0x0, 0x20(%rbx)
xorl %eax, %eax
movl 0x28(%rbx), %r12d
testq %rsi, %rsi
je 0x41fa
movl 0x2c(%rbx), %edx
callq 0x3e08
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x4367
movq 0x20(%rbx), %rax
jmp 0x4203
xorl %edx, %edx
jmp 0x4516
movq $0x0, 0x30(%rsp)
testq %rax, %rax
je 0x4244
movq (%rbx), %rdi
movl 0x2c(%rbx), %edx
movq %rax, %rsi
callq 0x3e08
testq %rax, %rax
je 0x4367
movq %rax, %rbp
movq 0x20(%rbx), %r15
movq %r15, %rdi
callq 0x10b0
leaq (%rax,%rbp), %rdi
movl 0x2c(%rbx), %edx
movq %r15, %rsi
callq 0x3e08
testq %rax, %rax
jne 0x421f
jmp 0x4246
xorl %ebp, %ebp
movq 0x28(%rsp), %r13
subl 0x8(%rsp), %r13d
testl %r13d, %r13d
jle 0x4295
movq %r14, %r15
testl %r12d, %r12d
jne 0x429d
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
callq 0x3e08
testq %rax, %rax
je 0x429a
movslq 0x8(%rsp), %r14
xorl %r12d, %r12d
addq %r14, %rax
incl %r12d
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rax, %rdi
callq 0x3e08
testq %rax, %rax
jne 0x4279
jmp 0x429d
movl %r14d, %r13d
jmp 0x42a7
xorl %r12d, %r12d
imull %r12d, %r13d
movq %r15, %r14
addl %r14d, %r13d
incl %r13d
movslq %r13d, %rdi
callq 0x1110
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x434a
movq %r14, 0x40(%rsp)
movq (%rbx), %r15
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %r15, %rdi
callq 0x3e08
testq %rax, %rax
movq %rbp, 0x38(%rsp)
je 0x436f
movslq 0x8(%rsp), %r14
cmpq $0x0, 0x18(%rbx)
jne 0x42f8
cmpq $0x0, 0x20(%rbx)
je 0x4371
testq %rbp, %rbp
setne %cl
movq 0x30(%rsp), %rsi
testq %rsi, %rsi
setne %dl
cmpq %rax, %rsi
setae %sil
andb %dl, %sil
cmpq %rax, %rbp
setbe %bpl
andb %cl, %bpl
orb %sil, %bpl
cmpb $0x1, %bpl
jne 0x4371
addq %r14, %rax
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rax, %rdi
callq 0x3e08
testq %rax, %rax
setne %cl
testb %bpl, %cl
movq 0x38(%rsp), %rbp
jne 0x42ea
jmp 0x4371
leaq 0x2b94(%rip), %rdi      # 0x6ee5
leaq 0x2bed(%rip), %rsi      # 0x6f45
movl $0x1f2, %edx            # imm = 0x1F2
movl %r13d, %ecx
xorl %eax, %eax
callq 0x3c5f
movq (%rbx), %rdx
jmp 0x4516
xorl %eax, %eax
movl %r12d, 0x14(%rsp)
movq 0x20(%rsp), %rdx
movq %rdx, %r12
testq %r15, %r15
je 0x4511
movslq 0x40(%rsp), %rcx
addq %rcx, 0x18(%rsp)
movslq 0x8(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq 0x28(%rsp), %rcx
leal -0x1(%rcx), %r14d
movq %rdx, %r12
testq %rax, %rax
movq %rax, %rbp
movq 0x18(%rsp), %rcx
cmoveq %rcx, %rbp
cmpq %rbp, %r15
jae 0x43e5
testl %r13d, %r13d
jle 0x43e5
incq %r15
movb -0x1(%r15), %al
movb %al, (%r12)
incq %r12
leal -0x1(%r13), %eax
cmpq %rbp, %r15
jae 0x43e8
incq %r15
cmpl $0x1, %r13d
movl %eax, %r13d
ja 0x43c3
jmp 0x43e8
movl %r13d, %eax
cmpq %rcx, %rbp
jae 0x4421
cmpl $0x0, 0x28(%rsp)
je 0x4421
testl %eax, %eax
jle 0x4421
movq 0x10(%rbx), %rdx
xorl %ecx, %ecx
movb (%rdx,%rcx), %sil
movb %sil, (%r12,%rcx)
leal -0x1(%rax), %r13d
movl %ecx, %esi
incq %rcx
cmpl %esi, %r14d
je 0x441c
cmpl $0x1, %eax
movl %r13d, %eax
ja 0x43fe
addq %rcx, %r12
jmp 0x4424
movl %eax, %r13d
testl %r13d, %r13d
jle 0x44e5
movl 0x14(%rsp), %ecx
decl %ecx
addq 0x8(%rsp), %rbp
movq %rbp, %r15
movl $0x0, %eax
movl %ecx, 0x14(%rsp)
testl %ecx, %ecx
je 0x43a7
cmpq 0x18(%rsp), %rbp
ja 0x450c
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rbp, %rdi
callq 0x3e08
movq %rax, %rcx
movq %rbp, %r15
movl $0x0, %eax
testq %rcx, %rcx
je 0x43a7
movq %rcx, %rax
cmpq $0x0, 0x18(%rbx)
jne 0x448b
cmpq $0x0, 0x20(%rbx)
je 0x44dd
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
setne %cl
movq 0x30(%rsp), %rsi
testq %rsi, %rsi
setne %dl
cmpq %rax, %rsi
setae %sil
andb %dl, %sil
cmpq %rax, %rdi
setbe %r15b
andb %cl, %r15b
orb %sil, %r15b
cmpb $0x1, %r15b
jne 0x44dd
addq 0x8(%rsp), %rax
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rax, %rdi
callq 0x3e08
testq %rax, %rax
setne %cl
testb %r15b, %cl
jne 0x447d
movq %rbp, %r15
jmp 0x43a7
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %r8
leaq 0x2a5a(%rip), %rdi      # 0x6f4e
leaq 0x2a4a(%rip), %rsi      # 0x6f45
movl $0x244, %edx            # imm = 0x244
xorl %eax, %eax
callq 0x3c5f
movb $0x0, (%r12)
movq 0x20(%rsp), %rdx
movb $0x0, (%r12)
movq %rdx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq