#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*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
char *strstr(char *, char *);
void replace_all(char *s, char *from, char *to) {
  char *save, *p;
  if (NULL != (s = strstr(s, from))) {
    save = strdup(s + strlen(from));
    p = to;
    while ('\0' != *p)
      *s++ = *p++;
    strcpy(s, save);
    replace_all(s, from, to);
  }
}


x86 assembly (clang, -O0):
<replace_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,%rcx
mov    %rcx,-0x8(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     1204 <replace_all+0xb4>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
call   1060 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
je     11e6 <replace_all+0x96>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    11ae <replace_all+0x5e>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <replace_all@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_all>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
call   1060 <strstr@plt>
test   %rax,%rax
je     11c3 <replace_all+0x83>
mov    %rax,%rbx
lea    0x1(%r14),%r12
jmp    118e <replace_all+0x4e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c3 <replace_all+0x83>
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
call   1050 <strdup@plt>
mov    (%r14),%cl
test   %cl,%cl
je     1170 <replace_all+0x30>
mov    %r12,%rdx
nopl   0x0(%rax,%rax,1)
mov    %cl,(%rbx)
add    $0x1,%rbx
movzbl (%rdx),%ecx
add    $0x1,%rdx
test   %cl,%cl
jne    11b0 <replace_all+0x70>
jmp    1170 <replace_all+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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