void clearcache(unsigned char *mem, unsigned size) {
  unsigned i, cl, eax, feat;
  asm("cpuid" : "=a"(eax), "=b"(cl), "=d"(feat) : "0"(1) : "cx");
  if (!(feat & (1 << 19)))
    fallback_clearcache();
  cl = ((cl >> 8) & 0xff) * 8;
  for (i = 0; i < size; i += cl)
    asm("clflush %0" ::"m"(mem[i]));
}


x86 assembly (clang, -O0):
<clearcache>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x1,%eax
cpuid
mov    %eax,-0x20(%rbp)
mov    %ebx,-0x1c(%rbp)
mov    %edx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0x80000,%eax
cmp    $0x0,%eax
jne    1148 <clearcache+0x38>
mov    $0x0,%al
call   1030 <fallback_clearcache@plt>
mov    -0x1c(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
shl    $0x3,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1187 <clearcache+0x77>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    %rcx,%rax
clflush (%rax)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1160 <clearcache+0x50>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<clearcache>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    $0x1,%eax
cpuid
test   $0x80000,%edx
jne    112f <clearcache+0x1f>
xor    %eax,%eax
call   1030 <fallback_clearcache@plt>
test   %ebp,%ebp
je     114d <clearcache+0x3d>
shr    $0x5,%ebx
and    $0x7f8,%ebx
xor    %eax,%eax
xchg   %ax,%ax
mov    %eax,%ecx
clflush (%r14,%rcx,1)
add    %ebx,%eax
cmp    %ebp,%eax
jb     1140 <clearcache+0x30>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @clearcache(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = call { i32, i32, i32 } asm "cpuid", "={ax},={bx},={dx},0,~{cx},~{dirflag},~{fpsr},~{flags}"(i32 1)
%10 = extractvalue { i32, i32, i32 } %9, 0
%11 = extractvalue { i32, i32, i32 } %9, 1
%12 = extractvalue { i32, i32, i32 } %9, 2
store i32 %10, i32* %7
store i32 %11, i32* %6
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = and i32 %13, 524288
%15 = icmp ne i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = call i32 (...) @fallback_clearcache()
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %6
%20 = lshr i32 %19, 8
%21 = and i32 %20, 255
%22 = mul i32 %21, 8
store i32 %22, i32* %6
store i32 0, i32* %5
br label %23
23: ; preds = %32, %18
%24 = load i32, i32* %5
%25 = load i32, i32* %4
%26 = icmp ult i32 %24, %25
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = load i32, i32* %5
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
br label %32
32: ; preds = %27
%33 = load i32, i32* %6
%34 = load i32, i32* %5
%35 = add i32 %34, %33
store i32 %35, i32* %5
br label %23
36: ; preds = %23
ret void
}

LLVM IR (-O2):
define void @clearcache(i8* %0, i32 %1) {
%3 = tail call { i32, i32, i32 } asm "cpuid", "={ax},={bx},={dx},0,~{cx},~{dirflag},~{fpsr},~{flags}"(i32 1)
%4 = extractvalue { i32, i32, i32 } %3, 1
%5 = extractvalue { i32, i32, i32 } %3, 2
%6 = and i32 %5, 524288
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (...) @fallback_clearcache()
br label %10
10: ; preds = %8, %2
%11 = lshr i32 %4, 5
%12 = and i32 %11, 2040
%13 = icmp eq i32 %1, 0
br i1 %13, label %20, label %14
14: ; preds = %10, %14
%15 = phi i32 [ %18, %14 ], [ 0, %10 ]
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
tail call void asm sideeffect "clflush $0", "*m,~{dirflag},~{fpsr},~{flags}"(i8* elementtype(i8) %17)
%18 = add i32 %15, %12
%19 = icmp ult i32 %18, %1
br i1 %19, label %14, label %20
20: ; preds = %14, %10
ret void
}