#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*/ memmove(char *, char *, size_t);
size_t strlen(char *);
char *strstr(char *, char *);
int replace_string_by_char(char *big, char *pattern, char repl) {
  char *c, *r;
  int count = 0;
  size_t patlen;
  patlen = strlen(pattern);
  c = big;
  for (;;) {
    c = strstr(c, pattern);
    if (c == NULL) {
      break;
    }
    *(c++) = repl;
    r = c + patlen - 1;
    memmove(c, r, strlen(r) + 1);
    count++;
  }
  return count;
}


x86 assembly (clang, -O0):
<replace_string_by_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1182 <replace_string_by_char+0x52>
jmp    11e1 <replace_string_by_char+0xb1>
mov    -0x11(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1040 <memmove@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1161 <replace_string_by_char+0x31>
mov    -0x2c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_string_by_char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %rbp,%rdi
mov    %r12,%rsi
call   1050 <strstr@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11ad <replace_string_by_char+0x7d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x1(%rax),%r13
mov    %r14b,(%rax)
lea    (%rax,%r15,1),%rbx
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <memmove@plt>
add    $0x1,%ebp
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
jne    1170 <replace_string_by_char+0x40>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @replace_string_by_char(i8* %0, i8* nocapture %1, i8 signext %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %19, label %7
7: ; preds = %3, %7
%8 = phi i8* [ %17, %7 ], [ %5, %3 ]
%9 = phi i32 [ %16, %7 ], [ 0, %3 ]
%10 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %2, i8* %8
%11 = getelementptr inbounds i8, i8* %10, i64 %4
%12 = getelementptr inbounds i8, i8* %11, i64 -1
%13 = tail call i64 @strlen(i8* %12)
%14 = add i64 %13, 1
%15 = tail call i32 @memmove(i8* %10, i8* %12, i64 %14)
%16 = add nuw nsw i32 %9, 1
%17 = tail call i8* @strstr(i8* dereferenceable(1) %10, i8* dereferenceable(1) %1)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %7
19: ; preds = %7, %3
%20 = phi i32 [ 0, %3 ], [ %16, %7 ]
ret i32 %20
}