#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 */
int EVP_CIPHER_CTX_block_size(int /*<<< orphan*/ *);
int EVP_CIPHER_CTX_ctrl(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                        int /*<<< orphan*/ *);
int EVP_CIPHER_CTX_reset(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_CTRL_GCM_SET_TAG;
int EVP_DecryptFinal_ex(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int *);
int EVP_DecryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                       int /*<<< orphan*/ const *);
int EVP_DecryptUpdate(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int *,
                      int /*<<< orphan*/ const *, int);
int /*<<< orphan*/ EVP_aes_128_gcm();
size_t INT_MAX;
int /*<<< orphan*/ assert(int);
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 chunkLen = -1;
  if (EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, key, iv) != 1) {
    return -6;
  }
  assert(recordLen > 16);
  size_t blockLen = recordLen - 16;
  uint8_t tag[16];
  memcpy(tag, &record[blockLen], 16);
  if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) != 1) {
    return -6;
  }
  if (blockLen > INT_MAX ||
      EVP_DecryptUpdate(ctx, block, &chunkLen, record, (int)blockLen) != 1) {
    return -6;
  }
  assert(EVP_CIPHER_CTX_block_size(ctx) == 1);
  if (EVP_DecryptFinal_ex(ctx, NULL, &chunkLen) != 1) {
    return -6;
  }
  if (EVP_CIPHER_CTX_reset(ctx) != 1) {
    return -6;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ece_decrypt_record>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    $0x0,%al
call   1040 <EVP_aes_128_gcm@plt>
mov    -0x98(%rbp),%rdi
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1090 <EVP_DecryptInit_ex@plt>
cmp    $0x1,%eax
je     11fb <ece_decrypt_record+0x6b>
movl   $0xfffffffa,-0x4(%rbp)
jmp    1315 <ece_decrypt_record+0x185>
cmpq   $0x10,-0x30(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x30(%rbp),%rax
sub    $0x10,%rax
mov    %rax,-0x48(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x48(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x10,%edx
call   1080 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    -0x90(%rbp),%rcx
mov    $0x10,%edx
call   10a0 <EVP_CIPHER_CTX_ctrl@plt>
cmp    $0x1,%eax
je     126c <ece_decrypt_record+0xdc>
movl   $0xfffffffa,-0x4(%rbp)
jmp    1315 <ece_decrypt_record+0x185>
mov    -0x48(%rbp),%rax
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%rax
ja     12a5 <ece_decrypt_record+0x115>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    -0x48(%rbp),%rax
mov    %eax,%r8d
lea    -0x3c(%rbp),%rdx
call   10b0 <EVP_DecryptUpdate@plt>
cmp    $0x1,%eax
je     12b1 <ece_decrypt_record+0x121>
movl   $0xfffffffa,-0x4(%rbp)
jmp    1315 <ece_decrypt_record+0x185>
mov    -0x10(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_block_size@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x3c(%rbp),%rdx
call   1030 <EVP_DecryptFinal_ex@plt>
cmp    $0x1,%eax
je     12f0 <ece_decrypt_record+0x160>
movl   $0xfffffffa,-0x4(%rbp)
jmp    1315 <ece_decrypt_record+0x185>
mov    -0x10(%rbp),%rdi
call   1050 <EVP_CIPHER_CTX_reset@plt>
cmp    $0x1,%eax
je     130e <ece_decrypt_record+0x17e>
movl   $0xfffffffa,-0x4(%rbp)
jmp    1315 <ece_decrypt_record+0x185>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%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,%r12
mov    %r8,%r13
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r14
movl   $0xffffffff,0xc(%rsp)
ff
xor    %eax,%eax
call   1040 <EVP_aes_128_gcm@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %rbp,%r8
call   1090 <EVP_DecryptInit_ex@plt>
mov    $0xfffffffa,%ebp
cmp    $0x1,%eax
jne    1295 <ece_decrypt_record+0x105>
xor    %edi,%edi
cmp    $0x11,%r13
setae  %dil
call   1070 <assert@plt>
lea    (%r15,%r13,4),%rsi
add    $0xffffffffffffffc0,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x10,%edx
call   1080 <memcpy@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
mov    $0x10,%edx
mov    %rbx,%rcx
call   10a0 <EVP_CIPHER_CTX_ctrl@plt>
mov    $0xfffffffa,%ebp
cmp    $0x1,%eax
jne    1295 <ece_decrypt_record+0x105>
add    $0xfffffffffffffff0,%r13
mov    0x2daa(%rip),%rax
cmp    (%rax),%r13
ja     1295 <ece_decrypt_record+0x105>
lea    0xc(%rsp),%rdx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15,%rcx
mov    %r13d,%r8d
call   10b0 <EVP_DecryptUpdate@plt>
cmp    $0x1,%eax
jne    1295 <ece_decrypt_record+0x105>
mov    %r14,%rdi
call   1060 <EVP_CIPHER_CTX_block_size@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1070 <assert@plt>
xor    %ebx,%ebx
lea    0xc(%rsp),%rdx
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <EVP_DecryptFinal_ex@plt>
cmp    $0x1,%eax
jne    1295 <ece_decrypt_record+0x105>
mov    %r14,%rdi
call   1050 <EVP_CIPHER_CTX_reset@plt>
cmp    $0x1,%eax
mov    $0xfffffffa,%ebp
cmove  %ebx,%ebp
mov    %ebp,%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
@INT_MAX = global i64 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 i32*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i64
%16 = alloca [16 x i32]
store i32* %0, i32** %8
store i32* %1, i32** %9
store i32* %2, i32** %10
store i32* %3, i32** %11
store i64 %4, i64* %12
store i32* %5, i32** %13
store i32 -1, i32* %14
%17 = load i32*, i32** %8
%18 = call i32 (...) @EVP_aes_128_gcm()
%19 = load i32*, i32** %9
%20 = load i32*, i32** %10
%21 = call i32 @EVP_DecryptInit_ex(i32* %17, i32 %18, i32* null, i32* %19, i32* %20)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %6
store i32 -6, i32* %7
br label %71
24: ; preds = %6
%25 = load i64, i64* %12
%26 = icmp ugt i64 %25, 16
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i64, i64* %12
%30 = sub i64 %29, 16
store i64 %30, i64* %15
%31 = getelementptr inbounds [16 x i32], [16 x i32]* %16, i64 0, i64 0
%32 = load i32*, i32** %11
%33 = load i64, i64* %15
%34 = getelementptr inbounds i32, i32* %32, i64 %33
%35 = call i32 @memcpy(i32* %31, i32* %34, i32 16)
%36 = load i32*, i32** %8
%37 = load i32, i32* @EVP_CTRL_GCM_SET_TAG
%38 = getelementptr inbounds [16 x i32], [16 x i32]* %16, i64 0, i64 0
%39 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %36, i32 %37, i32 16, i32* %38)
%40 = icmp ne i32 %39, 1
br i1 %40, label %41, label %42
41: ; preds = %24
store i32 -6, i32* %7
br label %71
42: ; preds = %24
%43 = load i64, i64* %15
%44 = load i64, i64* @INT_MAX
%45 = icmp ugt i64 %43, %44
br i1 %45, label %54, label %46
46: ; preds = %42
%47 = load i32*, i32** %8
%48 = load i32*, i32** %13
%49 = load i32*, i32** %11
%50 = load i64, i64* %15
%51 = trunc i64 %50 to i32
%52 = call i32 @EVP_DecryptUpdate(i32* %47, i32* %48, i32* %14, i32* %49, i32 %51)
%53 = icmp ne i32 %52, 1
br i1 %53, label %54, label %55
54: ; preds = %46, %42
store i32 -6, i32* %7
br label %71
55: ; preds = %46
%56 = load i32*, i32** %8
%57 = call i32 @EVP_CIPHER_CTX_block_size(i32* %56)
%58 = icmp eq i32 %57, 1
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
%61 = load i32*, i32** %8
%62 = call i32 @EVP_DecryptFinal_ex(i32* %61, i32* null, i32* %14)
%63 = icmp ne i32 %62, 1
br i1 %63, label %64, label %65
64: ; preds = %55
store i32 -6, i32* %7
br label %71
65: ; preds = %55
%66 = load i32*, i32** %8
%67 = call i32 @EVP_CIPHER_CTX_reset(i32* %66)
%68 = icmp ne i32 %67, 1
br i1 %68, label %69, label %70
69: ; preds = %65
store i32 -6, i32* %7
br label %71
70: ; preds = %65
store i32 0, i32* %7
br label %71
71: ; preds = %70, %69, %64, %54, %41, %23
%72 = load i32, i32* %7
ret i32 %72
}

LLVM IR (-O2):
@EVP_CTRL_GCM_SET_TAG = global i32 0
@INT_MAX = global i64 0
define i32 @ece_decrypt_record(i32* %0, i32* %1, i32* %2, i32* %3, i64 %4, i32* %5) {
%7 = alloca i32
%8 = alloca [16 x i32]
%9 = bitcast i32* %7 to i8*
store i32 -1, i32* %7
%10 = tail call i32 (...) @EVP_aes_128_gcm()
%11 = tail call i32 @EVP_DecryptInit_ex(i32* %0, i32 %10, i32* null, i32* %1, i32* %2)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %45
13: ; preds = %6
%14 = icmp ugt i64 %4, 16
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = add i64 %4, -16
%18 = bitcast [16 x i32]* %8 to i8*
%19 = getelementptr inbounds [16 x i32], [16 x i32]* %8, i64 0, i64 0
%20 = getelementptr inbounds i32, i32* %3, i64 %17
%21 = call i32 @memcpy(i32* %19, i32* %20, i32 16)
%22 = load i32, i32* @EVP_CTRL_GCM_SET_TAG
%23 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %0, i32 %22, i32 16, i32* %19)
%24 = icmp ne i32 %23, 1
%25 = load i64, i64* @INT_MAX
%26 = icmp ugt i64 %17, %25
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %43, label %28
28: ; preds = %13
%29 = trunc i64 %17 to i32
%30 = call i32 @EVP_DecryptUpdate(i32* %0, i32* %5, i32* %7, i32* %3, i32 %29)
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %43
32: ; preds = %28
%33 = call i32 @EVP_CIPHER_CTX_block_size(i32* %0)
%34 = icmp eq i32 %33, 1
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = call i32 @EVP_DecryptFinal_ex(i32* %0, i32* null, i32* %7)
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %43
39: ; preds = %32
%40 = call i32 @EVP_CIPHER_CTX_reset(i32* %0)
%41 = icmp eq i32 %40, 1
%42 = select i1 %41, i32 0, i32 -6
br label %43
43: ; preds = %39, %32, %28, %13
%44 = phi i32 [ -6, %13 ], [ -6, %28 ], [ -6, %32 ], [ %42, %39 ]
br label %45
45: ; preds = %6, %43
%46 = phi i32 [ %44, %43 ], [ -6, %6 ]
ret i32 %46
}