#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 count_initial_substrings(char const *, char const *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ replace_initial_substrings(char *, char const *,
                                              char const *, char const *);
size_t strlen(char const *);
char *alloc_replace_initial_substrings(const char *src_str,
                                       const char *old_sub_str,
                                       const char *new_sub_str) {
  char *dest_str;
  size_t old_sub_str_len = strlen(old_sub_str);
  size_t new_sub_str_len = strlen(new_sub_str);
  size_t sub_str_occs = count_initial_substrings(src_str, old_sub_str);
  size_t size_dest_str_len = strlen(src_str) - sub_str_occs * old_sub_str_len +
                             sub_str_occs * new_sub_str_len;
  dest_str = (char *)malloc(size_dest_str_len + 1);
  if (dest_str != NULL)
    replace_initial_substrings(dest_str, src_str, old_sub_str, new_sub_str);
  return (dest_str);
}


x86 assembly (clang, -O0):
<alloc_replace_initial_substrings>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <count_initial_substrings@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rcx
imul   -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    -0x38(%rbp),%rcx
imul   -0x30(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d5 <alloc_replace_initial_substrings+0x95>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1040 <replace_initial_substrings@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_replace_initial_substrings>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <count_initial_substrings@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1030 <strlen@plt>
sub    %r13,%rbx
imul   %rbp,%rbx
lea    (%rax,%rbx,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ad <alloc_replace_initial_substrings+0x6d>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <replace_initial_substrings@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @alloc_replace_initial_substrings(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%6 = tail call i64 @count_initial_substrings(i8* %0, i8* %1)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = sub i64 %5, %4
%9 = mul i64 %8, %6
%10 = add i64 %7, 1
%11 = add i64 %10, %9
%12 = tail call i64 @malloc(i64 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = tail call i32 @replace_initial_substrings(i8* %13, i8* %0, i8* %1, i8* %2)
br label %17
17: ; preds = %15, %3
ret i8* %13
}