#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*/ bzero(char *, size_t);
size_t str_match(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *string_replace(char *input, char *match, char *with, char *output,
                     size_t max_out) {
  size_t i_l = strlen(input), w_l = strlen(with), m_l = strlen(match);
  size_t m_off = str_match(input, match);
  if ((int)m_off < 0) {
    return output;
  }
  bzero(output, max_out);
  strncpy(output, input, m_off);
  strncpy(&output[m_off], with, w_l);
  strncpy(&output[m_off + w_l], &input[m_off + m_l], i_l - m_off - m_l);
  return output;
}


x86 assembly (clang, -O0):
<string_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    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <str_match@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    $0x0,%eax
jge    11ae <string_replace+0x6e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1218 <string_replace+0xd8>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <bzero@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rdi
add    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x50(%rbp),%rax
add    -0x40(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x50(%rbp),%rax
add    -0x48(%rbp),%rax
add    %rax,%rsi
mov    -0x38(%rbp),%rdx
sub    -0x50(%rbp),%rdx
sub    -0x48(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,(%rsp)
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %rax,0x10(%rsp)
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdi
mov    %r15,%rbx
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
mov    %rbp,%rsi
call   1050 <str_match@plt>
mov    %rax,%r15
test   $0x80000000,%r15d
jne    11e7 <string_replace+0xa7>
mov    %rbx,%rdi
mov    (%rsp),%rsi
call   1060 <bzero@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
lea    (%rbx,%r15,1),%rdi
mov    0x8(%rsp),%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
add    %r15,%r12
add    %rbx,%r12
add    %r15,%r13
add    %r13,%r14
mov    0x10(%rsp),%rdx
sub    %r13,%rdx
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @string_replace(i8* %0, i8* %1, i8* %2, i8* returned %3, i64 %4) {
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%9 = tail call i64 @str_match(i8* %0, i8* %1)
%10 = and i64 %9, 2147483648
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %5
%13 = tail call i32 @bzero(i8* %3, i64 %4)
%14 = tail call i32 @strncpy(i8* %3, i8* %0, i64 %9)
%15 = getelementptr inbounds i8, i8* %3, i64 %9
%16 = tail call i32 @strncpy(i8* %15, i8* %2, i64 %7)
%17 = add i64 %9, %7
%18 = getelementptr inbounds i8, i8* %3, i64 %17
%19 = add i64 %9, %8
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = sub i64 %6, %19
%22 = tail call i32 @strncpy(i8* %18, i8* %20, i64 %21)
br label %23
23: ; preds = %5, %12
ret i8* %3
}