#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 *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strstr(char *, char *);
void str_replace_first(char *buffer, char *s, char *by) {
  char *p = strstr(buffer, s);
  if (p != NULL) {
    size_t len_p = strlen(p), len_s = strlen(s), len_by = strlen(by);
    if (len_s != len_by) {
      memmove(p + len_by, p + len_s, len_p);
    }
    strncpy(p, by, len_by);
  }
}


x86 assembly (clang, -O0):
<str_replace_first>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11cf <str_replace_first+0x8f>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     11be <str_replace_first+0x7e>
mov    -0x20(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memmove@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <strncpy@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace_first>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
call   1060 <strstr@plt>
test   %rax,%rax
je     11aa <str_replace_first+0x6a>
mov    %rax,%r13
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
cmp    %rax,%r12
je     1193 <str_replace_first+0x53>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
add    %r13,%rdi
add    %r13,%r12
mov    %r12,%rsi
mov    %rax,%rdx
call   1050 <memmove@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <strncpy@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @str_replace_first(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
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%11 = load i8*, i8** %4
%12 = load i8*, i8** %5
%13 = call i8* @strstr(i8* %11, i8* %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %40
16: ; preds = %3
%17 = load i8*, i8** %7
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %8
%19 = load i8*, i8** %5
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %9
%21 = load i8*, i8** %6
%22 = call i64 @strlen(i8* %21)
store i64 %22, i64* %10
%23 = load i64, i64* %9
%24 = load i64, i64* %10
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %16
%27 = load i8*, i8** %7
%28 = load i64, i64* %10
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = load i8*, i8** %7
%31 = load i64, i64* %9
%32 = getelementptr inbounds i8, i8* %30, i64 %31
%33 = load i64, i64* %8
%34 = call i32 @memmove(i8* %29, i8* %32, i64 %33)
br label %35
35: ; preds = %26, %16
%36 = load i8*, i8** %7
%37 = load i8*, i8** %6
%38 = load i64, i64* %10
%39 = call i32 @strncpy(i8* %36, i8* %37, i64 %38)
br label %40
40: ; preds = %35, %3
ret void
}

LLVM IR (-O2):
define void @str_replace_first(i8* %0, i8* nocapture %1, i8* %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%9 = icmp eq i64 %7, %8
br i1 %9, label %15, label %10
10: ; preds = %6
%11 = tail call i64 @strlen(i8* %4)
%12 = getelementptr inbounds i8, i8* %4, i64 %8
%13 = getelementptr inbounds i8, i8* %4, i64 %7
%14 = tail call i32 @memmove(i8* %12, i8* %13, i64 %11)
br label %15
15: ; preds = %10, %6
%16 = tail call i32 @strncpy(i8* %4, i8* %2, i64 %8)
br label %17
17: ; preds = %15, %3
ret void
}