#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int CountStringSimple(char *, char const *);
char *ResizeIfNeeded(char *, int *, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char const *);
char *strstr(char *, char const *);
char *InplaceReplaceSimple(char *pBuffer, int *size, const char *S,
                           const char *D) {
  int lS = strlen(S), lD = strlen(D);
  pBuffer =
      ResizeIfNeeded(pBuffer, size, (lD - lS) * CountStringSimple(pBuffer, S));
  char *p = pBuffer;
  while ((p = strstr(p, S))) {
    memmove(p + lD, p + lS, strlen(p) - lS + 1);
    memcpy(p, D, strlen(D));
    p += lD;
  }
  return pBuffer;
}


x86 assembly (clang, -O0):
<InplaceReplaceSimple>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <CountStringSimple@plt>
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
imul   %eax,%edx
call   1040 <ResizeIfNeeded@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     126a <InplaceReplaceSimple+0x10a>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x68(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
sub    -0x24(%rbp),%edx
add    $0x1,%edx
call   1070 <memmove@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    11d5 <InplaceReplaceSimple+0x75>
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<InplaceReplaceSimple>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r12
mov    %rdx,%rbp
mov    %rsi,(%rsp)
mov    %rdi,%rbx
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%r14d
mov    %eax,%r13d
sub    %r15d,%r13d
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <CountStringSimple@plt>
imul   %eax,%r13d
mov    %rbx,%rdi
mov    (%rsp),%rsi
mov    %r13d,%edx
call   1040 <ResizeIfNeeded@plt>
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
mov    %rbp,(%rsp)
mov    %rbp,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     123d <InplaceReplaceSimple+0xdd>
mov    %rax,%rbx
movslq %r14d,%rax
mov    %rax,0x10(%rsp)
movslq %r15d,%r14
mov    $0x1,%r13d
sub    %r15d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rsp),%rax
lea    (%rbx,%rax,1),%rbp
lea    (%rbx,%r14,1),%r15
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    (%rax,%r13,1),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <memmove@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    %rbp,%rdi
mov    (%rsp),%rsi
call   1080 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f0 <InplaceReplaceSimple+0x90>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @InplaceReplaceSimple(i8* %0, i32* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%12 = load i8*, i8** %7
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %9
%14 = load i8*, i8** %8
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %10
%16 = load i8*, i8** %5
%17 = load i32*, i32** %6
%18 = load i32, i32* %10
%19 = load i32, i32* %9
%20 = sub nsw i32 %18, %19
%21 = load i8*, i8** %5
%22 = load i8*, i8** %7
%23 = call i32 @CountStringSimple(i8* %21, i8* %22)
%24 = mul nsw i32 %20, %23
%25 = call i8* @ResizeIfNeeded(i8* %16, i32* %17, i32 %24)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
store i8* %26, i8** %11
br label %27
27: ; preds = %32, %4
%28 = load i8*, i8** %11
%29 = load i8*, i8** %7
%30 = call i8* @strstr(i8* %28, i8* %29)
store i8* %30, i8** %11
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %56
32: ; preds = %27
%33 = load i8*, i8** %11
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8*, i8** %11
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8*, i8** %11
%42 = call i32 @strlen(i8* %41)
%43 = load i32, i32* %9
%44 = sub nsw i32 %42, %43
%45 = add nsw i32 %44, 1
%46 = call i32 @memmove(i8* %36, i8* %40, i32 %45)
%47 = load i8*, i8** %11
%48 = load i8*, i8** %8
%49 = load i8*, i8** %8
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @memcpy(i8* %47, i8* %48, i32 %50)
%52 = load i32, i32* %10
%53 = load i8*, i8** %11
%54 = sext i32 %52 to i64
%55 = getelementptr inbounds i8, i8* %53, i64 %54
store i8* %55, i8** %11
br label %27
56: ; preds = %27
%57 = load i8*, i8** %5
ret i8* %57
}

LLVM IR (-O2):
define i8* @InplaceReplaceSimple(i8* %0, i32* %1, i8* %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %2)
%6 = tail call i32 @strlen(i8* %3)
%7 = sub nsw i32 %6, %5
%8 = tail call i32 @CountStringSimple(i8* %0, i8* %2)
%9 = mul nsw i32 %7, %8
%10 = tail call i8* @ResizeIfNeeded(i8* %0, i32* %1, i32 %9)
%11 = tail call i8* @strstr(i8* dereferenceable(1) %10, i8* dereferenceable(1) %2)
%12 = icmp eq i8* %11, null
br i1 %12, label %28, label %13
13: ; preds = %4
%14 = sext i32 %6 to i64
%15 = sext i32 %5 to i64
%16 = sub i32 1, %5
br label %17
17: ; preds = %13, %17
%18 = phi i8* [ %11, %13 ], [ %26, %17 ]
%19 = getelementptr inbounds i8, i8* %18, i64 %14
%20 = getelementptr inbounds i8, i8* %18, i64 %15
%21 = tail call i32 @strlen(i8* %18)
%22 = add i32 %16, %21
%23 = tail call i32 @memmove(i8* %19, i8* %20, i32 %22)
%24 = tail call i32 @strlen(i8* %3)
%25 = tail call i32 @memcpy(i8* %18, i8* %3, i32 %24)
%26 = tail call i8* @strstr(i8* dereferenceable(1) %19, i8* dereferenceable(1) %2)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %17
28: ; preds = %17, %4
ret i8* %10
}