#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 */
scalar_t__ calloc(int, int);
int inStr(char *, int, char *);
char *str3cat(char *, char *, char *);
int strlen(char *);
char *substring(char *, int, int);
char *replace(char *text, char *from, char *to) {
  char *ret =
      (char *)calloc(strlen(text) + (strlen(to) - strlen(from)), sizeof(char));
  char *left;
  char *right;
  int pos = inStr(text, strlen(text), from);
  if (pos != -1) {
    left = substring(text, 0, pos);
    right = substring(text, pos + strlen(from),
                      strlen(text) - (pos + strlen(from)));
    ret = str3cat(left, to, right);
    return replace(ret, from, to);
  }
  return text;
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x50(%rbp),%edi
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
sub    %ecx,%eax
add    %eax,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
call   1040 <inStr@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
je     1275 <replace+0x115>
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%edx
xor    %esi,%esi
call   1050 <substring@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x64(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x64(%rbp),%edx
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%esi
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
add    %ecx,%eax
sub    %eax,%edx
call   1050 <substring@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1080 <str3cat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <replace@plt>
mov    %rax,-0x8(%rbp)
jmp    127d <replace+0x11d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%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,%r14
mov    %rsi,0x8(%rsp)
mov    %rdi,%r12
mov    0x8(%rsp),%r13
xchg   %ax,%ax
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %r15d,%ebp
sub    %eax,%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %r12,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1040 <inStr@plt>
cmp    $0xffffffff,%eax
je     121e <replace+0xce>
mov    %eax,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1050 <substring@plt>
mov    %rax,0x10(%rsp)
mov    %r13,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rbx,1),%ebp
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %r14,%r15
mov    %eax,%r14d
mov    %r13,%rdi
call   1030 <strlen@plt>
add    %ebx,%eax
sub    %eax,%r14d
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %r15,%r14
call   1050 <substring@plt>
mov    0x10(%rsp),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1070 <str3cat@plt>
mov    %rax,%r12
jmp    1170 <replace+0x20>
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @replace(i8* %0, i8* %1, i8* %2) {
br label %4
4: ; preds = %15, %3
%5 = phi i8* [ %0, %3 ], [ %24, %15 ]
%6 = tail call i32 @strlen(i8* %5)
%7 = tail call i32 @strlen(i8* %2)
%8 = tail call i32 @strlen(i8* %1)
%9 = add i32 %7, %6
%10 = sub i32 %9, %8
%11 = tail call i64 @calloc(i32 %10, i32 1)
%12 = tail call i32 @strlen(i8* %5)
%13 = tail call i32 @inStr(i8* %5, i32 %12, i8* %1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %25, label %15
15: ; preds = %4
%16 = tail call i8* @substring(i8* %5, i32 0, i32 %13)
%17 = tail call i32 @strlen(i8* %1)
%18 = add nsw i32 %17, %13
%19 = tail call i32 @strlen(i8* %5)
%20 = tail call i32 @strlen(i8* %1)
%21 = add i32 %13, %20
%22 = sub i32 %19, %21
%23 = tail call i8* @substring(i8* %5, i32 %18, i32 %22)
%24 = tail call i8* @str3cat(i8* %16, i8* %2, i8* %23)
br label %4
25: ; preds = %4
ret i8* %5
}