#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*/ memmove(char *, char *, int);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strstr(char *, char const *);
int replace(char *buf, size_t buflen, const char *from, size_t fromlen,
            const char *to, size_t tolen) {
  char *match = strstr(buf, from);
  if (match) {
    char *tail = match + fromlen;
    size_t tail_len = strlen(tail);
    if ((match + tolen + tail_len + sizeof(char)) > (buf + buflen)) {
      return 1;
    }
    memmove(match + tolen, tail, tail_len + sizeof(""));
    strncpy(match, to, tolen);
  }
  return 0;
}


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    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     11ee <replace+0xae>
mov    -0x40(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
add    -0x50(%rbp),%rax
add    $0x1,%rax
mov    -0x10(%rbp),%rcx
add    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jbe    11c2 <replace+0x82>
movl   $0x1,-0x4(%rbp)
jmp    11f5 <replace+0xb5>
mov    -0x40(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edx
call   1050 <memmove@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
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    %r9,%r15
mov    %r8,%r14
mov    %rcx,%r12
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rdx,%rsi
call   1060 <strstr@plt>
xor    %ecx,%ecx
test   %rax,%rax
je     11ad <replace+0x6d>
mov    %rax,%r13
add    %rax,%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    (%r15,%r13,1),%rdi
lea    (%rax,%rdi,1),%rdx
add    $0x1,%rdx
add    %rbp,%rbx
mov    $0x1,%ecx
cmp    %rbx,%rdx
ja     11ad <replace+0x6d>
add    $0x1,%eax
mov    %r12,%rsi
mov    %eax,%edx
call   1050 <memmove@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @replace(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4, i64 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i64
store i8* %0, i8** %8
store i64 %1, i64* %9
store i8* %2, i8** %10
store i64 %3, i64* %11
store i8* %4, i8** %12
store i64 %5, i64* %13
%17 = load i8*, i8** %8
%18 = load i8*, i8** %10
%19 = call i8* @strstr(i8* %17, i8* %18)
store i8* %19, i8** %14
%20 = load i8*, i8** %14
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %52
22: ; preds = %6
%23 = load i8*, i8** %14
%24 = load i64, i64* %11
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %15
%26 = load i8*, i8** %15
%27 = call i64 @strlen(i8* %26)
store i64 %27, i64* %16
%28 = load i8*, i8** %14
%29 = load i64, i64* %13
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = load i64, i64* %16
%32 = getelementptr inbounds i8, i8* %30, i64 %31
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load i8*, i8** %8
%35 = load i64, i64* %9
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = icmp ugt i8* %33, %36
br i1 %37, label %38, label %39
38: ; preds = %22
store i32 1, i32* %7
br label %53
39: ; preds = %22
%40 = load i8*, i8** %14
%41 = load i64, i64* %13
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = load i8*, i8** %15
%44 = load i64, i64* %16
%45 = add i64 %44, 1
%46 = trunc i64 %45 to i32
%47 = call i32 @memmove(i8* %42, i8* %43, i32 %46)
%48 = load i8*, i8** %14
%49 = load i8*, i8** %12
%50 = load i64, i64* %13
%51 = call i32 @strncpy(i8* %48, i8* %49, i64 %50)
br label %52
52: ; preds = %39, %6
store i32 0, i32* %7
br label %53
53: ; preds = %52, %38
%54 = load i32, i32* %7
ret i32 %54
}

LLVM IR (-O2):
define i32 @replace(i8* %0, i64 %1, i8* nocapture %2, i64 %3, i8* %4, i64 %5) {
%7 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %2)
%8 = icmp eq i8* %7, null
br i1 %8, label %22, label %9
9: ; preds = %6
%10 = getelementptr inbounds i8, i8* %7, i64 %3
%11 = tail call i64 @strlen(i8* %10)
%12 = getelementptr inbounds i8, i8* %7, i64 %5
%13 = getelementptr inbounds i8, i8* %12, i64 %11
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = getelementptr inbounds i8, i8* %0, i64 %1
%16 = icmp ugt i8* %14, %15
br i1 %16, label %22, label %17
17: ; preds = %9
%18 = trunc i64 %11 to i32
%19 = add i32 %18, 1
%20 = tail call i32 @memmove(i8* %12, i8* %10, i32 %19)
%21 = tail call i32 @strncpy(i8* %7, i8* %4, i64 %5)
br label %22
22: ; preds = %6, %17, %9
%23 = phi i32 [ 1, %9 ], [ 0, %17 ], [ 0, %6 ]
ret i32 %23
}