void test_str_erase(char *base, int start, int end) {
  char *o1 = strdup(base);
  char *o2 = strdup(base);
  char *o3 = strdup(base);
  unsigned long t1, t2;
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  old_str_erase(o1, start, end);
  asm volatile("rdtsc" : "=A"(t2) : :);
  ;
  printf("old = (%5lu) %s\n", t2 - t1, o1);
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  new_str_erase(o2, start, end);
  asm volatile("rdtsc" : "=A"(t2) : :);
  ;
  printf("new = (%5lu) %s\n", t2 - t1, o2);
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  asm_str_erase(o3, start, end);
  asm volatile("rdtsc" : "=A"(t2) : :);
  ;
  printf("asm = (%5lu) %s\n", t2 - t1, o3);
  free(o1);
  free(o2);
  free(o3);
}


x86 assembly (clang, -O0):
<test_str_erase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x28(%rbp)
rdtsc
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1060 <old_str_erase@plt>
rdtsc
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
rdtsc
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1040 <new_str_erase@plt>
rdtsc
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
rdtsc
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1070 <asm_str_erase@plt>
rdtsc
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_str_erase>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
mov    %esi,%ebx
mov    %rdi,%rbp
call   1080 <strdup@plt>
mov    %rax,%r14
mov    %rbp,%rdi
call   1080 <strdup@plt>
mov    %rax,%r15
mov    %rbp,%rdi
call   1080 <strdup@plt>
mov    %rax,%r12
rdtsc
mov    %rax,%rbp
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1060 <old_str_erase@plt>
rdtsc
sub    %rbp,%rax
lea    0xe4f(%rip),%rdi
mov    %rax,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
rdtsc
mov    %rax,%rbp
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1040 <new_str_erase@plt>
rdtsc
sub    %rbp,%rax
lea    0xe33(%rip),%rdi
mov    %rax,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
rdtsc
mov    %rax,%rbp
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1070 <asm_str_erase@plt>
rdtsc
sub    %rbp,%rax
lea    0xe17(%rip),%rdi
mov    %rax,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"old = (%5lu) %s\0A\00"
@.str.1 = [17 x i8] c"new = (%5lu) %s\0A\00"
@.str.2 = [17 x i8] c"asm = (%5lu) %s\0A\00"
define void @test_str_erase(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i8*, i8** %4
%13 = call i8* @strdup(i8* %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %4
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %8
%16 = load i8*, i8** %4
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %9
%18 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %18, i64* %10
%19 = load i8*, i8** %7
%20 = load i32, i32* %5
%21 = load i32, i32* %6
%22 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @old_str_erase to i32 (i8*, i32, i32, ...)*)(i8* %19, i32 %20, i32 %21)
%23 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %23, i64* %11
%24 = load i64, i64* %11
%25 = load i64, i64* %10
%26 = sub i64 %24, %25
%27 = load i8*, i8** %7
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %26, i8* %27)
%29 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %29, i64* %10
%30 = load i8*, i8** %8
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @new_str_erase to i32 (i8*, i32, i32, ...)*)(i8* %30, i32 %31, i32 %32)
%34 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %34, i64* %11
%35 = load i64, i64* %11
%36 = load i64, i64* %10
%37 = sub i64 %35, %36
%38 = load i8*, i8** %8
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %37, i8* %38)
%40 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %40, i64* %10
%41 = load i8*, i8** %9
%42 = load i32, i32* %5
%43 = load i32, i32* %6
%44 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @asm_str_erase to i32 (i8*, i32, i32, ...)*)(i8* %41, i32 %42, i32 %43)
%45 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %45, i64* %11
%46 = load i64, i64* %11
%47 = load i64, i64* %10
%48 = sub i64 %46, %47
%49 = load i8*, i8** %9
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %48, i8* %49)
%51 = load i8*, i8** %7
%52 = load i8*, i8** %8
%53 = load i8*, i8** %9
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"old = (%5lu) %s\0A\00"
@.str.1 = [17 x i8] c"new = (%5lu) %s\0A\00"
@.str.2 = [17 x i8] c"asm = (%5lu) %s\0A\00"
define void @test_str_erase(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i8* @strdup(i8* %0)
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i8* @strdup(i8* %0)
%7 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%8 = tail call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @old_str_erase to i32 (i8*, i32, i32, ...)*)(i8* %4, i32 %1, i32 %2)
%9 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%10 = sub i64 %9, %7
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %10, i8* %4)
%12 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%13 = tail call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @new_str_erase to i32 (i8*, i32, i32, ...)*)(i8* %5, i32 %1, i32 %2)
%14 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%15 = sub i64 %14, %12
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %15, i8* %5)
%17 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%18 = tail call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @asm_str_erase to i32 (i8*, i32, i32, ...)*)(i8* %6, i32 %1, i32 %2)
%19 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%20 = sub i64 %19, %17
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %20, i8* %6)
tail call void @free(i8* %4)
tail call void @free(i8* %5)
tail call void @free(i8* %6)
ret void
}