#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 *);
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, ...);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *euca_strreplace(char **haystack, char *search, char *value) {
  char *sBuffer = NULL;
  char *pStart = NULL;
  char *pSubStart = NULL;
  char *sToken = NULL;
  char *sNewString = NULL;
  if ((haystack == NULL) || (*haystack == NULL) || (search == NULL) ||
      (search[0] == '\0') || (value == NULL))
    return (NULL);
  if ((sBuffer = malloc(((32768 * 2)) * (sizeof(char)))) == NULL)
    return (NULL);
  if ((sNewString = calloc(((32768 * 2)), (sizeof(char)))) == NULL) {
    {
      free((sBuffer));
      (sBuffer) = NULL;
    };
    return (NULL);
  }
  pStart = *haystack;
  pSubStart = pStart;
  sToken = strstr(pStart, search);
  while (sToken != NULL) {
    *sToken = '\0';
    snprintf(sBuffer, (32768 * 2), "%s%s%s", sNewString, pSubStart, value);
    strncpy(sNewString, sBuffer, (32768 * 2));
    sToken += strlen(search);
    pSubStart = sToken;
    sToken = strstr(pSubStart, search);
  }
  snprintf(sBuffer, (32768 * 2), "%s%s", sNewString, pSubStart);
  strncpy(sNewString, sBuffer, (32768 * 2));
  {
    free((sBuffer));
    (sBuffer) = NULL;
  };
  {
    free((*haystack));
    (*haystack) = NULL;
  };
  *haystack = sNewString;
  return (sNewString);
}


x86 assembly (clang, -O0):
<euca_strreplace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11eb <euca_strreplace+0x7b>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11eb <euca_strreplace+0x7b>
cmpq   $0x0,-0x18(%rbp)
je     11eb <euca_strreplace+0x7b>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11eb <euca_strreplace+0x7b>
cmpq   $0x0,-0x20(%rbp)
jne    11f8 <euca_strreplace+0x88>
movq   $0x0,-0x8(%rbp)
jmp    1366 <euca_strreplace+0x1f6>
mov    $0x10000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    121d <euca_strreplace+0xad>
movq   $0x0,-0x8(%rbp)
jmp    1366 <euca_strreplace+0x1f6>
mov    $0x10000,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    1258 <euca_strreplace+0xe8>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x8(%rbp)
jmp    1366 <euca_strreplace+0x1f6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12fa <euca_strreplace+0x18a>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    $0x10000,%esi
lea    0xd56(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x10000,%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x40(%rbp)
jmp    127c <euca_strreplace+0x10c>
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    $0x10000,%esi
lea    0xcf0(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x10000,%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x48(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<euca_strreplace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     12c2 <euca_strreplace+0x152>
mov    %rsi,%r15
xor    %r12d,%r12d
test   %rsi,%rsi
je     12c5 <euca_strreplace+0x155>
mov    %rdi,%rbx
mov    (%rdi),%rbp
test   %rbp,%rbp
je     12c5 <euca_strreplace+0x155>
mov    %rdx,%r14
xor    %r12d,%r12d
test   %rdx,%rdx
je     12c5 <euca_strreplace+0x155>
cmpb   $0x0,(%r15)
je     12c5 <euca_strreplace+0x155>
mov    $0x10000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12c2 <euca_strreplace+0x152>
mov    %rax,%r13
mov    $0x10000,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     12ba <euca_strreplace+0x14a>
mov    %rax,%r12
mov    %r14,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %rbp,%rdi
mov    %r15,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     1274 <euca_strreplace+0x104>
mov    %rax,%r14
mov    0x8(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%r14)
mov    %r13,%rdi
mov    $0x10000,%esi
lea    0xdcd(%rip),%rdx
mov    %r12,%rcx
mov    %rbp,%r8
mov    %rbx,%r9
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    $0x10000,%edx
call   1040 <strncpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbp
add    %r14,%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
call   1090 <strstr@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1220 <euca_strreplace+0xb0>
lea    0xd87(%rip),%rdx
mov    %r13,%rdi
mov    $0x10000,%esi
mov    %r12,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    $0x10000,%edx
call   1040 <strncpy@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rbx
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    %r12,(%rbx)
jmp    12c5 <euca_strreplace+0x155>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"%s%s%s\00"
@.str.1 = [5 x i8] c"%s%s\00"
define i8* @euca_strreplace(i8** %0, i8* %1, i8* %2) {
%4 = icmp eq i8** %0, null
br i1 %4, label %43, label %5
5: ; preds = %3
%6 = load i8*, i8** %0
%7 = icmp eq i8* %6, null
%8 = icmp eq i8* %1, null
%9 = or i1 %8, %7
br i1 %9, label %43, label %10
10: ; preds = %5
%11 = load i8, i8* %1
%12 = icmp eq i8 %11, 0
%13 = icmp eq i8* %2, null
%14 = or i1 %13, %12
br i1 %14, label %43, label %15
15: ; preds = %10
%16 = tail call dereferenceable_or_null(65536) i8* @malloc(i32 65536)
%17 = icmp eq i8* %16, null
br i1 %17, label %43, label %18
18: ; preds = %15
%19 = tail call dereferenceable_or_null(65536) i8* @calloc(i32 65536, i32 1)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @free(i8* %16)
br label %43
23: ; preds = %18
%24 = tail call i8* @strstr(i8* dereferenceable(1) %6, i8* %1)
%25 = icmp eq i8* %24, null
br i1 %25, label %36, label %26
26: ; preds = %23, %26
%27 = phi i8* [ %34, %26 ], [ %24, %23 ]
%28 = phi i8* [ %33, %26 ], [ %6, %23 ]
store i8 0, i8* %27
%29 = tail call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %16, i32 65536, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %28, i8* %2)
%30 = tail call i32 @strncpy(i8* %19, i8* %16, i32 65536)
%31 = tail call i32 @strlen(i8* %1)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %27, i64 %32
%34 = tail call i8* @strstr(i8* %33, i8* %1)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %26
36: ; preds = %26, %23
%37 = phi i8* [ %6, %23 ], [ %33, %26 ]
%38 = tail call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %16, i32 65536, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %37)
%39 = tail call i32 @strncpy(i8* %19, i8* %16, i32 65536)
%40 = tail call i32 @free(i8* %16)
%41 = load i8*, i8** %0
%42 = tail call i32 @free(i8* %41)
store i8* %19, i8** %0
br label %43
43: ; preds = %15, %3, %5, %10, %36, %21
%44 = phi i8* [ null, %21 ], [ %19, %36 ], [ null, %10 ], [ null, %5 ], [ null, %3 ], [ null, %15 ]
ret i8* %44
}