#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_ctrl(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                       int, unsigned char *);
int /*<<< orphan*/ EVP_CIPHER_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_CIPHER_CTX_new();
int /*<<< orphan*/ EVP_CTRL_GCM_SET_IVLEN;
int /*<<< orphan*/ EVP_CTRL_GCM_SET_TAG;
int EVP_DecryptFinal_ex(int /*<<< orphan*/ *, unsigned char *, int *);
int /*<<< orphan*/ EVP_DecryptInit_ex(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *, unsigned char *,
                                      unsigned char *);
int /*<<< orphan*/ EVP_DecryptUpdate(int /*<<< orphan*/ *, unsigned char *,
                                     int *, unsigned char *, int);
int /*<<< orphan*/ *EVP_aes_256_gcm();
int /*<<< orphan*/ handleErrors();
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *aad,
            int aad_len, unsigned char *tag, unsigned char *key,
            unsigned char *iv, unsigned char *plaintext) {
  EVP_CIPHER_CTX *ctx;
  int len;
  int plaintext_len;
  int ret;
  if (!(ctx = EVP_CIPHER_CTX_new()))
    handleErrors();
  if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
    handleErrors();
  if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
    handleErrors();
  if (!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
    handleErrors();
  if (!EVP_DecryptUpdate(ctx, NULL, &len, aad, aad_len))
    handleErrors();
  if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
    handleErrors();
  plaintext_len = len;
  if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag))
    handleErrors();
  ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
  EVP_CIPHER_CTX_free(ctx);
  if (ret > 0) {
    plaintext_len += len;
    return plaintext_len;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<decrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    $0x0,%al
call   1090 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11c2 <decrypt+0x42>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1080 <EVP_aes_256_gcm@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1060 <EVP_DecryptInit_ex@plt>
cmp    $0x0,%eax
jne    11f8 <decrypt+0x78>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x40(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    $0x10,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <EVP_CIPHER_CTX_ctrl@plt>
cmp    $0x0,%eax
jne    1223 <decrypt+0xa3>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    0x10(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <EVP_DecryptInit_ex@plt>
cmp    $0x0,%eax
jne    124b <decrypt+0xcb>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%esi
lea    -0x44(%rbp),%rdx
call   10a0 <EVP_DecryptUpdate@plt>
cmp    $0x0,%eax
jne    1274 <decrypt+0xf4>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x40(%rbp),%rdi
mov    0x18(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
lea    -0x44(%rbp),%rdx
call   10a0 <EVP_DecryptUpdate@plt>
cmp    $0x0,%eax
jne    129d <decrypt+0x11d>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rcx
mov    $0x10,%edx
call   1070 <EVP_CIPHER_CTX_ctrl@plt>
cmp    $0x0,%eax
jne    12ce <decrypt+0x14e>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x40(%rbp),%rdi
mov    0x18(%rbp),%rsi
movslq -0x44(%rbp),%rax
add    %rax,%rsi
lea    -0x44(%rbp),%rdx
call   1030 <EVP_DecryptFinal_ex@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
cmpl   $0x0,-0x4c(%rbp)
jle    1310 <decrypt+0x190>
mov    -0x44(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1317 <decrypt+0x197>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r14
mov    %r8,0x10(%rsp)
mov    %ecx,%r13d
mov    %rdx,%r15
mov    %esi,0xc(%rsp)
mov    %rdi,%rbx
xor    %eax,%eax
call   1090 <EVP_CIPHER_CTX_new@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11b9 <decrypt+0x39>
xor    %eax,%eax
call   1050 <handleErrors@plt>
xor    %eax,%eax
call   1080 <EVP_aes_256_gcm@plt>
mov    %r12,%rdi
mov    %rax,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <EVP_DecryptInit_ex@plt>
test   %eax,%eax
jne    11dd <decrypt+0x5d>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    0x50(%rsp),%rbp
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
mov    $0x10,%edx
xor    %ecx,%ecx
call   1070 <EVP_CIPHER_CTX_ctrl@plt>
test   %eax,%eax
jne    1205 <decrypt+0x85>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    %r12,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r14,%rcx
mov    %rbp,%r8
call   1060 <EVP_DecryptInit_ex@plt>
test   %eax,%eax
jne    1222 <decrypt+0xa2>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    0x58(%rsp),%rbp
lea    0x8(%rsp),%rdx
mov    %r12,%rdi
xor    %esi,%esi
mov    %r15,%rcx
mov    %r13d,%r8d
call   10a0 <EVP_DecryptUpdate@plt>
test   %eax,%eax
jne    1247 <decrypt+0xc7>
xor    %eax,%eax
call   1050 <handleErrors@plt>
lea    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %rbx,%rcx
mov    0xc(%rsp),%r8d
call   10a0 <EVP_DecryptUpdate@plt>
test   %eax,%eax
jne    126a <decrypt+0xea>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    0x8(%rsp),%ebx
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
mov    $0x10,%edx
mov    0x10(%rsp),%rcx
call   1070 <EVP_CIPHER_CTX_ctrl@plt>
test   %eax,%eax
jne    1294 <decrypt+0x114>
xor    %eax,%eax
call   1050 <handleErrors@plt>
movslq 0x8(%rsp),%rax
add    %rax,%rbp
lea    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %rbp,%rsi
call   1030 <EVP_DecryptFinal_ex@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
add    0x8(%rsp),%ebx
test   %ebp,%ebp
mov    $0xffffffff,%eax
cmovg  %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_CTRL_GCM_SET_IVLEN = global i32 0
@EVP_CTRL_GCM_SET_TAG = global i32 0
define i32 @decrypt(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4, i8* %5, i8* %6, i8* %7) {
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca i32*
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
store i8* %0, i8** %10
store i32 %1, i32* %11
store i8* %2, i8** %12
store i32 %3, i32* %13
store i8* %4, i8** %14
store i8* %5, i8** %15
store i8* %6, i8** %16
store i8* %7, i8** %17
%22 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %22, i32** %18
%23 = icmp ne i32* %22, null
br i1 %23, label %26, label %24
24: ; preds = %8
%25 = call i32 (...) @handleErrors()
br label %26
26: ; preds = %24, %8
%27 = load i32*, i32** %18
%28 = call i32* (...) @EVP_aes_256_gcm()
%29 = call i32 @EVP_DecryptInit_ex(i32* %27, i32* %28, i32* null, i8* null, i8* null)
%30 = icmp ne i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = call i32 (...) @handleErrors()
br label %33
33: ; preds = %31, %26
%34 = load i32*, i32** %18
%35 = load i32, i32* @EVP_CTRL_GCM_SET_IVLEN
%36 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %34, i32 %35, i32 16, i8* null)
%37 = icmp ne i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = call i32 (...) @handleErrors()
br label %40
40: ; preds = %38, %33
%41 = load i32*, i32** %18
%42 = load i8*, i8** %15
%43 = load i8*, i8** %16
%44 = call i32 @EVP_DecryptInit_ex(i32* %41, i32* null, i32* null, i8* %42, i8* %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %40
%47 = call i32 (...) @handleErrors()
br label %48
48: ; preds = %46, %40
%49 = load i32*, i32** %18
%50 = load i8*, i8** %12
%51 = load i32, i32* %13
%52 = call i32 @EVP_DecryptUpdate(i32* %49, i8* null, i32* %19, i8* %50, i32 %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %48
%55 = call i32 (...) @handleErrors()
br label %56
56: ; preds = %54, %48
%57 = load i32*, i32** %18
%58 = load i8*, i8** %17
%59 = load i8*, i8** %10
%60 = load i32, i32* %11
%61 = call i32 @EVP_DecryptUpdate(i32* %57, i8* %58, i32* %19, i8* %59, i32 %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %65, label %63
63: ; preds = %56
%64 = call i32 (...) @handleErrors()
br label %65
65: ; preds = %63, %56
%66 = load i32, i32* %19
store i32 %66, i32* %20
%67 = load i32*, i32** %18
%68 = load i32, i32* @EVP_CTRL_GCM_SET_TAG
%69 = load i8*, i8** %14
%70 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %67, i32 %68, i32 16, i8* %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %74, label %72
72: ; preds = %65
%73 = call i32 (...) @handleErrors()
br label %74
74: ; preds = %72, %65
%75 = load i32*, i32** %18
%76 = load i8*, i8** %17
%77 = load i32, i32* %19
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
%80 = call i32 @EVP_DecryptFinal_ex(i32* %75, i8* %79, i32* %19)
store i32 %80, i32* %21
%81 = load i32*, i32** %18
%82 = call i32 @EVP_CIPHER_CTX_free(i32* %81)
%83 = load i32, i32* %21
%84 = icmp sgt i32 %83, 0
br i1 %84, label %85, label %90
85: ; preds = %74
%86 = load i32, i32* %19
%87 = load i32, i32* %20
%88 = add nsw i32 %87, %86
store i32 %88, i32* %20
%89 = load i32, i32* %20
store i32 %89, i32* %9
br label %91
90: ; preds = %74
store i32 -1, i32* %9
br label %91
91: ; preds = %90, %85
%92 = load i32, i32* %9
ret i32 %92
}

LLVM IR (-O2):
@EVP_CTRL_GCM_SET_IVLEN = global i32 0
@EVP_CTRL_GCM_SET_TAG = global i32 0
define i32 @decrypt(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4, i8* %5, i8* %6, i8* %7) {
%9 = alloca i32
%10 = bitcast i32* %9 to i8*
%11 = tail call i32* (...) @EVP_CIPHER_CTX_new()
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 (...) @handleErrors()
br label %15
15: ; preds = %13, %8
%16 = tail call i32* (...) @EVP_aes_256_gcm()
%17 = tail call i32 @EVP_DecryptInit_ex(i32* %11, i32* %16, i32* null, i8* null, i8* null)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 (...) @handleErrors()
br label %21
21: ; preds = %19, %15
%22 = load i32, i32* @EVP_CTRL_GCM_SET_IVLEN
%23 = tail call i32 @EVP_CIPHER_CTX_ctrl(i32* %11, i32 %22, i32 16, i8* null)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 (...) @handleErrors()
br label %27
27: ; preds = %25, %21
%28 = tail call i32 @EVP_DecryptInit_ex(i32* %11, i32* null, i32* null, i8* %5, i8* %6)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 (...) @handleErrors()
br label %32
32: ; preds = %30, %27
%33 = call i32 @EVP_DecryptUpdate(i32* %11, i8* null, i32* %9, i8* %2, i32 %3)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (...) @handleErrors()
br label %37
37: ; preds = %35, %32
%38 = call i32 @EVP_DecryptUpdate(i32* %11, i8* %7, i32* %9, i8* %0, i32 %1)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 (...) @handleErrors()
br label %42
42: ; preds = %40, %37
%43 = load i32, i32* %9
%44 = load i32, i32* @EVP_CTRL_GCM_SET_TAG
%45 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %11, i32 %44, i32 16, i8* %4)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 (...) @handleErrors()
br label %49
49: ; preds = %47, %42
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %7, i64 %51
%53 = call i32 @EVP_DecryptFinal_ex(i32* %11, i8* %52, i32* %9)
%54 = call i32 @EVP_CIPHER_CTX_free(i32* %11)
%55 = icmp sgt i32 %53, 0
%56 = load i32, i32* %9
%57 = add nsw i32 %56, %43
%58 = select i1 %55, i32 %57, i32 -1
ret i32 %58
}