void test_hex(char *conv) {
  unsigned long t1, t2;
  unsigned int res;
  res = hex(conv);
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  res = hex(conv);
  asm volatile("rdtsc" : "=A"(t2) : :);
  ;
  printf("old - %5lu cycles: %10s -> %u\n", t2 - t1, conv, res);
  res = asm_hex(conv);
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  res = asm_hex(conv);
  asm volatile("rdtsc" : "=A"(t2) : :);
  ;
  printf("asm - %5lu cycles: %10s -> %u\n", t2 - t1, conv, res);
}


x86 assembly (clang, -O0):
<test_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1050 <hex@plt>
mov    %eax,-0x1c(%rbp)
rdtsc
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1050 <hex@plt>
mov    %eax,-0x1c(%rbp)
rdtsc
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
sub    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1040 <asm_hex@plt>
mov    %eax,-0x1c(%rbp)
rdtsc
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1040 <asm_hex@plt>
mov    %eax,-0x1c(%rbp)
rdtsc
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
sub    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_hex>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <hex@plt>
rdtsc
mov    %rax,%r14
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <hex@plt>
mov    %eax,%ecx
rdtsc
sub    %r14,%rax
lea    0xea5(%rip),%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <asm_hex@plt>
rdtsc
mov    %rax,%r14
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <asm_hex@plt>
mov    %eax,%ecx
rdtsc
sub    %r14,%rax
lea    0xe90(%rip),%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"old - %5lu cycles: %10s -> %u\0A\00"
@.str.1 = [31 x i8] c"asm - %5lu cycles: %10s -> %u\0A\00"
define void @test_hex(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 (i8*, ...) bitcast (i32 (...)* @hex to i32 (i8*, ...)*)(i8* %6)
store i32 %7, i32* %5
%8 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %8, i64* %3
%9 = load i8*, i8** %2
%10 = call i32 (i8*, ...) bitcast (i32 (...)* @hex to i32 (i8*, ...)*)(i8* %9)
store i32 %10, i32* %5
%11 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %11, i64* %4
%12 = load i64, i64* %4
%13 = load i64, i64* %3
%14 = sub i64 %12, %13
%15 = load i8*, i8** %2
%16 = load i32, i32* %5
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %14, i8* %15, i32 %16)
%18 = load i8*, i8** %2
%19 = call i32 (i8*, ...) bitcast (i32 (...)* @asm_hex to i32 (i8*, ...)*)(i8* %18)
store i32 %19, i32* %5
%20 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %20, i64* %3
%21 = load i8*, i8** %2
%22 = call i32 (i8*, ...) bitcast (i32 (...)* @asm_hex to i32 (i8*, ...)*)(i8* %21)
store i32 %22, i32* %5
%23 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %23, i64* %4
%24 = load i64, i64* %4
%25 = load i64, i64* %3
%26 = sub i64 %24, %25
%27 = load i8*, i8** %2
%28 = load i32, i32* %5
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %26, i8* %27, i32 %28)
ret void
}

LLVM IR (-O2):
@.str = [31 x i8] c"old - %5lu cycles: %10s -> %u\0A\00"
@.str.1 = [31 x i8] c"asm - %5lu cycles: %10s -> %u\0A\00"
define void @test_hex(i8* %0) {
%2 = tail call i32 (i8*, ...) bitcast (i32 (...)* @hex to i32 (i8*, ...)*)(i8* %0)
%3 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%4 = tail call i32 (i8*, ...) bitcast (i32 (...)* @hex to i32 (i8*, ...)*)(i8* %0)
%5 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%6 = sub i64 %5, %3
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %6, i8* %0, i32 %4)
%8 = tail call i32 (i8*, ...) bitcast (i32 (...)* @asm_hex to i32 (i8*, ...)*)(i8* %0)
%9 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%10 = tail call i32 (i8*, ...) bitcast (i32 (...)* @asm_hex to i32 (i8*, ...)*)(i8* %0)
%11 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%12 = sub i64 %11, %9
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %12, i8* %0, i32 %10)
ret void
}