#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*/ memcpy(char *, char *, int);
char *strstr(char *, char *);
void replace_str(char *str, char *from, char *to, int len) {
  int pos = 0;
  char *p;
  while ((p = strstr(&str[pos], from)) != NULL) {
    memcpy(p, to, len);
    pos += len;
  }
}


x86 assembly (clang, -O0):
<replace_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     117e <replace_str+0x5e>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    113e <replace_str+0x1e>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_str>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <strstr@plt>
test   %rax,%rax
je     1171 <replace_str+0x51>
movslq %ebp,%r12
add    %r12,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <memcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strstr@plt>
add    %r12,%rbx
test   %rax,%rax
jne    1150 <replace_str+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @replace_str(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %11
11: ; preds = %19, %4
%12 = load i8*, i8** %5
%13 = load i32, i32* %9
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8*, i8** %6
%17 = call i8* @strstr(i8* %15, i8* %16)
store i8* %17, i8** %10
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %27
19: ; preds = %11
%20 = load i8*, i8** %10
%21 = load i8*, i8** %7
%22 = load i32, i32* %8
%23 = call i32 @memcpy(i8* %20, i8* %21, i32 %22)
%24 = load i32, i32* %8
%25 = load i32, i32* %9
%26 = add nsw i32 %25, %24
store i32 %26, i32* %9
br label %11
27: ; preds = %11
ret void
}

LLVM IR (-O2):
define void @replace_str(i8* %0, i8* nocapture %1, i8* %2, i32 %3) {
%5 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %17, label %7
7: ; preds = %4
%8 = sext i32 %3 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %13, %9 ]
%11 = phi i8* [ %5, %7 ], [ %15, %9 ]
%12 = tail call i32 @memcpy(i8* %11, i8* %2, i32 %3)
%13 = add i64 %10, %8
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = tail call i8* @strstr(i8* dereferenceable(1) %14, i8* dereferenceable(1) %1)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %9
17: ; preds = %9, %4
ret void
}