#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ printf(char *, int);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strstr(char *, char const *);
void replace(char *line, const char *from, const char *to) {
  char *idx = line;
  while ((idx = strstr(idx, from)) != NULL) {
    int len = strlen(line);
    int len_all = len - strlen(from) + strlen(to);
    int len_from = strlen(from);
    int len_to = strlen(to);
    int len_first = line - idx;
    int len_last = len - len_first - strlen(from);
    if (len_all > 10 * 1024) {
      printf("ERROR: Line to long (%d).\n", len);
      exit(EXIT_FAILURE);
    }
    if (len_from == len_to) {
      strncpy(idx, to, len_from);
    } else {
      memmove(idx + len_to, idx + len_from, len_last);
      strncpy(idx, to, len_to);
      line[len_all] = '\0';
    }
  }
}


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    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     129b <replace+0x13b>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
sub    -0x34(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x2800,-0x28(%rbp)
jle    123b <replace+0xdb>
mov    -0x24(%rbp),%esi
lea    0xdd8(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    125c <replace+0xfc>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncpy@plt>
jmp    1296 <replace+0x136>
mov    -0x20(%rbp),%rdi
movslq -0x30(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x38(%rbp),%edx
call   1060 <memmove@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    117c <replace+0x1c>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x10(%rsp)
mov    %rsi,%rbp
mov    %rdi,0x8(%rsp)
call   1080 <strstr@plt>
test   %rax,%rax
je     1255 <replace+0xf5>
mov    %rax,%r13
jmp    11e0 <replace+0x80>
xchg   %ax,%ax
mov    %r13d,%ecx
sub    0x8(%rsp),%ecx
add    %ecx,%r12d
sub    %eax,%r12d
movslq %r15d,%rdi
add    %r13,%rdi
movslq %r14d,%rsi
add    %r13,%rsi
mov    %r12d,%edx
call   1060 <memmove@plt>
mov    %r13,%rdi
mov    0x10(%rsp),%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
mov    0x8(%rsp),%rcx
movb   $0x0,(%rcx,%rax,1)
mov    %r13,%rdi
mov    %rbp,%rsi
call   1080 <strstr@plt>
mov    %rax,%r13
test   %rax,%rax
je     1255 <replace+0xf5>
mov    0x8(%rsp),%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rbp,%r14
mov    %r12d,%ebp
sub    %eax,%ebp
mov    0x10(%rsp),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
add    %ebp,%ebx
mov    %r14,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    $0x2801,%ebx
jge    1264 <replace+0x104>
cmp    %r15d,%r14d
jne    1190 <replace+0x30>
mov    %r13,%rdi
mov    0x10(%rsp),%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
jmp    11cd <replace+0x6d>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd95(%rip),%rdi
mov    %r12d,%esi
call   1050 <printf@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [27 x i8] c"ERROR: Line to long (%d).\0A\00"
@EXIT_FAILURE = global i32 0
define void @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 %44, label %6
6: ; preds = %3
%7 = ptrtoint i8* %0 to i64
br label %8
8: ; preds = %6, %41
%9 = phi i8* [ %4, %6 ], [ %42, %41 ]
%10 = tail call i32 @strlen(i8* %0)
%11 = tail call i32 @strlen(i8* %1)
%12 = sub nsw i32 %10, %11
%13 = tail call i32 @strlen(i8* %2)
%14 = add nsw i32 %12, %13
%15 = tail call i32 @strlen(i8* %1)
%16 = tail call i32 @strlen(i8* %2)
%17 = ptrtoint i8* %9 to i64
%18 = sub i64 %17, %7
%19 = trunc i64 %18 to i32
%20 = add i32 %10, %19
%21 = tail call i32 @strlen(i8* %1)
%22 = sub i32 %20, %21
%23 = icmp sgt i32 %14, 10240
br i1 %23, label %24, label %28
24: ; preds = %8
%25 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %10)
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %8
%29 = icmp eq i32 %15, %16
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = tail call i32 @strncpy(i8* %9, i8* %2, i32 %15)
br label %41
32: ; preds = %28
%33 = sext i32 %16 to i64
%34 = getelementptr inbounds i8, i8* %9, i64 %33
%35 = sext i32 %15 to i64
%36 = getelementptr inbounds i8, i8* %9, i64 %35
%37 = tail call i32 @memmove(i8* %34, i8* %36, i32 %22)
%38 = tail call i32 @strncpy(i8* %9, i8* %2, i32 %16)
%39 = sext i32 %14 to i64
%40 = getelementptr inbounds i8, i8* %0, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %32, %30
%42 = tail call i8* @strstr(i8* dereferenceable(1) %9, i8* dereferenceable(1) %1)
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %8
44: ; preds = %41, %3
ret void
}