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

/* Variables and functions */
scalar_t__ EVP_CIPHER_CTX_iv_length(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_CIPHER_CTX_key_length(int /*<<< orphan*/ *);
scalar_t__ EVP_CIPHER_CTX_rand_key(int /*<<< orphan*/ *, unsigned char *);
int /*<<< orphan*/ EVP_CIPHER_CTX_reset(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_EncryptInit_ex(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ const *,
                                      int /*<<< orphan*/ *, unsigned char *,
                                      unsigned char *);
int EVP_MAX_KEY_LENGTH;
int EVP_PKEY_encrypt_old(unsigned char *, unsigned char *, int /*<<< orphan*/,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ OPENSSL_cleanse(unsigned char *, int);
scalar_t__ RAND_bytes(unsigned char *, scalar_t__);
int EVP_SealInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                 unsigned char **ek, int *ekl, unsigned char *iv,
                 EVP_PKEY **pubk, int npubk) {
  unsigned char key[EVP_MAX_KEY_LENGTH];
  int i;
  int rv = 0;
  if (type) {
    EVP_CIPHER_CTX_reset(ctx);
    if (!EVP_EncryptInit_ex(ctx, type, NULL, NULL, NULL))
      return 0;
  }
  if ((npubk <= 0) || !pubk)
    return 1;
  if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
    return 0;
  if (EVP_CIPHER_CTX_iv_length(ctx) &&
      RAND_bytes(iv, EVP_CIPHER_CTX_iv_length(ctx)) <= 0)
    goto err;
  if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
    goto err;
  for (i = 0; i < npubk; i++) {
    ekl[i] = EVP_PKEY_encrypt_old(ek[i], key, EVP_CIPHER_CTX_key_length(ctx),
                                  pubk[i]);
    if (ekl[i] <= 0) {
      rv = -1;
      goto err;
    }
  }
  rv = npubk;
err:
  OPENSSL_cleanse(key, sizeof(key));
  return rv;
}


x86 assembly (clang, -O0):
<EVP_SealInit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x50(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     122a <EVP_SealInit+0xaa>
mov    -0x10(%rbp),%rdi
call   1030 <EVP_CIPHER_CTX_reset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <EVP_EncryptInit_ex@plt>
cmp    $0x0,%eax
jne    1225 <EVP_SealInit+0xa5>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1388 <EVP_SealInit+0x208>
jmp    122a <EVP_SealInit+0xaa>
cmpl   $0x0,0x10(%rbp)
jle    123f <EVP_SealInit+0xbf>
cmpq   $0x0,-0x38(%rbp)
jne    1252 <EVP_SealInit+0xd2>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1388 <EVP_SealInit+0x208>
mov    -0x60(%rbp),%rsi
mov    -0x10(%rbp),%rdi
call   1090 <EVP_CIPHER_CTX_rand_key@plt>
cmp    $0x0,%rax
jg     127c <EVP_SealInit+0xfc>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1388 <EVP_SealInit+0x208>
mov    -0x10(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_iv_length@plt>
cmp    $0x0,%rax
je     12bb <EVP_SealInit+0x13b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_iv_length@plt>
mov    -0x70(%rbp),%rdi
mov    %rax,%rsi
call   1040 <RAND_bytes@plt>
cmp    $0x0,%rax
jg     12bb <EVP_SealInit+0x13b>
jmp    136c <EVP_SealInit+0x1ec>
mov    -0x60(%rbp),%rcx
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1080 <EVP_EncryptInit_ex@plt>
cmp    $0x0,%eax
jne    12e1 <EVP_SealInit+0x161>
jmp    136c <EVP_SealInit+0x1ec>
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    0x10(%rbp),%eax
jge    1366 <EVP_SealInit+0x1e6>
mov    -0x20(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x78(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <EVP_CIPHER_CTX_key_length@plt>
mov    -0x78(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
mov    -0x38(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
call   1070 <EVP_PKEY_encrypt_old@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
movslq -0x4c(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jg     1353 <EVP_SealInit+0x1d3>
movl   $0xffffffff,-0x50(%rbp)
jmp    136c <EVP_SealInit+0x1ec>
jmp    1358 <EVP_SealInit+0x1d8>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    12e8 <EVP_SealInit+0x168>
mov    0x10(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rax
mov    %eax,%esi
call   1050 <OPENSSL_cleanse@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EVP_SealInit>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,-0x38(%rbp)
mov    %r8,-0x30(%rbp)
mov    %rcx,-0x48(%rbp)
mov    %rdx,-0x40(%rbp)
mov    %rdi,%r14
mov    0x2e3d(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r15
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
test   %rsi,%rsi
je     11eb <EVP_SealInit+0x6b>
mov    %rsi,%r13
mov    %r14,%rdi
call   1030 <EVP_CIPHER_CTX_reset@plt>
xor    %r12d,%r12d
mov    %r14,%rdi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <EVP_EncryptInit_ex@plt>
test   %eax,%eax
je     12d0 <EVP_SealInit+0x150>
mov    $0x1,%r12d
cmpq   $0x0,-0x38(%rbp)
je     12d0 <EVP_SealInit+0x150>
cmpl   $0x0,0x10(%rbp)
jle    12d0 <EVP_SealInit+0x150>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <EVP_CIPHER_CTX_rand_key@plt>
test   %rax,%rax
jle    12b2 <EVP_SealInit+0x132>
mov    %r14,%rdi
call   1060 <EVP_CIPHER_CTX_iv_length@plt>
test   %rax,%rax
mov    -0x30(%rbp),%r13
je     1243 <EVP_SealInit+0xc3>
mov    %r14,%rdi
call   1060 <EVP_CIPHER_CTX_iv_length@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1040 <RAND_bytes@plt>
test   %rax,%rax
jle    12b7 <EVP_SealInit+0x137>
xor    %r12d,%r12d
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r15,%rcx
mov    %r13,%r8
call   1080 <EVP_EncryptInit_ex@plt>
test   %eax,%eax
je     12c6 <EVP_SealInit+0x146>
mov    %rbx,-0x30(%rbp)
mov    0x10(%rbp),%r12d
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
mov    -0x40(%rbp),%rax
mov    (%rax,%r13,8),%rbx
mov    %r14,%rdi
call   10a0 <EVP_CIPHER_CTX_key_length@plt>
mov    -0x38(%rbp),%rcx
mov    (%rcx,%r13,8),%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1070 <EVP_PKEY_encrypt_old@plt>
mov    -0x48(%rbp),%rcx
mov    %eax,(%rcx,%r13,4)
test   %eax,%eax
jle    12bc <EVP_SealInit+0x13c>
add    $0x1,%r13
cmp    %r13,%r12
jne    1270 <EVP_SealInit+0xf0>
mov    0x10(%rbp),%eax
mov    %eax,%r12d
jmp    12c2 <EVP_SealInit+0x142>
xor    %r12d,%r12d
jmp    12d0 <EVP_SealInit+0x150>
xor    %r12d,%r12d
jmp    12c6 <EVP_SealInit+0x146>
mov    $0xffffffff,%r12d
mov    -0x30(%rbp),%rbx
mov    %r15,%rdi
mov    %ebx,%esi
call   1050 <OPENSSL_cleanse@plt>
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_MAX_KEY_LENGTH = global i32 0
define i32 @EVP_SealInit(i32* %0, i32* %1, i8** %2, i32* %3, i8* %4, i32** %5, i32 %6) {
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i8**
%12 = alloca i32*
%13 = alloca i8*
%14 = alloca i32**
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i64
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i32* %0, i32** %9
store i32* %1, i32** %10
store i8** %2, i8*** %11
store i32* %3, i32** %12
store i8* %4, i8** %13
store i32** %5, i32*** %14
store i32 %6, i32* %15
%21 = load i32, i32* @EVP_MAX_KEY_LENGTH
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %16
%24 = alloca i8, i64 %22
store i64 %22, i64* %17
store i32 0, i32* %19
%25 = load i32*, i32** %10
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %36
27: ; preds = %7
%28 = load i32*, i32** %9
%29 = call i32 @EVP_CIPHER_CTX_reset(i32* %28)
%30 = load i32*, i32** %9
%31 = load i32*, i32** %10
%32 = call i32 @EVP_EncryptInit_ex(i32* %30, i32* %31, i32* null, i8* null, i8* null)
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %27
store i32 0, i32* %8
store i32 1, i32* %20
br label %105
35: ; preds = %27
br label %36
36: ; preds = %35, %7
%37 = load i32, i32* %15
%38 = icmp sle i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = load i32**, i32*** %14
%41 = icmp ne i32** %40, null
br i1 %41, label %43, label %42
42: ; preds = %39, %36
store i32 1, i32* %8
store i32 1, i32* %20
br label %105
43: ; preds = %39
%44 = load i32*, i32** %9
%45 = call i64 @EVP_CIPHER_CTX_rand_key(i32* %44, i8* %24)
%46 = icmp sle i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
store i32 0, i32* %8
store i32 1, i32* %20
br label %105
48: ; preds = %43
%49 = load i32*, i32** %9
%50 = call i64 @EVP_CIPHER_CTX_iv_length(i32* %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %48
%53 = load i8*, i8** %13
%54 = load i32*, i32** %9
%55 = call i64 @EVP_CIPHER_CTX_iv_length(i32* %54)
%56 = call i64 @RAND_bytes(i8* %53, i64 %55)
%57 = icmp sle i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %52
br label %101
59: ; preds = %52, %48
%60 = load i32*, i32** %9
%61 = load i8*, i8** %13
%62 = call i32 @EVP_EncryptInit_ex(i32* %60, i32* null, i32* null, i8* %24, i8* %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %59
br label %101
65: ; preds = %59
store i32 0, i32* %18
br label %66
66: ; preds = %96, %65
%67 = load i32, i32* %18
%68 = load i32, i32* %15
%69 = icmp slt i32 %67, %68
br i1 %69, label %70, label %99
70: ; preds = %66
%71 = load i8**, i8*** %11
%72 = load i32, i32* %18
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8*, i8** %71, i64 %73
%75 = load i8*, i8** %74
%76 = load i32*, i32** %9
%77 = call i32 @EVP_CIPHER_CTX_key_length(i32* %76)
%78 = load i32**, i32*** %14
%79 = load i32, i32* %18
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i32*, i32** %78, i64 %80
%82 = load i32*, i32** %81
%83 = call i32 @EVP_PKEY_encrypt_old(i8* %75, i8* %24, i32 %77, i32* %82)
%84 = load i32*, i32** %12
%85 = load i32, i32* %18
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i32, i32* %84, i64 %86
store i32 %83, i32* %87
%88 = load i32*, i32** %12
%89 = load i32, i32* %18
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i32, i32* %88, i64 %90
%92 = load i32, i32* %91
%93 = icmp sle i32 %92, 0
br i1 %93, label %94, label %95
94: ; preds = %70
store i32 -1, i32* %19
br label %101
95: ; preds = %70
br label %96
96: ; preds = %95
%97 = load i32, i32* %18
%98 = add nsw i32 %97, 1
store i32 %98, i32* %18
br label %66
99: ; preds = %66
%100 = load i32, i32* %15
store i32 %100, i32* %19
br label %101
101: ; preds = %99, %94, %64, %58
%102 = trunc i64 %22 to i32
%103 = call i32 @OPENSSL_cleanse(i8* %24, i32 %102)
%104 = load i32, i32* %19
store i32 %104, i32* %8
store i32 1, i32* %20
br label %105
105: ; preds = %101, %47, %42, %34
%106 = load i8*, i8** %16
%107 = load i32, i32* %8
ret i32 %107
}

LLVM IR (-O2):
@EVP_MAX_KEY_LENGTH = global i32 0
define i32 @EVP_SealInit(i32* %0, i32* %1, i8** nocapture %2, i32* nocapture writeonly %3, i8* %4, i32** %5, i32 %6) {
%8 = load i32, i32* @EVP_MAX_KEY_LENGTH
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = icmp eq i32* %1, null
br i1 %11, label %16, label %12
12: ; preds = %7
%13 = tail call i32 @EVP_CIPHER_CTX_reset(i32* %0)
%14 = tail call i32 @EVP_EncryptInit_ex(i32* %0, i32* %1, i32* null, i8* null, i8* null)
%15 = icmp eq i32 %14, 0
br i1 %15, label %51, label %16
16: ; preds = %12, %7
%17 = icmp sgt i32 %6, 0
%18 = icmp ne i32** %5, null
%19 = and i1 %18, %17
br i1 %19, label %20, label %51
20: ; preds = %16
%21 = call i64 @EVP_CIPHER_CTX_rand_key(i32* %0, i8* %10)
%22 = icmp slt i64 %21, 1
br i1 %22, label %51, label %23
23: ; preds = %20
%24 = call i64 @EVP_CIPHER_CTX_iv_length(i32* %0)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = call i64 @EVP_CIPHER_CTX_iv_length(i32* %0)
%28 = call i64 @RAND_bytes(i8* %4, i64 %27)
%29 = icmp slt i64 %28, 1
br i1 %29, label %48, label %30
30: ; preds = %26, %23
%31 = call i32 @EVP_EncryptInit_ex(i32* %0, i32* null, i32* null, i8* %10, i8* %4)
%32 = icmp eq i32 %31, 0
br i1 %32, label %48, label %33
33: ; preds = %30
%34 = zext i32 %6 to i64
br label %37
35: ; preds = %37
%36 = icmp eq i64 %47, %34
br i1 %36, label %48, label %37
37: ; preds = %33, %35
%38 = phi i64 [ 0, %33 ], [ %47, %35 ]
%39 = getelementptr inbounds i8*, i8** %2, i64 %38
%40 = load i8*, i8** %39
%41 = call i32 @EVP_CIPHER_CTX_key_length(i32* %0)
%42 = getelementptr inbounds i32*, i32** %5, i64 %38
%43 = load i32*, i32** %42
%44 = call i32 @EVP_PKEY_encrypt_old(i8* %40, i8* %10, i32 %41, i32* %43)
%45 = getelementptr inbounds i32, i32* %3, i64 %38
store i32 %44, i32* %45
%46 = icmp slt i32 %44, 1
%47 = add nuw nsw i64 %38, 1
br i1 %46, label %48, label %35
48: ; preds = %37, %35, %30, %26
%49 = phi i32 [ 0, %26 ], [ 0, %30 ], [ -1, %37 ], [ %6, %35 ]
%50 = call i32 @OPENSSL_cleanse(i8* %10, i32 %8)
br label %51
51: ; preds = %20, %16, %12, %48
%52 = phi i32 [ %49, %48 ], [ 0, %12 ], [ 1, %16 ], [ 0, %20 ]
ret i32 %52
}