#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*/ memcpy(char *, char const *, void *);
int /*<<< orphan*/ memmove(char *, char *, void *);
void *strlen(char const *);
char *strstr(char *, char const *);
int strrepl(char *s, const char *orig, const char *new) {
  char *found;
  int replacements = 0;
  size_t olen = strlen(orig);
  size_t nlen = strlen(new);
  int diff = strlen(orig) - strlen(new);
  if (diff < 0)
    return 0;
  while ((found = strstr(s, orig))) {
    replacements++;
    memcpy(found, new, strlen(new));
    if (diff)
      memmove(found + nlen, found + olen, strlen(found + olen) + 1);
    s = found + nlen;
  }
  return replacements;
}


x86 assembly (clang, -O0):
<strrepl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11ae <strrepl+0x6e>
movl   $0x0,-0x4(%rbp)
jmp    125a <strrepl+0x11a>
jmp    11b3 <strrepl+0x73>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1254 <strrepl+0x114>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    %rax,%rdx
call   1040 <memcpy@plt>
cmpl   $0x0,-0x44(%rbp)
je     1243 <strrepl+0x103>
mov    -0x28(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x28(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rdi
add    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1050 <memmove@plt>
mov    -0x28(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    11b3 <strrepl+0x73>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrepl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,(%rsp)
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
call   1030 <strlen@plt>
xor    %ebp,%ebp
sub    %eax,%ebx
mov    %rbx,0x8(%rsp)
js     1216 <strrepl+0xd6>
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     1216 <strrepl+0xd6>
mov    %rax,%r15
xor    %ebp,%ebp
jmp    11c9 <strrepl+0x89>
nopl   0x0(%rax)
add    %r14,%r15
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
mov    %rax,%r15
add    $0x1,%ebp
test   %rax,%rax
je     1216 <strrepl+0xd6>
mov    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <memcpy@plt>
cmpl   $0x0,0x8(%rsp)
je     11b0 <strrepl+0x70>
mov    %r12,%rbx
lea    (%r15,%r14,1),%r12
mov    (%rsp),%rax
lea    (%r15,%rax,1),%r13
mov    %r13,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r12,%rdi
mov    %rbx,%r12
mov    %r13,%rsi
call   1050 <memmove@plt>
jmp    11b0 <strrepl+0x70>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @strrepl(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* @strlen(i8* %1)
%5 = ptrtoint i8* %4 to i64
%6 = tail call i8* @strlen(i8* %2)
%7 = ptrtoint i8* %6 to i64
%8 = tail call i8* @strlen(i8* %1)
%9 = tail call i8* @strlen(i8* %2)
%10 = ptrtoint i8* %8 to i64
%11 = ptrtoint i8* %9 to i64
%12 = sub i64 %10, %11
%13 = trunc i64 %12 to i32
%14 = icmp slt i32 %13, 0
br i1 %14, label %36, label %15
15: ; preds = %3
%16 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%17 = icmp eq i8* %16, null
br i1 %17, label %36, label %18
18: ; preds = %15
%19 = icmp eq i32 %13, 0
br label %20
20: ; preds = %18, %32
%21 = phi i8* [ %16, %18 ], [ %34, %32 ]
%22 = phi i32 [ 0, %18 ], [ %23, %32 ]
%23 = add nuw nsw i32 %22, 1
%24 = tail call i8* @strlen(i8* %2)
%25 = tail call i32 @memcpy(i8* %21, i8* %2, i8* %24)
br i1 %19, label %32, label %26
26: ; preds = %20
%27 = getelementptr inbounds i8, i8* %21, i64 %7
%28 = getelementptr inbounds i8, i8* %21, i64 %5
%29 = tail call i8* @strlen(i8* %28)
%30 = getelementptr i8, i8* %29, i64 1
%31 = tail call i32 @memmove(i8* %27, i8* %28, i8* %30)
br label %32
32: ; preds = %26, %20
%33 = getelementptr inbounds i8, i8* %21, i64 %7
%34 = tail call i8* @strstr(i8* dereferenceable(1) %33, i8* dereferenceable(1) %1)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %20
36: ; preds = %32, %15, %3
%37 = phi i32 [ 0, %3 ], [ 0, %15 ], [ %23, %32 ]
ret i32 %37
}