#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 */
scalar_t__ Calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int strnlen(char *, int);
char *strstr(char *, char *);
char *ReplaceSubStr(char *str, char *a, char *b) {
  char *buf = (char *)Calloc(2048, sizeof(char));
  char *p;
  if (strnlen(str, 2048) >= 2048) {
    fprintf(stderr, "[x] Error: string too long!\n");
    exit(1);
  }
  if (!(p = strstr(str, a)))
    return str;
  strncpy(buf, str, p - str);
  buf[p - str] = '\0';
  sprintf(buf + (p - str), "%s%s", b, p + strlen(a));
  return buf;
}


x86 assembly (clang, -O0):
<ReplaceSubStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x800,%edi
mov    $0x1,%esi
call   1090 <Calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x800,%esi
call   1050 <strnlen@plt>
cmp    $0x800,%eax
jl     11df <ReplaceSubStr+0x5f>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1207 <ReplaceSubStr+0x87>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1288 <ReplaceSubStr+0x108>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rcx
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xda2(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReplaceSubStr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    $0x800,%edi
mov    $0x1,%esi
call   1090 <Calloc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    $0x800,%esi
call   1050 <strnlen@plt>
cmp    $0x800,%eax
jge    1220 <ReplaceSubStr+0xa0>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     120e <ReplaceSubStr+0x8e>
mov    %rax,%r13
mov    %rax,%rbp
sub    %rbx,%rbp
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %r12,%rbx
add    %rbp,%rbx
movb   $0x0,(%r12,%rbp,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
add    %r13,%rcx
lea    0xe1d(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1070 <sprintf@plt>
mov    %r12,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"[x] Error: string too long!\0A\00"
@.str.1 = [5 x i8] c"%s%s\00"
define i8* @ReplaceSubStr(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = call i64 @Calloc(i32 2048, i32 1)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %8
%12 = load i8*, i8** %5
%13 = call i32 @strnlen(i8* %12, i32 2048)
%14 = icmp sge i32 %13, 2048
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = load i8*, i8** %6
%22 = call i8* @strstr(i8* %20, i8* %21)
store i8* %22, i8** %9
%23 = icmp ne i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = load i8*, i8** %5
store i8* %25, i8** %4
br label %58
26: ; preds = %19
%27 = load i8*, i8** %8
%28 = load i8*, i8** %5
%29 = load i8*, i8** %9
%30 = load i8*, i8** %5
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
%34 = trunc i64 %33 to i32
%35 = call i32 @strncpy(i8* %27, i8* %28, i32 %34)
%36 = load i8*, i8** %8
%37 = load i8*, i8** %9
%38 = load i8*, i8** %5
%39 = ptrtoint i8* %37 to i64
%40 = ptrtoint i8* %38 to i64
%41 = sub i64 %39, %40
%42 = getelementptr inbounds i8, i8* %36, i64 %41
store i8 0, i8* %42
%43 = load i8*, i8** %8
%44 = load i8*, i8** %9
%45 = load i8*, i8** %5
%46 = ptrtoint i8* %44 to i64
%47 = ptrtoint i8* %45 to i64
%48 = sub i64 %46, %47
%49 = getelementptr inbounds i8, i8* %43, i64 %48
%50 = load i8*, i8** %7
%51 = load i8*, i8** %9
%52 = load i8*, i8** %6
%53 = call i32 @strlen(i8* %52)
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
%56 = call i32 @sprintf(i8* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %50, i8* %55)
%57 = load i8*, i8** %8
store i8* %57, i8** %4
br label %58
58: ; preds = %26, %24
%59 = load i8*, i8** %4
ret i8* %59
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"[x] Error: string too long!\0A\00"
@.str.1 = [5 x i8] c"%s%s\00"
define i8* @ReplaceSubStr(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @Calloc(i32 2048, i32 1)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i32 @strnlen(i8* %0, i32 2048)
%7 = icmp sgt i32 %6, 2047
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %3
%13 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%14 = icmp eq i8* %13, null
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = ptrtoint i8* %13 to i64
%17 = ptrtoint i8* %0 to i64
%18 = sub i64 %16, %17
%19 = trunc i64 %18 to i32
%20 = tail call i32 @strncpy(i8* %5, i8* %0, i32 %19)
%21 = getelementptr inbounds i8, i8* %5, i64 %18
store i8 0, i8* %21
%22 = tail call i32 @strlen(i8* %1)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %13, i64 %23
%25 = tail call i32 @sprintf(i8* %21, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %24)
br label %26
26: ; preds = %12, %15
%27 = phi i8* [ %5, %15 ], [ %0, %12 ]
ret i8* %27
}