#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*/ freeChar(char **);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int strmlen(char *);
char *strmstr(char *, char *);
int strmreplace(char **string, char *replace_hint, char *insert_word) {
  int oldLen;
  int newLen;
  char *tmp[3];
  char *pointer;
  pointer = strmstr(*string, replace_hint);
  if (pointer == NULL)
    return -1;
  oldLen = strmlen(*string) + 1;
  newLen = oldLen - strmlen(replace_hint) + strmlen(insert_word) + 1;
  tmp[0] = malloc(oldLen);
  tmp[1] = malloc(oldLen);
  tmp[2] = malloc(oldLen + strmlen(insert_word));
  memcpy(tmp[0], *string, oldLen);
  *string = NULL;
  pointer = strmstr(tmp[0], replace_hint);
  pointer += strmlen(replace_hint);
  memcpy(tmp[2], pointer, strmlen(pointer) + 1);
  pointer = strmstr(tmp[0], replace_hint);
  *pointer = '\0';
  memcpy(tmp[1], tmp[0], strmlen(tmp[0]) + 1);
  *string = malloc(newLen + 1);
  pointer = *string;
  memcpy(pointer, tmp[1], strmlen(tmp[1]));
  pointer += strmlen(tmp[1]);
  memcpy(pointer, insert_word, strmlen(insert_word));
  pointer += strmlen(insert_word);
  memcpy(pointer, tmp[2], strmlen(tmp[2]) + 1);
  freeChar(&tmp[0]);
  freeChar(&tmp[1]);
  freeChar(&tmp[2]);
  return 0;
}


x86 assembly (clang, -O0):
<strmreplace>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strmstr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1192 <strmreplace+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d9 <strmreplace+0x289>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strmlen@plt>
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0xa4(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strmlen@plt>
mov    %eax,%ecx
mov    -0xa4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xa0(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strmlen@plt>
mov    %eax,%ecx
mov    -0xa0(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x9c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strmlen@plt>
mov    -0x9c(%rbp),%edi
add    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x24(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strmstr@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strmlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x48(%rbp),%rdi
call   1050 <strmlen@plt>
mov    -0x98(%rbp),%rdi
mov    -0x90(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strmstr@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <strmlen@plt>
mov    -0x88(%rbp),%rdi
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strmlen@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rdi
call   1050 <strmlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strmlen@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strmlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strmlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1060 <memcpy@plt>
lea    -0x40(%rbp),%rdi
call   1030 <freeChar@plt>
lea    -0x40(%rbp),%rdi
add    $0x8,%rdi
call   1030 <freeChar@plt>
lea    -0x40(%rbp),%rdi
add    $0x10,%rdi
call   1030 <freeChar@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strmreplace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%rbp
mov    (%rdi),%rdi
call   1040 <strmstr@plt>
test   %rax,%rax
je     1305 <strmreplace+0x1b5>
mov    0x0(%rbp),%rdi
call   1050 <strmlen@plt>
lea    0x1(%rax),%r15d
mov    %rax,%rbx
mov    %rax,0x38(%rsp)
mov    %r13,%rdi
call   1050 <strmlen@plt>
mov    %eax,0xc(%rsp)
mov    %r12,%rdi
call   1050 <strmlen@plt>
mov    %rax,0x40(%rsp)
mov    %r15d,%edi
call   1070 <malloc@plt>
mov    %rbp,0x30(%rsp)
mov    %rax,%r14
mov    %rax,0x10(%rsp)
mov    %r15d,%edi
call   1070 <malloc@plt>
mov    %rax,0x18(%rsp)
mov    %r12,%rdi
call   1050 <strmlen@plt>
lea    (%rax,%rbx,1),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,0x28(%rsp)
mov    %rax,0x20(%rsp)
mov    0x0(%rbp),%rsi
mov    %r14,%rdi
mov    %r15d,%edx
call   1060 <memcpy@plt>
movq   $0x0,0x0(%rbp)
mov    %r14,%rdi
mov    %r13,%rsi
call   1040 <strmstr@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1050 <strmlen@plt>
movslq %eax,%rbp
add    %rbx,%rbp
mov    %rbp,%rdi
call   1050 <strmlen@plt>
lea    0x1(%rax),%edx
mov    0x28(%rsp),%rdi
mov    %rbp,%rsi
call   1060 <memcpy@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1040 <strmstr@plt>
movb   $0x0,(%rax)
mov    0x10(%rsp),%rbx
mov    0x18(%rsp),%rbp
mov    %rbx,%rdi
call   1050 <strmlen@plt>
lea    0x1(%rax),%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <memcpy@plt>
mov    0x38(%rsp),%rax
sub    0xc(%rsp),%eax
mov    0x40(%rsp),%rcx
lea    (%rax,%rcx,1),%edi
add    $0x3,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    0x30(%rsp),%rax
mov    %rbx,(%rax)
mov    %rbp,%rdi
call   1050 <strmlen@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    %rbp,%rdi
call   1050 <strmlen@plt>
movslq %eax,%rbp
add    %rbx,%rbp
mov    %r12,%rdi
call   1050 <strmlen@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    %r12,%rdi
call   1050 <strmlen@plt>
movslq %eax,%rbx
add    %rbp,%rbx
mov    0x20(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <strmlen@plt>
lea    0x1(%rax),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <memcpy@plt>
lea    0x10(%rsp),%rdi
call   1030 <freeChar@plt>
lea    0x18(%rsp),%rdi
call   1030 <freeChar@plt>
lea    0x20(%rsp),%rdi
call   1030 <freeChar@plt>
xor    %eax,%eax
jmp    130a <strmreplace+0x1ba>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @strmreplace(i8** %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [3 x i8*]
%11 = alloca i8*
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%12 = load i8**, i8*** %5
%13 = load i8*, i8** %12
%14 = load i8*, i8** %6
%15 = call i8* @strmstr(i8* %13, i8* %14)
store i8* %15, i8** %11
%16 = load i8*, i8** %11
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %124
19: ; preds = %3
%20 = load i8**, i8*** %5
%21 = load i8*, i8** %20
%22 = call i32 @strmlen(i8* %21)
%23 = add nsw i32 %22, 1
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = load i8*, i8** %6
%26 = call i32 @strmlen(i8* %25)
%27 = sub nsw i32 %24, %26
%28 = load i8*, i8** %7
%29 = call i32 @strmlen(i8* %28)
%30 = add nsw i32 %27, %29
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
%32 = load i32, i32* %8
%33 = call i8* @malloc(i32 %32)
%34 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
store i8* %33, i8** %34
%35 = load i32, i32* %8
%36 = call i8* @malloc(i32 %35)
%37 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 1
store i8* %36, i8** %37
%38 = load i32, i32* %8
%39 = load i8*, i8** %7
%40 = call i32 @strmlen(i8* %39)
%41 = add nsw i32 %38, %40
%42 = call i8* @malloc(i32 %41)
%43 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 2
store i8* %42, i8** %43
%44 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%45 = load i8*, i8** %44
%46 = load i8**, i8*** %5
%47 = load i8*, i8** %46
%48 = load i32, i32* %8
%49 = call i32 @memcpy(i8* %45, i8* %47, i32 %48)
%50 = load i8**, i8*** %5
store i8* null, i8** %50
%51 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%52 = load i8*, i8** %51
%53 = load i8*, i8** %6
%54 = call i8* @strmstr(i8* %52, i8* %53)
store i8* %54, i8** %11
%55 = load i8*, i8** %6
%56 = call i32 @strmlen(i8* %55)
%57 = load i8*, i8** %11
%58 = sext i32 %56 to i64
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8* %59, i8** %11
%60 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 2
%61 = load i8*, i8** %60
%62 = load i8*, i8** %11
%63 = load i8*, i8** %11
%64 = call i32 @strmlen(i8* %63)
%65 = add nsw i32 %64, 1
%66 = call i32 @memcpy(i8* %61, i8* %62, i32 %65)
%67 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%68 = load i8*, i8** %67
%69 = load i8*, i8** %6
%70 = call i8* @strmstr(i8* %68, i8* %69)
store i8* %70, i8** %11
%71 = load i8*, i8** %11
store i8 0, i8* %71
%72 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 1
%73 = load i8*, i8** %72
%74 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%75 = load i8*, i8** %74
%76 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%77 = load i8*, i8** %76
%78 = call i32 @strmlen(i8* %77)
%79 = add nsw i32 %78, 1
%80 = call i32 @memcpy(i8* %73, i8* %75, i32 %79)
%81 = load i32, i32* %9
%82 = add nsw i32 %81, 1
%83 = call i8* @malloc(i32 %82)
%84 = load i8**, i8*** %5
store i8* %83, i8** %84
%85 = load i8**, i8*** %5
%86 = load i8*, i8** %85
store i8* %86, i8** %11
%87 = load i8*, i8** %11
%88 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 1
%89 = load i8*, i8** %88
%90 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 1
%91 = load i8*, i8** %90
%92 = call i32 @strmlen(i8* %91)
%93 = call i32 @memcpy(i8* %87, i8* %89, i32 %92)
%94 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 1
%95 = load i8*, i8** %94
%96 = call i32 @strmlen(i8* %95)
%97 = load i8*, i8** %11
%98 = sext i32 %96 to i64
%99 = getelementptr inbounds i8, i8* %97, i64 %98
store i8* %99, i8** %11
%100 = load i8*, i8** %11
%101 = load i8*, i8** %7
%102 = load i8*, i8** %7
%103 = call i32 @strmlen(i8* %102)
%104 = call i32 @memcpy(i8* %100, i8* %101, i32 %103)
%105 = load i8*, i8** %7
%106 = call i32 @strmlen(i8* %105)
%107 = load i8*, i8** %11
%108 = sext i32 %106 to i64
%109 = getelementptr inbounds i8, i8* %107, i64 %108
store i8* %109, i8** %11
%110 = load i8*, i8** %11
%111 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 2
%112 = load i8*, i8** %111
%113 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 2
%114 = load i8*, i8** %113
%115 = call i32 @strmlen(i8* %114)
%116 = add nsw i32 %115, 1
%117 = call i32 @memcpy(i8* %110, i8* %112, i32 %116)
%118 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%119 = call i32 @freeChar(i8** %118)
%120 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 1
%121 = call i32 @freeChar(i8** %120)
%122 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 2
%123 = call i32 @freeChar(i8** %122)
store i32 0, i32* %4
br label %124
124: ; preds = %19, %18
%125 = load i32, i32* %4
ret i32 %125
}

LLVM IR (-O2):
define i32 @strmreplace(i8** nocapture %0, i8* %1, i8* %2) {
%4 = alloca [3 x i8*]
%5 = bitcast [3 x i8*]* %4 to i8*
%6 = load i8*, i8** %0
%7 = tail call i8* @strmstr(i8* %6, i8* %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %59, label %9
9: ; preds = %3
%10 = load i8*, i8** %0
%11 = tail call i32 @strmlen(i8* %10)
%12 = add nsw i32 %11, 1
%13 = tail call i32 @strmlen(i8* %1)
%14 = tail call i32 @strmlen(i8* %2)
%15 = tail call i8* @malloc(i32 %12)
%16 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
store i8* %15, i8** %16
%17 = tail call i8* @malloc(i32 %12)
%18 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 1
store i8* %17, i8** %18
%19 = tail call i32 @strmlen(i8* %2)
%20 = add nsw i32 %19, %12
%21 = tail call i8* @malloc(i32 %20)
%22 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 2
store i8* %21, i8** %22
%23 = load i8*, i8** %0
%24 = tail call i32 @memcpy(i8* %15, i8* %23, i32 %12)
store i8* null, i8** %0
%25 = tail call i8* @strmstr(i8* %15, i8* %1)
%26 = tail call i32 @strmlen(i8* %1)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = tail call i32 @strmlen(i8* %28)
%30 = add nsw i32 %29, 1
%31 = tail call i32 @memcpy(i8* %21, i8* %28, i32 %30)
%32 = tail call i8* @strmstr(i8* %15, i8* %1)
store i8 0, i8* %32
%33 = load i8*, i8** %18
%34 = load i8*, i8** %16
%35 = tail call i32 @strmlen(i8* %34)
%36 = add nsw i32 %35, 1
%37 = tail call i32 @memcpy(i8* %33, i8* %34, i32 %36)
%38 = sub i32 %11, %13
%39 = add i32 %38, 3
%40 = add i32 %39, %14
%41 = tail call i8* @malloc(i32 %40)
store i8* %41, i8** %0
%42 = tail call i32 @strmlen(i8* %33)
%43 = tail call i32 @memcpy(i8* %41, i8* %33, i32 %42)
%44 = tail call i32 @strmlen(i8* %33)
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %41, i64 %45
%47 = tail call i32 @strmlen(i8* %2)
%48 = tail call i32 @memcpy(i8* %46, i8* %2, i32 %47)
%49 = tail call i32 @strmlen(i8* %2)
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %46, i64 %50
%52 = load i8*, i8** %22
%53 = tail call i32 @strmlen(i8* %52)
%54 = add nsw i32 %53, 1
%55 = tail call i32 @memcpy(i8* %51, i8* %52, i32 %54)
%56 = call i32 @freeChar(i8** %16)
%57 = call i32 @freeChar(i8** %18)
%58 = call i32 @freeChar(i8** %22)
br label %59
59: ; preds = %3, %9
%60 = phi i32 [ 0, %9 ], [ -1, %3 ]
ret i32 %60
}