void aesni_inverse_key(unsigned char *invkey, const unsigned char *fwdkey,
                       int nr) {
  unsigned char *ik = invkey;
  const unsigned char *fk = fwdkey + 16 * nr;
  memcpy(ik, fk, 16);
  for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16)
    asm("movdqu (%0), %%xmm0       \n\t"
        ".byte 0x66,0x0F,0x38,0xDB,"
        "0xC0"
        "\n\t"
        "movdqu %%xmm0, (%1)       \n\t"
        :
        : "r"(fk), "r"(ik)
        : "memory", "xmm0");
  memcpy(ik, fk, 16);
}


x86 assembly (clang, -O0):
<aesni_inverse_key>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x4,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rdx
mov    %rdx,(%rax)
mov    0x8(%rcx),%rcx
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rax
add    $0xfffffffffffffff0,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    1199 <aesni_inverse_key+0x99>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movdqu (%rax),%xmm0
aesimc %xmm0,%xmm0
movdqu %xmm0,(%rcx)
mov    -0x28(%rbp),%rax
add    $0xfffffffffffffff0,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x20(%rbp)
jmp    1159 <aesni_inverse_key+0x59>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rdx
mov    %rdx,(%rax)
mov    0x8(%rcx),%rcx
mov    %rcx,0x8(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<aesni_inverse_key>:
shl    $0x4,%edx
movslq %edx,%rax
movups (%rsi,%rax,1),%xmm0
movups %xmm0,(%rdi)
add    %rsi,%rax
add    $0xfffffffffffffff0,%rax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x10,%rdi
cmp    %rsi,%rax
jbe    113c <aesni_inverse_key+0x3c>
movdqu (%rax),%xmm0
aesimc %xmm0,%xmm0
movdqu %xmm0,(%rdi)
add    $0xfffffffffffffff0,%rax
jmp    1120 <aesni_inverse_key+0x20>
movups (%rax),%xmm0
movups %xmm0,(%rdi)
ret

LLVM IR (-O0):
define void @aesni_inverse_key(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
store i8* %9, i8** %7
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = mul nsw i32 16, %11
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
store i8* %14, i8** %8
%15 = load i8*, i8** %7
%16 = load i8*, i8** %8
%17 = load i8*, i8** %8
%18 = getelementptr inbounds i8, i8* %17, i64 -16
store i8* %18, i8** %8
%19 = load i8*, i8** %7
%20 = getelementptr inbounds i8, i8* %19, i64 16
store i8* %20, i8** %7
br label %21
21: ; preds = %28, %3
%22 = load i8*, i8** %8
%23 = load i8*, i8** %5
%24 = icmp ugt i8* %22, %23
br i1 %24, label %25, label %33
25: ; preds = %21
%26 = load i8*, i8** %8
%27 = load i8*, i8** %7
br label %28
28: ; preds = %25
%29 = load i8*, i8** %8
%30 = getelementptr inbounds i8, i8* %29, i64 -16
store i8* %30, i8** %8
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i64 16
store i8* %32, i8** %7
br label %21
33: ; preds = %21
%34 = load i8*, i8** %7
%35 = load i8*, i8** %8
ret void
}

LLVM IR (-O2):
define void @aesni_inverse_key(i8* %0, i8* %1, i32 %2) {
%4 = shl nsw i32 %2, 4
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %1, i64 %5
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(16) %0, i8* align 1 dereferenceable(16) %6, i64 16, i1 false)
%7 = getelementptr inbounds i8, i8* %6, i64 -16
%8 = getelementptr inbounds i8, i8* %0, i64 16
%9 = icmp ugt i8* %7, %1
br i1 %9, label %10, label %16
10: ; preds = %3, %10
%11 = phi i8* [ %14, %10 ], [ %8, %3 ]
%12 = phi i8* [ %13, %10 ], [ %7, %3 ]
tail call void asm sideeffect "movdqu ($0), %xmm0 \0A\09.byte 0x66,0x0F,0x38,0xDB,0xC0\0A\09movdqu %xmm0, ($1) \0A\09", "r,r,~{memory},~{xmm0},~{dirflag},~{fpsr},~{flags}"(i8* %12, i8* %11)
%13 = getelementptr inbounds i8, i8* %12, i64 -16
%14 = getelementptr inbounds i8, i8* %11, i64 16
%15 = icmp ugt i8* %13, %1
br i1 %15, label %10, label %16
16: ; preds = %10, %3
%17 = phi i8* [ %7, %3 ], [ %13, %10 ]
%18 = phi i8* [ %8, %3 ], [ %14, %10 ]
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(16) %18, i8* align 1 dereferenceable(16) %17, i64 16, i1 false)
ret void
}