#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);
int strlen(char const *);
char *strstr(char *, char const *);
void str_replace(char *str, const char *delim, const char *replace) {
  char *sp;
  if ((sp = strstr(str, delim)) == NULL) {
    return;
  }
  int search_len = strlen(delim);
  int replace_len = strlen(replace);
  int tail_len = 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
jne    1174 <str_replace+0x34>
jmp    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>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
call   1060 <strstr@plt>
test   %rax,%rax
je     11a7 <str_replace+0x67>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
movslq %ebx,%rbx
add    %r15,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %ebp,%rbp
mov    %r15,%rdi
add    %rbp,%rdi
lea    0x1(%rax),%edx
mov    %rbx,%rsi
call   1050 <memmove@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <memcpy@plt>
add    $0x8,%rsp
pop    %rbx
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
%11 = load i8*, i8** %4
%12 = load i8*, i8** %5
%13 = call i8* @strstr(i8* %11, i8* %12)
store i8* %13, i8** %7
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
br label %41
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %8
%19 = load i8*, i8** %6
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %9
%21 = load i8*, i8** %7
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = call i32 @strlen(i8* %24)
store i32 %25, i32* %10
%26 = load i8*, i8** %7
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8*, i8** %7
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i32, i32* %10
%35 = add nsw i32 %34, 1
%36 = call i32 @memmove(i8* %29, i8* %33, i32 %35)
%37 = load i8*, i8** %7
%38 = load i8*, i8** %6
%39 = load i32, i32* %9
%40 = call i32 @memcpy(i8* %37, i8* %38, i32 %39)
br label %41
41: ; preds = %16, %15
ret void
}

LLVM IR (-O2):
define void @str_replace(i8* %0, i8* %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 %17, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %1)
%8 = tail call i32 @strlen(i8* %2)
%9 = sext i32 %7 to i64
%10 = getelementptr inbounds i8, i8* %4, i64 %9
%11 = tail call i32 @strlen(i8* %10)
%12 = sext i32 %8 to i64
%13 = getelementptr inbounds i8, i8* %4, i64 %12
%14 = add nsw i32 %11, 1
%15 = tail call i32 @memmove(i8* %13, i8* %10, i32 %14)
%16 = tail call i32 @memcpy(i8* %4, i8* %2, i32 %8)
br label %17
17: ; preds = %3, %6
ret void
}