#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ strcat(char *, char const *);
char *strdup(char const *);
size_t strlen(char const *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *strstr(char *, char const *);
char *str_replace(const char *haystack, const char *from, const char *to) {
  char *s = strdup(haystack);
  size_t from_len = strlen(from);
  size_t to_len = strlen(to);
  char *p;
  while ((p = strstr(s, from)) != NULL) {
    char *n = malloc(strlen(s) - from_len + to_len + 1);
    assert(n != NULL);
    n[0] = '\0';
    strncat(n, s, p - s);
    strcat(n, to);
    strcat(n, p + from_len);
    free(s);
    s = n;
  }
  return s;
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     125e <str_replace+0xde>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
sub    -0x28(%rbp),%rdi
add    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <strncat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
add    -0x28(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11bb <str_replace+0x3b>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%r15
call   1090 <strdup@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    %r15,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     123f <str_replace+0xbf>
mov    %rax,%rbp
mov    (%rsp),%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
sub    %r12,%r13
add    $0x1,%r13
nop
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rax,%r13,1),%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
movb   $0x0,(%rbx)
mov    %ebp,%edx
sub    %r14d,%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strncat@plt>
mov    %rbx,%rdi
mov    (%rsp),%rsi
call   1080 <strcat@plt>
add    %r12,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <strcat@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10a0 <strstr@plt>
mov    %rax,%rbp
mov    %rbx,%r14
test   %rax,%rax
jne    11d0 <str_replace+0x50>
jmp    1242 <str_replace+0xc2>
mov    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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