#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 *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char *);
char *strstr(char *, char *);
int replace(char *str, char *what, char *by, int max_length) {
  char *foo, *bar = str;
  int i = 0;
  int str_length, what_length, by_length;
  if (!str)
    return 0;
  if (!what)
    return 0;
  if (!by)
    return 0;
  what_length = strlen(what);
  by_length = strlen(by);
  str_length = strlen(str);
  foo = strstr(bar, what);
  while ((foo) && ((str_length + by_length - what_length) < (max_length - 1))) {
    bar = foo + strlen(by);
    memmove(bar, foo + strlen(what), strlen(foo + strlen(what)) + 1);
    memcpy(foo, by, strlen(by));
    i++;
    foo = strstr(bar, what);
    str_length = strlen(str);
  }
  return i;
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1180 <replace+0x40>
movl   $0x0,-0x4(%rbp)
jmp    12f4 <replace+0x1b4>
cmpq   $0x0,-0x18(%rbp)
jne    1197 <replace+0x57>
movl   $0x0,-0x4(%rbp)
jmp    12f4 <replace+0x1b4>
cmpq   $0x0,-0x20(%rbp)
jne    11ae <replace+0x6e>
movl   $0x0,-0x4(%rbp)
jmp    12f4 <replace+0x1b4>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x49(%rbp)
je     120a <replace+0xca>
mov    -0x40(%rbp),%eax
add    -0x48(%rbp),%eax
sub    -0x44(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    121a <replace+0xda>
jmp    12ee <replace+0x1ae>
mov    -0x30(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x88(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x80(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x68(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x78(%rbp),%rdi
cltq
add    %rax,%rdi
call   1030 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1050 <memmove@plt>
mov    -0x30(%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    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x40(%rbp)
jmp    11e3 <replace+0xa3>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%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    %ecx,0xc(%rsp)
xor    %ebx,%ebx
mov    %rdi,0x10(%rsp)
test   %rdi,%rdi
je     124e <replace+0x10e>
mov    %rsi,%r12
test   %rsi,%rsi
je     124e <replace+0x10e>
mov    %rdx,%r15
test   %rdx,%rdx
je     124e <replace+0x10e>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,0x8(%rsp)
mov    0x10(%rsp),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     124e <replace+0x10e>
mov    %rax,%r14
sub    %r13d,0x8(%rsp)
addl   $0xffffffff,0xc(%rsp)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    0x8(%rsp),%ebp
cmp    0xc(%rsp),%ebp
jge    124e <replace+0x10e>
mov    %r15,%rdi
call   1030 <strlen@plt>
movslq %eax,%r13
add    %r14,%r13
mov    %r12,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbp
add    %r14,%rbp
mov    %r12,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r13,%rdi
mov    %rbp,%rsi
call   1050 <memmove@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <memcpy@plt>
add    $0x1,%ebx
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
mov    %rax,%r14
mov    0x10(%rsp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
test   %r14,%r14
jne    11d0 <replace+0x90>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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