#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_GET_TAG;
scalar_t__ EVP_EncryptFinal_ex(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                               int *);
scalar_t__ EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                              int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *);
scalar_t__ EVP_EncryptUpdate(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int *,
                             int /*<<< orphan*/ const *, int);
int /*<<< orphan*/ EVP_aes_128_gcm();
int ece_aes128gcm_encrypt_block(EVP_CIPHER_CTX *ctx, const uint8_t *key,
                                const uint8_t *iv, const uint8_t *block,
                                size_t blockLen, const uint8_t *pad,
                                size_t padLen, uint8_t *record) {
  int err = 0;
  if (EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, key, iv) <= 0) {
    err = -18;
    goto end;
  }
  int chunkLen = -1;
  if (EVP_EncryptUpdate(ctx, record, &chunkLen, block, (int)blockLen) <= 0) {
    err = -18;
    goto end;
  }
  if (EVP_EncryptUpdate(ctx, &record[blockLen], &chunkLen, pad, (int)padLen) <=
      0) {
    err = -18;
    goto end;
  }
  if (EVP_EncryptFinal_ex(ctx, NULL, &chunkLen) <= 0) {
    err = -18;
    goto end;
  }
  if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16,
                          &record[blockLen + padLen]) <= 0) {
    err = -18;
    goto end;
  }
end:
  EVP_CIPHER_CTX_reset(ctx);
  return err;
}


x86 assembly (clang, -O0):
<ece_aes128gcm_encrypt_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
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,-0x40(%rbp)
mov    $0x0,%al
call   1030 <EVP_aes_128_gcm@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1070 <EVP_EncryptInit_ex@plt>
cmp    $0x0,%rax
jg     11cb <ece_aes128gcm_encrypt_block+0x6b>
movl   $0xffffffee,-0x34(%rbp)
jmp    12ad <ece_aes128gcm_encrypt_block+0x14d>
movl   $0xffffffff,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %eax,%r8d
lea    -0x38(%rbp),%rdx
call   1050 <EVP_EncryptUpdate@plt>
cmp    $0x0,%rax
jg     1204 <ece_aes128gcm_encrypt_block+0xa4>
movl   $0xffffffee,-0x34(%rbp)
jmp    12ad <ece_aes128gcm_encrypt_block+0x14d>
mov    -0x8(%rbp),%rdi
mov    0x18(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%rax
mov    %eax,%r8d
lea    -0x38(%rbp),%rdx
call   1050 <EVP_EncryptUpdate@plt>
cmp    $0x0,%rax
jg     1241 <ece_aes128gcm_encrypt_block+0xe1>
movl   $0xffffffee,-0x34(%rbp)
jmp    12ad <ece_aes128gcm_encrypt_block+0x14d>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
call   1060 <EVP_EncryptFinal_ex@plt>
cmp    $0x0,%rax
jg     1268 <ece_aes128gcm_encrypt_block+0x108>
movl   $0xffffffee,-0x34(%rbp)
jmp    12ad <ece_aes128gcm_encrypt_block+0x14d>
mov    -0x8(%rbp),%rdi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
mov    0x18(%rbp),%rcx
mov    -0x28(%rbp),%rax
add    0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rcx
mov    $0x10,%edx
call   1080 <EVP_CIPHER_CTX_ctrl@plt>
cmp    $0x0,%rax
jg     12a8 <ece_aes128gcm_encrypt_block+0x148>
movl   $0xffffffee,-0x34(%rbp)
jmp    12ad <ece_aes128gcm_encrypt_block+0x14d>
jmp    12ad <ece_aes128gcm_encrypt_block+0x14d>
mov    -0x8(%rbp),%rdi
call   1040 <EVP_CIPHER_CTX_reset@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ece_aes128gcm_encrypt_block>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x10(%rsp)
mov    %r8,%r14
mov    %rcx,%r13
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %eax,%eax
call   1030 <EVP_aes_128_gcm@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %rbp,%r8
call   1070 <EVP_EncryptInit_ex@plt>
mov    $0xffffffee,%r12d
test   %rax,%rax
jle    1244 <ece_aes128gcm_encrypt_block+0xe4>
mov    0x58(%rsp),%rbp
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xc(%rsp),%rdx
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r13,%rcx
mov    %r14d,%r8d
call   1050 <EVP_EncryptUpdate@plt>
test   %rax,%rax
jle    1244 <ece_aes128gcm_encrypt_block+0xe4>
mov    0x50(%rsp),%r13
lea    0x0(,%r14,4),%rsi
add    %rbp,%rsi
lea    0xc(%rsp),%rdx
mov    %r15,%rdi
mov    0x10(%rsp),%rcx
mov    %r13d,%r8d
call   1050 <EVP_EncryptUpdate@plt>
test   %rax,%rax
jle    1244 <ece_aes128gcm_encrypt_block+0xe4>
xor    %ebx,%ebx
lea    0xc(%rsp),%rdx
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <EVP_EncryptFinal_ex@plt>
test   %rax,%rax
jle    1244 <ece_aes128gcm_encrypt_block+0xe4>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
add    %r14,%r13
lea    0x0(,%r13,4),%rcx
add    %rbp,%rcx
mov    %r15,%rdi
mov    $0x10,%edx
call   1080 <EVP_CIPHER_CTX_ctrl@plt>
test   %rax,%rax
mov    $0xffffffee,%eax
cmovle %eax,%ebx
mov    %ebx,%r12d
mov    %r15,%rdi
call   1040 <EVP_CIPHER_CTX_reset@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_CTRL_GCM_GET_TAG = global i32 0
define i32 @ece_aes128gcm_encrypt_block(i32* %0, i32* %1, i32* %2, i32* %3, i64 %4, i32* %5, i64 %6, i32* %7) {
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i32*
%15 = alloca i64
%16 = alloca i32*
%17 = alloca i32
%18 = alloca i32
store i32* %0, i32** %9
store i32* %1, i32** %10
store i32* %2, i32** %11
store i32* %3, i32** %12
store i64 %4, i64* %13
store i32* %5, i32** %14
store i64 %6, i64* %15
store i32* %7, i32** %16
store i32 0, i32* %17
%19 = load i32*, i32** %9
%20 = call i32 (...) @EVP_aes_128_gcm()
%21 = load i32*, i32** %10
%22 = load i32*, i32** %11
%23 = call i64 @EVP_EncryptInit_ex(i32* %19, i32 %20, i32* null, i32* %21, i32* %22)
%24 = icmp sle i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %8
store i32 -18, i32* %17
br label %63
26: ; preds = %8
store i32 -1, i32* %18
%27 = load i32*, i32** %9
%28 = load i32*, i32** %16
%29 = load i32*, i32** %12
%30 = load i64, i64* %13
%31 = trunc i64 %30 to i32
%32 = call i64 @EVP_EncryptUpdate(i32* %27, i32* %28, i32* %18, i32* %29, i32 %31)
%33 = icmp sle i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %26
store i32 -18, i32* %17
br label %63
35: ; preds = %26
%36 = load i32*, i32** %9
%37 = load i32*, i32** %16
%38 = load i64, i64* %13
%39 = getelementptr inbounds i32, i32* %37, i64 %38
%40 = load i32*, i32** %14
%41 = load i64, i64* %15
%42 = trunc i64 %41 to i32
%43 = call i64 @EVP_EncryptUpdate(i32* %36, i32* %39, i32* %18, i32* %40, i32 %42)
%44 = icmp sle i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %35
store i32 -18, i32* %17
br label %63
46: ; preds = %35
%47 = load i32*, i32** %9
%48 = call i64 @EVP_EncryptFinal_ex(i32* %47, i32* null, i32* %18)
%49 = icmp sle i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 -18, i32* %17
br label %63
51: ; preds = %46
%52 = load i32*, i32** %9
%53 = load i32, i32* @EVP_CTRL_GCM_GET_TAG
%54 = load i32*, i32** %16
%55 = load i64, i64* %13
%56 = load i64, i64* %15
%57 = add i64 %55, %56
%58 = getelementptr inbounds i32, i32* %54, i64 %57
%59 = call i64 @EVP_CIPHER_CTX_ctrl(i32* %52, i32 %53, i32 16, i32* %58)
%60 = icmp sle i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %51
store i32 -18, i32* %17
br label %63
62: ; preds = %51
br label %63
63: ; preds = %62, %61, %50, %45, %34, %25
%64 = load i32*, i32** %9
%65 = call i32 @EVP_CIPHER_CTX_reset(i32* %64)
%66 = load i32, i32* %17
ret i32 %66
}

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