#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*/ uint8_t;
typedef int intmax_t;
typedef int /*<<< orphan*/ EVP_CIPHER_CTX;

/* Variables and functions */
int EVP_BytesToKey(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_CIPHER_CTX_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DecryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_aes_256_cbc();
int /*<<< orphan*/ EVP_sha1();
int /*<<< orphan*/ printf(char *, int);
intmax_t aes_init(uint8_t *key_data, intmax_t key_data_len, uint8_t *salt,
                  EVP_CIPHER_CTX *e_ctx, EVP_CIPHER_CTX *d_ctx) {
  intmax_t i, nrounds = 5;
  uint8_t key[32], iv[32];
  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", (int)i);
    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    $0x170,%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   1040 <EVP_aes_256_cbc@plt>
mov    %eax,-0x144(%rbp)
mov    $0x0,%al
call   1050 <EVP_sha1@plt>
mov    -0x144(%rbp),%edi
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    -0x38(%rbp),%r9d
lea    -0xc0(%rbp),%r10
lea    -0x140(%rbp),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1030 <EVP_BytesToKey@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x20,-0x34(%rbp)
je     1205 <aes_init+0x95>
mov    -0x34(%rbp),%esi
lea    0xe0c(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <aes_init+0x112>
mov    -0x28(%rbp),%rdi
call   1070 <EVP_CIPHER_CTX_init@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x158(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    -0x158(%rbp),%rdi
mov    %eax,%esi
lea    -0xc0(%rbp),%rcx
lea    -0x140(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1090 <EVP_EncryptInit_ex@plt>
mov    -0x30(%rbp),%rdi
call   1070 <EVP_CIPHER_CTX_init@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x150(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_256_cbc@plt>
mov    -0x150(%rbp),%rdi
mov    %eax,%esi
lea    -0xc0(%rbp),%rcx
lea    -0x140(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1080 <EVP_DecryptInit_ex@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%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    0x90(%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    122f <aes_init+0xbf>
mov    %r12,%rdi
call   1070 <EVP_CIPHER_CTX_init@plt>
xor    %eax,%eax
call   1040 <EVP_aes_256_cbc@plt>
lea    0x90(%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   1070 <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>
lea    0xdca(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x118,%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, i32* %2, i32* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca [32 x i32]
%15 = alloca [32 x i32]
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store i32* %4, i32** %11
store i32 5, i32* %13
%16 = call i32 (...) @EVP_aes_256_cbc()
%17 = call i32 (...) @EVP_sha1()
%18 = load i32*, i32** %9
%19 = load i32*, i32** %7
%20 = load i32, i32* %8
%21 = load i32, i32* %13
%22 = getelementptr inbounds [32 x i32], [32 x i32]* %14, i64 0, i64 0
%23 = getelementptr inbounds [32 x i32], [32 x i32]* %15, i64 0, i64 0
%24 = call i32 @EVP_BytesToKey(i32 %16, i32 %17, i32* %18, i32* %19, i32 %20, i32 %21, i32* %22, i32* %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 %45
30: ; preds = %5
%31 = load i32*, i32** %10
%32 = call i32 @EVP_CIPHER_CTX_init(i32* %31)
%33 = load i32*, i32** %10
%34 = call i32 (...) @EVP_aes_256_cbc()
%35 = getelementptr inbounds [32 x i32], [32 x i32]* %14, i64 0, i64 0
%36 = getelementptr inbounds [32 x i32], [32 x i32]* %15, i64 0, i64 0
%37 = call i32 @EVP_EncryptInit_ex(i32* %33, i32 %34, i32* null, i32* %35, i32* %36)
%38 = load i32*, i32** %11
%39 = call i32 @EVP_CIPHER_CTX_init(i32* %38)
%40 = load i32*, i32** %11
%41 = call i32 (...) @EVP_aes_256_cbc()
%42 = getelementptr inbounds [32 x i32], [32 x i32]* %14, i64 0, i64 0
%43 = getelementptr inbounds [32 x i32], [32 x i32]* %15, i64 0, i64 0
%44 = call i32 @EVP_DecryptInit_ex(i32* %40, i32 %41, i32* null, i32* %42, i32* %43)
store i32 0, i32* %6
br label %45
45: ; preds = %30, %27
%46 = load i32, i32* %6
ret i32 %46
}

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, i32* %2, i32* %3, i32* %4) {
%6 = alloca [32 x i32]
%7 = alloca [32 x i32]
%8 = bitcast [32 x i32]* %6 to i8*
%9 = bitcast [32 x i32]* %7 to i8*
%10 = tail call i32 (...) @EVP_aes_256_cbc()
%11 = tail call i32 (...) @EVP_sha1()
%12 = getelementptr inbounds [32 x i32], [32 x i32]* %6, i64 0, i64 0
%13 = getelementptr inbounds [32 x i32], [32 x i32]* %7, i64 0, i64 0
%14 = call i32 @EVP_BytesToKey(i32 %10, i32 %11, i32* %2, i32* %0, i32 %1, i32 5, i32* %12, i32* %13)
%15 = icmp eq i32 %14, 32
br i1 %15, label %18, label %16
16: ; preds = %5
%17 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %25
18: ; preds = %5
%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, i32* %12, i32* %13)
%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, i32* %12, i32* %13)
br label %25
25: ; preds = %18, %16
%26 = phi i32 [ -1, %16 ], [ 0, %18 ]
ret i32 %26
}