#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 *);
char *snprintf_wrap(char *, char *, char *, char *);
char *strdup(char const *);
int strlen(char *);
char *strstr(char *, char *);
char *change(const char *str, char *from, char *to) {
  char *copy = strdup(str);
  if (!copy)
    return NULL;
  char *pos = strstr(copy, from);
  char *tmp = NULL;
  while (pos) {
    *pos = '\0';
    tmp = snprintf_wrap("%s%s%s", copy, to, pos + strlen(from));
    if (tmp) {
      free(copy);
      copy = tmp;
    }
    pos = strstr(copy + strlen(to), from);
  }
  return copy;
}


x86 assembly (clang, -O0):
<change>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1189 <change+0x39>
movq   $0x0,-0x8(%rbp)
jmp    1246 <change+0xf6>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     123e <change+0xee>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rcx
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xe13(%rip),%rdi
call   1050 <snprintf_wrap@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1212 <change+0xc2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x30(%rbp)
jmp    11a2 <change+0x52>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
call   1060 <strdup@plt>
test   %rax,%rax
je     11e2 <change+0x92>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11e5 <change+0x95>
mov    %rax,%rbx
lea    0xe7a(%rip),%r12
jmp    11ae <change+0x5e>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %r13,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e5 <change+0x95>
movb   $0x0,(%rbx)
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
add    %rbx,%rcx
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1050 <snprintf_wrap@plt>
test   %rax,%rax
je     1190 <change+0x40>
mov    %rax,%rbx
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%r13
jmp    1190 <change+0x40>
xor    %r13d,%r13d
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s%s%s\00"
define i8* @change(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = call i8* @strdup(i8* %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %3
store i8* null, i8** %4
br label %49
16: ; preds = %3
%17 = load i8*, i8** %8
%18 = load i8*, i8** %6
%19 = call i8* @strstr(i8* %17, i8* %18)
store i8* %19, i8** %9
store i8* null, i8** %10
br label %20
20: ; preds = %39, %16
%21 = load i8*, i8** %9
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %47
23: ; preds = %20
%24 = load i8*, i8** %9
store i8 0, i8* %24
%25 = load i8*, i8** %8
%26 = load i8*, i8** %7
%27 = load i8*, i8** %9
%28 = load i8*, i8** %6
%29 = call i32 @strlen(i8* %28)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = call i8* @snprintf_wrap(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %26, i8* %31)
store i8* %32, i8** %10
%33 = load i8*, i8** %10
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %39
35: ; preds = %23
%36 = load i8*, i8** %8
%37 = call i32 @free(i8* %36)
%38 = load i8*, i8** %10
store i8* %38, i8** %8
br label %39
39: ; preds = %35, %23
%40 = load i8*, i8** %8
%41 = load i8*, i8** %7
%42 = call i32 @strlen(i8* %41)
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
%45 = load i8*, i8** %6
%46 = call i8* @strstr(i8* %44, i8* %45)
store i8* %46, i8** %9
br label %20
47: ; preds = %20
%48 = load i8*, i8** %8
store i8* %48, i8** %4
br label %49
49: ; preds = %47, %15
%50 = load i8*, i8** %4
ret i8* %50
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s%s%s\00"
define i8* @change(i8* nocapture %0, i8* %1, i8* %2) {
%4 = tail call i8* @strdup(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %26, label %6
6: ; preds = %3
%7 = tail call i8* @strstr(i8* %4, i8* dereferenceable(1) %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %26, label %9
9: ; preds = %6, %19
%10 = phi i8* [ %24, %19 ], [ %7, %6 ]
%11 = phi i8* [ %20, %19 ], [ %4, %6 ]
store i8 0, i8* %10
%12 = tail call i32 @strlen(i8* %1)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
%15 = tail call i8* @snprintf_wrap(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %2, i8* %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %9
%18 = tail call i32 @free(i8* %11)
br label %19
19: ; preds = %17, %9
%20 = phi i8* [ %15, %17 ], [ %11, %9 ]
%21 = tail call i32 @strlen(i8* %2)
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = tail call i8* @strstr(i8* dereferenceable(1) %23, i8* dereferenceable(1) %1)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %9
26: ; preds = %19, %6, %3
%27 = phi i8* [ null, %3 ], [ %4, %6 ], [ %20, %19 ]
ret i8* %27
}