#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_CIPHER_CTX_set_padding(int /*<<< orphan*/ *, int /*<<< orphan*/);
int EVP_EncryptFinal_ex(int /*<<< orphan*/ *, unsigned char *, int *);
int EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/ *, unsigned char *,
                       int /*<<< orphan*/ *);
int EVP_EncryptUpdate(int /*<<< orphan*/ *, unsigned char *, int *,
                      unsigned char *, int);
int /*<<< orphan*/ EVP_aes_128_ecb();
int /*<<< orphan*/ handleErrors();
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
            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_128_ecb(), NULL, key, NULL))
    handleErrors();
  if (1 != EVP_CIPHER_CTX_set_padding(ctx, 0))
    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    $0x0,%al
call   10a0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b3 <encrypt+0x33>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1060 <EVP_aes_128_ecb@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
call   1090 <EVP_EncryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     11ef <encrypt+0x6f>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
call   1070 <EVP_CIPHER_CTX_set_padding@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1210 <encrypt+0x90>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
lea    -0x2c(%rbp),%rdx
call   1040 <EVP_EncryptUpdate@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     123f <encrypt+0xbf>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <EVP_EncryptFinal_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1273 <encrypt+0xf3>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <EVP_CIPHER_CTX_free@plt>
mov    -0x30(%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    %rcx,%r14
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %rdi,%r12
xor    %eax,%eax
call   10a0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11ad <encrypt+0x2d>
xor    %eax,%eax
call   1050 <handleErrors@plt>
xor    %eax,%eax
call   1060 <EVP_aes_128_ecb@plt>
mov    %r13,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbp,%rcx
xor    %r8d,%r8d
call   1090 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
je     11d2 <encrypt+0x52>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    %r13,%rdi
xor    %esi,%esi
call   1070 <EVP_CIPHER_CTX_set_padding@plt>
cmp    $0x1,%eax
je     11e8 <encrypt+0x68>
xor    %eax,%eax
call   1050 <handleErrors@plt>
lea    0x4(%rsp),%rdx
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r12,%rcx
mov    %r15d,%r8d
call   1040 <EVP_EncryptUpdate@plt>
cmp    $0x1,%eax
je     120a <encrypt+0x8a>
xor    %eax,%eax
call   1050 <handleErrors@plt>
movslq 0x4(%rsp),%rbx
add    %rbx,%r14
lea    0x4(%rsp),%rdx
mov    %r13,%rdi
mov    %r14,%rsi
call   1080 <EVP_EncryptFinal_ex@plt>
cmp    $0x1,%eax
je     122e <encrypt+0xae>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    0x4(%rsp),%ebp
add    %ebx,%ebp
mov    %r13,%rdi
call   1030 <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) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%12 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %12, i32** %9
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %4
%15 = call i32 (...) @handleErrors()
br label %16
16: ; preds = %14, %4
%17 = load i32*, i32** %9
%18 = call i32 (...) @EVP_aes_128_ecb()
%19 = load i8*, i8** %7
%20 = call i32 @EVP_EncryptInit_ex(i32* %17, i32 %18, i32* null, i8* %19, i32* null)
%21 = icmp ne i32 1, %20
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 (...) @handleErrors()
br label %24
24: ; preds = %22, %16
%25 = load i32*, i32** %9
%26 = call i32 @EVP_CIPHER_CTX_set_padding(i32* %25, i32 0)
%27 = icmp ne i32 1, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 (...) @handleErrors()
br label %30
30: ; preds = %28, %24
%31 = load i32*, i32** %9
%32 = load i8*, i8** %8
%33 = load i8*, i8** %5
%34 = load i32, i32* %6
%35 = call i32 @EVP_EncryptUpdate(i32* %31, i8* %32, i32* %10, i8* %33, i32 %34)
%36 = icmp ne i32 1, %35
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 (...) @handleErrors()
br label %39
39: ; preds = %37, %30
%40 = load i32, i32* %10
store i32 %40, i32* %11
%41 = load i32*, i32** %9
%42 = load i8*, i8** %8
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = call i32 @EVP_EncryptFinal_ex(i32* %41, i8* %45, i32* %10)
%47 = icmp ne i32 1, %46
br i1 %47, label %48, label %50
48: ; preds = %39
%49 = call i32 (...) @handleErrors()
br label %50
50: ; preds = %48, %39
%51 = load i32, i32* %10
%52 = load i32, i32* %11
%53 = add nsw i32 %52, %51
store i32 %53, i32* %11
%54 = load i32*, i32** %9
%55 = call i32 @EVP_CIPHER_CTX_free(i32* %54)
%56 = load i32, i32* %11
ret i32 %56
}

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