#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*/ free(char *);
size_t gs_nstr(char *, char const *);
char *gs_replace(char *, char const *, char const *);
scalar_t__ realloc(char *, size_t);
size_t strlen(char const *);
char *gs_replace_r(char *str, const char *src, const char *repl) {
  char *save;
  size_t len;
  if (!str)
    return NULL;
  len = gs_nstr(str, src);
  if (!len)
    return str;
  len = strlen(str) - (len * strlen(src)) + (len * strlen(repl)) + 1;
  str = (char *)realloc(save = str, len);
  if (!str) {
    free(save);
    return 0;
  }
  return gs_replace(str, src, repl);
}


x86 assembly (clang, -O0):
<gs_replace_r>:
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    117c <gs_replace_r+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    124f <gs_replace_r+0xff>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <gs_nstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a5 <gs_replace_r+0x55>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    124f <gs_replace_r+0xff>
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    -0x10(%rbp),%rdi
mov    %rdi,-0x28(%rbp)
mov    -0x30(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    123a <gs_replace_r+0xea>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    124f <gs_replace_r+0xff>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <gs_replace@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gs_replace_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11d4 <gs_replace_r+0x84>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1050 <gs_nstr@plt>
test   %rax,%rax
je     11d6 <gs_replace_r+0x86>
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),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     11cc <gs_replace_r+0x7c>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <gs_replace@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @gs_replace_r(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 %49
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 %21, label %19
19: ; preds = %13
%20 = load i8*, i8** %5
store i8* %20, i8** %4
br label %49
21: ; preds = %13
%22 = load i8*, i8** %5
%23 = call i64 @strlen(i8* %22)
%24 = load i64, i64* %9
%25 = load i8*, i8** %6
%26 = call i64 @strlen(i8* %25)
%27 = mul i64 %24, %26
%28 = sub i64 %23, %27
%29 = load i64, i64* %9
%30 = load i8*, i8** %7
%31 = call i64 @strlen(i8* %30)
%32 = mul i64 %29, %31
%33 = add i64 %28, %32
%34 = add i64 %33, 1
store i64 %34, i64* %9
%35 = load i8*, i8** %5
store i8* %35, i8** %8
%36 = load i64, i64* %9
%37 = call i64 @realloc(i8* %35, i64 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %5
%39 = load i8*, i8** %5
%40 = icmp ne i8* %39, null
br i1 %40, label %44, label %41
41: ; preds = %21
%42 = load i8*, i8** %8
%43 = call i32 @free(i8* %42)
store i8* null, i8** %4
br label %49
44: ; preds = %21
%45 = load i8*, i8** %5
%46 = load i8*, i8** %6
%47 = load i8*, i8** %7
%48 = call i8* @gs_replace(i8* %45, i8* %46, i8* %47)
store i8* %48, i8** %4
br label %49
49: ; preds = %44, %41, %19, %12
%50 = load i8*, i8** %4
ret i8* %50
}

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