#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*/ key;
typedef int /*<<< orphan*/ iv;
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*/ dump(char *, unsigned char *, int);
int /*<<< orphan*/ printf(char *, int);
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 i, nrounds = 100;
  unsigned char key[32], iv[16];
  i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt, key_data,
                     key_data_len, nrounds, key, iv);
  if (i != 32) {
    printf("Key size is %d bits - should be 256 bits\n", i);
    return -1;
  }
  dump("key", key, sizeof(key));
  dump("iv", iv, sizeof(iv));
  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    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x64,-0x38(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    %eax,-0x74(%rbp)
mov    $0x0,%al
call   1050 <EVP_sha1@plt>
mov    -0x74(%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    -0x70(%rbp),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1030 <EVP_BytesToKey@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x20,-0x34(%rbp)
je     1209 <aes_init+0x89>
mov    -0x34(%rbp),%esi
lea    0xe08(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <aes_init+0x11e>
lea    -0x60(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x20,%edx
call   1080 <dump@plt>
lea    -0x70(%rbp),%rsi
lea    0xe05(%rip),%rdi
mov    $0x10,%edx
call   1080 <dump@plt>
mov    -0x28(%rbp),%rdi
call   1070 <EVP_CIPHER_CTX_init@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    -0x88(%rbp),%rdi
mov    %eax,%esi
lea    -0x60(%rbp),%rcx
lea    -0x70(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <EVP_EncryptInit_ex@plt>
mov    -0x30(%rbp),%rdi
call   1070 <EVP_CIPHER_CTX_init@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    -0x80(%rbp),%rdi
mov    %eax,%esi
lea    -0x60(%rbp),%rcx
lea    -0x70(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1090 <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    %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    0x20(%rsp),%r11
mov    %r14d,%edi
mov    %eax,%esi
mov    %rbx,%rdx
mov    %rbp,%rcx
mov    %r13d,%r8d
mov    $0x64,%r9d
push   %r10
push   %r11
call   1030 <EVP_BytesToKey@plt>
add    $0x10,%rsp
cmp    $0x20,%eax
jne    125e <aes_init+0xde>
lea    0xe40(%rip),%rdi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x20,%edx
call   1080 <dump@plt>
lea    0xe2b(%rip),%rdi
lea    0x10(%rsp),%r13
mov    %r13,%rsi
mov    $0x10,%edx
call   1080 <dump@plt>
mov    %r12,%rdi
call   1070 <EVP_CIPHER_CTX_init@plt>
xor    %eax,%eax
call   1040 <EVP_aes_256_cbc@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %r13,%r8
call   10a0 <EVP_EncryptInit_ex@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <EVP_CIPHER_CTX_init@plt>
xor    %eax,%eax
call   1040 <EVP_aes_256_cbc@plt>
mov    %rbp,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %r13,%r8
call   1090 <EVP_DecryptInit_ex@plt>
jmp    1272 <aes_init+0xf2>
lea    0xd9b(%rip),%rdi
mov    %eax,%esi
call   1060 <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"
@.str.1 = [4 x i8] c"key\00"
@.str.2 = [3 x i8] c"iv\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 [16 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 100, 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 [16 x i8], [16 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 %30
27: ; preds = %5
%28 = load i32, i32* %12
%29 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %28)
store i32 -1, i32* %6
br label %49
30: ; preds = %5
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%32 = call i32 @dump(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %31, i32 32)
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%34 = call i32 @dump(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %33, i32 16)
%35 = load i32*, i32** %10
%36 = call i32 @EVP_CIPHER_CTX_init(i32* %35)
%37 = load i32*, i32** %10
%38 = call i32 (...) @EVP_aes_256_cbc()
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%41 = call i32 @EVP_EncryptInit_ex(i32* %37, i32 %38, i32* null, i8* %39, i8* %40)
%42 = load i32*, i32** %11
%43 = call i32 @EVP_CIPHER_CTX_init(i32* %42)
%44 = load i32*, i32** %11
%45 = call i32 (...) @EVP_aes_256_cbc()
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%48 = call i32 @EVP_DecryptInit_ex(i32* %44, i32 %45, i32* null, i8* %46, i8* %47)
store i32 0, i32* %6
br label %49
49: ; preds = %30, %27
%50 = load i32, i32* %6
ret i32 %50
}

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