#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, char *);
int strlen(char *);
void replace_str_at(char **str, int loc, int len, char *subst) {
  char *prefix, *postfix, *target;
  int total;
  if (!len || !subst) {
    return;
  }
  prefix = malloc(loc + 1);
  memset(prefix, 0, loc + 1);
  memcpy(prefix, *str, loc);
  postfix = *str + loc + len;
  total = strlen(prefix) + strlen(subst) + strlen(postfix) + 1;
  target = malloc(total);
  snprintf(target, total, "%s%s%s", prefix, subst, postfix);
  free(prefix);
  free(*str);
  *str = target;
}


x86 assembly (clang, -O0):
<replace_str_at>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     118b <replace_str_at+0x2b>
cmpq   $0x0,-0x18(%rbp)
jne    1190 <replace_str_at+0x30>
jmp    125c <replace_str_at+0xfc>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0xc(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x28(%rbp),%r9
lea    0xdc9(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_str_at>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     1215 <replace_str_at+0xb5>
mov    %rcx,%r15
test   %rcx,%rcx
je     1215 <replace_str_at+0xb5>
mov    %edx,%r13d
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x1(%rbx),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
mov    (%r14),%rsi
mov    %r12,%rdi
mov    %ebx,%edx
call   1070 <memcpy@plt>
movslq %ebx,%rax
add    (%r14),%rax
movslq %r13d,%rbp
add    %rax,%rbp
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <strlen@plt>
add    %r13d,%ebx
add    %eax,%ebx
add    $0x1,%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
lea    0xe11(%rip),%rdx
mov    %rax,%rdi
mov    %ebx,%esi
mov    %r12,%rcx
mov    %r15,%r8
mov    %rbp,%r9
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    %r13,(%r14)
add    $0x8,%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"
define void @replace_str_at(i8** %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = load i8*, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %15, %4
br label %64
19: ; preds = %15
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %9
%23 = load i8*, i8** %9
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
%26 = call i32 @memset(i8* %23, i32 0, i32 %25)
%27 = load i8*, i8** %9
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = load i32, i32* %6
%31 = call i32 @memcpy(i8* %27, i8* %29, i32 %30)
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8* %39, i8** %10
%40 = load i8*, i8** %9
%41 = call i32 @strlen(i8* %40)
%42 = load i8*, i8** %8
%43 = call i32 @strlen(i8* %42)
%44 = add nsw i32 %41, %43
%45 = load i8*, i8** %10
%46 = call i32 @strlen(i8* %45)
%47 = add nsw i32 %44, %46
%48 = add nsw i32 %47, 1
store i32 %48, i32* %12
%49 = load i32, i32* %12
%50 = call i8* @malloc(i32 %49)
store i8* %50, i8** %11
%51 = load i8*, i8** %11
%52 = load i32, i32* %12
%53 = load i8*, i8** %9
%54 = load i8*, i8** %8
%55 = load i8*, i8** %10
%56 = call i32 @snprintf(i8* %51, i32 %52, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %53, i8* %54, i8* %55)
%57 = load i8*, i8** %9
%58 = call i32 @free(i8* %57)
%59 = load i8**, i8*** %5
%60 = load i8*, i8** %59
%61 = call i32 @free(i8* %60)
%62 = load i8*, i8** %11
%63 = load i8**, i8*** %5
store i8* %62, i8** %63
br label %64
64: ; preds = %19, %18
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s%s%s\00"
define void @replace_str_at(i8** nocapture %0, i32 %1, i32 %2, i8* %3) {
%5 = icmp ne i32 %2, 0
%6 = icmp ne i8* %3, null
%7 = and i1 %5, %6
br i1 %7, label %8, label %30
8: ; preds = %4
%9 = add nsw i32 %1, 1
%10 = tail call i8* @malloc(i32 %9)
%11 = tail call i32 @memset(i8* %10, i32 0, i32 %9)
%12 = load i8*, i8** %0
%13 = tail call i32 @memcpy(i8* %10, i8* %12, i32 %1)
%14 = load i8*, i8** %0
%15 = sext i32 %1 to i64
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = sext i32 %2 to i64
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = tail call i32 @strlen(i8* %10)
%20 = tail call i32 @strlen(i8* %3)
%21 = tail call i32 @strlen(i8* %18)
%22 = add i32 %19, 1
%23 = add i32 %22, %20
%24 = add i32 %23, %21
%25 = tail call i8* @malloc(i32 %24)
%26 = tail call i32 @snprintf(i8* %25, i32 %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %10, i8* %3, i8* %18)
%27 = tail call i32 @free(i8* %10)
%28 = load i8*, i8** %0
%29 = tail call i32 @free(i8* %28)
store i8* %25, i8** %0
br label %30
30: ; preds = %4, %8
ret void
}