#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*/ free(char *);
int /*<<< orphan*/ printf(char *, char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
char *strstr(char *, char *);
void sub(char *buf, char *from, char *to) {
  char *p, *posfix;
  int fs;
  fs = strlen(from);
  printf("sub [%s] for [%s], fs=%d\n", from, to, fs);
  for (;;) {
    p = strstr(buf, from);
    if (p == NULL)
      return;
    *p = 'X';
    posfix = strdup(p + fs);
    strcpy(p, to);
    strcat(p, posfix);
    free(posfix);
  }
}


x86 assembly (clang, -O0):
<sub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
lea    0xe5e(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c9 <sub+0x59>
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x20(%rbp),%rax
movb   $0x58,(%rax)
mov    -0x20(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
call   1080 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    11a7 <sub+0x37>

x86 assembly (clang, -O2):
<sub>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
lea    0xe6b(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %eax,%ecx
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     1200 <sub+0x90>
mov    %rax,%rbx
movslq %ebp,%r13
nopl   0x0(%rax,%rax,1)
movb   $0x58,(%rbx)
lea    (%rbx,%r13,1),%rdi
call   1080 <strdup@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <strcat@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c0 <sub+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"sub [%s] for [%s], fs=%d\0A\00"
define void @sub(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %9
%12 = load i8*, i8** %5
%13 = load i8*, i8** %6
%14 = load i32, i32* %9
%15 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %13, i32 %14)
br label %16
16: ; preds = %23, %3
%17 = load i8*, i8** %4
%18 = load i8*, i8** %5
%19 = call i8* @strstr(i8* %17, i8* %18)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %16
ret void
23: ; preds = %16
%24 = load i8*, i8** %7
store i8 88, i8* %24
%25 = load i8*, i8** %7
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = call i8* @strdup(i8* %28)
store i8* %29, i8** %8
%30 = load i8*, i8** %7
%31 = load i8*, i8** %6
%32 = call i32 @strcpy(i8* %30, i8* %31)
%33 = load i8*, i8** %7
%34 = load i8*, i8** %8
%35 = call i32 @strcat(i8* %33, i8* %34)
%36 = load i8*, i8** %8
%37 = call i32 @free(i8* %36)
br label %16
}

LLVM IR (-O2):
@.str = [26 x i8] c"sub [%s] for [%s], fs=%d\0A\00"
define void @sub(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2, i32 %4)
%6 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = sext i32 %4 to i64
br label %11
10: ; preds = %11, %3
ret void
11: ; preds = %8, %11
%12 = phi i8* [ %6, %8 ], [ %18, %11 ]
store i8 88, i8* %12
%13 = getelementptr inbounds i8, i8* %12, i64 %9
%14 = tail call i8* @strdup(i8* %13)
%15 = tail call i32 @strcpy(i8* %12, i8* %2)
%16 = tail call i32 @strcat(i8* %12, i8* %14)
%17 = tail call i32 @free(i8* %14)
%18 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%19 = icmp eq i8* %18, null
br i1 %19, label %10, label %11
}