// File: /workspace/github_c_makefile_star2_O3/inflex[P]ripMIME/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 0xc1eb
movq %rdi, 0x18(%rsp)
callq 0x2110
movq %rax, %r14
movq 0x8(%rbx), %rdi
callq 0x2110
movq %rax, 0x8(%rsp)
movq 0x10(%rbx), %rdi
callq 0x2110
movq %rax, 0x28(%rsp)
movq 0x18(%rbx), %rsi
testq %rsi, %rsi
je 0xc1a7
cmpb $0x0, (%rsi)
jne 0xc1a9
movq $0x0, 0x18(%rbx)
xorl %esi, %esi
movq 0x20(%rbx), %rax
testq %rax, %rax
movq 0x18(%rsp), %rdi
je 0xc1c4
cmpb $0x0, (%rax)
jne 0xc1c6
movq $0x0, 0x20(%rbx)
xorl %eax, %eax
movl 0x28(%rbx), %r12d
testq %rsi, %rsi
je 0xc1f2
movl 0x2c(%rbx), %edx
callq 0xbe00
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0xc35f
movq 0x20(%rbx), %rax
jmp 0xc1fb
xorl %edx, %edx
jmp 0xc50e
movq $0x0, 0x30(%rsp)
testq %rax, %rax
je 0xc23c
movq (%rbx), %rdi
movl 0x2c(%rbx), %edx
movq %rax, %rsi
callq 0xbe00
testq %rax, %rax
je 0xc35f
movq %rax, %rbp
movq 0x20(%rbx), %r15
movq %r15, %rdi
callq 0x2110
leaq (%rax,%rbp), %rdi
movl 0x2c(%rbx), %edx
movq %r15, %rsi
callq 0xbe00
testq %rax, %rax
jne 0xc217
jmp 0xc23e
xorl %ebp, %ebp
movq 0x28(%rsp), %r13
subl 0x8(%rsp), %r13d
testl %r13d, %r13d
jle 0xc28d
movq %r14, %r15
testl %r12d, %r12d
jne 0xc295
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
callq 0xbe00
testq %rax, %rax
je 0xc292
movslq 0x8(%rsp), %r14
xorl %r12d, %r12d
addq %r14, %rax
incl %r12d
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rax, %rdi
callq 0xbe00
testq %rax, %rax
jne 0xc271
jmp 0xc295
movl %r14d, %r13d
jmp 0xc29f
xorl %r12d, %r12d
imull %r12d, %r13d
movq %r15, %r14
addl %r14d, %r13d
incl %r13d
movslq %r13d, %rdi
callq 0x22a0
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0xc342
movq %r14, 0x40(%rsp)
movq (%rbx), %r15
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %r15, %rdi
callq 0xbe00
testq %rax, %rax
movq %rbp, 0x38(%rsp)
je 0xc367
movslq 0x8(%rsp), %r14
cmpq $0x0, 0x18(%rbx)
jne 0xc2f0
cmpq $0x0, 0x20(%rbx)
je 0xc369
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 0xc369
addq %r14, %rax
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rax, %rdi
callq 0xbe00
testq %rax, %rax
setne %cl
testb %bpl, %cl
movq 0x38(%rsp), %rbp
jne 0xc2e2
jmp 0xc369
leaq 0xb578(%rip), %rdi      # 0x178c1
leaq 0xb5d1(%rip), %rsi      # 0x17921
movl $0x1b8, %edx            # imm = 0x1B8
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc84f
movq (%rbx), %rdx
jmp 0xc50e
xorl %eax, %eax
movl %r12d, 0x14(%rsp)
movq 0x20(%rsp), %rdx
movq %rdx, %r12
testq %r15, %r15
je 0xc509
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 0xc3dd
testl %r13d, %r13d
jle 0xc3dd
incq %r15
movb -0x1(%r15), %al
movb %al, (%r12)
incq %r12
leal -0x1(%r13), %eax
cmpq %rbp, %r15
jae 0xc3e0
incq %r15
cmpl $0x1, %r13d
movl %eax, %r13d
ja 0xc3bb
jmp 0xc3e0
movl %r13d, %eax
cmpq %rcx, %rbp
jae 0xc419
cmpl $0x0, 0x28(%rsp)
je 0xc419
testl %eax, %eax
jle 0xc419
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 0xc414
cmpl $0x1, %eax
movl %r13d, %eax
ja 0xc3f6
addq %rcx, %r12
jmp 0xc41c
movl %eax, %r13d
testl %r13d, %r13d
jle 0xc4dd
movl 0x14(%rsp), %ecx
decl %ecx
addq 0x8(%rsp), %rbp
movq %rbp, %r15
movl $0x0, %eax
movl %ecx, 0x14(%rsp)
testl %ecx, %ecx
je 0xc39f
cmpq 0x18(%rsp), %rbp
ja 0xc504
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rbp, %rdi
callq 0xbe00
movq %rax, %rcx
movq %rbp, %r15
movl $0x0, %eax
testq %rcx, %rcx
je 0xc39f
movq %rcx, %rax
cmpq $0x0, 0x18(%rbx)
jne 0xc483
cmpq $0x0, 0x20(%rbx)
je 0xc4d5
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 0xc4d5
addq 0x8(%rsp), %rax
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rax, %rdi
callq 0xbe00
testq %rax, %rax
setne %cl
testb %r15b, %cl
jne 0xc475
movq %rbp, %r15
jmp 0xc39f
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %r8
leaq 0xb43e(%rip), %rdi      # 0x1792a
leaq 0xb42e(%rip), %rsi      # 0x17921
movl $0x209, %edx            # imm = 0x209
xorl %eax, %eax
callq 0xc84f
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