#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 *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *StrUtils_createStrWithFragmentReplaced(char *orig_str, int pos_start,
                                             int chars, char *replacement) {
  int final_str_len =
      sizeof(char) * (strlen(orig_str) - chars + strlen(replacement)) + 1;
  int suffix_len = strlen(orig_str) - chars - pos_start;
  char *final_str = malloc(final_str_len);
  strncpy(final_str, orig_str, pos_start);
  final_str[pos_start] = 0;
  strcat(final_str, replacement);
  if (suffix_len > 0) {
    char suffix[suffix_len];
    strncpy(suffix, orig_str + pos_start + chars,
            strlen(orig_str) - chars - pos_start);
    suffix[suffix_len] = 0;
    strcat(final_str, suffix);
  }
  final_str[final_str_len - 1] = 0;
  return final_str;
}


x86 assembly (clang, -O0):
<StrUtils_createStrWithFragmentReplaced>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
sub    -0x10(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
cltq
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
sub    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
cmpl   $0x0,-0x20(%rbp)
jle    1249 <StrUtils_createStrWithFragmentReplaced+0x109>
mov    -0x20(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
sub    -0x10(%rbp),%edx
sub    -0xc(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x48(%rbp),%rsi
movslq -0x20(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
mov    -0x28(%rbp),%rdi
call   1060 <strcat@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrUtils_createStrWithFragmentReplaced>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rcx,-0x30(%rbp)
mov    %edx,%r12d
mov    %esi,%r13d
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%ebx
sub    %r12d,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%ecx
add    %eax,%ecx
mov    %ecx,-0x38(%rbp)
add    %eax,%ebx
add    $0x1,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    %r12,-0x48(%rbp)
lea    (%r12,%r13,1),%eax
mov    %eax,-0x34(%rbp)
sub    %eax,%r14d
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
call   1030 <strncpy@plt>
movslq %r13d,%r13
movb   $0x0,(%r12,%r13,1)
mov    %r12,%rdi
mov    -0x30(%rbp),%rsi
call   1060 <strcat@plt>
test   %r14d,%r14d
jle    121e <StrUtils_createStrWithFragmentReplaced+0xde>
mov    %rsp,-0x30(%rbp)
mov    %r14d,%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
add    %r15,%r13
movslq -0x48(%rbp),%r14
add    %r13,%r14
mov    %r15,%rdi
call   1040 <strlen@plt>
sub    -0x34(%rbp),%eax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax,%rbx,1)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <strcat@plt>
mov    -0x30(%rbp),%rsp
movslq -0x38(%rbp),%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @StrUtils_createStrWithFragmentReplaced(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
%16 = load i32, i32* %7
%17 = sub nsw i32 %15, %16
%18 = load i8*, i8** %8
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %17, %19
%21 = sext i32 %20 to i64
%22 = mul i64 1, %21
%23 = add i64 %22, 1
%24 = trunc i64 %23 to i32
store i32 %24, i32* %9
%25 = load i8*, i8** %5
%26 = call i32 @strlen(i8* %25)
%27 = load i32, i32* %7
%28 = sub nsw i32 %26, %27
%29 = load i32, i32* %6
%30 = sub nsw i32 %28, %29
store i32 %30, i32* %10
%31 = load i32, i32* %9
%32 = call i8* @malloc(i32 %31)
store i8* %32, i8** %11
%33 = load i8*, i8** %11
%34 = load i8*, i8** %5
%35 = load i32, i32* %6
%36 = call i32 @strncpy(i8* %33, i8* %34, i32 %35)
%37 = load i8*, i8** %11
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %11
%42 = load i8*, i8** %8
%43 = call i32 @strcat(i8* %41, i8* %42)
%44 = load i32, i32* %10
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %71
46: ; preds = %4
%47 = load i32, i32* %10
%48 = zext i32 %47 to i64
%49 = call i8* @llvm.stacksave()
store i8* %49, i8** %12
%50 = alloca i8, i64 %48
store i64 %48, i64* %13
%51 = load i8*, i8** %5
%52 = load i32, i32* %6
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = load i32, i32* %7
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8*, i8** %5
%59 = call i32 @strlen(i8* %58)
%60 = load i32, i32* %7
%61 = sub nsw i32 %59, %60
%62 = load i32, i32* %6
%63 = sub nsw i32 %61, %62
%64 = call i32 @strncpy(i8* %50, i8* %57, i32 %63)
%65 = load i32, i32* %10
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %50, i64 %66
store i8 0, i8* %67
%68 = load i8*, i8** %11
%69 = call i32 @strcat(i8* %68, i8* %50)
%70 = load i8*, i8** %12
br label %71
71: ; preds = %46, %4
%72 = load i8*, i8** %11
%73 = load i32, i32* %9
%74 = sub nsw i32 %73, 1
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %72, i64 %75
store i8 0, i8* %76
%77 = load i8*, i8** %11
ret i8* %77
}

LLVM IR (-O2):
define i8* @StrUtils_createStrWithFragmentReplaced(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %0)
%6 = sub nsw i32 %5, %2
%7 = tail call i32 @strlen(i8* %3)
%8 = add nsw i32 %6, %7
%9 = add i32 %8, 1
%10 = tail call i32 @strlen(i8* %0)
%11 = add i32 %2, %1
%12 = sub i32 %10, %11
%13 = tail call i8* @malloc(i32 %9)
%14 = tail call i32 @strncpy(i8* %13, i8* %0, i32 %1)
%15 = sext i32 %1 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 0, i8* %16
%17 = tail call i32 @strcat(i8* %13, i8* %3)
%18 = icmp sgt i32 %12, 0
br i1 %18, label %19, label %31
19: ; preds = %4
%20 = zext i32 %12 to i64
%21 = tail call i8* @llvm.stacksave()
%22 = alloca i8, i64 %20
%23 = getelementptr inbounds i8, i8* %0, i64 %15
%24 = sext i32 %2 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = tail call i32 @strlen(i8* %0)
%27 = sub i32 %26, %11
%28 = call i32 @strncpy(i8* %22, i8* %25, i32 %27)
%29 = getelementptr inbounds i8, i8* %22, i64 %20
store i8 0, i8* %29
%30 = call i32 @strcat(i8* %13, i8* %22)
br label %31
31: ; preds = %19, %4
%32 = sext i32 %8 to i64
%33 = getelementptr inbounds i8, i8* %13, i64 %32
store i8 0, i8* %33
ret i8* %13
}