#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 *my_malloc(scalar_t__);
int /*<<< orphan*/ snprintf(char *, scalar_t__, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
scalar_t__ strlen(char *);
char *str_replace(int start, int len, char *origStr, char *tarStr) {
  char *newStr = NULL;
  char temp1[256] = {0};
  char temp2[256] = {0};
  char *p = origStr;
  p = p + start;
  snprintf(temp1, strlen(origStr) - strlen(p) + 1, "%s", origStr);
  p = p + len;
  sprintf(temp2, "%s", p);
  newStr = my_malloc(strlen(temp1) + strlen(temp2) + strlen(tarStr) + 1);
  if (newStr == NULL)
    return NULL;
  sprintf(newStr, "%s%s%s", temp1, tarStr, temp2);
  return newStr;
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x238(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x250(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x258(%rbp)
mov    -0x238(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x258(%rbp),%rsi
mov    -0x250(%rbp),%rdi
sub    %rax,%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rcx
lea    0xe03(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x238(%rbp),%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x238(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x238(%rbp),%rdx
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x130(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x248(%rbp)
lea    -0x230(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x248(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x240(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x240(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1030 <my_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12a2 <str_replace+0x152>
movq   $0x0,-0x8(%rbp)
jmp    12ce <str_replace+0x17e>
mov    -0x28(%rbp),%rdi
lea    -0x130(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x230(%rbp),%r8
lea    0xd41(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %esi,%r15d
xorps  %xmm0,%xmm0
movaps %xmm0,0x1f0(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movslq %edi,%rbp
add    %rdx,%rbp
mov    %rdx,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1050 <strlen@plt>
sub    %rax,%rbx
add    $0x1,%rbx
lea    0xd89(%rip),%rdx
lea    0x100(%rsp),%r13
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rcx
call   1060 <snprintf@plt>
movslq %r15d,%rsi
add    %rbp,%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %rbp,%rbx
lea    (%rax,%rbx,1),%rdi
add    $0x1,%rdi
call   1030 <my_malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12f7 <str_replace+0x1a7>
lea    0xd21(%rip),%rsi
lea    0x100(%rsp),%rdx
mov    %rsp,%r8
mov    %rbx,%rdi
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [7 x i8] c"%s%s%s\00"
define i8* @str_replace(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [256 x i8]
%12 = alloca [256 x i8]
%13 = alloca i8*
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* null, i8** %10
%14 = bitcast [256 x i8]* %11 to i8*
%15 = bitcast [256 x i8]* %12 to i8*
%16 = load i8*, i8** %8
store i8* %16, i8** %13
%17 = load i8*, i8** %13
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8* %20, i8** %13
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%22 = load i8*, i8** %8
%23 = call i64 @strlen(i8* %22)
%24 = load i8*, i8** %13
%25 = call i64 @strlen(i8* %24)
%26 = sub nsw i64 %23, %25
%27 = add nsw i64 %26, 1
%28 = load i8*, i8** %8
%29 = call i32 @snprintf(i8* %21, i64 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %13
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8* %33, i8** %13
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%35 = load i8*, i8** %13
%36 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%38 = call i64 @strlen(i8* %37)
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%40 = call i64 @strlen(i8* %39)
%41 = add nsw i64 %38, %40
%42 = load i8*, i8** %9
%43 = call i64 @strlen(i8* %42)
%44 = add nsw i64 %41, %43
%45 = add nsw i64 %44, 1
%46 = call i8* @my_malloc(i64 %45)
store i8* %46, i8** %10
%47 = load i8*, i8** %10
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %50
49: ; preds = %4
store i8* null, i8** %5
br label %57
50: ; preds = %4
%51 = load i8*, i8** %10
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%53 = load i8*, i8** %9
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%55 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %51, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %52, i8* %53, i8* %54)
%56 = load i8*, i8** %10
store i8* %56, i8** %5
br label %57
57: ; preds = %50, %49
%58 = load i8*, i8** %5
ret i8* %58
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [7 x i8] c"%s%s%s\00"
define i8* @str_replace(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%9 = sext i32 %0 to i64
%10 = getelementptr inbounds i8, i8* %2, i64 %9
%11 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%12 = tail call i64 @strlen(i8* dereferenceable(1) %10)
%13 = add i64 %11, 1
%14 = sub i64 %13, %12
%15 = call i32 @snprintf(i8* %7, i64 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %2)
%16 = sext i32 %1 to i64
%17 = getelementptr inbounds i8, i8* %10, i64 %16
%18 = call i8* @strcpy(i8* %8, i8* dereferenceable(1) %17)
%19 = call i64 @strlen(i8* %7)
%20 = call i64 @strlen(i8* %8)
%21 = call i64 @strlen(i8* dereferenceable(1) %3)
%22 = add i64 %19, 1
%23 = add i64 %22, %20
%24 = add i64 %23, %21
%25 = call i8* @my_malloc(i64 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %29, label %27
27: ; preds = %4
%28 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %25, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* %3, i8* %8)
br label %29
29: ; preds = %4, %27
ret i8* %25
}