#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*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
char *insert(char *source, const char *to_insert, int index) {
  char rest_str[100];
  if (strlen(source) <= index) {
    strcat(source, to_insert);
  } else {
    strcpy(rest_str, &source[index]);
    strcpy(&source[index], to_insert);
    strcat(source, rest_str);
  }
  return (source);
}


x86 assembly (clang, -O0):
<insert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cmp    -0x14(%rbp),%eax
jg     116a <insert+0x3a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcat@plt>
jmp    119f <insert+0x6f>
lea    -0x80(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
lea    -0x80(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insert>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    %ebp,%eax
jle    1172 <insert+0x42>
movslq %ebp,%rbp
add    %rbx,%rbp
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
jmp    1178 <insert+0x48>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
mov    %rbx,%rax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @insert(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [100 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = load i32, i32* %6
%11 = icmp sle i32 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i8*, i8** %4
%14 = load i8*, i8** %5
%15 = call i32 @strcat(i8* %13, i8* %14)
br label %32
16: ; preds = %3
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%18 = load i8*, i8** %4
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = call i32 @strcpy(i8* %17, i8* %21)
%23 = load i8*, i8** %4
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8*, i8** %5
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %4
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%31 = call i32 @strcat(i8* %29, i8* %30)
br label %32
32: ; preds = %16, %12
%33 = load i8*, i8** %4
ret i8* %33
}

LLVM IR (-O2):
define i8* @insert(i8* returned %0, i8* %1, i32 %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp sgt i32 %6, %2
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @strcat(i8* %0, i8* %1)
br label %16
10: ; preds = %3
%11 = sext i32 %2 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = call i32 @strcpy(i8* %5, i8* %12)
%14 = call i32 @strcpy(i8* %12, i8* %1)
%15 = call i32 @strcat(i8* %0, i8* %5)
br label %16
16: ; preds = %10, %8
ret i8* %0
}