#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_cast5_ecb();
int /*<<< orphan*/ handleErrors();
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
            unsigned char *iv, unsigned char *plaintext) {
  EVP_CIPHER_CTX *ctx;
  int plaintext_len;
  int len;
  if (!(ctx = EVP_CIPHER_CTX_new()))
    handleErrors();
  if (1 != EVP_DecryptInit_ex(ctx, EVP_cast5_ecb(), 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)
cmp    $0x0,%rax
jne    11a7 <decrypt+0x37>
mov    $0x0,%al
call   1060 <handleErrors@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <EVP_cast5_ecb@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     11e3 <decrypt+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    -0x38(%rbp),%rdx
call   1090 <EVP_DecryptUpdate@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1212 <decrypt+0xa2>
mov    $0x0,%al
call   1060 <handleErrors@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x38(%rbp),%rax
add    %rax,%rsi
lea    -0x38(%rbp),%rdx
call   1030 <EVP_DecryptFinal_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1246 <decrypt+0xd6>
mov    $0x0,%al
call   1060 <handleErrors@plt>
mov    -0x38(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <EVP_CIPHER_CTX_free@plt>
mov    -0x34(%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>
xor    %eax,%eax
call   1040 <EVP_cast5_ecb@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     11c4 <decrypt+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   1090 <EVP_DecryptUpdate@plt>
cmp    $0x1,%eax
je     11e6 <decrypt+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   1030 <EVP_DecryptFinal_ex@plt>
cmp    $0x1,%eax
je     120a <decrypt+0x9a>
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 = 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_cast5_ecb()
%21 = load i8*, i8** %8
%22 = load i8*, i8** %9
%23 = call i32 @EVP_DecryptInit_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_DecryptUpdate(i32* %28, i8* %29, i32* %13, 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* %13
store i32 %37, i32* %12
%38 = load i32*, i32** %11
%39 = load i8*, i8** %10
%40 = load i32, i32* %13
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = call i32 @EVP_DecryptFinal_ex(i32* %38, i8* %42, i32* %13)
%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* %13
%49 = load i32, i32* %12
%50 = add nsw i32 %49, %48
store i32 %50, i32* %12
%51 = load i32*, i32** %11
%52 = call i32 @EVP_CIPHER_CTX_free(i32* %51)
%53 = load i32, i32* %12
ret i32 %53
}

LLVM IR (-O2):
define i32 @decrypt(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_cast5_ecb()
%14 = tail call i32 @EVP_DecryptInit_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_DecryptUpdate(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_DecryptFinal_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
}