void aes_encrypt(const unsigned char *input, int input_len,
                 unsigned char *output, const unsigned char *iv,
                 const unsigned char *key, int key_length) {
  unsigned char input_block[16];
  while (input_len >= 16) {
    memcpy(input_block, input, 16);
    xor(input_block, iv, 16);
    aes_block_encrypt(input_block, output, key, key_length);
    memcpy((void *)iv, (void *)output, 16);
    input += 16;
    output += 16;
    input_len -= 16;
  }
}


x86 assembly (clang, -O0):
<aes_encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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     11c2 <aes_encrypt+0xa2>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
mov    %rcx,-0x40(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x10,%edx
mov    $0x0,%al
call   1030 <xor@plt>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x0,%al
call   1040 <aes_block_encrypt@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%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_encrypt+0x1f>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,0xc(%rsp)
cmp    $0x10,%esi
jl     1198 <aes_encrypt+0x78>
mov    %r8,%r15
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r13
add    $0x10,%ebp
lea    0x10(%rsp),%r14
xchg   %ax,%ax
movups 0x0(%r13),%xmm0
movaps %xmm0,0x10(%rsp)
mov    %r14,%rdi
mov    %r12,%rsi
mov    $0x10,%edx
xor    %eax,%eax
call   1030 <xor@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    0xc(%rsp),%ecx
xor    %eax,%eax
call   1040 <aes_block_encrypt@plt>
movups (%rbx),%xmm0
movups %xmm0,(%r12)
add    $0x10,%r13
add    $0x10,%rbx
add    $0xfffffff0,%ebp
cmp    $0x1f,%ebp
ja     1150 <aes_encrypt+0x30>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @aes_encrypt(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
%13 = alloca [16 x i8]
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 %14
14: ; preds = %17, %6
%15 = load i32, i32* %8
%16 = icmp sge i32 %15, 16
br i1 %16, label %17, label %36
17: ; preds = %14
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
%19 = load i8*, i8** %7
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
%21 = load i8*, i8** %10
%22 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @xor to i32 (i8*, i8*, i32, ...)*)(i8* %20, i8* %21, i32 16)
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
%24 = load i8*, i8** %9
%25 = load i8*, i8** %11
%26 = load i32, i32* %12
%27 = call i32 (i8*, i8*, i8*, i32, ...) bitcast (i32 (...)* @aes_block_encrypt to i32 (i8*, i8*, i8*, i32, ...)*)(i8* %23, i8* %24, i8* %25, i32 %26)
%28 = load i8*, i8** %10
%29 = load i8*, i8** %9
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 16
store i8* %31, i8** %7
%32 = load i8*, i8** %9
%33 = getelementptr inbounds i8, i8* %32, i64 16
store i8* %33, i8** %9
%34 = load i32, i32* %8
%35 = sub nsw i32 %34, 16
store i32 %35, i32* %8
br label %14
36: ; preds = %14
ret void
}

LLVM IR (-O2):
define void @aes_encrypt(i8* nocapture %0, i32 %1, i8* %2, i8* %3, i8* %4, i32 %5) {
%7 = alloca [16 x i8]
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%9 = icmp sgt i32 %1, 15
br i1 %9, label %10, label %20
10: ; preds = %6, %10
%11 = phi i8* [ %16, %10 ], [ %0, %6 ]
%12 = phi i32 [ %18, %10 ], [ %1, %6 ]
%13 = phi i8* [ %17, %10 ], [ %2, %6 ]
%14 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @xor to i32 (i8*, i8*, i32, ...)*)(i8* %8, i8* %3, i32 16)
%15 = call i32 (i8*, i8*, i8*, i32, ...) bitcast (i32 (...)* @aes_block_encrypt to i32 (i8*, i8*, i8*, i32, ...)*)(i8* %8, i8* %13, i8* %4, i32 %5)
%16 = getelementptr inbounds i8, i8* %11, i64 16
%17 = getelementptr inbounds i8, i8* %13, i64 16
%18 = add nsw i32 %12, -16
%19 = icmp ugt i32 %12, 31
br i1 %19, label %10, label %20
20: ; preds = %10, %6
ret void
}