#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
char *strstr(char *, char *);
int find_and_replace_text(char *sbuf, int buflen, char *from, char *to) {
  char *buf1;
  char *startbuf;
  char *cstart;
  int offs = 0;
  int roffs = 0;
  int len = 0;
  startbuf = sbuf;
  buf1 = malloc(sizeof(char) * buflen);
  if (buf1 == NULL)
    return -1;
  memset(buf1, 0, buflen);
  cstart = strstr(startbuf, from);
  while (cstart != NULL) {
    len = cstart - startbuf;
    roffs = cstart - sbuf;
    printf("\n\rfound %s @ %d\n\r", from, roffs);
    memcpy(buf1 + offs, startbuf, len);
    offs += len;
    memcpy(buf1 + offs, to, strlen(to));
    offs += strlen(to);
    roffs += strlen(from);
    startbuf = sbuf + roffs;
    cstart = strstr(startbuf, from);
  }
  if ((offs + strlen(startbuf)) > buflen) {
    printf("error - replace would overflow\n\r");
  } else {
    memcpy(buf1 + offs, sbuf + roffs, strlen(sbuf + roffs));
    memset(sbuf, 0, buflen);
    memcpy(sbuf, buf1, strlen(buf1));
  }
  free(buf1);
  return 0;
}


x86 assembly (clang, -O0):
<find_and_replace_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d1 <find_and_replace_text+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <find_and_replace_text+0x209>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12be <find_and_replace_text+0x14e>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x48(%rbp),%edx
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
movslq -0x44(%rbp),%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x4c(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x4c(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x44(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x48(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x40(%rbp)
jmp    11f0 <find_and_replace_text+0x80>
mov    -0x44(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x64(%rbp),%eax
add    %ecx,%eax
cmp    -0x14(%rbp),%eax
jle    12f0 <find_and_replace_text+0x180>
lea    0xd2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1369 <find_and_replace_text+0x1f9>
mov    -0x30(%rbp),%rax
movslq -0x44(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x88(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x48(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x80(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x48(%rbp),%rax
add    %rax,%rdi
call   1040 <strlen@plt>
mov    -0x88(%rbp),%rdi
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_and_replace_text>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1279 <find_and_replace_text+0x109>
xor    %r13d,%r13d
mov    %rax,(%rsp)
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,0xc(%rsp)
mov    %ebp,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     1283 <find_and_replace_text+0x113>
mov    %rax,%r15
xor    %r13d,%r13d
mov    %r12,0x10(%rsp)
mov    %rbx,%r12
mov    %r14,0x18(%rsp)
mov    %rbx,0x20(%rsp)
nopl   0x0(%rax)
mov    %r15d,%ebp
sub    %r12d,%ebp
sub    %ebx,%r15d
lea    0xe10(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movslq %r13d,%rbx
mov    (%rsp),%r14
lea    (%r14,%rbx,1),%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
add    %ebp,%ebx
movslq %ebx,%r13
mov    0x20(%rsp),%r12
lea    (%r14,%r13,1),%rbp
mov    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %eax,%r13d
mov    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
add    %r15d,%eax
movslq %eax,%r14
mov    %r12,%rbx
add    %r14,%r12
mov    %r12,%rdi
mov    %rbp,%rsi
call   1090 <strstr@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11e0 <find_and_replace_text+0x70>
jmp    1289 <find_and_replace_text+0x119>
mov    $0xffffffff,%eax
jmp    1306 <find_and_replace_text+0x196>
mov    %rbx,%r12
xor    %r14d,%r14d
mov    %r12,%rdi
call   1040 <strlen@plt>
add    %r13d,%eax
mov    0xc(%rsp),%r12d
cmp    %r12d,%eax
jle    12b2 <find_and_replace_text+0x142>
lea    0xd6d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rbx
jmp    12fc <find_and_replace_text+0x18c>
movslq %r13d,%rax
mov    %rbx,%r15
mov    (%rsp),%rbx
mov    %rbx,%rbp
add    %rax,%rbp
add    %r15,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"\0A\0Dfound %s @ %d\0A\0D\00"
@.str.1 = [33 x i8] c"error - replace would overflow\0A\0D\00"
define i32 @find_and_replace_text(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
%16 = load i8*, i8** %6
store i8* %16, i8** %11
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = mul i64 1, %18
%20 = trunc i64 %19 to i32
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %10
%22 = load i8*, i8** %10
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %4
store i32 -1, i32* %5
br label %119
25: ; preds = %4
%26 = load i8*, i8** %10
%27 = load i32, i32* %7
%28 = call i32 @memset(i8* %26, i32 0, i32 %27)
%29 = load i8*, i8** %11
%30 = load i8*, i8** %8
%31 = call i8* @strstr(i8* %29, i8* %30)
store i8* %31, i8** %12
br label %32
32: ; preds = %35, %25
%33 = load i8*, i8** %12
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %84
35: ; preds = %32
%36 = load i8*, i8** %12
%37 = load i8*, i8** %11
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
%41 = trunc i64 %40 to i32
store i32 %41, i32* %15
%42 = load i8*, i8** %12
%43 = load i8*, i8** %6
%44 = ptrtoint i8* %42 to i64
%45 = ptrtoint i8* %43 to i64
%46 = sub i64 %44, %45
%47 = trunc i64 %46 to i32
store i32 %47, i32* %14
%48 = load i8*, i8** %8
%49 = load i32, i32* %14
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %48, i32 %49)
%51 = load i8*, i8** %10
%52 = load i32, i32* %13
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = load i8*, i8** %11
%56 = load i32, i32* %15
%57 = call i32 @memcpy(i8* %54, i8* %55, i32 %56)
%58 = load i32, i32* %15
%59 = load i32, i32* %13
%60 = add nsw i32 %59, %58
store i32 %60, i32* %13
%61 = load i8*, i8** %10
%62 = load i32, i32* %13
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i8*, i8** %9
%66 = load i8*, i8** %9
%67 = call i32 @strlen(i8* %66)
%68 = call i32 @memcpy(i8* %64, i8* %65, i32 %67)
%69 = load i8*, i8** %9
%70 = call i32 @strlen(i8* %69)
%71 = load i32, i32* %13
%72 = add nsw i32 %71, %70
store i32 %72, i32* %13
%73 = load i8*, i8** %8
%74 = call i32 @strlen(i8* %73)
%75 = load i32, i32* %14
%76 = add nsw i32 %75, %74
store i32 %76, i32* %14
%77 = load i8*, i8** %6
%78 = load i32, i32* %14
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %77, i64 %79
store i8* %80, i8** %11
%81 = load i8*, i8** %11
%82 = load i8*, i8** %8
%83 = call i8* @strstr(i8* %81, i8* %82)
store i8* %83, i8** %12
br label %32
84: ; preds = %32
%85 = load i32, i32* %13
%86 = load i8*, i8** %11
%87 = call i32 @strlen(i8* %86)
%88 = add nsw i32 %85, %87
%89 = load i32, i32* %7
%90 = icmp sgt i32 %88, %89
br i1 %90, label %91, label %93
91: ; preds = %84
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %116
93: ; preds = %84
%94 = load i8*, i8** %10
%95 = load i32, i32* %13
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i8, i8* %94, i64 %96
%98 = load i8*, i8** %6
%99 = load i32, i32* %14
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8, i8* %98, i64 %100
%102 = load i8*, i8** %6
%103 = load i32, i32* %14
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds i8, i8* %102, i64 %104
%106 = call i32 @strlen(i8* %105)
%107 = call i32 @memcpy(i8* %97, i8* %101, i32 %106)
%108 = load i8*, i8** %6
%109 = load i32, i32* %7
%110 = call i32 @memset(i8* %108, i32 0, i32 %109)
%111 = load i8*, i8** %6
%112 = load i8*, i8** %10
%113 = load i8*, i8** %10
%114 = call i32 @strlen(i8* %113)
%115 = call i32 @memcpy(i8* %111, i8* %112, i32 %114)
br label %116
116: ; preds = %93, %91
%117 = load i8*, i8** %10
%118 = call i32 @free(i8* %117)
store i32 0, i32* %5
br label %119
119: ; preds = %116, %24
%120 = load i32, i32* %5
ret i32 %120
}

LLVM IR (-O2):
@.str = [18 x i8] c"\0A\0Dfound %s @ %d\0A\0D\00"
@.str.1 = [33 x i8] c"error - replace would overflow\0A\0D\00"
define i32 @find_and_replace_text(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = tail call i8* @malloc(i32 %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %60, label %7
7: ; preds = %4
%8 = tail call i32 @memset(i8* %5, i32 0, i32 %1)
%9 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %2)
%10 = icmp eq i8* %9, null
br i1 %10, label %40, label %11
11: ; preds = %7
%12 = ptrtoint i8* %0 to i64
br label %13
13: ; preds = %11, %13
%14 = phi i32 [ 0, %11 ], [ %33, %13 ]
%15 = phi i8* [ %9, %11 ], [ %38, %13 ]
%16 = phi i8* [ %0, %11 ], [ %37, %13 ]
%17 = ptrtoint i8* %15 to i64
%18 = ptrtoint i8* %16 to i64
%19 = sub i64 %17, %18
%20 = trunc i64 %19 to i32
%21 = sub i64 %17, %12
%22 = trunc i64 %21 to i32
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %22)
%24 = sext i32 %14 to i64
%25 = getelementptr inbounds i8, i8* %5, i64 %24
%26 = tail call i32 @memcpy(i8* %25, i8* %16, i32 %20)
%27 = add nsw i32 %14, %20
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %5, i64 %28
%30 = tail call i32 @strlen(i8* %3)
%31 = tail call i32 @memcpy(i8* %29, i8* %3, i32 %30)
%32 = tail call i32 @strlen(i8* %3)
%33 = add nsw i32 %32, %27
%34 = tail call i32 @strlen(i8* %2)
%35 = add nsw i32 %34, %22
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %0, i64 %36
%38 = tail call i8* @strstr(i8* dereferenceable(1) %37, i8* dereferenceable(1) %2)
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %13
40: ; preds = %13, %7
%41 = phi i8* [ %0, %7 ], [ %37, %13 ]
%42 = phi i32 [ 0, %7 ], [ %33, %13 ]
%43 = phi i64 [ 0, %7 ], [ %36, %13 ]
%44 = tail call i32 @strlen(i8* %41)
%45 = add nsw i32 %44, %42
%46 = icmp sgt i32 %45, %1
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %58
49: ; preds = %40
%50 = sext i32 %42 to i64
%51 = getelementptr inbounds i8, i8* %5, i64 %50
%52 = getelementptr inbounds i8, i8* %0, i64 %43
%53 = tail call i32 @strlen(i8* %52)
%54 = tail call i32 @memcpy(i8* %51, i8* %52, i32 %53)
%55 = tail call i32 @memset(i8* %0, i32 0, i32 %1)
%56 = tail call i32 @strlen(i8* %5)
%57 = tail call i32 @memcpy(i8* %0, i8* %5, i32 %56)
br label %58
58: ; preds = %49, %47
%59 = tail call i32 @free(i8* %5)
br label %60
60: ; preds = %4, %58
%61 = phi i32 [ 0, %58 ], [ -1, %4 ]
ret i32 %61
}