// File: /workspace/github_cpp_makefile_star2_O2/gpertea[P]gclib/GStr.cpp
// Function: GStr::replace(char const*, char const*)
GStr&  GStr::replace(const char *rfrom, const char* rto) {
 if (length() == 0 || rfrom==NULL || strlen(rfrom)==0)
        return *this;
 unsigned int l=strlen(rfrom);
 unsigned int tl= (rto==NULL)?0:strlen(rto);
 make_unique(); //edit operation ahead
 char* p;
 char* dest;
 char* newdest=NULL;
 char* s = my_data->chars;
 if (tl!=l) { //reallocation
   if (tl>l) {  //possible enlargement
       GMALLOC(newdest, length()*(tl-l+1)+1);
       }
      else  {//delete or replace with a shorter string
       GMALLOC(newdest, length() + 1);
       }
      dest=newdest;
      if (tl==0) {//deletion
           while ((p=strstr(s,rfrom))!=NULL) {
               //rfrom found at position p
                memcpy(dest,s,p-s);
                dest+=p-s;
                s+=p-s+l; //s positioned in string after rfrom
                }
           //no more occurences, copy the remaining string
           strcpy(dest, s);
          }
        else { //replace with another string
          while ((p=strstr(s,rfrom))!=NULL) {
              memcpy(dest,s,p-s); //copy up rto the match
              dest+=p-s;
              memcpy(dest,rto,tl); //put the replacement string
              dest+=tl;
              s+=p-s+l;
              }
          //not found any more, copy rto end of string
          strcpy(dest, s);
          }
       Data* data=new_data(newdest);
       replace_data(data);
       GFREE(newdest);
       }
  else { //inplace editing: no need rto reallocate
    while ((p=strstr(s,rfrom))!=NULL) {
        memcpy(p,rto,l);
        s+=p-s+l;
        }
   }
 return *this;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
testq %rsi, %rsi
je 0x63c8
movq 0x20(%rbx), %rax
cmpl $0x0, 0x8(%rax)
je 0x63c8
movq %rdx, %r14
movq %rsi, %r12
movq %rsi, %rdi
callq 0x2140
testq %rax, %rax
je 0x63c8
movq %rax, %r13
testq %r14, %r14
je 0x6248
movq %r14, %rdi
callq 0x2140
movq %rax, %rbp
jmp 0x624a
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x46a4
andq $0x0, (%rsp)
movq 0x20(%rbx), %rax
leaq 0xc(%rax), %r15
movl %ebp, %esi
subl %r13d, %esi
jne 0x6293
movl %r13d, %r13d
movq %r15, %rdi
movq %r12, %rsi
callq 0x2090
testq %rax, %rax
je 0x63c8
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x21e0
addq %r13, %r15
jmp 0x6269
movslq 0x8(%rax), %rax
movq %r12, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %rbp, 0x8(%rsp)
jbe 0x62d7
incl %esi
imull %eax, %esi
incl %esi
movq %rsp, %rdi
callq 0x415c
testb %al, %al
jne 0x62c9
leaq 0x5d5e(%rip), %rdi      # 0xc020
xorl %eax, %eax
callq 0x25ca
movq %r13, %rax
movq %r14, 0x10(%rsp)
movq (%rsp), %rbp
jmp 0x630a
incq %rax
movq %rsp, %rdi
movq %rax, %rsi
callq 0x415c
testb %al, %al
jne 0x62f7
leaq 0x5d30(%rip), %rdi      # 0xc020
xorl %eax, %eax
callq 0x25ca
movq (%rsp), %rbp
cmpl $0x0, 0x8(%rsp)
je 0x635f
movq %r13, %rax
movq %r14, 0x10(%rsp)
movl 0x8(%rsp), %r13d
movl %eax, %r14d
movq %r15, %rdi
movq %r12, %rsi
callq 0x2090
testq %rax, %rax
je 0x6399
movq %rax, %rbx
movq %rax, %r12
subq %r15, %r12
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21e0
addq %rbp, %r12
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x21e0
addq %r13, %r12
addq %r14, %rbx
movq %r12, %rbp
movq 0x18(%rsp), %r12
movq %rbx, %r15
jmp 0x6312
movl %r13d, %r14d
movq %r15, %rdi
movq %r12, %rsi
callq 0x2090
testq %rax, %rax
je 0x6399
movq %rax, %rbx
movq %rax, %r12
subq %r15, %r12
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21e0
addq %r12, %rbp
movq 0x18(%rsp), %r12
addq %r14, %rbx
movq %rbx, %r15
jmp 0x6362
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2220
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x453a
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x46ea
movq (%rsp), %rdi
callq 0x22e0
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq