#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*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
void replace(char *source, char *target, char *from, char *to) {
  char *match = strstr(source, from);
  if (!match)
    strcpy(target, source);
  else {
    int len = match - source;
    strncpy(target, source, len);
    target[len] = '\0';
    sprintf(target + len, "%s%s", to, match + strlen(from));
  }
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1196 <replace+0x46>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    1205 <replace+0xb5>
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rcx
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xe00(%rip),%rsi
call   1060 <sprintf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rdx,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11c4 <replace+0x74>
mov    %rax,%r13
mov    %rax,%rbp
sub    %rbx,%rbp
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movslq %ebp,%rax
lea    (%r12,%rax,1),%rbx
movb   $0x0,(%r12,%rax,1)
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
add    %r13,%rcx
lea    0xe55(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <sprintf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <strcpy@plt>

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

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