#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_DecryptFinal_ex(int /*<<< orphan*/ *, unsigned char *, int *);
int EVP_DecryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/ *, unsigned char *, unsigned char *);
int EVP_DecryptUpdate(int /*<<< orphan*/ *, unsigned char *, int *,
                      unsigned char *, int);
int /*<<< orphan*/ EVP_aes_256_cbc();
int /*<<< orphan*/ handleErrors();
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
            unsigned char *iv, unsigned char *plaintext) {
  EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  if (ctx == NULL) {
    handleErrors();
  }
  int len = 0;
  int plaintext_len = 0;
  if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) {
    handleErrors();
  }
  if (1 !=
      EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) {
    handleErrors();
  }
  plaintext_len = len;
  if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) {
    handleErrors();
  }
  plaintext_len += len;
  EVP_CIPHER_CTX_free(ctx);
  return plaintext_len;
}


x86 assembly (clang, -O0):
<decrypt>:
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   1080 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a8 <decrypt+0x38>
mov    $0x0,%al
call   1060 <handleErrors@plt>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <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_DecryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     11f2 <decrypt+0x82>
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   1090 <EVP_DecryptUpdate@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1221 <decrypt+0xb1>
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   1030 <EVP_DecryptFinal_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1255 <decrypt+0xe5>
mov    $0x0,%al
call   1060 <handleErrors@plt>
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <EVP_CIPHER_CTX_free@plt>
mov    -0x38(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decrypt>:
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   1080 <EVP_CIPHER_CTX_new@plt>
mov    %rax,%r15
test   %rax,%rax
jne    119f <decrypt+0x2f>
xor    %eax,%eax
call   1060 <handleErrors@plt>
movl   $0x0,0x4(%rsp)
xor    %eax,%eax
call   1040 <EVP_aes_256_cbc@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbp,%rcx
mov    %r13,%r8
call   1070 <EVP_DecryptInit_ex@plt>
cmp    $0x1,%eax
je     11cc <decrypt+0x5c>
xor    %eax,%eax
call   1060 <handleErrors@plt>
lea    0x4(%rsp),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rcx
mov    %ebx,%r8d
call   1090 <EVP_DecryptUpdate@plt>
cmp    $0x1,%eax
je     11ee <decrypt+0x7e>
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   1030 <EVP_DecryptFinal_ex@plt>
cmp    $0x1,%eax
je     1212 <decrypt+0xa2>
xor    %eax,%eax
call   1060 <handleErrors@plt>
mov    0x4(%rsp),%ebp
add    %ebx,%ebp
mov    %r15,%rdi
call   1050 <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 @decrypt(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 = load i32*, i32** %11
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = call i32 (...) @handleErrors()
br label %19
19: ; preds = %17, %5
store i32 0, i32* %12
store i32 0, i32* %13
%20 = load i32*, i32** %11
%21 = call i32 (...) @EVP_aes_256_cbc()
%22 = load i8*, i8** %8
%23 = load i8*, i8** %9
%24 = call i32 @EVP_DecryptInit_ex(i32* %20, i32 %21, i32* null, i8* %22, i8* %23)
%25 = icmp ne i32 1, %24
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 (...) @handleErrors()
br label %28
28: ; preds = %26, %19
%29 = load i32*, i32** %11
%30 = load i8*, i8** %10
%31 = load i8*, i8** %6
%32 = load i32, i32* %7
%33 = call i32 @EVP_DecryptUpdate(i32* %29, i8* %30, i32* %12, i8* %31, i32 %32)
%34 = icmp ne i32 1, %33
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 (...) @handleErrors()
br label %37
37: ; preds = %35, %28
%38 = load i32, i32* %12
store i32 %38, i32* %13
%39 = load i32*, i32** %11
%40 = load i8*, i8** %10
%41 = load i32, i32* %12
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = call i32 @EVP_DecryptFinal_ex(i32* %39, i8* %43, i32* %12)
%45 = icmp ne i32 1, %44
br i1 %45, label %46, label %48
46: ; preds = %37
%47 = call i32 (...) @handleErrors()
br label %48
48: ; preds = %46, %37
%49 = load i32, i32* %12
%50 = load i32, i32* %13
%51 = add nsw i32 %50, %49
store i32 %51, i32* %13
%52 = load i32*, i32** %11
%53 = call i32 @EVP_CIPHER_CTX_free(i32* %52)
%54 = load i32, i32* %13
ret i32 %54
}

LLVM IR (-O2):
define i32 @decrypt(i8* %0, i32 %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = tail call i32* (...) @EVP_CIPHER_CTX_new()
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 (...) @handleErrors()
br label %11
11: ; preds = %9, %5
%12 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%13 = tail call i32 (...) @EVP_aes_256_cbc()
%14 = tail call i32 @EVP_DecryptInit_ex(i32* %7, i32 %13, i32* null, i8* %2, i8* %3)
%15 = icmp eq i32 %14, 1
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 (...) @handleErrors()
br label %18
18: ; preds = %16, %11
%19 = call i32 @EVP_DecryptUpdate(i32* %7, 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_DecryptFinal_ex(i32* %7, 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* %7)
ret i32 %33
}