#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 /*<<< orphan*/ append(char **, char const *);
char *calloc(int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
size_t strlen(char const *);
char *strstr(char *, char const *);
char *substring(char *, int /*<<< orphan*/, int);
void replace(char **where, const char *what, const char *with) {
  char *offset = *where;
  char *position = 0;
  char *result = calloc(1, 0);
  size_t newlen = strlen(with);
  size_t oldlen = strlen(what);
  while (position = strstr(offset, what)) {
    char *prefix = substring(offset, 0, (position - offset));
    append(&result, prefix);
    free(prefix);
    append(&result, with);
    offset = position + oldlen;
  }
  append(&result, offset);
  free(*where);
  *where = result;
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     121c <replace+0xbc>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <substring@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   1040 <append@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   1040 <append@plt>
mov    -0x28(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11b1 <replace+0x51>
mov    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   1040 <append@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,0x10(%rsp)
mov    (%rdi),%rbx
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <calloc@plt>
mov    %rax,0x8(%rsp)
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11f9 <replace+0x99>
mov    %rax,%r14
lea    0x8(%rsp),%rbp
mov    %r14d,%edx
sub    %ebx,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <substring@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
call   1040 <append@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1040 <append@plt>
mov    %r14,%rbx
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11b0 <replace+0x50>
lea    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1040 <append@plt>
mov    0x10(%rsp),%rbx
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rax
mov    %rax,(%rbx)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @replace(i8** %0, i8* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%13 = load i8**, i8*** %4
%14 = load i8*, i8** %13
store i8* %14, i8** %7
store i8* null, i8** %8
%15 = call i8* @calloc(i32 1, i32 0)
store i8* %15, i8** %9
%16 = load i8*, i8** %6
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %10
%18 = load i8*, i8** %5
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %11
br label %20
20: ; preds = %25, %3
%21 = load i8*, i8** %7
%22 = load i8*, i8** %5
%23 = call i8* @strstr(i8* %21, i8* %22)
store i8* %23, i8** %8
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %43
25: ; preds = %20
%26 = load i8*, i8** %7
%27 = load i8*, i8** %8
%28 = load i8*, i8** %7
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
%32 = trunc i64 %31 to i32
%33 = call i8* @substring(i8* %26, i32 0, i32 %32)
store i8* %33, i8** %12
%34 = load i8*, i8** %12
%35 = call i32 @append(i8** %9, i8* %34)
%36 = load i8*, i8** %12
%37 = call i32 @free(i8* %36)
%38 = load i8*, i8** %6
%39 = call i32 @append(i8** %9, i8* %38)
%40 = load i8*, i8** %8
%41 = load i64, i64* %11
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8* %42, i8** %7
br label %20
43: ; preds = %20
%44 = load i8*, i8** %7
%45 = call i32 @append(i8** %9, i8* %44)
%46 = load i8**, i8*** %4
%47 = load i8*, i8** %46
%48 = call i32 @free(i8* %47)
%49 = load i8*, i8** %9
%50 = load i8**, i8*** %4
store i8* %49, i8** %50
ret void
}

LLVM IR (-O2):
define void @replace(i8** nocapture %0, i8* nocapture %1, i8* %2) {
%4 = alloca i8*
%5 = load i8*, i8** %0
%6 = bitcast i8** %4 to i8*
%7 = tail call i8* @calloc(i32 1, i32 0)
store i8* %7, i8** %4
%8 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%9 = call i8* @strstr(i8* dereferenceable(1) %5, i8* dereferenceable(1) %1)
%10 = icmp eq i8* %9, null
br i1 %10, label %25, label %11
11: ; preds = %3, %11
%12 = phi i8* [ %23, %11 ], [ %9, %3 ]
%13 = phi i8* [ %22, %11 ], [ %5, %3 ]
%14 = ptrtoint i8* %12 to i64
%15 = ptrtoint i8* %13 to i64
%16 = sub i64 %14, %15
%17 = trunc i64 %16 to i32
%18 = call i8* @substring(i8* %13, i32 0, i32 %17)
%19 = call i32 @append(i8** %4, i8* %18)
%20 = call i32 @free(i8* %18)
%21 = call i32 @append(i8** %4, i8* %2)
%22 = getelementptr inbounds i8, i8* %12, i64 %8
%23 = call i8* @strstr(i8* dereferenceable(1) %22, i8* dereferenceable(1) %1)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %11
25: ; preds = %11, %3
%26 = phi i8* [ %5, %3 ], [ %22, %11 ]
%27 = call i32 @append(i8** %4, i8* %26)
%28 = load i8*, i8** %0
%29 = call i32 @free(i8* %28)
%30 = load i8*, i8** %4
store i8* %30, i8** %0
ret void
}