void aes_decrypt(const unsigned char *input, int input_len,
                 unsigned char *output, const unsigned char *iv,
                 const unsigned char *key, int key_length) {
  while (input_len >= 16) {
    aes_block_decrypt(input, output, key, key_length);
    xor(output, iv, 16);
    memcpy((void *)iv, (void *)input, 16);
    input += 16;
    output += 16;
    input_len -= 16;
  }
}


x86 assembly (clang, -O0):
<aes_decrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
cmpl   $0x10,-0xc(%rbp)
jl     11af <aes_decrypt+0x8f>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x0,%al
call   1040 <aes_block_decrypt@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x10,%edx
mov    $0x0,%al
call   1030 <xor@plt>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    (%rcx),%rdx
mov    %rdx,(%rax)
mov    0x8(%rcx),%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x10,%eax
mov    %eax,-0xc(%rbp)
jmp    113f <aes_decrypt+0x1f>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_decrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x10,%esi
jl     1190 <aes_decrypt+0x70>
mov    %r9d,%r14d
mov    %r8,%r15
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %esi,%r13d
mov    %rdi,%rbp
add    $0x10,%r13d
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <aes_block_decrypt@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x10,%edx
xor    %eax,%eax
call   1030 <xor@plt>
movups 0x0(%rbp),%xmm0
movups %xmm0,(%r12)
add    $0x10,%rbp
add    $0x10,%rbx
add    $0xfffffff0,%r13d
cmp    $0x1f,%r13d
ja     1150 <aes_decrypt+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @aes_decrypt(i8* %0, i32 %1, i8* %2, i8* %3, i8* %4, i32 %5) {
%7 = icmp sgt i32 %1, 15
br i1 %7, label %8, label %18
8: ; preds = %6, %8
%9 = phi i8* [ %14, %8 ], [ %0, %6 ]
%10 = phi i32 [ %16, %8 ], [ %1, %6 ]
%11 = phi i8* [ %15, %8 ], [ %2, %6 ]
%12 = tail call i32 (i8*, i8*, i8*, i32, ...) bitcast (i32 (...)* @aes_block_decrypt to i32 (i8*, i8*, i8*, i32, ...)*)(i8* %9, i8* %11, i8* %4, i32 %5)
%13 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @xor to i32 (i8*, i8*, i32, ...)*)(i8* %11, i8* %3, i32 16)
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(16) %3, i8* align 1 dereferenceable(16) %9, i64 16, i1 false)
%14 = getelementptr inbounds i8, i8* %9, i64 16
%15 = getelementptr inbounds i8, i8* %11, i64 16
%16 = add nsw i32 %10, -16
%17 = icmp ugt i32 %10, 31
br i1 %17, label %8, label %18
18: ; preds = %8, %6
ret void
}