#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strstr(char *, char *);
void strsandr(char text[], char find[], char replace[]) {
  char *cptr;
  int rlen = strlen(replace);
  int flen = strlen(find);
  char *buff = malloc((strlen(text) * 2) + 512);
  char *bptr;
  char *cprtold = text;
  buff[0] = '\0';
  cptr = text;
  bptr = buff;
  strcpy(buff, text);
  while ((cptr = strstr(cptr, find))) {
    bptr = (bptr + (cptr - cprtold));
    cptr = (cptr + flen);
    strcpy(bptr, replace);
    bptr = (bptr + rlen);
    strcat(bptr, cptr);
    cprtold = cptr;
  }
  strcpy(text, buff);
  free(buff);
}


x86 assembly (clang, -O0):
<strsandr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
shl    $0x1,%edi
add    $0x200,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     124b <strsandr+0xeb>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x40(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11d5 <strsandr+0x75>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strsandr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%rbx
mov    %rdx,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rax,1),%edi
add    $0x200,%edi
call   1060 <malloc@plt>
movb   $0x0,(%rax)
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
mov    %r13,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     121b <strsandr+0xbb>
movslq %ebp,%r14
movslq %r12d,%r12
mov    0x10(%rsp),%rbp
mov    0x8(%rsp),%rcx
nopw   0x0(%rax,%rax,1)
mov    %rax,%rbx
sub    %rbp,%rbx
add    %rcx,%rbx
mov    %rax,%rbp
add    %r14,%rbp
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
add    %r12,%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <strcat@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1080 <strstr@plt>
mov    %rbx,%rcx
test   %rax,%rax
jne    11e0 <strsandr+0x80>
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

LLVM IR (-O2):
define void @strsandr(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %2)
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i32 @strlen(i8* %0)
%7 = shl nsw i32 %6, 1
%8 = add nsw i32 %7, 512
%9 = tail call i8* @malloc(i32 %8)
store i8 0, i8* %9
%10 = tail call i32 @strcpy(i8* %9, i8* %0)
%11 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %30, label %13
13: ; preds = %3
%14 = sext i32 %5 to i64
%15 = sext i32 %4 to i64
br label %16
16: ; preds = %13, %16
%17 = phi i8* [ %11, %13 ], [ %28, %16 ]
%18 = phi i8* [ %0, %13 ], [ %24, %16 ]
%19 = phi i8* [ %9, %13 ], [ %26, %16 ]
%20 = ptrtoint i8* %17 to i64
%21 = ptrtoint i8* %18 to i64
%22 = sub i64 %20, %21
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = getelementptr inbounds i8, i8* %17, i64 %14
%25 = tail call i32 @strcpy(i8* %23, i8* %2)
%26 = getelementptr inbounds i8, i8* %23, i64 %15
%27 = tail call i32 @strcat(i8* %26, i8* %24)
%28 = tail call i8* @strstr(i8* dereferenceable(1) %24, i8* dereferenceable(1) %1)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %16
30: ; preds = %16, %3
%31 = tail call i32 @strcpy(i8* %0, i8* %9)
%32 = tail call i32 @free(i8* %9)
ret void
}