#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*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memmove(char *, char *, scalar_t__);
scalar_t__ realloc(char *, scalar_t__);
scalar_t__ strlen(char *);
char *strins(char **loc, size_t pos, const char *src, size_t n) {
  char *dest = *loc;
  dest = (char *)realloc(dest, strlen(dest) + n + 1);
  memmove(&dest[pos + n], &dest[pos], strlen(&dest[pos]) + 1);
  memcpy(&dest[pos], src, n);
  *loc = dest;
  return dest;
}


x86 assembly (clang, -O0):
<strins>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
add    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
add    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1060 <memmove@plt>
mov    -0x28(%rbp),%rdi
add    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strins>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rdi),%r13
mov    %r13,%rdi
call   1030 <strlen@plt>
lea    (%r12,%rax,1),%rsi
add    $0x1,%rsi
mov    %r13,%rdi
call   1050 <realloc@plt>
mov    %rax,%r13
lea    (%r12,%rbx,1),%rbp
add    %rax,%rbp
add    %rax,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <memmove@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %r13,(%r14)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strins(i8** %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%10 = load i8**, i8*** %5
%11 = load i8*, i8** %10
store i8* %11, i8** %9
%12 = load i8*, i8** %9
%13 = load i8*, i8** %9
%14 = call i64 @strlen(i8* %13)
%15 = load i64, i64* %8
%16 = add i64 %14, %15
%17 = add i64 %16, 1
%18 = call i64 @realloc(i8* %12, i64 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = load i64, i64* %6
%22 = load i64, i64* %8
%23 = add i64 %21, %22
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = load i8*, i8** %9
%26 = load i64, i64* %6
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i8*, i8** %9
%29 = load i64, i64* %6
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = call i64 @strlen(i8* %30)
%32 = add nsw i64 %31, 1
%33 = call i32 @memmove(i8* %24, i8* %27, i64 %32)
%34 = load i8*, i8** %9
%35 = load i64, i64* %6
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = load i8*, i8** %7
%38 = load i64, i64* %8
%39 = call i32 @memcpy(i8* %36, i8* %37, i64 %38)
%40 = load i8*, i8** %9
%41 = load i8**, i8*** %5
store i8* %40, i8** %41
%42 = load i8*, i8** %9
ret i8* %42
}

LLVM IR (-O2):
define i8* @strins(i8** nocapture %0, i64 %1, i8* %2, i64 %3) {
%5 = load i8*, i8** %0
%6 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%7 = add i64 %3, 1
%8 = add i64 %7, %6
%9 = tail call i64 @realloc(i8* %5, i64 %8)
%10 = inttoptr i64 %9 to i8*
%11 = add i64 %3, %1
%12 = getelementptr inbounds i8, i8* %10, i64 %11
%13 = getelementptr inbounds i8, i8* %10, i64 %1
%14 = tail call i64 @strlen(i8* dereferenceable(1) %13)
%15 = add nsw i64 %14, 1
%16 = tail call i32 @memmove(i8* %12, i8* %13, i64 %15)
%17 = tail call i32 @memcpy(i8* %13, i8* %2, i64 %3)
store i8* %10, i8** %0
ret i8* %10
}