#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 /*<<< orphan*/ EVP_EncryptFinal_ex(int /*<<< orphan*/ *, unsigned char *,
                                       int *);
int /*<<< orphan*/ EVP_EncryptInit_ex(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_EncryptUpdate(int /*<<< orphan*/ *, unsigned char *,
                                     int *, unsigned char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
unsigned char *aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext,
                           int *len) {
  int c_len = *len + 256 - 1, f_len = 0;
  unsigned char *ciphertext = (unsigned char *)malloc(c_len);
  if (!EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL)) {
    printf("ERROR in EVP_EncryptInit_ex \n");
    return NULL;
  }
  if (!EVP_EncryptUpdate(e, ciphertext, &c_len, plaintext, *len)) {
    printf("ERROR in EVP_EncryptUpdate \n");
    return NULL;
  }
  if (!EVP_EncryptFinal_ex(e, ciphertext + c_len, &f_len)) {
    printf("ERROR in EVP_EncryptFinal_ex \n");
    return NULL;
  }
  *len = c_len + f_len;
  return ciphertext;
}


x86 assembly (clang, -O0):
<aes_encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    $0x100,%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rsi
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <EVP_EncryptInit_ex@plt>
cmp    $0x0,%eax
jne    11c1 <aes_encrypt+0x71>
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    124d <aes_encrypt+0xfd>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8d
lea    -0x24(%rbp),%rdx
call   1040 <EVP_EncryptUpdate@plt>
cmp    $0x0,%eax
jne    11ff <aes_encrypt+0xaf>
lea    0xe31(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    124d <aes_encrypt+0xfd>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
lea    -0x28(%rbp),%rdx
call   1060 <EVP_EncryptFinal_ex@plt>
cmp    $0x0,%eax
jne    1239 <aes_encrypt+0xe9>
lea    0xe14(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    124d <aes_encrypt+0xfd>
mov    -0x24(%rbp),%ecx
add    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_encrypt>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
mov    $0xff,%edi
add    (%rdx),%edi
mov    %edi,(%rsp)
movl   $0x0,0x4(%rsp)
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <EVP_EncryptInit_ex@plt>
test   %eax,%eax
je     11cf <aes_encrypt+0x7f>
mov    (%r14),%r8d
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rcx
call   1040 <EVP_EncryptUpdate@plt>
test   %eax,%eax
je     11d8 <aes_encrypt+0x88>
movslq (%rsp),%rax
mov    %r15,%rsi
add    %rax,%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1060 <EVP_EncryptFinal_ex@plt>
test   %eax,%eax
je     11e1 <aes_encrypt+0x91>
mov    0x4(%rsp),%eax
add    (%rsp),%eax
mov    %eax,(%r14)
jmp    11f0 <aes_encrypt+0xa0>
lea    0xe2a(%rip),%rdi
jmp    11e8 <aes_encrypt+0x98>
lea    0xe3e(%rip),%rdi
jmp    11e8 <aes_encrypt+0x98>
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [30 x i8] c"ERROR in EVP_EncryptInit_ex \0A\00"
@.str.1 = [29 x i8] c"ERROR in EVP_EncryptUpdate \0A\00"
@.str.2 = [31 x i8] c"ERROR in EVP_EncryptFinal_ex \0A\00"
define i8* @aes_encrypt(i32* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%11 = load i32*, i32** %7
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 256
%14 = sub nsw i32 %13, 1
store i32 %14, i32* %8
store i32 0, i32* %9
%15 = load i32, i32* %8
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %10
%18 = load i32*, i32** %5
%19 = call i32 @EVP_EncryptInit_ex(i32* %18, i32* null, i32* null, i32* null, i32* null)
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %3
%22 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %49
23: ; preds = %3
%24 = load i32*, i32** %5
%25 = load i8*, i8** %10
%26 = load i8*, i8** %6
%27 = load i32*, i32** %7
%28 = load i32, i32* %27
%29 = call i32 @EVP_EncryptUpdate(i32* %24, i8* %25, i32* %8, i8* %26, i32 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %23
%32 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %4
br label %49
33: ; preds = %23
%34 = load i32*, i32** %5
%35 = load i8*, i8** %10
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = call i32 @EVP_EncryptFinal_ex(i32* %34, i8* %38, i32* %9)
%40 = icmp ne i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %33
%42 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %4
br label %49
43: ; preds = %33
%44 = load i32, i32* %8
%45 = load i32, i32* %9
%46 = add nsw i32 %44, %45
%47 = load i32*, i32** %7
store i32 %46, i32* %47
%48 = load i8*, i8** %10
store i8* %48, i8** %4
br label %49
49: ; preds = %43, %41, %31, %21
%50 = load i8*, i8** %4
ret i8* %50
}

LLVM IR (-O2):
@str = [29 x i8] c"ERROR in EVP_EncryptInit_ex \00"
@str.3 = [28 x i8] c"ERROR in EVP_EncryptUpdate \00"
@str.4 = [30 x i8] c"ERROR in EVP_EncryptFinal_ex \00"
define i8* @aes_encrypt(i32* %0, i8* %1, i32* nocapture %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* %2
%8 = add nsw i32 %7, 255
store i32 %8, i32* %4
%9 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%10 = tail call i64 @malloc(i32 %8)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @EVP_EncryptInit_ex(i32* %0, i32* null, i32* null, i32* null, i32* null)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %34
16: ; preds = %3
%17 = load i32, i32* %2
%18 = call i32 @EVP_EncryptUpdate(i32* %0, i8* %11, i32* %4, i8* %1, i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.3, i64 0, i64 0))
br label %34
22: ; preds = %16
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %11, i64 %24
%26 = call i32 @EVP_EncryptFinal_ex(i32* %0, i8* %25, i32* %5)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.4, i64 0, i64 0))
br label %34
30: ; preds = %22
%31 = load i32, i32* %4
%32 = load i32, i32* %5
%33 = add nsw i32 %32, %31
store i32 %33, i32* %2
br label %34
34: ; preds = %30, %28, %20, %14
%35 = phi i8* [ %11, %30 ], [ null, %28 ], [ null, %20 ], [ null, %14 ]
ret i8* %35
}