#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 const *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
scalar_t__ strlen(char const *);
char *strstr(char *, char const *);
void str_replace(char *line, const char *search, const char *replace) {
  char *sp;
  while ((sp = strstr(line, search)) != NULL) {
    int search_len = (int)strlen(search);
    int replace_len = (int)strlen(replace);
    int tail_len = (int)strlen(sp + search_len);
    memmove(sp + replace_len, sp + search_len, tail_len + 1);
    memcpy(sp, replace, replace_len);
  }
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11d0 <str_replace+0x90>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
add    $0x1,%edx
call   1050 <memmove@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <memcpy@plt>
jmp    1154 <str_replace+0x14>
add    $0x30,%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,%r14
mov    %rsi,%r12
mov    %rdi,%r15
call   1060 <strstr@plt>
test   %rax,%rax
je     11cb <str_replace+0x8b>
mov    %rax,%rbx
mov    %r15,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
movslq %r13d,%rbp
add    %rbx,%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
movslq %r15d,%r15
lea    (%rbx,%r15,1),%rdi
lea    0x1(%rax),%edx
mov    %rbp,%rsi
call   1050 <memmove@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
mov    (%rsp),%r15
call   1040 <memcpy@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <str_replace+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @str_replace(i8* %0, i8* nocapture %1, i8* %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %24, label %6
6: ; preds = %3, %6
%7 = phi i8* [ %22, %6 ], [ %4, %3 ]
%8 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%10 = trunc i64 %9 to i32
%11 = shl i64 %8, 32
%12 = ashr exact i64 %11, 32
%13 = getelementptr inbounds i8, i8* %7, i64 %12
%14 = tail call i64 @strlen(i8* %13)
%15 = trunc i64 %14 to i32
%16 = shl i64 %9, 32
%17 = ashr exact i64 %16, 32
%18 = getelementptr inbounds i8, i8* %7, i64 %17
%19 = add nsw i32 %15, 1
%20 = tail call i32 @memmove(i8* %18, i8* %13, i32 %19)
%21 = tail call i32 @memcpy(i8* %7, i8* %2, i32 %10)
%22 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %6
24: ; preds = %6, %3
ret void
}