#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*/ assert(char *);
char *strings_alloc(unsigned int);
unsigned int strings_length(char *);
char *strings_replace_imp(char *string, char old_char, char new_char,
                          bool (*equals_function)(char, char)) {
  assert(string);
  unsigned int string_length = strings_length(string);
  char *ret = strings_alloc(string_length + 1);
  for (unsigned int k = 0; k < string_length; k++) {
    if (equals_function(string[k], old_char))
      ret[k] = new_char;
    else
      ret[k] = string[k];
  }
  ret[string_length] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<strings_replace_imp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %sil,%dl
mov    %rdi,-0x8(%rbp)
mov    %dl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strings_length@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1040 <strings_alloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    11db <strings_replace_imp+0xab>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    -0x2c(%rbp),%edx
mov    (%rcx,%rdx,1),%cl
movsbl %cl,%edi
movsbl -0x9(%rbp),%esi
call   *%rax
cmp    $0x0,%eax
je     11b4 <strings_replace_imp+0x84>
mov    -0xa(%rbp),%dl
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
jmp    11c8 <strings_replace_imp+0x98>
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    (%rax,%rcx,1),%dl
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
jmp    11cd <strings_replace_imp+0x9d>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1176 <strings_replace_imp+0x46>
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strings_replace_imp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,(%rsp)
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%rbx
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1030 <strings_length@plt>
mov    %eax,%ebp
lea    0x1(%rbp),%edi
call   1040 <strings_alloc@plt>
mov    %rax,%r13
test   %ebp,%ebp
je     11a6 <strings_replace_imp+0x76>
mov    %ebp,%r12d
mov    %r14d,%eax
xor    %r14d,%r14d
movsbl %al,%ebp
jmp    118e <strings_replace_imp+0x5e>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %cl,0x0(%r13,%r14,1)
add    $0x1,%r14
cmp    %r14,%r12
je     11a9 <strings_replace_imp+0x79>
movsbl (%rbx,%r14,1),%edi
mov    %ebp,%esi
call   *(%rsp)
mov    %r15d,%ecx
test   %eax,%eax
jne    1180 <strings_replace_imp+0x50>
movzbl (%rbx,%r14,1),%ecx
jmp    1180 <strings_replace_imp+0x50>
xor    %r12d,%r12d
movb   $0x0,0x0(%r13,%r12,1)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @strings_replace_imp(i8* %0, i8 signext %1, i8 signext %2, i32 (i8, i8)* nocapture %3) {
%5 = tail call i32 @assert(i8* %0)
%6 = tail call i32 @strings_length(i8* %0)
%7 = add i32 %6, 1
%8 = tail call i8* @strings_alloc(i32 %7)
%9 = icmp eq i32 %6, 0
br i1 %9, label %12, label %10
10: ; preds = %4
%11 = zext i32 %6 to i64
br label %15
12: ; preds = %23, %4
%13 = phi i64 [ 0, %4 ], [ %11, %23 ]
%14 = getelementptr inbounds i8, i8* %8, i64 %13
store i8 0, i8* %14
ret i8* %8
15: ; preds = %10, %23
%16 = phi i64 [ 0, %10 ], [ %26, %23 ]
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = tail call i32 %3(i8 signext %18, i8 signext %1)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = load i8, i8* %17
br label %23
23: ; preds = %15, %21
%24 = phi i8 [ %22, %21 ], [ %2, %15 ]
%25 = getelementptr inbounds i8, i8* %8, i64 %16
store i8 %24, i8* %25
%26 = add nuw nsw i64 %16, 1
%27 = icmp eq i64 %26, %11
br i1 %27, label %12, label %15
}