#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 */
char *calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ sprintf(char *, char *);
char *strdup(char *);
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char const *);
void replaceSubString(char **input, const char *oldval, const char *newval) {
  char *result = calloc(1, sizeof(char) * 100 * 2);
  strncpy(result, *input, 100);
  char *needle = strstr(result, oldval);
  int oldlen = strlen(oldval);
  if (needle == NULL) {
    free(result);
    return;
  } else {
    while (needle != NULL) {
      *needle = '\0';
      *(needle + oldlen - 1) = '\0';
      needle = needle + oldlen;
      char *temp = strdup(needle);
      strncat(result, newval, 100);
      strncat(result, temp, 100);
      free(temp);
      needle = strstr(result, oldval);
    }
  }
  sprintf(*input, result);
  free(result);
}


x86 assembly (clang, -O0):
<replaceSubString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x1,%edi
mov    $0xc8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    $0x64,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f2 <replaceSubString+0x72>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1293 <replaceSubString+0x113>
jmp    11f7 <replaceSubString+0x77>
cmpq   $0x0,-0x28(%rbp)
je     1275 <replaceSubString+0xf5>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x64,%edx
call   1060 <strncat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0x64,%edx
call   1060 <strncat@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x28(%rbp)
jmp    11f7 <replaceSubString+0x77>
jmp    127a <replaceSubString+0xfa>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <sprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replaceSubString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    $0x1,%edi
mov    $0xc8,%esi
call   1070 <calloc@plt>
mov    %rax,%r13
mov    (%r14),%rsi
mov    %rax,%rdi
mov    $0x64,%edx
call   1040 <strncpy@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   10a0 <strstr@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1050 <strlen@plt>
test   %rbx,%rbx
je     123c <replaceSubString+0xbc>
movslq %eax,%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movb   $0x0,(%rbx)
movb   $0x0,-0x1(%rbx,%rbp,1)
add    %rbp,%rbx
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %r15,%rsi
mov    $0x64,%edx
call   1060 <strncat@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1060 <strncat@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   10a0 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e0 <replaceSubString+0x60>
mov    (%r14),%rdi
mov    %r13,%rsi
call   1080 <sprintf@plt>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @replaceSubString(i8** %0, i8* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%11 = call i8* @calloc(i32 1, i32 200)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = load i8**, i8*** %4
%14 = load i8*, i8** %13
%15 = call i32 @strncpy(i8* %12, i8* %14, i32 100)
%16 = load i8*, i8** %7
%17 = load i8*, i8** %5
%18 = call i8* @strstr(i8* %16, i8* %17)
store i8* %18, i8** %8
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %9
%21 = load i8*, i8** %8
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = load i8*, i8** %7
%25 = call i32 @free(i8* %24)
br label %62
26: ; preds = %3
br label %27
27: ; preds = %30, %26
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %54
30: ; preds = %27
%31 = load i8*, i8** %8
store i8 0, i8* %31
%32 = load i8*, i8** %8
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = getelementptr inbounds i8, i8* %35, i64 -1
store i8 0, i8* %36
%37 = load i8*, i8** %8
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8* %40, i8** %8
%41 = load i8*, i8** %8
%42 = call i8* @strdup(i8* %41)
store i8* %42, i8** %10
%43 = load i8*, i8** %7
%44 = load i8*, i8** %6
%45 = call i32 @strncat(i8* %43, i8* %44, i32 100)
%46 = load i8*, i8** %7
%47 = load i8*, i8** %10
%48 = call i32 @strncat(i8* %46, i8* %47, i32 100)
%49 = load i8*, i8** %10
%50 = call i32 @free(i8* %49)
%51 = load i8*, i8** %7
%52 = load i8*, i8** %5
%53 = call i8* @strstr(i8* %51, i8* %52)
store i8* %53, i8** %8
br label %27
54: ; preds = %27
br label %55
55: ; preds = %54
%56 = load i8**, i8*** %4
%57 = load i8*, i8** %56
%58 = load i8*, i8** %7
%59 = call i32 @sprintf(i8* %57, i8* %58)
%60 = load i8*, i8** %7
%61 = call i32 @free(i8* %60)
br label %62
62: ; preds = %55, %23
ret void
}

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