#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 */
typedef int int32_t;

/* Variables and functions */
scalar_t__ calloc(int const, int);
int /*<<< orphan*/ memcpy(char *, char const *, int const);
int const strlen(char const *);
char *stringInsert(const char *string, const char *substr,
                   const int32_t offset) {
  char *newstr =
      (char *)calloc(strlen(string) + strlen(substr) + 2, sizeof(char));
  if (newstr == NULL)
    return NULL;
  memcpy(newstr, string, offset);
  memcpy(newstr + offset, substr, strlen(substr));
  memcpy(newstr + offset + strlen(substr), string + offset,
         strlen(string) - offset + 1);
  return newstr;
}


x86 assembly (clang, -O0):
<stringInsert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x2c(%rbp),%edi
add    %eax,%edi
add    $0x2,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1186 <stringInsert+0x56>
movq   $0x0,-0x8(%rbp)
jmp    1222 <stringInsert+0xf2>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
sub    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stringInsert>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     11c4 <stringInsert+0x94>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
movslq %r12d,%r12
mov    %r13,%rbx
add    %r12,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbp
add    %rbx,%rbp
lea    (%r14,%r12,1),%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
sub    %r12d,%eax
lea    0x1(%rax),%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <memcpy@plt>
jmp    11c7 <stringInsert+0x97>
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @stringInsert(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = add i32 %4, 2
%7 = add i32 %6, %5
%8 = tail call i64 @calloc(i32 %7, i32 1)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %8, 0
br i1 %10, label %25, label %11
11: ; preds = %3
%12 = tail call i32 @memcpy(i8* %9, i8* %0, i32 %2)
%13 = sext i32 %2 to i64
%14 = getelementptr inbounds i8, i8* %9, i64 %13
%15 = tail call i32 @strlen(i8* %1)
%16 = tail call i32 @memcpy(i8* %14, i8* %1, i32 %15)
%17 = tail call i32 @strlen(i8* %1)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %14, i64 %18
%20 = getelementptr inbounds i8, i8* %0, i64 %13
%21 = tail call i32 @strlen(i8* %0)
%22 = sub i32 1, %2
%23 = add i32 %22, %21
%24 = tail call i32 @memcpy(i8* %19, i8* %20, i32 %23)
br label %25
25: ; preds = %3, %11
%26 = phi i8* [ %9, %11 ], [ null, %3 ]
ret i8* %26
}