#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);
char *replaceStatic(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);
  return result;
}


x86 assembly (clang, -O0):
<replaceStatic>:
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,-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     1219 <replaceStatic+0xb9>
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    11ae <replaceStatic+0x4e>
mov    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   1040 <append@plt>
mov    -0x30(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replaceStatic>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <calloc@plt>
mov    %rax,(%rsp)
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11f8 <replaceStatic+0x98>
mov    %rax,%rbp
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edx
sub    %ebx,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <substring@plt>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %rax,%rsi
call   1040 <append@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   1040 <append@plt>
mov    %rbp,%rbx
add    %r12,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strstr@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11b0 <replaceStatic+0x50>
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1040 <append@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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