#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*/ uint8_t;
typedef int /*<<< orphan*/ EVP_CIPHER_CTX;

/* Variables and functions */
scalar_t__ EVP_CIPHER_CTX_ctrl(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_CIPHER_CTX_reset(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_CTRL_GCM_SET_TAG;
scalar_t__ EVP_DecryptFinal_ex(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                               int *);
scalar_t__ EVP_DecryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                              int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *);
scalar_t__ EVP_DecryptUpdate(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int *,
                             int /*<<< orphan*/ const *, int);
int /*<<< orphan*/ EVP_aes_128_gcm();
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          int);
int ece_decrypt_record(EVP_CIPHER_CTX *ctx, const uint8_t *key,
                       const uint8_t *iv, const uint8_t *record,
                       size_t recordLen, uint8_t *block) {
  int err = 0;
  if (EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, key, iv) <= 0) {
    err = -6;
    goto end;
  }
  uint8_t tag[16];
  memcpy(tag, &record[recordLen - 16], 16);
  if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0) {
    err = -6;
    goto end;
  }
  int updateLen = 0;
  if (EVP_DecryptUpdate(ctx, block, &updateLen, record, (int)recordLen - 16) <=
      0) {
    err = -6;
    goto end;
  }
  int finalLen = -1;
  if (EVP_DecryptFinal_ex(ctx, NULL, &finalLen) <= 0) {
    err = -6;
    goto end;
  }
end:
  EVP_CIPHER_CTX_reset(ctx);
  return err;
}


x86 assembly (clang, -O0):
<ece_decrypt_record>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_128_gcm@plt>
mov    -0x90(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1070 <EVP_DecryptInit_ex@plt>
cmp    $0x0,%rax
jg     11dc <ece_decrypt_record+0x6c>
movl   $0xfffffffa,-0x34(%rbp)
jmp    12aa <ece_decrypt_record+0x13a>
lea    -0x80(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rax
sub    $0x10,%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x10,%edx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    -0x80(%rbp),%rcx
mov    $0x10,%edx
call   1080 <EVP_CIPHER_CTX_ctrl@plt>
cmp    $0x0,%rax
jg     122e <ece_decrypt_record+0xbe>
movl   $0xfffffffa,-0x34(%rbp)
jmp    12aa <ece_decrypt_record+0x13a>
movl   $0x0,-0x84(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %eax,%r8d
sub    $0x10,%r8d
lea    -0x84(%rbp),%rdx
call   1090 <EVP_DecryptUpdate@plt>
cmp    $0x0,%rax
jg     1271 <ece_decrypt_record+0x101>
movl   $0xfffffffa,-0x34(%rbp)
jmp    12aa <ece_decrypt_record+0x13a>
movl   $0xffffffff,-0x88(%rbp)
ff ff ff
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x88(%rbp),%rdx
call   1030 <EVP_DecryptFinal_ex@plt>
cmp    $0x0,%rax
jg     12a5 <ece_decrypt_record+0x135>
movl   $0xfffffffa,-0x34(%rbp)
jmp    12aa <ece_decrypt_record+0x13a>
jmp    12aa <ece_decrypt_record+0x13a>
mov    -0x8(%rbp),%rdi
call   1050 <EVP_CIPHER_CTX_reset@plt>
mov    -0x34(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ece_decrypt_record>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,0x8(%rsp)
mov    %r8,%r13
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %eax,%eax
call   1040 <EVP_aes_128_gcm@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %rbp,%r8
call   1070 <EVP_DecryptInit_ex@plt>
mov    $0xfffffffa,%r14d
test   %rax,%rax
jle    123e <ece_decrypt_record+0xce>
lea    (%r15,%r13,4),%rsi
add    $0xffffffffffffffc0,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x10,%edx
call   1060 <memcpy@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
mov    $0x10,%edx
mov    %rbx,%rcx
call   1080 <EVP_CIPHER_CTX_ctrl@plt>
test   %rax,%rax
jle    123e <ece_decrypt_record+0xce>
movl   $0x0,0x4(%rsp)
add    $0xfffffff0,%r13d
lea    0x4(%rsp),%rdx
mov    %r12,%rdi
mov    0x8(%rsp),%rsi
mov    %r15,%rcx
mov    %r13d,%r8d
call   1090 <EVP_DecryptUpdate@plt>
test   %rax,%rax
jle    123e <ece_decrypt_record+0xce>
movl   $0xffffffff,(%rsp)
xor    %ebx,%ebx
mov    %rsp,%rdx
mov    %r12,%rdi
xor    %esi,%esi
call   1030 <EVP_DecryptFinal_ex@plt>
test   %rax,%rax
mov    $0xfffffffa,%r14d
cmovg  %ebx,%r14d
mov    %r12,%rdi
call   1050 <EVP_CIPHER_CTX_reset@plt>
mov    %r14d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_CTRL_GCM_SET_TAG = global i32 0
define i32 @ece_decrypt_record(i32* %0, i32* %1, i32* %2, i32* %3, i64 %4, i32* %5) {
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i32*
%13 = alloca i32
%14 = alloca [16 x i32]
%15 = alloca i32
%16 = alloca i32
store i32* %0, i32** %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store i64 %4, i64* %11
store i32* %5, i32** %12
store i32 0, i32* %13
%17 = load i32*, i32** %7
%18 = call i32 (...) @EVP_aes_128_gcm()
%19 = load i32*, i32** %8
%20 = load i32*, i32** %9
%21 = call i64 @EVP_DecryptInit_ex(i32* %17, i32 %18, i32* null, i32* %19, i32* %20)
%22 = icmp sle i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %6
store i32 -6, i32* %13
br label %53
24: ; preds = %6
%25 = getelementptr inbounds [16 x i32], [16 x i32]* %14, i64 0, i64 0
%26 = load i32*, i32** %10
%27 = load i64, i64* %11
%28 = sub i64 %27, 16
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = call i32 @memcpy(i32* %25, i32* %29, i32 16)
%31 = load i32*, i32** %7
%32 = load i32, i32* @EVP_CTRL_GCM_SET_TAG
%33 = getelementptr inbounds [16 x i32], [16 x i32]* %14, i64 0, i64 0
%34 = call i64 @EVP_CIPHER_CTX_ctrl(i32* %31, i32 %32, i32 16, i32* %33)
%35 = icmp sle i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %24
store i32 -6, i32* %13
br label %53
37: ; preds = %24
store i32 0, i32* %15
%38 = load i32*, i32** %7
%39 = load i32*, i32** %12
%40 = load i32*, i32** %10
%41 = load i64, i64* %11
%42 = trunc i64 %41 to i32
%43 = sub nsw i32 %42, 16
%44 = call i64 @EVP_DecryptUpdate(i32* %38, i32* %39, i32* %15, i32* %40, i32 %43)
%45 = icmp sle i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %37
store i32 -6, i32* %13
br label %53
47: ; preds = %37
store i32 -1, i32* %16
%48 = load i32*, i32** %7
%49 = call i64 @EVP_DecryptFinal_ex(i32* %48, i32* null, i32* %16)
%50 = icmp sle i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %47
store i32 -6, i32* %13
br label %53
52: ; preds = %47
br label %53
53: ; preds = %52, %51, %46, %36, %23
%54 = load i32*, i32** %7
%55 = call i32 @EVP_CIPHER_CTX_reset(i32* %54)
%56 = load i32, i32* %13
ret i32 %56
}

LLVM IR (-O2):
@EVP_CTRL_GCM_SET_TAG = global i32 0
define i32 @ece_decrypt_record(i32* %0, i32* %1, i32* %2, i32* %3, i64 %4, i32* %5) {
%7 = alloca [16 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = tail call i32 (...) @EVP_aes_128_gcm()
%11 = tail call i64 @EVP_DecryptInit_ex(i32* %0, i32 %10, i32* null, i32* %1, i32* %2)
%12 = icmp slt i64 %11, 1
br i1 %12, label %30, label %13
13: ; preds = %6
%14 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 0
%15 = add i64 %4, -16
%16 = getelementptr inbounds i32, i32* %3, i64 %15
%17 = call i32 @memcpy(i32* %14, i32* %16, i32 16)
%18 = load i32, i32* @EVP_CTRL_GCM_SET_TAG
%19 = call i64 @EVP_CIPHER_CTX_ctrl(i32* %0, i32 %18, i32 16, i32* %14)
%20 = icmp slt i64 %19, 1
br i1 %20, label %30, label %21
21: ; preds = %13
store i32 0, i32* %8
%22 = trunc i64 %4 to i32
%23 = add nsw i32 %22, -16
%24 = call i64 @EVP_DecryptUpdate(i32* %0, i32* %5, i32* %8, i32* %3, i32 %23)
%25 = icmp slt i64 %24, 1
br i1 %25, label %30, label %26
26: ; preds = %21
store i32 -1, i32* %9
%27 = call i64 @EVP_DecryptFinal_ex(i32* %0, i32* null, i32* %9)
%28 = icmp slt i64 %27, 1
%29 = select i1 %28, i32 -6, i32 0
br label %30
30: ; preds = %26, %21, %13, %6
%31 = phi i32 [ -6, %6 ], [ -6, %13 ], [ -6, %21 ], [ %29, %26 ]
%32 = call i32 @EVP_CIPHER_CTX_reset(i32* %0)
ret i32 %31
}