#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*/ memmove(char *, char const *, int);
int strlen(char const *);
char *replace_string(const char *in, int pos_start, int pos_end,
                     const char *with_what) {
  int str_len_in = strlen(in);
  int str_len_ww = strlen(with_what);
  int n_remove = (pos_end - pos_start) + 1;
  int new_len = str_len_in + str_len_ww - n_remove;
  char *out = malloc(new_len + 1);
  memmove(out, in, pos_start);
  memmove(&out[pos_start], with_what, str_len_ww);
  memmove(&out[pos_start + str_len_ww], &in[pos_end + 1],
          str_len_in - (pos_end + 1));
  out[new_len] = 0x00;
  return out;
}


x86 assembly (clang, -O0):
<replace_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <memmove@plt>
mov    -0x30(%rbp),%rdi
movslq -0xc(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <memmove@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%eax
add    -0x20(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%eax
add    $0x1,%eax
sub    %eax,%edx
call   1050 <memmove@plt>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbx
mov    %rcx,0x20(%rsp)
mov    %edx,%ebp
mov    %edx,0x4(%rsp)
mov    %esi,%r12d
mov    %rdi,0x18(%rsp)
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,0x10(%rsp)
mov    %ebp,%ebx
not    %ebx
add    %r12d,%eax
mov    %rax,0x8(%rsp)
add    %ebx,%eax
lea    (%rax,%r13,1),%edi
add    $0x1,%edi
mov    %eax,%r15d
add    %r13d,%r15d
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    0x18(%rsp),%rbp
mov    %rbp,%rsi
mov    %r12d,%edx
call   1050 <memmove@plt>
movslq %r12d,%rdi
add    %r14,%rdi
mov    0x20(%rsp),%rsi
mov    0x10(%rsp),%rdx
call   1050 <memmove@plt>
movslq 0x8(%rsp),%rdi
add    %r14,%rdi
movslq 0x4(%rsp),%rax
lea    (%rax,%rbp,1),%rsi
add    $0x1,%rsi
add    %r13d,%ebx
mov    %ebx,%edx
call   1050 <memmove@plt>
movslq %r15d,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @replace_string(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 i32
%12 = alloca i32
%13 = alloca i8*
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)
store i32 %15, i32* %9
%16 = load i8*, i8** %8
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %10
%18 = load i32, i32* %7
%19 = load i32, i32* %6
%20 = sub nsw i32 %18, %19
%21 = add nsw i32 %20, 1
store i32 %21, i32* %11
%22 = load i32, i32* %9
%23 = load i32, i32* %10
%24 = add nsw i32 %22, %23
%25 = load i32, i32* %11
%26 = sub nsw i32 %24, %25
store i32 %26, i32* %12
%27 = load i32, i32* %12
%28 = add nsw i32 %27, 1
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %13
%30 = load i8*, i8** %13
%31 = load i8*, i8** %5
%32 = load i32, i32* %6
%33 = call i32 @memmove(i8* %30, i8* %31, i32 %32)
%34 = load i8*, i8** %13
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8*, i8** %8
%39 = load i32, i32* %10
%40 = call i32 @memmove(i8* %37, i8* %38, i32 %39)
%41 = load i8*, i8** %13
%42 = load i32, i32* %6
%43 = load i32, i32* %10
%44 = add nsw i32 %42, %43
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %41, i64 %45
%47 = load i8*, i8** %5
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
%52 = load i32, i32* %9
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 1
%55 = sub nsw i32 %52, %54
%56 = call i32 @memmove(i8* %46, i8* %51, i32 %55)
%57 = load i8*, i8** %13
%58 = load i32, i32* %12
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
store i8 0, i8* %60
%61 = load i8*, i8** %13
ret i8* %61
}

LLVM IR (-O2):
define i8* @replace_string(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %0)
%6 = tail call i32 @strlen(i8* %3)
%7 = xor i32 %2, -1
%8 = add i32 %6, %1
%9 = add i32 %8, %7
%10 = add i32 %9, %5
%11 = add nsw i32 %10, 1
%12 = tail call i8* @malloc(i32 %11)
%13 = tail call i32 @memmove(i8* %12, i8* %0, i32 %1)
%14 = sext i32 %1 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = tail call i32 @memmove(i8* %15, i8* %3, i32 %6)
%17 = sext i32 %8 to i64
%18 = getelementptr inbounds i8, i8* %12, i64 %17
%19 = add nsw i32 %2, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = sub nsw i32 %5, %19
%23 = tail call i32 @memmove(i8* %18, i8* %21, i32 %22)
%24 = sext i32 %10 to i64
%25 = getelementptr inbounds i8, i8* %12, i64 %24
store i8 0, i8* %25
ret i8* %12
}