#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_CIPHER_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_CIPHER_CTX_new();
int EVP_EncryptFinal_ex(int /*<<< orphan*/ *, unsigned char *, int *);
int EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/ *, unsigned char *, unsigned char *);
int EVP_EncryptUpdate(int /*<<< orphan*/ *, unsigned char *, int *,
                      unsigned char *, int);
int /*<<< orphan*/ EVP_aes_256_cbc();
int /*<<< orphan*/ handleErrors();
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
            unsigned char *iv, unsigned char *ciphertext) {
  EVP_CIPHER_CTX *ctx;
  int len;
  int ciphertext_len;
  if (!(ctx = EVP_CIPHER_CTX_new()))
    handleErrors();
  if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
    handleErrors();
  if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
    handleErrors();
  ciphertext_len = len;
  if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
    handleErrors();
  ciphertext_len += len;
  EVP_CIPHER_CTX_free(ctx);
  return ciphertext_len;
}


x86 assembly (clang, -O0):
<encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    $0x0,%al
call   1090 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11a7 <encrypt+0x37>
mov    $0x0,%al
call   1060 <handleErrors@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <EVP_aes_256_cbc@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1080 <EVP_EncryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     11e3 <encrypt+0x73>
mov    $0x0,%al
call   1060 <handleErrors@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
lea    -0x34(%rbp),%rdx
call   1050 <EVP_EncryptUpdate@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1212 <encrypt+0xa2>
mov    $0x0,%al
call   1060 <handleErrors@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x34(%rbp),%rax
add    %rax,%rsi
lea    -0x34(%rbp),%rdx
call   1070 <EVP_EncryptFinal_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1246 <encrypt+0xd6>
mov    $0x0,%al
call   1060 <handleErrors@plt>
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
mov    -0x38(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r13
mov    %rdx,%rbp
mov    %esi,%ebx
mov    %rdi,%r12
xor    %eax,%eax
call   1090 <EVP_CIPHER_CTX_new@plt>
mov    %rax,%r15
test   %rax,%rax
jne    119f <encrypt+0x2f>
xor    %eax,%eax
call   1060 <handleErrors@plt>
xor    %eax,%eax
call   1030 <EVP_aes_256_cbc@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbp,%rcx
mov    %r13,%r8
call   1080 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
je     11c4 <encrypt+0x54>
xor    %eax,%eax
call   1060 <handleErrors@plt>
lea    0x4(%rsp),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rcx
mov    %ebx,%r8d
call   1050 <EVP_EncryptUpdate@plt>
cmp    $0x1,%eax
je     11e6 <encrypt+0x76>
xor    %eax,%eax
call   1060 <handleErrors@plt>
movslq 0x4(%rsp),%rbx
add    %rbx,%r14
lea    0x4(%rsp),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <EVP_EncryptFinal_ex@plt>
cmp    $0x1,%eax
je     120a <encrypt+0x9a>
xor    %eax,%eax
call   1060 <handleErrors@plt>
mov    0x4(%rsp),%ebp
add    %ebx,%ebp
mov    %r15,%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @encrypt(i8* %0, i32 %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%14 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %14, i32** %11
%15 = icmp ne i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %5
%17 = call i32 (...) @handleErrors()
br label %18
18: ; preds = %16, %5
%19 = load i32*, i32** %11
%20 = call i32 (...) @EVP_aes_256_cbc()
%21 = load i8*, i8** %8
%22 = load i8*, i8** %9
%23 = call i32 @EVP_EncryptInit_ex(i32* %19, i32 %20, i32* null, i8* %21, i8* %22)
%24 = icmp ne i32 1, %23
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 (...) @handleErrors()
br label %27
27: ; preds = %25, %18
%28 = load i32*, i32** %11
%29 = load i8*, i8** %10
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = call i32 @EVP_EncryptUpdate(i32* %28, i8* %29, i32* %12, i8* %30, i32 %31)
%33 = icmp ne i32 1, %32
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 (...) @handleErrors()
br label %36
36: ; preds = %34, %27
%37 = load i32, i32* %12
store i32 %37, i32* %13
%38 = load i32*, i32** %11
%39 = load i8*, i8** %10
%40 = load i32, i32* %12
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = call i32 @EVP_EncryptFinal_ex(i32* %38, i8* %42, i32* %12)
%44 = icmp ne i32 1, %43
br i1 %44, label %45, label %47
45: ; preds = %36
%46 = call i32 (...) @handleErrors()
br label %47
47: ; preds = %45, %36
%48 = load i32, i32* %12
%49 = load i32, i32* %13
%50 = add nsw i32 %49, %48
store i32 %50, i32* %13
%51 = load i32*, i32** %11
%52 = call i32 @EVP_CIPHER_CTX_free(i32* %51)
%53 = load i32, i32* %13
ret i32 %53
}

LLVM IR (-O2):
define i32 @encrypt(i8* %0, i32 %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = tail call i32* (...) @EVP_CIPHER_CTX_new()
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 (...) @handleErrors()
br label %12
12: ; preds = %10, %5
%13 = tail call i32 (...) @EVP_aes_256_cbc()
%14 = tail call i32 @EVP_EncryptInit_ex(i32* %8, i32 %13, i32* null, i8* %2, i8* %3)
%15 = icmp eq i32 %14, 1
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 (...) @handleErrors()
br label %18
18: ; preds = %16, %12
%19 = call i32 @EVP_EncryptUpdate(i32* %8, i8* %4, i32* %6, i8* %0, i32 %1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 (...) @handleErrors()
br label %23
23: ; preds = %21, %18
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %4, i64 %25
%27 = call i32 @EVP_EncryptFinal_ex(i32* %8, i8* %26, i32* %6)
%28 = icmp eq i32 %27, 1
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = call i32 (...) @handleErrors()
br label %31
31: ; preds = %29, %23
%32 = load i32, i32* %6
%33 = add nsw i32 %32, %24
%34 = call i32 @EVP_CIPHER_CTX_free(i32* %8)
ret i32 %33
}