#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 *malloc(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strstr(char *, char const *);
char *replace_substring(char *string, const char *substring_1,
                        const char *substring_2) {
  char *temp;
  char *new_string;
  temp = strstr(string, substring_1);
  if (temp == NULL) {
    return NULL;
  }
  new_string =
      malloc(strlen(string) + 1 - strlen(substring_1) + strlen(substring_2));
  strcpy(new_string, string);
  strcpy(new_string + (temp - string), substring_2);
  strcpy(new_string + (temp - string) + strlen(substring_2),
         temp + strlen(substring_1));
  return new_string;
}


x86 assembly (clang, -O0):
<replace_substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117d <replace_substring+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    1238 <replace_substring+0xf8>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
add    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_substring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbp
call   1060 <strstr@plt>
test   %rax,%rax
je     11d0 <replace_substring+0x90>
mov    %rax,%r12
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %r12,%rbx
sub    %rbp,%rbx
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbp
add    %rbx,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%rsi
add    %r12,%rsi
mov    %rbp,%rdi
call   1030 <strcpy@plt>
jmp    11d3 <replace_substring+0x93>
xor    %r13d,%r13d
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_substring(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 = load i8*, i8** %5
%11 = load i8*, i8** %6
%12 = call i8* @strstr(i8* %10, i8* %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
store i8* null, i8** %4
br label %57
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 1
%20 = load i8*, i8** %6
%21 = call i32 @strlen(i8* %20)
%22 = sub nsw i32 %19, %21
%23 = load i8*, i8** %7
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %22, %24
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %9
%27 = load i8*, i8** %9
%28 = load i8*, i8** %5
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = load i8*, i8** %9
%31 = load i8*, i8** %8
%32 = load i8*, i8** %5
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = getelementptr inbounds i8, i8* %30, i64 %35
%37 = load i8*, i8** %7
%38 = call i32 @strcpy(i8* %36, i8* %37)
%39 = load i8*, i8** %9
%40 = load i8*, i8** %8
%41 = load i8*, i8** %5
%42 = ptrtoint i8* %40 to i64
%43 = ptrtoint i8* %41 to i64
%44 = sub i64 %42, %43
%45 = getelementptr inbounds i8, i8* %39, i64 %44
%46 = load i8*, i8** %7
%47 = call i32 @strlen(i8* %46)
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
%50 = load i8*, i8** %8
%51 = load i8*, i8** %6
%52 = call i32 @strlen(i8* %51)
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
%55 = call i32 @strcpy(i8* %49, i8* %54)
%56 = load i8*, i8** %9
store i8* %56, i8** %4
br label %57
57: ; preds = %16, %15
%58 = load i8*, i8** %4
ret i8* %58
}

LLVM IR (-O2):
define i8* @replace_substring(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %27, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, 1
%9 = tail call i32 @strlen(i8* %1)
%10 = sub i32 %8, %9
%11 = tail call i32 @strlen(i8* %2)
%12 = add nsw i32 %10, %11
%13 = tail call i8* @malloc(i32 %12)
%14 = tail call i32 @strcpy(i8* %13, i8* %0)
%15 = ptrtoint i8* %4 to i64
%16 = ptrtoint i8* %0 to i64
%17 = sub i64 %15, %16
%18 = getelementptr inbounds i8, i8* %13, i64 %17
%19 = tail call i32 @strcpy(i8* %18, i8* %2)
%20 = tail call i32 @strlen(i8* %2)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = tail call i32 @strlen(i8* %1)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %4, i64 %24
%26 = tail call i32 @strcpy(i8* %22, i8* %25)
br label %27
27: ; preds = %3, %6
%28 = phi i8* [ %13, %6 ], [ null, %3 ]
ret i8* %28
}