#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*/ *, void *, int *);
int EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/ *, void *, void *);
int EVP_EncryptUpdate(int /*<<< orphan*/ *, void *, int *, void *, size_t);
int /*<<< orphan*/ EVP_aes_256_cbc();
int /*<<< orphan*/ secreteLibSSLError();
int encrypt(void *plaintext, size_t plaintext_len, void *key, void *iv,
            void *ciphertext) {
  EVP_CIPHER_CTX *ctx;
  int len;
  int ciphertext_len;
  if (!(ctx = EVP_CIPHER_CTX_new())) {
    secreteLibSSLError();
  }
  if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) {
    secreteLibSSLError();
  }
  if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) {
    secreteLibSSLError();
  }
  ciphertext_len = len;
  if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) {
    secreteLibSSLError();
  }
  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    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    $0x0,%al
call   1080 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11a8 <encrypt+0x38>
mov    $0x0,%al
call   1090 <secreteLibSSLError@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   1070 <EVP_EncryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     11e4 <encrypt+0x74>
mov    $0x0,%al
call   1090 <secreteLibSSLError@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    -0x34(%rbp),%rdx
call   1050 <EVP_EncryptUpdate@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1213 <encrypt+0xa3>
mov    $0x0,%al
call   1090 <secreteLibSSLError@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   1060 <EVP_EncryptFinal_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1247 <encrypt+0xd7>
mov    $0x0,%al
call   1090 <secreteLibSSLError@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    %rsi,%rbx
mov    %rdi,%r12
xor    %eax,%eax
call   1080 <EVP_CIPHER_CTX_new@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11a0 <encrypt+0x30>
xor    %eax,%eax
call   1090 <secreteLibSSLError@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   1070 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
je     11c5 <encrypt+0x55>
xor    %eax,%eax
call   1090 <secreteLibSSLError@plt>
lea    0x4(%rsp),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rcx
mov    %rbx,%r8
call   1050 <EVP_EncryptUpdate@plt>
cmp    $0x1,%eax
je     11e7 <encrypt+0x77>
xor    %eax,%eax
call   1090 <secreteLibSSLError@plt>
movslq 0x4(%rsp),%rbx
add    %rbx,%r14
lea    0x4(%rsp),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <EVP_EncryptFinal_ex@plt>
cmp    $0x1,%eax
je     120b <encrypt+0x9b>
xor    %eax,%eax
call   1090 <secreteLibSSLError@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, i64 %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %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 (...) @secreteLibSSLError()
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 (...) @secreteLibSSLError()
br label %27
27: ; preds = %25, %18
%28 = load i32*, i32** %11
%29 = load i8*, i8** %10
%30 = load i8*, i8** %6
%31 = load i64, i64* %7
%32 = call i32 @EVP_EncryptUpdate(i32* %28, i8* %29, i32* %12, i8* %30, i64 %31)
%33 = icmp ne i32 1, %32
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 (...) @secreteLibSSLError()
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 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 (...) @secreteLibSSLError()
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, i64 %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 (...) @secreteLibSSLError()
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 (...) @secreteLibSSLError()
br label %18
18: ; preds = %16, %12
%19 = call i32 @EVP_EncryptUpdate(i32* %8, i8* %4, i32* %6, i8* %0, i64 %1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 (...) @secreteLibSSLError()
br label %23
23: ; preds = %21, %18
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr 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 (...) @secreteLibSSLError()
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
}