#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(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char const *);
char *strstr(char *, char const *);
void _str_subst(char *s1, int len, const char *s2, const char *s3) {
  int s2len = strlen(s2);
  int s3len = strlen(s3);
  char *p;
  while ((p = strstr(s1, s2)) != NULL) {
    assert(strlen(s1) + (s3len - s2len) + 1 <= len);
    memmove(p + s3len, p + s2len, strlen(p + s2len) + 1);
    memcpy(p, s3, s3len);
  }
}


x86 assembly (clang, -O0):
<_str_subst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1213 <_str_subst+0xc3>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%ecx
sub    -0x24(%rbp),%ecx
add    %ecx,%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1060 <memmove@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1050 <memcpy@plt>
jmp    117f <_str_subst+0x2f>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_str_subst>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%r12
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %eax,0x8(%rsp)
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     1235 <_str_subst+0xe5>
mov    %rax,%rbx
mov    0x8(%rsp),%eax
movslq %eax,%rcx
mov    %rcx,0x18(%rsp)
sub    %r13d,%eax
mov    %eax,0xc(%rsp)
movslq %r13d,%rax
mov    %rax,0x10(%rsp)
mov    %r12,0x20(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <strlen@plt>
add    0xc(%rsp),%eax
xor    %edi,%edi
cmp    %r15d,%eax
setl   %dil
call   1040 <assert@plt>
mov    %r15d,%r12d
mov    0x18(%rsp),%rax
lea    (%rbx,%rax,1),%r15
mov    %r14,%r13
mov    0x10(%rsp),%rax
lea    (%rbx,%rax,1),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r15,%rdi
mov    %r12d,%r15d
mov    0x20(%rsp),%r12
mov    %r14,%rsi
mov    %r13,%r14
call   1060 <memmove@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    0x8(%rsp),%edx
call   1050 <memcpy@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c0 <_str_subst+0x70>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @_str_subst(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %2)
%6 = tail call i32 @strlen(i8* %3)
%7 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %2)
%8 = icmp eq i8* %7, null
br i1 %8, label %28, label %9
9: ; preds = %4
%10 = sub i32 %6, %5
%11 = sext i32 %6 to i64
%12 = sext i32 %5 to i64
br label %13
13: ; preds = %9, %13
%14 = phi i8* [ %7, %9 ], [ %26, %13 ]
%15 = tail call i32 @strlen(i8* %0)
%16 = add nsw i32 %10, %15
%17 = icmp slt i32 %16, %1
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = getelementptr inbounds i8, i8* %14, i64 %11
%21 = getelementptr inbounds i8, i8* %14, i64 %12
%22 = tail call i32 @strlen(i8* %21)
%23 = add nsw i32 %22, 1
%24 = tail call i32 @memmove(i8* %20, i8* %21, i32 %23)
%25 = tail call i32 @memcpy(i8* %14, i8* %3, i32 %6)
%26 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %2)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %13
28: ; preds = %13, %4
ret void
}