#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*/ _snprintf(char *, int, char *, char *, char const *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int strlen(char const *);
char *strstr(char *, char const *);
void replacedata(const char *from, const char *to, char *line, int maxlen) {
  char *p;
  int len;
  char *before, *after;
  if ((p = strstr(line, from)) != NULL) {
    if (p <= line) {
      before = malloc(1);
      *before = '\0';
    } else {
      len = p - line;
      before = malloc(len + 1);
      memcpy(before, line, len);
      before[len] = '\0';
    }
    p += strlen(from);
    if (strlen(p) < 1) {
      after = malloc(1);
      *after = '\0';
    } else {
      len = strlen(p);
      after = malloc(len + 1);
      memcpy(after, p, len);
      after[len] = '\0';
    }
    _snprintf(line, maxlen - 1, "%s%s%s", before, to, after);
    free(before);
    free(after);
  }
}


x86 assembly (clang, -O0):
<replacedata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12a3 <replacedata+0x143>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     11ba <replacedata+0x5a>
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
jmp    11f3 <replacedata+0x93>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x1,%eax
jge    1238 <replacedata+0xd8>
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
jmp    126f <replacedata+0x10f>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x40(%rbp),%r9
lea    0xd74(%rip),%rdx
call   1070 <_snprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replacedata>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r12d
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rdx,%rdi
mov    %r15,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11bf <replacedata+0x5f>
mov    %rax,%r14
mov    %rax,%rbp
sub    %r13,%rbp
mov    %rbx,0x10(%rsp)
mov    %r12d,0xc(%rsp)
jbe    11ce <replacedata+0x6e>
lea    0x1(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rax
add    %r12,%rax
jmp    11db <replacedata+0x7b>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
movb   $0x0,(%rax)
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1221 <replacedata+0xc1>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0x1(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rax
add    %rbx,%rax
jmp    122e <replacedata+0xce>
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    0xc(%rsp),%esi
add    $0xffffffff,%esi
lea    0xdc1(%rip),%rdx
mov    %r13,%rdi
mov    %r12,%rcx
mov    0x10(%rsp),%r8
mov    %rbx,%r9
call   1070 <_snprintf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"%s%s%s\00"
define void @replacedata(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%13 = load i8*, i8** %7
%14 = load i8*, i8** %5
%15 = call i8* @strstr(i8* %13, i8* %14)
store i8* %15, i8** %9
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %80
17: ; preds = %4
%18 = load i8*, i8** %9
%19 = load i8*, i8** %7
%20 = icmp ule i8* %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i8* @malloc(i32 1)
store i8* %22, i8** %11
%23 = load i8*, i8** %11
store i8 0, i8* %23
br label %42
24: ; preds = %17
%25 = load i8*, i8** %9
%26 = load i8*, i8** %7
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = add nsw i32 %31, 1
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %11
%34 = load i8*, i8** %11
%35 = load i8*, i8** %7
%36 = load i32, i32* %10
%37 = call i32 @memcpy(i8* %34, i8* %35, i32 %36)
%38 = load i8*, i8** %11
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 0, i8* %41
br label %42
42: ; preds = %24, %21
%43 = load i8*, i8** %5
%44 = call i32 @strlen(i8* %43)
%45 = load i8*, i8** %9
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8* %47, i8** %9
%48 = load i8*, i8** %9
%49 = call i32 @strlen(i8* %48)
%50 = icmp slt i32 %49, 1
br i1 %50, label %51, label %54
51: ; preds = %42
%52 = call i8* @malloc(i32 1)
store i8* %52, i8** %12
%53 = load i8*, i8** %12
store i8 0, i8* %53
br label %68
54: ; preds = %42
%55 = load i8*, i8** %9
%56 = call i32 @strlen(i8* %55)
store i32 %56, i32* %10
%57 = load i32, i32* %10
%58 = add nsw i32 %57, 1
%59 = call i8* @malloc(i32 %58)
store i8* %59, i8** %12
%60 = load i8*, i8** %12
%61 = load i8*, i8** %9
%62 = load i32, i32* %10
%63 = call i32 @memcpy(i8* %60, i8* %61, i32 %62)
%64 = load i8*, i8** %12
%65 = load i32, i32* %10
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8 0, i8* %67
br label %68
68: ; preds = %54, %51
%69 = load i8*, i8** %7
%70 = load i32, i32* %8
%71 = sub nsw i32 %70, 1
%72 = load i8*, i8** %11
%73 = load i8*, i8** %6
%74 = load i8*, i8** %12
%75 = call i32 @_snprintf(i8* %69, i32 %71, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %72, i8* %73, i8* %74)
%76 = load i8*, i8** %11
%77 = call i32 @free(i8* %76)
%78 = load i8*, i8** %12
%79 = call i32 @free(i8* %78)
br label %80
80: ; preds = %68, %4
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s%s%s\00"
define void @replacedata(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = tail call i8* @strstr(i8* dereferenceable(1) %2, i8* dereferenceable(1) %0)
%6 = icmp eq i8* %5, null
br i1 %6, label %46, label %7
7: ; preds = %4
%8 = icmp ugt i8* %5, %2
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
br label %22
11: ; preds = %7
%12 = ptrtoint i8* %5 to i64
%13 = ptrtoint i8* %2 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = add nsw i32 %15, 1
%17 = tail call i8* @malloc(i32 %16)
%18 = tail call i32 @memcpy(i8* %17, i8* %2, i32 %15)
%19 = shl i64 %14, 32
%20 = ashr exact i64 %19, 32
%21 = getelementptr inbounds i8, i8* %17, i64 %20
br label %22
22: ; preds = %11, %9
%23 = phi i8* [ %21, %11 ], [ %10, %9 ]
%24 = phi i8* [ %17, %11 ], [ %10, %9 ]
store i8 0, i8* %23
%25 = tail call i32 @strlen(i8* %0)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %5, i64 %26
%28 = tail call i32 @strlen(i8* %27)
%29 = icmp slt i32 %28, 1
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
br label %39
32: ; preds = %22
%33 = tail call i32 @strlen(i8* %27)
%34 = add nsw i32 %33, 1
%35 = tail call i8* @malloc(i32 %34)
%36 = tail call i32 @memcpy(i8* %35, i8* %27, i32 %33)
%37 = sext i32 %33 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
br label %39
39: ; preds = %32, %30
%40 = phi i8* [ %38, %32 ], [ %31, %30 ]
%41 = phi i8* [ %35, %32 ], [ %31, %30 ]
store i8 0, i8* %40
%42 = add nsw i32 %3, -1
%43 = tail call i32 @_snprintf(i8* %2, i32 %42, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %1, i8* %41)
%44 = tail call i32 @free(i8* %24)
%45 = tail call i32 @free(i8* %41)
br label %46
46: ; preds = %39, %4
ret void
}