#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ stderr;
int strlen(char *);
char *strstr(char *, char *);
char *replace(char *pattern, char *replacement, char *original) {
  char *p_temp = original;
  if (strlen(replacement) > strlen(pattern)) {
    fprintf(stderr, "Failed, strings are not equals\n");
    exit(EXIT_FAILURE);
  }
  while ((p_temp = strstr(p_temp, pattern))) {
    memcpy(p_temp, replacement, strlen(replacement));
    memmove(p_temp + strlen(replacement), p_temp + strlen(pattern),
            strlen(p_temp + strlen(pattern)) + 1);
  }
  return original;
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jle    11c1 <replace+0x61>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11c6 <replace+0x66>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1281 <replace+0x121>
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
cltq
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>
jmp    11c6 <replace+0x66>
mov    -0x18(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1030 <strlen@plt>
cmp    %eax,%ebx
jg     1227 <replace+0xc7>
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1215 <replace+0xb5>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
movslq %eax,%r13
add    %rbx,%r13
mov    %r12,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbp
add    %rbx,%rbp
mov    %r12,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r13,%rdi
mov    %rbp,%rsi
call   1060 <memmove@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b0 <replace+0x50>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Failed, strings are not equals\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @replace(i8* %0, i8* %1, i8* returned %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = tail call i32 @strlen(i8* %0)
%6 = icmp sgt i32 %4, %5
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = tail call i8* @strstr(i8* dereferenceable(1) %2, i8* dereferenceable(1) %0)
%9 = icmp eq i8* %8, null
br i1 %9, label %33, label %15
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %7, %15
%16 = phi i8* [ %31, %15 ], [ %8, %7 ]
%17 = tail call i32 @strlen(i8* %1)
%18 = tail call i32 @memcpy(i8* %16, i8* %1, i32 %17)
%19 = tail call i32 @strlen(i8* %1)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %16, i64 %20
%22 = tail call i32 @strlen(i8* %0)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %16, i64 %23
%25 = tail call i32 @strlen(i8* %0)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %16, i64 %26
%28 = tail call i32 @strlen(i8* %27)
%29 = add nsw i32 %28, 1
%30 = tail call i32 @memmove(i8* %21, i8* %24, i32 %29)
%31 = tail call i8* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) %0)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %15
33: ; preds = %15, %7
ret i8* %2
}