#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 */
char *calloc(size_t, int);
int /*<<< orphan*/ free(size_t *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
size_t *strutil_indices_of(char *, char, size_t *);
char *strutil_replace_all(char *string, char old, char new) {
  size_t amount = 0;
  size_t length = strlen(string);
  size_t *indices = strutil_indices_of(string, old, &amount);
  char *replaced = calloc(length + 1, sizeof(char));
  strncpy(replaced, string, length);
  for (size_t i = 0; i < amount; i++) {
    replaced[indices[i]] = new;
  }
  free(indices);
  return replaced;
}


x86 assembly (clang, -O0):
<strutil_replace_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %sil,%cl
mov    %rdi,-0x8(%rbp)
mov    %cl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rdx
movsbl -0x9(%rbp),%esi
call   1070 <strutil_indices_of@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <strncpy@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11f5 <strutil_replace_all+0xa5>
mov    -0xa(%rbp),%dl
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%rsi
mov    (%rcx,%rsi,8),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11c0 <strutil_replace_all+0x70>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strutil_replace_all>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
call   1050 <strlen@plt>
mov    %rax,%r12
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    %ebx,%esi
call   1070 <strutil_indices_of@plt>
mov    %rax,%rbx
lea    0x1(%r12),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <strncpy@plt>
cmpq   $0x0,0x8(%rsp)
je     11d4 <strutil_replace_all+0x84>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%rax,8),%rcx
mov    %r14b,0x0(%rbp,%rcx,1)
add    $0x1,%rax
cmp    0x8(%rsp),%rax
jb     11c0 <strutil_replace_all+0x70>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strutil_replace_all(i8* %0, i8 signext %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %4
store i8 %1, i8* %5
store i8 %2, i8* %6
store i64 0, i64* %7
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %8
%14 = load i8*, i8** %4
%15 = load i8, i8* %5
%16 = call i64* @strutil_indices_of(i8* %14, i8 signext %15, i64* %7)
store i64* %16, i64** %9
%17 = load i64, i64* %8
%18 = add i64 %17, 1
%19 = call i8* @calloc(i64 %18, i32 1)
store i8* %19, i8** %10
%20 = load i8*, i8** %10
%21 = load i8*, i8** %4
%22 = load i64, i64* %8
%23 = call i32 @strncpy(i8* %20, i8* %21, i64 %22)
store i64 0, i64* %11
br label %24
24: ; preds = %36, %3
%25 = load i64, i64* %11
%26 = load i64, i64* %7
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %39
28: ; preds = %24
%29 = load i8, i8* %6
%30 = load i8*, i8** %10
%31 = load i64*, i64** %9
%32 = load i64, i64* %11
%33 = getelementptr inbounds i64, i64* %31, i64 %32
%34 = load i64, i64* %33
%35 = getelementptr inbounds i8, i8* %30, i64 %34
store i8 %29, i8* %35
br label %36
36: ; preds = %28
%37 = load i64, i64* %11
%38 = add i64 %37, 1
store i64 %38, i64* %11
br label %24
39: ; preds = %24
%40 = load i64*, i64** %9
%41 = call i32 @free(i64* %40)
%42 = load i8*, i8** %10
ret i8* %42
}

LLVM IR (-O2):
define i8* @strutil_replace_all(i8* %0, i8 signext %1, i8 signext %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = call i64* @strutil_indices_of(i8* %0, i8 signext %1, i64* %4)
%8 = add i64 %6, 1
%9 = call i8* @calloc(i64 %8, i32 1)
%10 = call i32 @strncpy(i8* %9, i8* %0, i64 %6)
%11 = load i64, i64* %4
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %15, %3
%14 = call i32 @free(i64* %7)
ret i8* %9
15: ; preds = %3, %15
%16 = phi i64 [ %20, %15 ], [ 0, %3 ]
%17 = getelementptr inbounds i64, i64* %7, i64 %16
%18 = load i64, i64* %17
%19 = getelementptr inbounds i8, i8* %9, i64 %18
store i8 %2, i8* %19
%20 = add nuw i64 %16, 1
%21 = load i64, i64* %4
%22 = icmp ult i64 %20, %21
br i1 %22, label %15, label %13
}