#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*/ assert(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *strstr(char *, char *);
char *replace(char *s, char *term, char *new_term) {
  char *nw = NULL, *pos;
  char *cur = s;
  assert(s);
  assert(term);
  assert(new_term);
  while (pos = strstr(cur, term)) {
    nw = (char *)realloc(nw, pos - cur + strlen(new_term));
    strncat(nw, cur, pos - cur);
    strcat(nw, new_term);
    cur = pos + strlen(term);
  }
  strcat(nw, cur);
  free(s);
  return nw;
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1060 <assert@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1252 <replace+0xe2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <strncat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    11af <replace+0x3f>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
call   1060 <assert@plt>
mov    %r12,%rdi
call   1060 <assert@plt>
mov    %r15,%rdi
call   1060 <assert@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     121a <replace+0xaa>
mov    %rax,%r14
xor    %r13d,%r13d
mov    %rbx,(%rsp)
mov    %rbx,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%ebx
sub    %ebp,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbx,1),%esi
mov    %r13,%rdi
call   1070 <realloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1050 <strncat@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbp
add    %r14,%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11c0 <replace+0x50>
mov    (%rsp),%rbx
jmp    1220 <replace+0xb0>
xor    %r13d,%r13d
mov    %rbx,%rbp
mov    %r13,%rdi
mov    %rbp,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @replace(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** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i8* null, i8** %7
%10 = load i8*, i8** %4
store i8* %10, i8** %9
%11 = load i8*, i8** %4
%12 = call i32 @assert(i8* %11)
%13 = load i8*, i8** %5
%14 = call i32 @assert(i8* %13)
%15 = load i8*, i8** %6
%16 = call i32 @assert(i8* %15)
br label %17
17: ; preds = %22, %3
%18 = load i8*, i8** %9
%19 = load i8*, i8** %5
%20 = call i8* @strstr(i8* %18, i8* %19)
store i8* %20, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %53
22: ; preds = %17
%23 = load i8*, i8** %7
%24 = load i8*, i8** %8
%25 = load i8*, i8** %9
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
%29 = load i8*, i8** %6
%30 = call i32 @strlen(i8* %29)
%31 = sext i32 %30 to i64
%32 = add nsw i64 %28, %31
%33 = trunc i64 %32 to i32
%34 = call i64 @realloc(i8* %23, i32 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = load i8*, i8** %9
%38 = load i8*, i8** %8
%39 = load i8*, i8** %9
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
%43 = trunc i64 %42 to i32
%44 = call i32 @strncat(i8* %36, i8* %37, i32 %43)
%45 = load i8*, i8** %7
%46 = load i8*, i8** %6
%47 = call i32 @strcat(i8* %45, i8* %46)
%48 = load i8*, i8** %8
%49 = load i8*, i8** %5
%50 = call i32 @strlen(i8* %49)
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
store i8* %52, i8** %9
br label %17
53: ; preds = %17
%54 = load i8*, i8** %7
%55 = load i8*, i8** %9
%56 = call i32 @strcat(i8* %54, i8* %55)
%57 = load i8*, i8** %4
%58 = call i32 @free(i8* %57)
%59 = load i8*, i8** %7
ret i8* %59
}

LLVM IR (-O2):
define i8* @replace(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @assert(i8* %0)
%5 = tail call i32 @assert(i8* %1)
%6 = tail call i32 @assert(i8* %2)
%7 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %30, label %9
9: ; preds = %3, %9
%10 = phi i8* [ %26, %9 ], [ %7, %3 ]
%11 = phi i8* [ %25, %9 ], [ %0, %3 ]
%12 = phi i8* [ %20, %9 ], [ null, %3 ]
%13 = ptrtoint i8* %10 to i64
%14 = ptrtoint i8* %11 to i64
%15 = sub i64 %13, %14
%16 = tail call i32 @strlen(i8* %2)
%17 = trunc i64 %15 to i32
%18 = add i32 %16, %17
%19 = tail call i64 @realloc(i8* %12, i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = tail call i32 @strncat(i8* %20, i8* %11, i32 %17)
%22 = tail call i32 @strcat(i8* %20, i8* %2)
%23 = tail call i32 @strlen(i8* %1)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %10, i64 %24
%26 = tail call i8* @strstr(i8* dereferenceable(1) %25, i8* dereferenceable(1) %1)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %9
28: ; preds = %9
%29 = inttoptr i64 %19 to i8*
br label %30
30: ; preds = %28, %3
%31 = phi i8* [ null, %3 ], [ %29, %28 ]
%32 = phi i8* [ %0, %3 ], [ %25, %28 ]
%33 = tail call i32 @strcat(i8* %31, i8* %32)
%34 = tail call i32 @free(i8* %0)
ret i8* %31
}