#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 *mem_begin(char *);
char *str_erase(char *, size_t);
char *str_insert(char *, char *);
char *str_replace(char *dst, size_t len, char *src) {
  char *dst_start = NULL;
  size_t dst_offset = 0;
  if (src == NULL)
    return (NULL);
  if (dst == NULL)
    return (NULL);
  dst_start = mem_begin(dst);
  dst_offset = dst - dst_start;
  dst_start = str_erase(dst, len);
  dst = dst_start + dst_offset;
  dst_start = str_insert(dst, src);
  return (dst_start);
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116c <str_replace+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    11d6 <str_replace+0xa6>
cmpq   $0x0,-0x10(%rbp)
jne    1184 <str_replace+0x54>
movq   $0x0,-0x8(%rbp)
jmp    11d6 <str_replace+0xa6>
mov    -0x10(%rbp),%rdi
call   1040 <mem_begin@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <str_erase@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <str_insert@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     117a <str_replace+0x4a>
mov    %rdx,%r15
test   %rdx,%rdx
je     117a <str_replace+0x4a>
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <mem_begin@plt>
mov    %r12,%rbx
sub    %rax,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <str_erase@plt>
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <str_insert@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_replace(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i8* null, i8** %8
store i64 0, i64* %9
%10 = load i8*, i8** %7
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %3
store i8* null, i8** %4
br label %35
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %13
store i8* null, i8** %4
br label %35
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = call i8* @mem_begin(i8* %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %5
%21 = load i8*, i8** %8
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
store i64 %24, i64* %9
%25 = load i8*, i8** %5
%26 = load i64, i64* %6
%27 = call i8* @str_erase(i8* %25, i64 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = load i64, i64* %9
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8* %30, i8** %5
%31 = load i8*, i8** %5
%32 = load i8*, i8** %7
%33 = call i8* @str_insert(i8* %31, i8* %32)
store i8* %33, i8** %8
%34 = load i8*, i8** %8
store i8* %34, i8** %4
br label %35
35: ; preds = %17, %16, %12
%36 = load i8*, i8** %4
ret i8* %36
}

LLVM IR (-O2):
define i8* @str_replace(i8* %0, i64 %1, i8* %2) {
%4 = icmp eq i8* %2, null
%5 = icmp eq i8* %0, null
%6 = or i1 %5, %4
br i1 %6, label %15, label %7
7: ; preds = %3
%8 = tail call i8* @mem_begin(i8* %0)
%9 = ptrtoint i8* %0 to i64
%10 = ptrtoint i8* %8 to i64
%11 = sub i64 %9, %10
%12 = tail call i8* @str_erase(i8* %0, i64 %1)
%13 = getelementptr inbounds i8, i8* %12, i64 %11
%14 = tail call i8* @str_insert(i8* %13, i8* %2)
br label %15
15: ; preds = %3, %7
%16 = phi i8* [ %14, %7 ], [ null, %3 ]
ret i8* %16
}