#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*/ delete_str(char *, int, int);
int /*<<< orphan*/ insert_str(char *, char *, int);
int instr(char *, char *, int);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
char *search_replace(char *search_str, char *replace_str,
                     char *source_destination) {
  int here, len_search = strlen(search_str);
  char *return_pointer;
  int str_len = strlen(replace_str);
  return_pointer = source_destination;
  if (strcmp(search_str, replace_str) == 0)
    return return_pointer;
  while (1) {
    if ((here = instr(search_str, source_destination, 1)) == -1) {
      return return_pointer;
    }
    delete_str(source_destination, here, len_search);
    insert_str(replace_str, source_destination, here);
    source_destination += (here + str_len);
  }
}


x86 assembly (clang, -O0):
<search_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11a8 <search_replace+0x58>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1210 <search_replace+0xc0>
jmp    11ad <search_replace+0x5d>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <instr@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11d8 <search_replace+0x88>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1210 <search_replace+0xc0>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1070 <delete_str@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <insert_str@plt>
mov    -0x24(%rbp),%ecx
add    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    11ad <search_replace+0x5d>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%r12d
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11ea <search_replace+0x9a>
mov    %rbx,%rdi
mov    (%rsp),%rsi
mov    $0x1,%edx
call   1040 <instr@plt>
cmp    $0xffffffff,%eax
je     11ea <search_replace+0x9a>
mov    %eax,%ebp
mov    (%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r12d,%edx
call   1070 <delete_str@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1060 <insert_str@plt>
add    %r13d,%ebp
movslq %ebp,%rax
add    %rax,%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <instr@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11b0 <search_replace+0x60>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @search_replace(i8* %0, i8* %1, i8* returned %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i64 @strcmp(i8* %0, i8* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %21, label %8
8: ; preds = %3
%9 = tail call i32 @instr(i8* %0, i8* %2, i32 1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %21, label %11
11: ; preds = %8, %11
%12 = phi i32 [ %19, %11 ], [ %9, %8 ]
%13 = phi i8* [ %18, %11 ], [ %2, %8 ]
%14 = tail call i32 @delete_str(i8* %13, i32 %12, i32 %4)
%15 = tail call i32 @insert_str(i8* %1, i8* %13, i32 %12)
%16 = add nsw i32 %12, %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %13, i64 %17
%19 = tail call i32 @instr(i8* %0, i8* %18, i32 1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %11
21: ; preds = %11, %8, %3
ret i8* %2
}