#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 */
size_t gs_nstr(char const *, char const *);
char *gs_replace(char *, char const *, char const *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(char const *);
size_t strlen(char const *);
char *gs_replace_m(const char *str, const char *src, const char *repl) {
  char *str_m;
  size_t len;
  if (!str)
    return NULL;
  len = gs_nstr(str, src);
  if (!len)
    return strdup(str);
  len = (strlen(str) - (len * strlen(src))) + (len * strlen(repl)) + 1;
  str_m = (char *)malloc(len);
  if (!str_m)
    return NULL;
  strcpy(str_m, str);
  return gs_replace(str_m, src, repl);
}


x86 assembly (clang, -O0):
<gs_replace_m>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118c <gs_replace_m+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    1260 <gs_replace_m+0x100>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <gs_nstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ba <gs_replace_m+0x5a>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1260 <gs_replace_m+0x100>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rcx
mov    %rax,%rdx
mov    -0x48(%rbp),%rax
imul   %rdx,%rcx
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rcx
mov    %rax,%rdx
mov    -0x38(%rbp),%rax
imul   %rdx,%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123e <gs_replace_m+0xde>
movq   $0x0,-0x8(%rbp)
jmp    1260 <gs_replace_m+0x100>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <gs_replace@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gs_replace_m>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11e7 <gs_replace_m+0x87>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1060 <gs_nstr@plt>
test   %rax,%rax
je     11f8 <gs_replace_m+0x98>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
sub    %rbp,%rax
imul   %r12,%rax
lea    (%rax,%r13,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11e7 <gs_replace_m+0x87>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <gs_replace@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <strdup@plt>

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

LLVM IR (-O2):
define i8* @gs_replace_m(i8* %0, i8* %1, i8* %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %24, label %5
5: ; preds = %3
%6 = tail call i64 @gs_nstr(i8* %0, i8* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i8* @strdup(i8* %0)
br label %24
10: ; preds = %5
%11 = tail call i64 @strlen(i8* %0)
%12 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%13 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%14 = sub i64 %13, %12
%15 = mul i64 %14, %6
%16 = add i64 %11, 1
%17 = add i64 %16, %15
%18 = tail call i64 @malloc(i64 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %24, label %21
21: ; preds = %10
%22 = tail call i32 @strcpy(i8* %19, i8* %0)
%23 = tail call i8* @gs_replace(i8* %19, i8* %1, i8* %2)
br label %24
24: ; preds = %10, %3, %21, %8
%25 = phi i8* [ %23, %21 ], [ %9, %8 ], [ null, %3 ], [ null, %10 ]
ret i8* %25
}