void test_str_clean(char *base, const char *set) {
  char *o1 = strdup(base);
  char *o2 = strdup(base);
  char *o3 = strdup(base);
  unsigned long t1, t2;
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  old_str_clean(o1, set);
  asm volatile("rdtsc" : "=A"(t2) : :);
  ;
  printf("old = (%5lu) %s\n", t2 - t1, o1);
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  new_str_clean(o2, set);
  asm volatile("rdtsc" : "=A"(t2) : :);
  ;
  printf("new = (%5lu) %s\n", t2 - t1, o2);
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  asm_str_clean(o3, set);
  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_clean>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-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    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1040 <old_str_clean@plt>
rdtsc
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
rdtsc
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1060 <new_str_clean@plt>
rdtsc
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
rdtsc
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1070 <asm_str_clean@plt>
rdtsc
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdf3(%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_clean>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r13
mov    %rdi,%rbx
call   1080 <strdup@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,%r12
rdtsc
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1040 <old_str_clean@plt>
rdtsc
sub    %rbx,%rax
lea    0xe55(%rip),%rdi
mov    %rax,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
rdtsc
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <new_str_clean@plt>
rdtsc
sub    %rbx,%rax
lea    0xe3b(%rip),%rdi
mov    %rax,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
rdtsc
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <asm_str_clean@plt>
rdtsc
sub    %rbx,%rax
lea    0xe21(%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
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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_clean(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i8* @strdup(i8* %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %3
%13 = call i8* @strdup(i8* %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %3
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %7
%16 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %16, i64* %8
%17 = load i8*, i8** %5
%18 = load i8*, i8** %4
%19 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @old_str_clean to i32 (i8*, i8*, ...)*)(i8* %17, i8* %18)
%20 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %20, i64* %9
%21 = load i64, i64* %9
%22 = load i64, i64* %8
%23 = sub i64 %21, %22
%24 = load i8*, i8** %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %23, i8* %24)
%26 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %26, i64* %8
%27 = load i8*, i8** %6
%28 = load i8*, i8** %4
%29 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @new_str_clean to i32 (i8*, i8*, ...)*)(i8* %27, i8* %28)
%30 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %30, i64* %9
%31 = load i64, i64* %9
%32 = load i64, i64* %8
%33 = sub i64 %31, %32
%34 = load i8*, i8** %6
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %33, i8* %34)
%36 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %36, i64* %8
%37 = load i8*, i8** %7
%38 = load i8*, i8** %4
%39 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @asm_str_clean to i32 (i8*, i8*, ...)*)(i8* %37, i8* %38)
%40 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %40, i64* %9
%41 = load i64, i64* %9
%42 = load i64, i64* %8
%43 = sub i64 %41, %42
%44 = load i8*, i8** %7
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %43, i8* %44)
%46 = load i8*, i8** %5
%47 = load i8*, i8** %6
%48 = load i8*, i8** %7
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_clean(i8* nocapture %0, i8* %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = tail call i8* @strdup(i8* %0)
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%7 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @old_str_clean to i32 (i8*, i8*, ...)*)(i8* %3, i8* %1)
%8 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%9 = sub i64 %8, %6
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %9, i8* %3)
%11 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%12 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @new_str_clean to i32 (i8*, i8*, ...)*)(i8* %4, i8* %1)
%13 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%14 = sub i64 %13, %11
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %14, i8* %4)
%16 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%17 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @asm_str_clean to i32 (i8*, i8*, ...)*)(i8* %5, i8* %1)
%18 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%19 = sub i64 %18, %16
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %19, i8* %5)
tail call void @free(i8* %3)
tail call void @free(i8* %4)
tail call void @free(i8* %5)
ret void
}