#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;
typedef int /*<<< orphan*/ ENGINE;

/* Variables and functions */
int /*<<< orphan*/ ENGINE_METHOD_ALL;
int /*<<< orphan*/ *ENGINE_by_id(char *);
int /*<<< orphan*/ ENGINE_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ ENGINE_load_builtin_engines();
int /*<<< orphan*/ ENGINE_set_default(int /*<<< orphan*/ *, int /*<<< orphan*/);
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 /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
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 = 5;
  unsigned char key[32], iv[32];
  ENGINE *e;
  ENGINE_load_builtin_engines();
  if (!(e = ENGINE_by_id("cryptodev")))
    fprintf(stderr, "Error finding specified ENGINE\n");
  else if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
    fprintf(stderr, "Error using ENGINE\n");
  else
    fprintf(stderr, "Engine successfully enabled\n");
  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;
  }
  EVP_CIPHER_CTX_init(e_ctx);
  EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), e, key, iv);
  EVP_CIPHER_CTX_init(d_ctx);
  EVP_DecryptInit_ex(d_ctx, EVP_aes_256_cbc(), e, key, iv);
  ENGINE_free(e);
  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   $0x5,-0x38(%rbp)
mov    $0x0,%al
call   10d0 <ENGINE_load_builtin_engines@plt>
lea    0xe0d(%rip),%rdi
call   10e0 <ENGINE_by_id@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    1223 <aes_init+0x63>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    1275 <aes_init+0xb5>
mov    -0x88(%rbp),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
call   1040 <ENGINE_set_default@plt>
cmp    $0x0,%eax
jne    125b <aes_init+0x9b>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    1270 <aes_init+0xb0>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    1275 <aes_init+0xb5>
mov    $0x0,%al
call   1060 <EVP_aes_256_cbc@plt>
mov    %eax,-0x8c(%rbp)
mov    $0x0,%al
call   1070 <EVP_sha1@plt>
mov    -0x8c(%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   1050 <EVP_BytesToKey@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x20,-0x34(%rbp)
je     12df <aes_init+0x11f>
mov    -0x34(%rbp),%esi
lea    0xd8d(%rip),%rdi
call   1080 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1362 <aes_init+0x1a2>
mov    -0x28(%rbp),%rdi
call   1090 <EVP_CIPHER_CTX_init@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    $0x0,%al
call   1060 <EVP_aes_256_cbc@plt>
mov    -0xa0(%rbp),%rdi
mov    %eax,%esi
mov    -0x88(%rbp),%rdx
lea    -0x60(%rbp),%rcx
lea    -0x80(%rbp),%r8
call   10c0 <EVP_EncryptInit_ex@plt>
mov    -0x30(%rbp),%rdi
call   1090 <EVP_CIPHER_CTX_init@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    $0x0,%al
call   1060 <EVP_aes_256_cbc@plt>
mov    -0x98(%rbp),%rdi
mov    %eax,%esi
mov    -0x88(%rbp),%rdx
lea    -0x60(%rbp),%rcx
lea    -0x80(%rbp),%r8
call   10b0 <EVP_DecryptInit_ex@plt>
mov    -0x88(%rbp),%rdi
call   1030 <ENGINE_free@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    $0x68,%rsp
mov    %r8,%r15
mov    %rcx,%r12
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %eax,%eax
call   10d0 <ENGINE_load_builtin_engines@plt>
lea    0xe15(%rip),%rdi
call   10e0 <ENGINE_by_id@plt>
mov    %rax,%r13
test   %rax,%rax
mov    %r15,0x8(%rsp)
mov    %rbp,0x18(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r12,%rbx
je     1231 <aes_init+0x71>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1040 <ENGINE_set_default@plt>
mov    0x2db6(%rip),%rcx
mov    (%rcx),%edi
test   %eax,%eax
je     1243 <aes_init+0x83>
lea    0xe0f(%rip),%rsi
jmp    124a <aes_init+0x8a>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
jmp    124a <aes_init+0x8a>
lea    0xde0(%rip),%rsi
call   10a0 <fprintf@plt>
xor    %r12d,%r12d
xor    %eax,%eax
call   1060 <EVP_aes_256_cbc@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1070 <EVP_sha1@plt>
lea    0x20(%rsp),%rbp
lea    0x40(%rsp),%r10
mov    %r15d,%edi
mov    %eax,%esi
mov    0x18(%rsp),%rdx
mov    0x10(%rsp),%rcx
mov    %r14d,%r8d
mov    $0x5,%r9d
push   %rbp
push   %r10
call   1050 <EVP_BytesToKey@plt>
add    $0x10,%rsp
cmp    $0x20,%eax
jne    12f3 <aes_init+0x133>
mov    %rbx,%rdi
call   1090 <EVP_CIPHER_CTX_init@plt>
xor    %eax,%eax
call   1060 <EVP_aes_256_cbc@plt>
lea    0x40(%rsp),%r14
lea    0x20(%rsp),%r15
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r13,%rdx
mov    %r14,%rcx
mov    %r15,%r8
call   10c0 <EVP_EncryptInit_ex@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <EVP_CIPHER_CTX_init@plt>
xor    %eax,%eax
call   1060 <EVP_aes_256_cbc@plt>
mov    %rbp,%rdi
mov    %eax,%esi
mov    %r13,%rdx
mov    %r14,%rcx
mov    %r15,%r8
call   10b0 <EVP_DecryptInit_ex@plt>
mov    %r13,%rdi
call   1030 <ENGINE_free@plt>
jmp    1307 <aes_init+0x147>
lea    0xd61(%rip),%rdi
mov    %eax,%esi
call   1080 <printf@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"cryptodev\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Error finding specified ENGINE\0A\00"
@ENGINE_METHOD_ALL = global i32 0
@.str.2 = [20 x i8] c"Error using ENGINE\0A\00"
@.str.3 = [29 x i8] c"Engine successfully enabled\0A\00"
@.str.4 = [42 x i8] c"Key size is %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]
%16 = alloca i32*
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 5, i32* %13
%17 = call i32 (...) @ENGINE_load_builtin_engines()
%18 = call i32* @ENGINE_by_id(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %16
%19 = icmp ne i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %5
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %35
23: ; preds = %5
%24 = load i32*, i32** %16
%25 = load i32, i32* @ENGINE_METHOD_ALL
%26 = call i32 @ENGINE_set_default(i32* %24, i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %34
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %31, %28
br label %35
35: ; preds = %34, %20
%36 = call i32 (...) @EVP_aes_256_cbc()
%37 = call i32 (...) @EVP_sha1()
%38 = load i8*, i8** %9
%39 = load i8*, i8** %7
%40 = load i32, i32* %8
%41 = load i32, i32* %13
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %15, i64 0, i64 0
%44 = call i32 @EVP_BytesToKey(i32 %36, i32 %37, i8* %38, i8* %39, i32 %40, i32 %41, i8* %42, i8* %43)
store i32 %44, i32* %12
%45 = load i32, i32* %12
%46 = icmp ne i32 %45, 32
br i1 %46, label %47, label %50
47: ; preds = %35
%48 = load i32, i32* %12
%49 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %48)
store i32 -1, i32* %6
br label %69
50: ; preds = %35
%51 = load i32*, i32** %10
%52 = call i32 @EVP_CIPHER_CTX_init(i32* %51)
%53 = load i32*, i32** %10
%54 = call i32 (...) @EVP_aes_256_cbc()
%55 = load i32*, i32** %16
%56 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%57 = getelementptr inbounds [32 x i8], [32 x i8]* %15, i64 0, i64 0
%58 = call i32 @EVP_EncryptInit_ex(i32* %53, i32 %54, i32* %55, i8* %56, i8* %57)
%59 = load i32*, i32** %11
%60 = call i32 @EVP_CIPHER_CTX_init(i32* %59)
%61 = load i32*, i32** %11
%62 = call i32 (...) @EVP_aes_256_cbc()
%63 = load i32*, i32** %16
%64 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%65 = getelementptr inbounds [32 x i8], [32 x i8]* %15, i64 0, i64 0
%66 = call i32 @EVP_DecryptInit_ex(i32* %61, i32 %62, i32* %63, i8* %64, i8* %65)
%67 = load i32*, i32** %16
%68 = call i32 @ENGINE_free(i32* %67)
store i32 0, i32* %6
br label %69
69: ; preds = %50, %47
%70 = load i32, i32* %6
ret i32 %70
}

LLVM IR (-O2):
@.str = [10 x i8] c"cryptodev\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Error finding specified ENGINE\0A\00"
@ENGINE_METHOD_ALL = global i32 0
@.str.2 = [20 x i8] c"Error using ENGINE\0A\00"
@.str.3 = [29 x i8] c"Engine successfully enabled\0A\00"
@.str.4 = [42 x i8] c"Key size is %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 (...) @ENGINE_load_builtin_engines()
%11 = tail call i32* @ENGINE_by_id(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %25
16: ; preds = %5
%17 = load i32, i32* @ENGINE_METHOD_ALL
%18 = tail call i32 @ENGINE_set_default(i32* %11, i32 %17)
%19 = icmp eq i32 %18, 0
%20 = load i32, i32* @stderr
br i1 %19, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %25
23: ; preds = %16
%24 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %21, %23, %13
%26 = tail call i32 (...) @EVP_aes_256_cbc()
%27 = tail call i32 (...) @EVP_sha1()
%28 = call i32 @EVP_BytesToKey(i32 %26, i32 %27, i8* %2, i8* %0, i32 %1, i32 5, i8* %8, i8* %9)
%29 = icmp eq i32 %28, 32
br i1 %29, label %32, label %30
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %28)
br label %40
32: ; preds = %25
%33 = call i32 @EVP_CIPHER_CTX_init(i32* %3)
%34 = call i32 (...) @EVP_aes_256_cbc()
%35 = call i32 @EVP_EncryptInit_ex(i32* %3, i32 %34, i32* %11, i8* %8, i8* %9)
%36 = call i32 @EVP_CIPHER_CTX_init(i32* %4)
%37 = call i32 (...) @EVP_aes_256_cbc()
%38 = call i32 @EVP_DecryptInit_ex(i32* %4, i32 %37, i32* %11, i8* %8, i8* %9)
%39 = call i32 @ENGINE_free(i32* %11)
br label %40
40: ; preds = %32, %30
%41 = phi i32 [ -1, %30 ], [ 0, %32 ]
ret i32 %41
}