#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ AES_KEY;

/* Variables and functions */
int AES_BLOCK_SIZE;
int /*<<< orphan*/ AES_DECRYPT;
int /*<<< orphan*/ AES_ENCRYPT;
int /*<<< orphan*/ AES_cbc_encrypt(unsigned char const *, unsigned char *,
                                   unsigned long const, int /*<<< orphan*/ *,
                                   unsigned char *, int /*<<< orphan*/);
int /*<<< orphan*/ AES_set_decrypt_key(unsigned char const *, int,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ AES_set_encrypt_key(unsigned char const *, int,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int aes_128_cbc(const unsigned char key[16], const unsigned char *in,
                const unsigned long length, unsigned char *out, int enc) {
  AES_KEY akey;
  unsigned char iv[AES_BLOCK_SIZE];
  memset(&iv[0], 0, AES_BLOCK_SIZE);
  if (enc) {
    AES_set_encrypt_key(&key[0], 128, &akey);
    AES_cbc_encrypt(in, out, length, &akey, iv, AES_ENCRYPT);
  } else {
    AES_set_decrypt_key(&key[0], 128, &akey);
    AES_cbc_encrypt(in, out, length, &akey, iv, AES_DECRYPT);
  }
  return 0;
}


x86 assembly (clang, -O0):
<aes_128_cbc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <memset@plt>
cmpl   $0x0,-0x24(%rbp)
je     11dc <aes_128_cbc+0x9c>
mov    -0x8(%rbp),%rdi
mov    $0x80,%esi
lea    -0x28(%rbp),%rdx
call   1030 <AES_set_encrypt_key@plt>
mov    -0x40(%rbp),%r8
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2e05(%rip),%rax
mov    (%rax),%r9d
lea    -0x28(%rbp),%rcx
call   1050 <AES_cbc_encrypt@plt>
jmp    1211 <aes_128_cbc+0xd1>
mov    -0x8(%rbp),%rdi
mov    $0x80,%esi
lea    -0x28(%rbp),%rdx
call   1060 <AES_set_decrypt_key@plt>
mov    -0x40(%rbp),%r8
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2ddb(%rip),%rax
mov    (%rax),%r9d
lea    -0x28(%rbp),%rcx
call   1050 <AES_cbc_encrypt@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_128_cbc>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r14d
mov    %rcx,-0x38(%rbp)
mov    %rdx,-0x40(%rbp)
mov    %rsi,%r12
mov    %rdi,%r13
mov    %rsp,%r15
mov    0x2e7c(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
lea    -0x2c(%rbp),%rdx
mov    %r13,%rdi
mov    $0x80,%esi
test   %r14d,%r14d
je     11aa <aes_128_cbc+0x6a>
call   1030 <AES_set_encrypt_key@plt>
mov    0x2e28(%rip),%rax
jmp    11b6 <aes_128_cbc+0x76>
call   1060 <AES_set_decrypt_key@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%r9d
lea    -0x2c(%rbp),%rcx
mov    %r12,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    %rbx,%r8
call   1050 <AES_cbc_encrypt@plt>
mov    %r15,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@AES_BLOCK_SIZE = global i32 0
@AES_ENCRYPT = global i32 0
@AES_DECRYPT = global i32 0
define i32 @aes_128_cbc(i8* %0, i8* %1, i64 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%14 = load i32, i32* @AES_BLOCK_SIZE
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %12
%17 = alloca i8, i64 %15
store i64 %15, i64* %13
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i32, i32* @AES_BLOCK_SIZE
%20 = call i32 @memset(i8* %18, i32 0, i32 %19)
%21 = load i32, i32* %10
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %5
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = call i32 @AES_set_encrypt_key(i8* %25, i32 128, i32* %11)
%27 = load i8*, i8** %7
%28 = load i8*, i8** %9
%29 = load i64, i64* %8
%30 = load i32, i32* @AES_ENCRYPT
%31 = call i32 @AES_cbc_encrypt(i8* %27, i8* %28, i64 %29, i32* %11, i8* %17, i32 %30)
br label %41
32: ; preds = %5
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = call i32 @AES_set_decrypt_key(i8* %34, i32 128, i32* %11)
%36 = load i8*, i8** %7
%37 = load i8*, i8** %9
%38 = load i64, i64* %8
%39 = load i32, i32* @AES_DECRYPT
%40 = call i32 @AES_cbc_encrypt(i8* %36, i8* %37, i64 %38, i32* %11, i8* %17, i32 %39)
br label %41
41: ; preds = %32, %23
%42 = load i8*, i8** %12
ret i32 0
}

LLVM IR (-O2):
@AES_BLOCK_SIZE = global i32 0
@AES_ENCRYPT = global i32 0
@AES_DECRYPT = global i32 0
define i32 @aes_128_cbc(i8* %0, i8* %1, i64 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = load i32, i32* @AES_BLOCK_SIZE
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = load i32, i32* @AES_BLOCK_SIZE
%13 = call i32 @memset(i8* %11, i32 0, i32 %12)
%14 = icmp eq i32 %4, 0
br i1 %14, label %17, label %15
15: ; preds = %5
%16 = call i32 @AES_set_encrypt_key(i8* %0, i32 128, i32* %6)
br label %19
17: ; preds = %5
%18 = call i32 @AES_set_decrypt_key(i8* %0, i32 128, i32* %6)
br label %19
19: ; preds = %17, %15
%20 = phi i32* [ @AES_DECRYPT, %17 ], [ @AES_ENCRYPT, %15 ]
%21 = load i32, i32* %20
%22 = call i32 @AES_cbc_encrypt(i8* %1, i8* %3, i64 %2, i32* %6, i8* %11, i32 %21)
ret i32 0
}