#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*/, unsigned char *,
                   unsigned char *, 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_sha1();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdout;
int aes_init(unsigned char *key_data, int key_data_len, unsigned char *salt,
             EVP_CIPHER_CTX *e_ctx, EVP_CIPHER_CTX *d_ctx) {
  int x = 0, nrounds = 5;
  unsigned char key[32], iv[32];
  x = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt, key_data,
                     key_data_len, nrounds, key, iv);
  if (x != 32) {
    fprintf(stdout, "Key size %d bits - should be 256 bits\n", x);
    return -1;
  }
  EVP_CIPHER_CTX_init(e_ctx);
  EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), NULL, key, iv);
  EVP_CIPHER_CTX_init(d_ctx);
  EVP_DecryptInit_ex(d_ctx, EVP_aes_256_cbc(), NULL, key, iv);
  return 0;
}


x86 assembly (clang, -O0):
<aes_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x5,-0x38(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    %eax,-0x84(%rbp)
mov    $0x0,%al
call   1050 <EVP_sha1@plt>
mov    -0x84(%rbp),%edi
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    -0x38(%rbp),%r9d
lea    -0x60(%rbp),%r10
lea    -0x80(%rbp),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1030 <EVP_BytesToKey@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x20,-0x34(%rbp)
je     120f <aes_init+0x9f>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%edx
lea    0xe02(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <aes_init+0x110>
mov    -0x28(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_init@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    -0x98(%rbp),%rdi
mov    %eax,%esi
lea    -0x60(%rbp),%rcx
lea    -0x80(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1090 <EVP_EncryptInit_ex@plt>
mov    -0x30(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_init@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    -0x90(%rbp),%rdi
mov    %eax,%esi
lea    -0x60(%rbp),%rcx
lea    -0x80(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1080 <EVP_DecryptInit_ex@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r8,0x8(%rsp)
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %esi,%r13d
mov    %rdi,%rbp
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <EVP_aes_256_cbc@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1050 <EVP_sha1@plt>
lea    0x10(%rsp),%r10
lea    0x30(%rsp),%r11
mov    %r14d,%edi
mov    %eax,%esi
mov    %rbx,%rdx
mov    %rbp,%rcx
mov    %r13d,%r8d
mov    $0x5,%r9d
push   %r10
push   %r11
call   1030 <EVP_BytesToKey@plt>
add    $0x10,%rsp
cmp    $0x20,%eax
jne    1226 <aes_init+0xb6>
mov    %r12,%rdi
call   1060 <EVP_CIPHER_CTX_init@plt>
xor    %eax,%eax
call   1040 <EVP_aes_256_cbc@plt>
lea    0x30(%rsp),%r14
lea    0x10(%rsp),%rbp
mov    %r12,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %r14,%rcx
mov    %rbp,%r8
call   1090 <EVP_EncryptInit_ex@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <EVP_CIPHER_CTX_init@plt>
xor    %eax,%eax
call   1040 <EVP_aes_256_cbc@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %r14,%rcx
mov    %rbp,%r8
call   1080 <EVP_DecryptInit_ex@plt>
jmp    1243 <aes_init+0xd3>
mov    0x2db3(%rip),%rcx
mov    (%rcx),%edi
lea    0xdca(%rip),%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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