#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 *concatenateStrings(int, char *, char const *, char *);
int /*<<< orphan*/ free(char *);
char *getSubstring(char *, int const, int const);
int const strlen(char *);
void insertSubstringAtPosition(char **string, const char *substring,
                               const int position) {
  char *partBefore = getSubstring(*string, 1, position - 1);
  char *partAfter =
      getSubstring(*string, position, strlen(*string) - position + 1);
  char *newString = concatenateStrings(3, partBefore, substring, partAfter);
  free(partBefore);
  free(partAfter);
  free(*string);
  *string = newString;
}


x86 assembly (clang, -O0):
<insertSubstringAtPosition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
mov    $0x1,%esi
call   1060 <getSubstring@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
sub    -0x14(%rbp),%edx
add    $0x1,%edx
call   1060 <getSubstring@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x3,%edi
call   1040 <concatenateStrings@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%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):
<insertSubstringAtPosition>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edx,%r13d
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rdi
lea    -0x1(%r13),%edx
mov    $0x1,%esi
call   1060 <getSubstring@plt>
mov    %rax,%r12
mov    (%r15),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
sub    %r13d,%eax
lea    0x1(%rax),%edx
mov    %rbx,%rdi
mov    %r13d,%esi
call   1060 <getSubstring@plt>
mov    %rax,%rbx
mov    $0x3,%edi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %rax,%rcx
call   1040 <concatenateStrings@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
mov    %r14,(%r15)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @insertSubstringAtPosition(i8** %0, i8* %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8**, i8*** %4
%11 = load i8*, i8** %10
%12 = load i32, i32* %6
%13 = sub nsw i32 %12, 1
%14 = call i8* @getSubstring(i8* %11, i32 1, i32 %13)
store i8* %14, i8** %7
%15 = load i8**, i8*** %4
%16 = load i8*, i8** %15
%17 = load i32, i32* %6
%18 = load i8**, i8*** %4
%19 = load i8*, i8** %18
%20 = call i32 @strlen(i8* %19)
%21 = load i32, i32* %6
%22 = sub nsw i32 %20, %21
%23 = add nsw i32 %22, 1
%24 = call i8* @getSubstring(i8* %16, i32 %17, i32 %23)
store i8* %24, i8** %8
%25 = load i8*, i8** %7
%26 = load i8*, i8** %5
%27 = load i8*, i8** %8
%28 = call i8* @concatenateStrings(i32 3, i8* %25, i8* %26, i8* %27)
store i8* %28, i8** %9
%29 = load i8*, i8** %7
%30 = call i32 @free(i8* %29)
%31 = load i8*, i8** %8
%32 = call i32 @free(i8* %31)
%33 = load i8**, i8*** %4
%34 = load i8*, i8** %33
%35 = call i32 @free(i8* %34)
%36 = load i8*, i8** %9
%37 = load i8**, i8*** %4
store i8* %36, i8** %37
ret void
}

LLVM IR (-O2):
define void @insertSubstringAtPosition(i8** nocapture %0, i8* %1, i32 %2) {
%4 = load i8*, i8** %0
%5 = add nsw i32 %2, -1
%6 = tail call i8* @getSubstring(i8* %4, i32 1, i32 %5)
%7 = load i8*, i8** %0
%8 = tail call i32 @strlen(i8* %7)
%9 = sub i32 1, %2
%10 = add i32 %9, %8
%11 = tail call i8* @getSubstring(i8* %7, i32 %2, i32 %10)
%12 = tail call i8* @concatenateStrings(i32 3, i8* %6, i8* %1, i8* %11)
%13 = tail call i32 @free(i8* %6)
%14 = tail call i32 @free(i8* %11)
%15 = load i8*, i8** %0
%16 = tail call i32 @free(i8* %15)
store i8* %12, i8** %0
ret void
}