// File: /workspace/github_cpp_makefile_star2_O3/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, %rbp
testq %rsi, %rsi
je 0x68d1
movq 0x20(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x68d1
movq %rdx, %r14
movq %rsi, %r13
movq %rsi, %rdi
callq 0x2140
testq %rax, %rax
je 0x68d1
movq %rax, %r15
testq %r14, %r14
je 0x6756
movq %r14, %rdi
callq 0x2140
movq %rax, %r12
jmp 0x6759
xorl %r12d, %r12d
movq %rbp, %rdi
callq 0x4a4c
movq 0x20(%rbp), %rax
leaq 0xc(%rax), %rbx
movl %r12d, %ecx
subl %r15d, %ecx
jne 0x67b4
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2090
testq %rax, %rax
je 0x68d1
movq %rax, %rbx
movl %r15d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x21e0
addq %r12, %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2090
movq %rax, %rbx
testq %rax, %rax
jne 0x678b
jmp 0x68d1
movq %r15, 0x8(%rsp)
movl 0x8(%rax), %eax
jbe 0x67ee
movq %rbp, 0x10(%rsp)
incl %ecx
imull %ecx, %eax
incl %eax
je 0x67db
movl %eax, %edi
callq 0x2330
movq %rax, %r15
testq %rax, %rax
jne 0x6821
leaq 0x783e(%rip), %rdi      # 0xe020
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x286c
jmp 0x6821
incl %eax
je 0x6802
movslq %eax, %rdi
callq 0x2330
movq %rax, %r15
testq %rax, %rax
jne 0x6813
leaq 0x7817(%rip), %rdi      # 0xe020
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x286c
testl %r12d, %r12d
je 0x68e3
movq %rbp, 0x10(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2090
testq %rax, %rax
movq %r15, 0x18(%rsp)
je 0x6897
movq %rax, %rbp
movl %r12d, %r12d
movl 0x8(%rsp), %eax
movq %rax, 0x8(%rsp)
movq %r14, 0x20(%rsp)
movq %r13, %r14
movq %rbp, %r13
subq %rbx, %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x21e0
addq %r15, %r13
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %r12, %rdx
callq 0x21e0
addq %r12, %r13
movq %rbp, %rbx
addq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2090
movq %rax, %rbp
movq %r13, %r15
testq %rax, %rax
jne 0x684d
jmp 0x689a
movq %r15, %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2220
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x48e2
movq %rbp, %rdi
movq %rax, %rsi
callq 0x4a92
testq %rbx, %rbx
je 0x68d1
movq %rbx, %rdi
callq 0x22e0
movq %rbp, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2090
testq %rax, %rax
movq %r15, 0x18(%rsp)
je 0x693c
movq %rax, %r12
movl 0x8(%rsp), %eax
movq %rax, 0x8(%rsp)
movq %r13, %r14
movq %r12, %r13
subq %rbx, %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x21e0
addq %r13, %r15
movq %r14, %r13
movq %r12, %rbx
addq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2090
movq %rax, %r12
testq %rax, %rax
jne 0x6904
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2220
jmp 0x68aa