#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 GetStackSize(int, char *);
int GetStringLength(char *);
char *ReturnNullMallocStack(char *);
char *ReturnOutputMallocStack(char *, char *, int);
char *InsertInString(char *output, char *input, char *addition,
                     const int index) {
  if (index < 0)
    return ReturnNullMallocStack(output);
  int length1 = GetStringLength(input);
  int length2 = GetStringLength(addition);
  if (length1 < 1 || length2 < 1 || index > length1 - 1) {
    return ReturnNullMallocStack(output);
  }
  int length0 = length1 + length2;
  char output2[GetStackSize(length0, "char")];
  int i0 = -1;
  int i = 0;
  for (i = 0; i < length1; i++) {
    if (i == index) {
      int i2 = 0;
      for (i2 = 0; i2 < length2; i2++) {
        i0++;
        output2[i0] = addition[i2];
      }
    }
    i0++;
    output2[i0] = input[i];
  }
  i0++;
  output2[i0] = '\0';
  return ReturnOutputMallocStack(output, output2, i0);
}


x86 assembly (clang, -O0):
<InsertInString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1173 <InsertInString+0x33>
mov    -0x10(%rbp),%rdi
call   1030 <ReturnNullMallocStack@plt>
mov    %rax,-0x8(%rbp)
jmp    12d3 <InsertInString+0x193>
mov    -0x18(%rbp),%rdi
call   1050 <GetStringLength@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <GetStringLength@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x28(%rbp)
jl     11b0 <InsertInString+0x70>
cmpl   $0x1,-0x2c(%rbp)
jl     11b0 <InsertInString+0x70>
mov    -0x24(%rbp),%eax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11c2 <InsertInString+0x82>
mov    -0x10(%rbp),%rdi
call   1030 <ReturnNullMallocStack@plt>
mov    %rax,-0x8(%rbp)
jmp    12d3 <InsertInString+0x193>
mov    -0x28(%rbp),%eax
mov    -0x2c(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
lea    0xe29(%rip),%rsi
call   1060 <GetStackSize@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0xffffffff,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    12a7 <InsertInString+0x167>
mov    -0x48(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    127a <InsertInString+0x13a>
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1275 <InsertInString+0x135>
mov    -0x58(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x44(%rbp)
mov    -0x20(%rbp),%rcx
movslq -0x4c(%rbp),%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x44(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    123c <InsertInString+0xfc>
jmp    127a <InsertInString+0x13a>
mov    -0x58(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x44(%rbp)
mov    -0x18(%rbp),%rcx
movslq -0x48(%rbp),%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x44(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1216 <InsertInString+0xd6>
mov    -0x58(%rbp),%rsi
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
movslq -0x44(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
mov    -0x10(%rbp),%rdi
mov    -0x44(%rbp),%edx
call   1040 <ReturnOutputMallocStack@plt>
mov    %rax,-0x8(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<InsertInString>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
test   %ecx,%ecx
js     123f <InsertInString+0xef>
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rdi
call   1050 <GetStringLength@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1050 <GetStringLength@plt>
mov    %rax,-0x30(%rbp)
cmp    %r15d,%r12d
jle    123f <InsertInString+0xef>
test   %r12d,%r12d
jle    123f <InsertInString+0xef>
cmpl   $0x0,-0x30(%rbp)
jle    123f <InsertInString+0xef>
mov    %r14,-0x58(%rbp)
mov    %rbx,-0x48(%rbp)
mov    -0x30(%rbp),%r14
lea    (%r14,%r12,1),%edi
lea    0xe40(%rip),%rsi
call   1070 <GetStackSize@plt>
mov    %rsp,-0x40(%rbp)
mov    %eax,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r14d,%eax
mov    %rax,-0x50(%rbp)
mov    %r15d,%r15d
mov    %r12d,%r12d
mov    $0xffffffff,%r13d
xor    %r14d,%r14d
mov    -0x38(%rbp),%rcx
jmp    1219 <InsertInString+0xc9>
nopl   0x0(%rax,%rax,1)
movslq %r13d,%rdx
lea    0x1(%rdx),%r13
movzbl (%rcx,%r14,1),%eax
mov    %al,0x1(%rbx,%rdx,1)
add    $0x1,%r14
cmp    %r14,%r12
je     1255 <InsertInString+0x105>
cmp    %r14,%r15
jne    1200 <InsertInString+0xb0>
lea    0x1(%r13),%eax
movslq %eax,%rdi
add    %rbx,%rdi
mov    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rcx
add    -0x30(%rbp),%r13d
jmp    1200 <InsertInString+0xb0>
mov    %rbx,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <ReturnNullMallocStack@plt>
movb   $0x0,0x2(%rbx,%rdx,1)
add    $0x2,%rdx
mov    -0x48(%rbp),%rdi
mov    %rbx,%rsi
call   1040 <ReturnOutputMallocStack@plt>
mov    -0x40(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"char\00"
define i8* @InsertInString(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%18 = load i32, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %4
%21 = load i8*, i8** %6
%22 = call i8* @ReturnNullMallocStack(i8* %21)
store i8* %22, i8** %5
br label %102
23: ; preds = %4
%24 = load i8*, i8** %7
%25 = call i32 @GetStringLength(i8* %24)
store i32 %25, i32* %10
%26 = load i8*, i8** %8
%27 = call i32 @GetStringLength(i8* %26)
store i32 %27, i32* %11
%28 = load i32, i32* %10
%29 = icmp slt i32 %28, 1
br i1 %29, label %38, label %30
30: ; preds = %23
%31 = load i32, i32* %11
%32 = icmp slt i32 %31, 1
br i1 %32, label %38, label %33
33: ; preds = %30
%34 = load i32, i32* %9
%35 = load i32, i32* %10
%36 = sub nsw i32 %35, 1
%37 = icmp sgt i32 %34, %36
br i1 %37, label %38, label %41
38: ; preds = %33, %30, %23
%39 = load i8*, i8** %6
%40 = call i8* @ReturnNullMallocStack(i8* %39)
store i8* %40, i8** %5
br label %102
41: ; preds = %33
%42 = load i32, i32* %10
%43 = load i32, i32* %11
%44 = add nsw i32 %42, %43
store i32 %44, i32* %12
%45 = load i32, i32* %12
%46 = call i32 @GetStackSize(i32 %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%47 = zext i32 %46 to i64
%48 = call i8* @llvm.stacksave()
store i8* %48, i8** %13
%49 = alloca i8, i64 %47
store i64 %47, i64* %14
store i32 -1, i32* %15
store i32 0, i32* %16
store i32 0, i32* %16
br label %50
50: ; preds = %89, %41
%51 = load i32, i32* %16
%52 = load i32, i32* %10
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %92
54: ; preds = %50
%55 = load i32, i32* %16
%56 = load i32, i32* %9
%57 = icmp eq i32 %55, %56
br i1 %57, label %58, label %78
58: ; preds = %54
store i32 0, i32* %17
store i32 0, i32* %17
br label %59
59: ; preds = %74, %58
%60 = load i32, i32* %17
%61 = load i32, i32* %11
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %77
63: ; preds = %59
%64 = load i32, i32* %15
%65 = add nsw i32 %64, 1
store i32 %65, i32* %15
%66 = load i8*, i8** %8
%67 = load i32, i32* %17
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
%70 = load i8, i8* %69
%71 = load i32, i32* %15
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %49, i64 %72
store i8 %70, i8* %73
br label %74
74: ; preds = %63
%75 = load i32, i32* %17
%76 = add nsw i32 %75, 1
store i32 %76, i32* %17
br label %59
77: ; preds = %59
br label %78
78: ; preds = %77, %54
%79 = load i32, i32* %15
%80 = add nsw i32 %79, 1
store i32 %80, i32* %15
%81 = load i8*, i8** %7
%82 = load i32, i32* %16
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %81, i64 %83
%85 = load i8, i8* %84
%86 = load i32, i32* %15
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %49, i64 %87
store i8 %85, i8* %88
br label %89
89: ; preds = %78
%90 = load i32, i32* %16
%91 = add nsw i32 %90, 1
store i32 %91, i32* %16
br label %50
92: ; preds = %50
%93 = load i32, i32* %15
%94 = add nsw i32 %93, 1
store i32 %94, i32* %15
%95 = load i32, i32* %15
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i8, i8* %49, i64 %96
store i8 0, i8* %97
%98 = load i8*, i8** %6
%99 = load i32, i32* %15
%100 = call i8* @ReturnOutputMallocStack(i8* %98, i8* %49, i32 %99)
store i8* %100, i8** %5
%101 = load i8*, i8** %13
br label %102
102: ; preds = %92, %38, %20
%103 = load i8*, i8** %5
ret i8* %103
}

LLVM IR (-O2):
@.str = [5 x i8] c"char\00"
define i8* @InsertInString(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = icmp slt i32 %3, 0
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i8* @ReturnNullMallocStack(i8* %0)
br label %50
8: ; preds = %4
%9 = tail call i32 @GetStringLength(i8* %1)
%10 = tail call i32 @GetStringLength(i8* %2)
%11 = icmp sgt i32 %9, 0
%12 = icmp sgt i32 %10, 0
%13 = select i1 %11, i1 %12, i1 false
%14 = icmp sgt i32 %9, %3
%15 = and i1 %14, %13
br i1 %15, label %18, label %16
16: ; preds = %8
%17 = tail call i8* @ReturnNullMallocStack(i8* %0)
br label %50
18: ; preds = %8
%19 = add nuw nsw i32 %10, %9
%20 = tail call i32 @GetStackSize(i32 %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = zext i32 %20 to i64
%22 = tail call i8* @llvm.stacksave()
%23 = alloca i8, i64 %21
%24 = zext i32 %10 to i64
%25 = zext i32 %3 to i64
%26 = zext i32 %9 to i64
br label %27
27: ; preds = %18, %36
%28 = phi i64 [ 0, %18 ], [ %43, %36 ]
%29 = phi i32 [ -1, %18 ], [ %38, %36 ]
%30 = icmp eq i64 %28, %25
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = add i32 %29, 1
%33 = sext i32 %32 to i64
%34 = getelementptr i8, i8* %23, i64 %33
%35 = add i32 %10, %29
br label %36
36: ; preds = %31, %27
%37 = phi i32 [ %29, %27 ], [ %35, %31 ]
%38 = add nsw i32 %37, 1
%39 = getelementptr inbounds i8, i8* %1, i64 %28
%40 = load i8, i8* %39
%41 = sext i32 %38 to i64
%42 = getelementptr inbounds i8, i8* %23, i64 %41
store i8 %40, i8* %42
%43 = add nuw nsw i64 %28, 1
%44 = icmp eq i64 %43, %26
br i1 %44, label %45, label %27
45: ; preds = %36
%46 = add nsw i32 %37, 2
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %23, i64 %47
store i8 0, i8* %48
%49 = call i8* @ReturnOutputMallocStack(i8* %0, i8* %23, i32 %46)
br label %50
50: ; preds = %16, %45, %6
%51 = phi i8* [ %7, %6 ], [ %17, %16 ], [ %49, %45 ]
ret i8* %51
}