#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 EVP_CIPHER_CTX_ctrl(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                        int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_CIPHER_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_CIPHER_CTX_new();
int /*<<< orphan*/ EVP_CTRL_GCM_SET_IVLEN;
int EVP_EncryptFinal_ex(int /*<<< orphan*/ *, unsigned char *, int *);
int EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *, unsigned char *, unsigned char *);
int EVP_EncryptUpdate(int /*<<< orphan*/ *, unsigned char *, int *,
                      unsigned char *, int);
int /*<<< orphan*/ *EVP_aes_256_gcm();
int /*<<< orphan*/ handleErrors();
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *aad,
            int aad_len, unsigned char *key, unsigned char *iv,
            unsigned char *ciphertext, unsigned char *tag) {
  EVP_CIPHER_CTX *ctx;
  int len;
  int ciphertext_len;
  if (!(ctx = EVP_CIPHER_CTX_new()))
    handleErrors();
  if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
    handleErrors();
  if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
    handleErrors();
  if (1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
    handleErrors();
  if (1 != EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len))
    handleErrors();
  if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
    handleErrors();
  ciphertext_len = len;
  if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
    handleErrors();
  ciphertext_len += len;
  EVP_CIPHER_CTX_free(ctx);
  return ciphertext_len;
}


x86 assembly (clang, -O0):
<encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    $0x0,%al
call   10a0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11c2 <encrypt+0x42>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1090 <EVP_aes_256_gcm@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <EVP_EncryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     11fe <encrypt+0x7e>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x38(%rbp),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    $0x10,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <EVP_CIPHER_CTX_ctrl@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     122f <encrypt+0xaf>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1070 <EVP_EncryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     125d <encrypt+0xdd>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%esi
lea    -0x3c(%rbp),%rdx
call   1040 <EVP_EncryptUpdate@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     128c <encrypt+0x10c>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x38(%rbp),%rdi
mov    0x10(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
lea    -0x3c(%rbp),%rdx
call   1040 <EVP_EncryptUpdate@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     12bb <encrypt+0x13b>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x10(%rbp),%rsi
movslq -0x3c(%rbp),%rax
add    %rax,%rsi
lea    -0x3c(%rbp),%rdx
call   1060 <EVP_EncryptFinal_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     12ef <encrypt+0x16f>
mov    $0x0,%al
call   1050 <handleErrors@plt>
mov    -0x3c(%rbp),%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <EVP_CIPHER_CTX_free@plt>
mov    -0x40(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%rbp
mov    %r8,%r14
mov    %ecx,%r12d
mov    %rdx,%r13
mov    %esi,0x4(%rsp)
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b4 <encrypt+0x34>
xor    %eax,%eax
call   1050 <handleErrors@plt>
xor    %eax,%eax
call   1090 <EVP_aes_256_gcm@plt>
mov    %r15,%rdi
mov    %rax,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
je     11d9 <encrypt+0x59>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
mov    $0x10,%edx
xor    %ecx,%ecx
call   1080 <EVP_CIPHER_CTX_ctrl@plt>
cmp    $0x1,%eax
je     11fd <encrypt+0x7d>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    %r15,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r14,%rcx
mov    %rbp,%r8
call   1070 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
je     121b <encrypt+0x9b>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    0x40(%rsp),%rbp
mov    %rsp,%rdx
mov    %r15,%rdi
xor    %esi,%esi
mov    %r13,%rcx
mov    %r12d,%r8d
call   1040 <EVP_EncryptUpdate@plt>
cmp    $0x1,%eax
je     123f <encrypt+0xbf>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    %rsp,%rdx
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %rbx,%rcx
mov    0x4(%rsp),%r8d
call   1040 <EVP_EncryptUpdate@plt>
cmp    $0x1,%eax
je     1261 <encrypt+0xe1>
xor    %eax,%eax
call   1050 <handleErrors@plt>
movslq (%rsp),%rbx
add    %rbx,%rbp
mov    %rsp,%rdx
mov    %r15,%rdi
mov    %rbp,%rsi
call   1060 <EVP_EncryptFinal_ex@plt>
cmp    $0x1,%eax
je     1282 <encrypt+0x102>
xor    %eax,%eax
call   1050 <handleErrors@plt>
mov    (%rsp),%ebp
add    %ebx,%ebp
mov    %r15,%rdi
call   1030 <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):
@EVP_CTRL_GCM_SET_IVLEN = global i32 0
define i32 @encrypt(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4, i8* %5, i8* %6, i8* %7) {
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i32*
%18 = alloca i32
%19 = alloca i32
store i8* %0, i8** %9
store i32 %1, i32* %10
store i8* %2, i8** %11
store i32 %3, i32* %12
store i8* %4, i8** %13
store i8* %5, i8** %14
store i8* %6, i8** %15
store i8* %7, i8** %16
%20 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %20, i32** %17
%21 = icmp ne i32* %20, null
br i1 %21, label %24, label %22
22: ; preds = %8
%23 = call i32 (...) @handleErrors()
br label %24
24: ; preds = %22, %8
%25 = load i32*, i32** %17
%26 = call i32* (...) @EVP_aes_256_gcm()
%27 = call i32 @EVP_EncryptInit_ex(i32* %25, i32* %26, i32* null, i8* null, i8* null)
%28 = icmp ne i32 1, %27
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 (...) @handleErrors()
br label %31
31: ; preds = %29, %24
%32 = load i32*, i32** %17
%33 = load i32, i32* @EVP_CTRL_GCM_SET_IVLEN
%34 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %32, i32 %33, i32 16, i32* null)
%35 = icmp ne i32 1, %34
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 (...) @handleErrors()
br label %38
38: ; preds = %36, %31
%39 = load i32*, i32** %17
%40 = load i8*, i8** %13
%41 = load i8*, i8** %14
%42 = call i32 @EVP_EncryptInit_ex(i32* %39, i32* null, i32* null, i8* %40, i8* %41)
%43 = icmp ne i32 1, %42
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 (...) @handleErrors()
br label %46
46: ; preds = %44, %38
%47 = load i32*, i32** %17
%48 = load i8*, i8** %11
%49 = load i32, i32* %12
%50 = call i32 @EVP_EncryptUpdate(i32* %47, i8* null, i32* %18, i8* %48, i32 %49)
%51 = icmp ne i32 1, %50
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = call i32 (...) @handleErrors()
br label %54
54: ; preds = %52, %46
%55 = load i32*, i32** %17
%56 = load i8*, i8** %15
%57 = load i8*, i8** %9
%58 = load i32, i32* %10
%59 = call i32 @EVP_EncryptUpdate(i32* %55, i8* %56, i32* %18, i8* %57, i32 %58)
%60 = icmp ne i32 1, %59
br i1 %60, label %61, label %63
61: ; preds = %54
%62 = call i32 (...) @handleErrors()
br label %63
63: ; preds = %61, %54
%64 = load i32, i32* %18
store i32 %64, i32* %19
%65 = load i32*, i32** %17
%66 = load i8*, i8** %15
%67 = load i32, i32* %18
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
%70 = call i32 @EVP_EncryptFinal_ex(i32* %65, i8* %69, i32* %18)
%71 = icmp ne i32 1, %70
br i1 %71, label %72, label %74
72: ; preds = %63
%73 = call i32 (...) @handleErrors()
br label %74
74: ; preds = %72, %63
%75 = load i32, i32* %18
%76 = load i32, i32* %19
%77 = add nsw i32 %76, %75
store i32 %77, i32* %19
%78 = load i32*, i32** %17
%79 = call i32 @EVP_CIPHER_CTX_free(i32* %78)
%80 = load i32, i32* %19
ret i32 %80
}

LLVM IR (-O2):
@EVP_CTRL_GCM_SET_IVLEN = global i32 0
define i32 @encrypt(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4, i8* %5, i8* %6, i8* nocapture readnone %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_EncryptInit_ex(i32* %11, i32* %16, i32* null, i8* null, i8* null)
%18 = icmp eq i32 %17, 1
br i1 %18, label %21, label %19
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, i32* null)
%24 = icmp eq i32 %23, 1
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = tail call i32 (...) @handleErrors()
br label %27
27: ; preds = %25, %21
%28 = tail call i32 @EVP_EncryptInit_ex(i32* %11, i32* null, i32* null, i8* %4, i8* %5)
%29 = icmp eq i32 %28, 1
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = tail call i32 (...) @handleErrors()
br label %32
32: ; preds = %30, %27
%33 = call i32 @EVP_EncryptUpdate(i32* %11, i8* null, i32* %9, i8* %2, i32 %3)
%34 = icmp eq i32 %33, 1
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = call i32 (...) @handleErrors()
br label %37
37: ; preds = %35, %32
%38 = call i32 @EVP_EncryptUpdate(i32* %11, i8* %6, i32* %9, i8* %0, i32 %1)
%39 = icmp eq i32 %38, 1
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 (...) @handleErrors()
br label %42
42: ; preds = %40, %37
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %6, i64 %44
%46 = call i32 @EVP_EncryptFinal_ex(i32* %11, i8* %45, i32* %9)
%47 = icmp eq i32 %46, 1
br i1 %47, label %50, label %48
48: ; preds = %42
%49 = call i32 (...) @handleErrors()
br label %50
50: ; preds = %48, %42
%51 = load i32, i32* %9
%52 = add nsw i32 %51, %43
%53 = call i32 @EVP_CIPHER_CTX_free(i32* %11)
ret i32 %52
}