#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 *ResizeIfNeeded(char *, int *, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char const *);
char *InplaceInsert(char *pBuffer, const char *S, char *master, int *size) {
  char *m = ResizeIfNeeded(master, size, strlen(S));
  if (m != master) {
    pBuffer += (m - master);
    master = m;
  }
  char *p = pBuffer;
  int lS = strlen(S), ll = strlen(pBuffer);
  memmove(p + lS, p, ll + 1);
  memcpy(p, S, lS);
  return master;
}


x86 assembly (clang, -O0):
<InplaceInsert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1040 <ResizeIfNeeded@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11ad <InplaceInsert+0x6d>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
add    $0x1,%edx
call   1060 <memmove@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<InplaceInsert>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <ResizeIfNeeded@plt>
mov    %rax,%r14
xor    %ebx,%ebx
sub    %r13,%rax
cmovne %rax,%rbx
add    %r12,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %ebp,%rbp
lea    (%rbx,%rbp,1),%rdi
lea    0x1(%rax),%edx
mov    %rbx,%rsi
call   1060 <memmove@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @InplaceInsert(i8* %0, i8* %1, i8* %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32* %3, i32** %8
%13 = load i8*, i8** %7
%14 = load i32*, i32** %8
%15 = load i8*, i8** %6
%16 = call i32 @strlen(i8* %15)
%17 = call i8* @ResizeIfNeeded(i8* %13, i32* %14, i32 %16)
store i8* %17, i8** %9
%18 = load i8*, i8** %9
%19 = load i8*, i8** %7
%20 = icmp ne i8* %18, %19
br i1 %20, label %21, label %30
21: ; preds = %4
%22 = load i8*, i8** %9
%23 = load i8*, i8** %7
%24 = ptrtoint i8* %22 to i64
%25 = ptrtoint i8* %23 to i64
%26 = sub i64 %24, %25
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i64 %26
store i8* %28, i8** %5
%29 = load i8*, i8** %9
store i8* %29, i8** %7
br label %30
30: ; preds = %21, %4
%31 = load i8*, i8** %5
store i8* %31, i8** %10
%32 = load i8*, i8** %6
%33 = call i32 @strlen(i8* %32)
store i32 %33, i32* %11
%34 = load i8*, i8** %5
%35 = call i32 @strlen(i8* %34)
store i32 %35, i32* %12
%36 = load i8*, i8** %10
%37 = load i32, i32* %11
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8*, i8** %10
%41 = load i32, i32* %12
%42 = add nsw i32 %41, 1
%43 = call i32 @memmove(i8* %39, i8* %40, i32 %42)
%44 = load i8*, i8** %10
%45 = load i8*, i8** %6
%46 = load i32, i32* %11
%47 = call i32 @memcpy(i8* %44, i8* %45, i32 %46)
%48 = load i8*, i8** %7
ret i8* %48
}

LLVM IR (-O2):
define i8* @InplaceInsert(i8* %0, i8* %1, i8* %2, i32* %3) {
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i8* @ResizeIfNeeded(i8* %2, i32* %3, i32 %5)
%7 = icmp eq i8* %6, %2
%8 = ptrtoint i8* %6 to i64
%9 = ptrtoint i8* %2 to i64
%10 = sub i64 %8, %9
%11 = select i1 %7, i64 0, i64 %10
%12 = getelementptr i8, i8* %0, i64 %11
%13 = tail call i32 @strlen(i8* %1)
%14 = tail call i32 @strlen(i8* %12)
%15 = sext i32 %13 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
%17 = add nsw i32 %14, 1
%18 = tail call i32 @memmove(i8* %16, i8* %12, i32 %17)
%19 = tail call i32 @memcpy(i8* %12, i8* %1, i32 %13)
ret i8* %6
}