#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 */
scalar_t__ isalnum(char);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char const *);
char *strstr(char *, char const *);
void subst(char *buf, const char *from, const char *to) {
  char *p = buf;
  int fromlen = strlen(from);
  int tolen = strlen(to);
  while (p = strstr(p, from)) {
    if (isalnum(p[fromlen])) {
      p++;
      continue;
    }
    memmove(p + tolen, p + fromlen, strlen(p + fromlen) + 1);
    memcpy(p, to, tolen);
  }
}


x86 assembly (clang, -O0):
<subst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1220 <subst+0xd0>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%rax
je     11cb <subst+0x7b>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1184 <subst+0x34>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1060 <memmove@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1050 <memcpy@plt>
jmp    1184 <subst+0x34>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<subst>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,0xc(%rsp)
mov    %rbp,%rdi
mov    %r13,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11f8 <subst+0xa8>
mov    %rax,%rbp
movslq %r12d,%r14
movslq 0xc(%rsp),%r15
jmp    11b7 <subst+0x67>
add    $0x1,%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11f8 <subst+0xa8>
movsbl 0x0(%rbp,%r14,1),%edi
call   1040 <isalnum@plt>
test   %rax,%rax
jne    11a0 <subst+0x50>
lea    (%r14,%rbp,1),%rbx
lea    (%r15,%rbp,1),%r12
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <memmove@plt>
mov    %rbp,%rdi
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%edx
call   1050 <memcpy@plt>
jmp    11a4 <subst+0x54>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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