#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*/ asprintf(char **, char *, char *, char *, char *);
int /*<<< orphan*/ free(char *);
char *strdup(char *);
int strlen(char *);
char *strndup(char *, int);
char *strstr(char *, char *);
char *replace(char *str, char *find, char *repl) {
  char *p;
  char *newstr;
  char *tmp;
  if (!(p = strstr(str, find)))
    return strdup(str);
  tmp = strndup(str, p - str);
  asprintf(&newstr, "%s%s%s", tmp, repl, p + strlen(find));
  free(tmp);
  return newstr;
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a1 <replace+0x41>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    120e <replace+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1050 <strndup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%r8
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rcx
cltq
add    %rax,%r8
lea    -0x30(%rbp),%rdi
lea    0xe08(%rip),%rsi
call   1060 <asprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1080 <strstr@plt>
test   %rax,%rax
je     11c9 <replace+0x69>
mov    %rax,%r12
mov    %r12d,%esi
sub    %ebx,%esi
mov    %rbx,%rdi
call   1050 <strndup@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%r8
add    %r12,%r8
lea    0xe5d(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1060 <asprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <strdup@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"%s%s%s\00"
define i8* @replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = load i8*, i8** %6
%13 = call i8* @strstr(i8* %11, i8* %12)
store i8* %13, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %4
br label %38
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = load i8*, i8** %8
%21 = load i8*, i8** %5
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = call i8* @strndup(i8* %19, i32 %25)
store i8* %26, i8** %10
%27 = load i8*, i8** %10
%28 = load i8*, i8** %7
%29 = load i8*, i8** %8
%30 = load i8*, i8** %6
%31 = call i32 @strlen(i8* %30)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = call i32 @asprintf(i8** %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %27, i8* %28, i8* %33)
%35 = load i8*, i8** %10
%36 = call i32 @free(i8* %35)
%37 = load i8*, i8** %9
store i8* %37, i8** %4
br label %38
38: ; preds = %18, %15
%39 = load i8*, i8** %4
ret i8* %39
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s%s%s\00"
define i8* @replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i8* @strdup(i8* %0)
br label %22
10: ; preds = %3
%11 = ptrtoint i8* %6 to i64
%12 = ptrtoint i8* %0 to i64
%13 = sub i64 %11, %12
%14 = trunc i64 %13 to i32
%15 = tail call i8* @strndup(i8* %0, i32 %14)
%16 = tail call i32 @strlen(i8* %1)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %6, i64 %17
%19 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %2, i8* %18)
%20 = call i32 @free(i8* %15)
%21 = load i8*, i8** %4
br label %22
22: ; preds = %10, %8
%23 = phi i8* [ %21, %10 ], [ %9, %8 ]
ret i8* %23
}