#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*/ memmove(char *, char *, size_t);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *strstr(char *, char *);
char *str_replace(char *src, size_t buf_size, char *replace, char *subject) {
  char *p = NULL;
  char *pcp = NULL;
  size_t s_len = 0;
  char t_buf[2 * 1024] = {0};
  if (NULL == src || buf_size <= 0 || NULL == replace || NULL == subject) {
    goto FINISH;
  }
  s_len = strlen(subject);
  pcp = src;
  p = strstr(src, subject);
  if (NULL == p) {
    goto FINISH;
  }
  while (NULL != p) {
    *p = '\0';
    strncat(t_buf, pcp, 2 * 1024 - strlen(t_buf) - 1);
    strncat(t_buf, replace, 2 * 1024 - strlen(t_buf) - 1);
    p += s_len;
    pcp = p;
    p = strstr(pcp, subject);
  }
  if (pcp) {
    strncat(t_buf, pcp, 2 * 1024 - strlen(t_buf) - 1);
  }
  if (strlen(t_buf)) {
    memmove(src, t_buf, buf_size - 1);
    src[buf_size - 1] = '\0';
  }
FINISH:
  return src;
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x870,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x840(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1040 <memset@plt>
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     11c5 <str_replace+0x75>
cmpq   $0x0,-0x10(%rbp)
jbe    11c5 <str_replace+0x75>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     11c5 <str_replace+0x75>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    11ca <str_replace+0x7a>
jmp    1358 <str_replace+0x208>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
cltq
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1203 <str_replace+0xb3>
jmp    1358 <str_replace+0x208>
jmp    1208 <str_replace+0xb8>
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
je     12c9 <str_replace+0x179>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x840(%rbp),%rax
mov    %rax,-0x860(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x858(%rbp)
lea    -0x840(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x860(%rbp),%rdi
mov    -0x858(%rbp),%rsi
mov    $0x800,%edx
sub    %eax,%edx
sub    $0x1,%edx
call   1050 <strncat@plt>
lea    -0x840(%rbp),%rax
mov    %rax,-0x850(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x848(%rbp)
lea    -0x840(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x850(%rbp),%rdi
mov    -0x848(%rbp),%rsi
mov    $0x800,%edx
sub    %eax,%edx
sub    $0x1,%edx
call   1050 <strncat@plt>
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
jmp    1208 <str_replace+0xb8>
cmpq   $0x0,-0x30(%rbp)
je     1316 <str_replace+0x1c6>
lea    -0x840(%rbp),%rax
mov    %rax,-0x870(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x868(%rbp)
lea    -0x840(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x870(%rbp),%rdi
mov    -0x868(%rbp),%rsi
mov    $0x800,%edx
sub    %eax,%edx
sub    $0x1,%edx
call   1050 <strncat@plt>
lea    -0x840(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
je     1353 <str_replace+0x203>
mov    -0x8(%rbp),%rdi
lea    -0x840(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
call   1060 <memmove@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1358 <str_replace+0x208>
mov    -0x8(%rbp),%rax
add    $0x870,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%rdi
mov    $0x800,%edx
xor    %esi,%esi
call   1040 <memset@plt>
test   %rbp,%rbp
je     127b <str_replace+0x12b>
test   %rbx,%rbx
je     127b <str_replace+0x12b>
test   %r12,%r12
je     127b <str_replace+0x12b>
test   %r13,%r13
je     127b <str_replace+0x12b>
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
mov    %r13,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     127b <str_replace+0x12b>
mov    %rax,%r14
mov    %rbx,(%rsp)
movslq %r15d,%r15
lea    0x10(%rsp),%rbx
mov    %rbp,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
movb   $0x0,(%r14)
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    $0x7ff,%edx
sub    %eax,%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <strncat@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    $0x7ff,%edx
sub    %eax,%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <strncat@plt>
mov    %r14,%rbp
add    %r15,%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
call   1070 <strstr@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11e0 <str_replace+0x90>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    $0x7ff,%edx
sub    %eax,%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <strncat@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
mov    0x8(%rsp),%rbp
mov    (%rsp),%rbx
je     127b <str_replace+0x12b>
lea    -0x1(%rbx),%rdx
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
call   1060 <memmove@plt>
movb   $0x0,-0x1(%rbp,%rbx,1)
mov    %rbp,%rax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @str_replace(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca [2048 x i8]
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
store i8* null, i8** %9
store i8* null, i8** %10
store i64 0, i64* %11
%13 = bitcast [2048 x i8]* %12 to i8*
%14 = load i8*, i8** %5
%15 = icmp eq i8* null, %14
br i1 %15, label %25, label %16
16: ; preds = %4
%17 = load i64, i64* %6
%18 = icmp ule i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = load i8*, i8** %7
%21 = icmp eq i8* null, %20
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i8*, i8** %8
%24 = icmp eq i8* null, %23
br i1 %24, label %25, label %26
25: ; preds = %22, %19, %16, %4
br label %90
26: ; preds = %22
%27 = load i8*, i8** %8
%28 = call i32 @strlen(i8* %27)
%29 = sext i32 %28 to i64
store i64 %29, i64* %11
%30 = load i8*, i8** %5
store i8* %30, i8** %10
%31 = load i8*, i8** %5
%32 = load i8*, i8** %8
%33 = call i8* @strstr(i8* %31, i8* %32)
store i8* %33, i8** %9
%34 = load i8*, i8** %9
%35 = icmp eq i8* null, %34
br i1 %35, label %36, label %37
36: ; preds = %26
br label %90
37: ; preds = %26
br label %38
38: ; preds = %41, %37
%39 = load i8*, i8** %9
%40 = icmp ne i8* null, %39
br i1 %40, label %41, label %64
41: ; preds = %38
%42 = load i8*, i8** %9
store i8 0, i8* %42
%43 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%44 = load i8*, i8** %10
%45 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = sub nsw i32 2048, %46
%48 = sub nsw i32 %47, 1
%49 = call i32 @strncat(i8* %43, i8* %44, i32 %48)
%50 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%51 = load i8*, i8** %7
%52 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%53 = call i32 @strlen(i8* %52)
%54 = sub nsw i32 2048, %53
%55 = sub nsw i32 %54, 1
%56 = call i32 @strncat(i8* %50, i8* %51, i32 %55)
%57 = load i64, i64* %11
%58 = load i8*, i8** %9
%59 = getelementptr inbounds i8, i8* %58, i64 %57
store i8* %59, i8** %9
%60 = load i8*, i8** %9
store i8* %60, i8** %10
%61 = load i8*, i8** %10
%62 = load i8*, i8** %8
%63 = call i8* @strstr(i8* %61, i8* %62)
store i8* %63, i8** %9
br label %38
64: ; preds = %38
%65 = load i8*, i8** %10
%66 = icmp ne i8* %65, null
br i1 %66, label %67, label %75
67: ; preds = %64
%68 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%69 = load i8*, i8** %10
%70 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%71 = call i32 @strlen(i8* %70)
%72 = sub nsw i32 2048, %71
%73 = sub nsw i32 %72, 1
%74 = call i32 @strncat(i8* %68, i8* %69, i32 %73)
br label %75
75: ; preds = %67, %64
%76 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%77 = call i32 @strlen(i8* %76)
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %89
79: ; preds = %75
%80 = load i8*, i8** %5
%81 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%82 = load i64, i64* %6
%83 = sub i64 %82, 1
%84 = call i32 @memmove(i8* %80, i8* %81, i64 %83)
%85 = load i8*, i8** %5
%86 = load i64, i64* %6
%87 = sub i64 %86, 1
%88 = getelementptr inbounds i8, i8* %85, i64 %87
store i8 0, i8* %88
br label %89
89: ; preds = %79, %75
br label %90
90: ; preds = %89, %36, %25
%91 = load i8*, i8** %5
ret i8* %91
}

LLVM IR (-O2):
define i8* @str_replace(i8* returned %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca [2048 x i8]
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%7 = icmp eq i8* %0, null
%8 = icmp eq i64 %1, 0
%9 = or i1 %7, %8
%10 = icmp eq i8* %2, null
%11 = or i1 %9, %10
%12 = icmp eq i8* %3, null
%13 = or i1 %11, %12
br i1 %13, label %41, label %14
14: ; preds = %4
%15 = tail call i32 @strlen(i8* %3)
%16 = sext i32 %15 to i64
%17 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %3)
%18 = icmp eq i8* %17, null
br i1 %18, label %41, label %19
19: ; preds = %14, %19
%20 = phi i8* [ %28, %19 ], [ %0, %14 ]
%21 = phi i8* [ %29, %19 ], [ %17, %14 ]
store i8 0, i8* %21
%22 = call i32 @strlen(i8* %6)
%23 = sub i32 2047, %22
%24 = call i32 @strncat(i8* %6, i8* %20, i32 %23)
%25 = call i32 @strlen(i8* %6)
%26 = sub i32 2047, %25
%27 = call i32 @strncat(i8* %6, i8* %2, i32 %26)
%28 = getelementptr inbounds i8, i8* %21, i64 %16
%29 = call i8* @strstr(i8* %28, i8* dereferenceable(1) %3)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %19
31: ; preds = %19
%32 = call i32 @strlen(i8* %6)
%33 = sub i32 2047, %32
%34 = call i32 @strncat(i8* %6, i8* %28, i32 %33)
%35 = call i32 @strlen(i8* %6)
%36 = icmp eq i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %31
%38 = add i64 %1, -1
%39 = call i32 @memmove(i8* %0, i8* %6, i64 %38)
%40 = getelementptr inbounds i8, i8* %0, i64 %38
store i8 0, i8* %40
br label %41
41: ; preds = %31, %37, %14, %4
ret i8* %0
}