#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*/ EVP_CIPHER_CTX;

/* Variables and functions */
int EVP_BytesToKey(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
                   unsigned char const *, unsigned int, int, unsigned char *,
                   unsigned char *);
int /*<<< orphan*/ EVP_CIPHER_CTX_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DecryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                      int /*<<< orphan*/ *, unsigned char *,
                                      unsigned char *);
int /*<<< orphan*/ EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                      int /*<<< orphan*/ *, unsigned char *,
                                      unsigned char *);
int /*<<< orphan*/ EVP_aes_256_cbc();
int /*<<< orphan*/ EVP_sha256();
int /*<<< orphan*/ printf(char *, int);
int aes_init(EVP_CIPHER_CTX *encryption_ctx, EVP_CIPHER_CTX *decryption_ctx,
             const unsigned char *keyData, unsigned int keyDataLength) {
  int i, nrounds = 5;
  unsigned char key[32];
  unsigned char iv[32];
  i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), NULL, keyData,
                     keyDataLength, nrounds, key, iv);
  if (i != 256 / 8) {
    printf("Key size is %d bits - should be 256 bits\n", i);
    return -1;
  }
  EVP_CIPHER_CTX_init(encryption_ctx);
  EVP_EncryptInit_ex(encryption_ctx, EVP_aes_256_cbc(), NULL, key, iv);
  EVP_CIPHER_CTX_init(decryption_ctx);
  EVP_DecryptInit_ex(decryption_ctx, EVP_aes_256_cbc(), NULL, key, iv);
  return 0;
}


x86 assembly (clang, -O0):
<aes_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x5,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    %eax,-0x74(%rbp)
mov    $0x0,%al
call   1090 <EVP_sha256@plt>
mov    -0x74(%rbp),%edi
mov    %eax,%esi
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
mov    -0x2c(%rbp),%r9d
lea    -0x50(%rbp),%r10
lea    -0x70(%rbp),%rax
xor    %edx,%edx
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1030 <EVP_BytesToKey@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x20,-0x28(%rbp)
je     11f3 <aes_init+0x83>
mov    -0x28(%rbp),%esi
lea    0xe1e(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <aes_init+0xee>
mov    -0x10(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_init@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    -0x88(%rbp),%rdi
mov    %eax,%esi
lea    -0x50(%rbp),%rcx
lea    -0x70(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1080 <EVP_EncryptInit_ex@plt>
mov    -0x18(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_init@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    -0x80(%rbp),%rdi
mov    %eax,%esi
lea    -0x50(%rbp),%rcx
lea    -0x70(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1070 <EVP_DecryptInit_ex@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %ecx,%r13d
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r12
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <EVP_aes_256_cbc@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <EVP_sha256@plt>
mov    %rsp,%r10
lea    0x20(%rsp),%r11
mov    %ebp,%edi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %r13d,%r8d
mov    $0x5,%r9d
push   %r10
push   %r11
call   1030 <EVP_BytesToKey@plt>
add    $0x10,%rsp
cmp    $0x20,%eax
jne    1215 <aes_init+0xa5>
mov    %r12,%rdi
call   1060 <EVP_CIPHER_CTX_init@plt>
xor    %eax,%eax
call   1040 <EVP_aes_256_cbc@plt>
lea    0x20(%rsp),%rbx
mov    %rsp,%rbp
mov    %r12,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %rbp,%r8
call   1080 <EVP_EncryptInit_ex@plt>
mov    %r14,%rdi
call   1060 <EVP_CIPHER_CTX_init@plt>
xor    %eax,%eax
call   1040 <EVP_aes_256_cbc@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %rbp,%r8
call   1070 <EVP_DecryptInit_ex@plt>
jmp    1229 <aes_init+0xb9>
lea    0xde4(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Key size is %d bits - should be 256 bits\0A\00"
define i32 @aes_init(i32* %0, i32* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [32 x i8]
%13 = alloca [32 x i8]
store i32* %0, i32** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 5, i32* %11
%14 = call i32 (...) @EVP_aes_256_cbc()
%15 = call i32 (...) @EVP_sha256()
%16 = load i8*, i8** %8
%17 = load i32, i32* %9
%18 = load i32, i32* %11
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%21 = call i32 @EVP_BytesToKey(i32 %14, i32 %15, i32* null, i8* %16, i32 %17, i32 %18, i8* %19, i8* %20)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp ne i32 %22, 32
br i1 %23, label %24, label %27
24: ; preds = %4
%25 = load i32, i32* %10
%26 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 -1, i32* %5
br label %42
27: ; preds = %4
%28 = load i32*, i32** %6
%29 = call i32 @EVP_CIPHER_CTX_init(i32* %28)
%30 = load i32*, i32** %6
%31 = call i32 (...) @EVP_aes_256_cbc()
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%34 = call i32 @EVP_EncryptInit_ex(i32* %30, i32 %31, i32* null, i8* %32, i8* %33)
%35 = load i32*, i32** %7
%36 = call i32 @EVP_CIPHER_CTX_init(i32* %35)
%37 = load i32*, i32** %7
%38 = call i32 (...) @EVP_aes_256_cbc()
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%40 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%41 = call i32 @EVP_DecryptInit_ex(i32* %37, i32 %38, i32* null, i8* %39, i8* %40)
store i32 0, i32* %5
br label %42
42: ; preds = %27, %24
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
@.str = [42 x i8] c"Key size is %d bits - should be 256 bits\0A\00"
define i32 @aes_init(i32* %0, i32* %1, i8* %2, i32 %3) {
%5 = alloca [32 x i8]
%6 = alloca [32 x i8]
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%9 = tail call i32 (...) @EVP_aes_256_cbc()
%10 = tail call i32 (...) @EVP_sha256()
%11 = call i32 @EVP_BytesToKey(i32 %9, i32 %10, i32* null, i8* %2, i32 %3, i32 5, i8* %7, i8* %8)
%12 = icmp eq i32 %11, 32
br i1 %12, label %15, label %13
13: ; preds = %4
%14 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %22
15: ; preds = %4
%16 = call i32 @EVP_CIPHER_CTX_init(i32* %0)
%17 = call i32 (...) @EVP_aes_256_cbc()
%18 = call i32 @EVP_EncryptInit_ex(i32* %0, i32 %17, i32* null, i8* %7, i8* %8)
%19 = call i32 @EVP_CIPHER_CTX_init(i32* %1)
%20 = call i32 (...) @EVP_aes_256_cbc()
%21 = call i32 @EVP_DecryptInit_ex(i32* %1, i32 %20, i32* null, i8* %7, i8* %8)
br label %22
22: ; preds = %15, %13
%23 = phi i32 [ -1, %13 ], [ 0, %15 ]
ret i32 %23
}