#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ enc_key;
  int /*<<< orphan*/ cert;
};
typedef TYPE_1__ PKCS7_RECIP_INFO;
typedef int /*<<< orphan*/ EVP_PKEY_CTX;
typedef int /*<<< orphan*/ EVP_PKEY;

/* Variables and functions */
int /*<<< orphan*/ ASN1_STRING_set0(int /*<<< orphan*/, unsigned char *,
                                    size_t);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ EVP_PKEY_CTRL_PKCS7_ENCRYPT;
scalar_t__ EVP_PKEY_CTX_ctrl(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                             int /*<<< orphan*/, int /*<<< orphan*/,
                             TYPE_1__ *);
int /*<<< orphan*/ EVP_PKEY_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_CTX_new(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_OP_ENCRYPT;
scalar_t__ EVP_PKEY_encrypt(int /*<<< orphan*/ *, unsigned char *, size_t *,
                            unsigned char *, int);
scalar_t__ EVP_PKEY_encrypt_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ OPENSSL_free(unsigned char *);
unsigned char *OPENSSL_malloc(size_t);
int /*<<< orphan*/ PKCS7_F_PKCS7_ENCODE_RINFO;
int /*<<< orphan*/ PKCS7_R_CTRL_ERROR;
int /*<<< orphan*/ PKCS7err(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *X509_get_pubkey(int /*<<< orphan*/);
int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri, unsigned char *key, int keylen) {
  EVP_PKEY_CTX *pctx = NULL;
  EVP_PKEY *pkey = NULL;
  unsigned char *ek = NULL;
  int ret = 0;
  size_t eklen;
  pkey = X509_get_pubkey(ri->cert);
  if (!pkey)
    return 0;
  pctx = EVP_PKEY_CTX_new(pkey, NULL);
  if (!pctx)
    return 0;
  if (EVP_PKEY_encrypt_init(pctx) <= 0)
    goto err;
  if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
                        EVP_PKEY_CTRL_PKCS7_ENCRYPT, 0, ri) <= 0) {
    PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, PKCS7_R_CTRL_ERROR);
    goto err;
  }
  if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
    goto err;
  ek = OPENSSL_malloc(eklen);
  if (ek == NULL) {
    PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, ERR_R_MALLOC_FAILURE);
    goto err;
  }
  if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
    goto err;
  ASN1_STRING_set0(ri->enc_key, ek, eklen);
  ek = NULL;
  ret = 1;
err:
  if (pkey)
    EVP_PKEY_free(pkey);
  if (pctx)
    EVP_PKEY_CTX_free(pctx);
  if (ek)
    OPENSSL_free(ek);
  return ret;
}


x86 assembly (clang, -O0):
<pkcs7_encode_rinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1080 <X509_get_pubkey@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1209 <pkcs7_encode_rinfo+0x59>
movl   $0x0,-0x4(%rbp)
jmp    137e <pkcs7_encode_rinfo+0x1ce>
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <EVP_PKEY_CTX_new@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1231 <pkcs7_encode_rinfo+0x81>
movl   $0x0,-0x4(%rbp)
jmp    137e <pkcs7_encode_rinfo+0x1ce>
mov    -0x28(%rbp),%rdi
call   1040 <EVP_PKEY_encrypt_init@plt>
cmp    $0x0,%rax
jg     1249 <pkcs7_encode_rinfo+0x99>
jmp    133c <pkcs7_encode_rinfo+0x18c>
mov    -0x28(%rbp),%rdi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
mov    0x2d73(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%r9
mov    $0xffffffff,%esi
xor    %r8d,%r8d
call   1060 <EVP_PKEY_CTX_ctrl@plt>
cmp    $0x0,%rax
jg     1296 <pkcs7_encode_rinfo+0xe6>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
jmp    133c <pkcs7_encode_rinfo+0x18c>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%esi
lea    -0x48(%rbp),%rdx
call   10b0 <EVP_PKEY_encrypt@plt>
cmp    $0x0,%rax
jg     12be <pkcs7_encode_rinfo+0x10e>
jmp    133c <pkcs7_encode_rinfo+0x18c>
mov    -0x48(%rbp),%rdi
call   1090 <OPENSSL_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12f2 <pkcs7_encode_rinfo+0x142>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
mov    0x2cf2(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
jmp    133c <pkcs7_encode_rinfo+0x18c>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
lea    -0x48(%rbp),%rdx
call   10b0 <EVP_PKEY_encrypt@plt>
cmp    $0x0,%rax
jg     131a <pkcs7_encode_rinfo+0x16a>
jmp    133c <pkcs7_encode_rinfo+0x18c>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   10a0 <ASN1_STRING_set0@plt>
movq   $0x0,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1350 <pkcs7_encode_rinfo+0x1a0>
mov    -0x30(%rbp),%rdi
call   1050 <EVP_PKEY_free@plt>
cmpq   $0x0,-0x28(%rbp)
je     1364 <pkcs7_encode_rinfo+0x1b4>
mov    -0x28(%rbp),%rdi
call   10d0 <EVP_PKEY_CTX_free@plt>
cmpq   $0x0,-0x38(%rbp)
je     1378 <pkcs7_encode_rinfo+0x1c8>
mov    -0x38(%rbp),%rdi
call   10c0 <OPENSSL_free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkcs7_encode_rinfo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r13d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    0x4(%rdi),%edi
call   1080 <X509_get_pubkey@plt>
test   %rax,%rax
je     129a <pkcs7_encode_rinfo+0xea>
mov    %rax,%r15
xor    %r12d,%r12d
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <EVP_PKEY_CTX_new@plt>
test   %rax,%rax
je     12eb <pkcs7_encode_rinfo+0x13b>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <EVP_PKEY_encrypt_init@plt>
test   %rax,%rax
jle    12c8 <pkcs7_encode_rinfo+0x118>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    0x2dbb(%rip),%rax
mov    (%rax),%ecx
xor    %r12d,%r12d
mov    %r14,%rdi
mov    $0xffffffff,%esi
xor    %r8d,%r8d
mov    %rbx,%r9
call   1060 <EVP_PKEY_CTX_ctrl@plt>
test   %rax,%rax
jle    129f <pkcs7_encode_rinfo+0xef>
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbp,%rcx
mov    %r13d,%r8d
call   10b0 <EVP_PKEY_encrypt@plt>
test   %rax,%rax
jle    1295 <pkcs7_encode_rinfo+0xe5>
mov    %r13d,0x14(%rsp)
mov    0x8(%rsp),%rdi
call   1090 <OPENSSL_malloc@plt>
test   %rax,%rax
je     12b1 <pkcs7_encode_rinfo+0x101>
mov    %rax,%r13
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    %rax,%rsi
mov    %rbp,%rcx
mov    0x14(%rsp),%r8d
call   10b0 <EVP_PKEY_encrypt@plt>
test   %rax,%rax
jle    12ce <pkcs7_encode_rinfo+0x11e>
mov    (%rbx),%edi
mov    0x8(%rsp),%rdx
mov    %r13,%rsi
call   10a0 <ASN1_STRING_set0@plt>
mov    $0x1,%r12d
xor    %r13d,%r13d
jmp    12ce <pkcs7_encode_rinfo+0x11e>
xor    %r12d,%r12d
jmp    12eb <pkcs7_encode_rinfo+0x13b>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
mov    0x2d39(%rip),%rax
jmp    12c1 <pkcs7_encode_rinfo+0x111>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
xor    %r13d,%r13d
xor    %r12d,%r12d
mov    %r15,%rdi
call   1050 <EVP_PKEY_free@plt>
mov    %r14,%rdi
call   10d0 <EVP_PKEY_CTX_free@plt>
test   %r13,%r13
je     12eb <pkcs7_encode_rinfo+0x13b>
mov    %r13,%rdi
call   10c0 <OPENSSL_free@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32 }
@EVP_PKEY_OP_ENCRYPT = global i32 0
@EVP_PKEY_CTRL_PKCS7_ENCRYPT = global i32 0
@PKCS7_F_PKCS7_ENCODE_RINFO = global i32 0
@PKCS7_R_CTRL_ERROR = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32 @pkcs7_encode_rinfo(%struct.TYPE_4__* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.TYPE_4__*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i64
store %struct.TYPE_4__* %0, %struct.TYPE_4__** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* null, i32** %8
store i32* null, i32** %9
store i8* null, i8** %10
store i32 0, i32* %11
%13 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%14 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i32* @X509_get_pubkey(i32 %15)
store i32* %16, i32** %9
%17 = load i32*, i32** %9
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %3
store i32 0, i32* %4
br label %93
20: ; preds = %3
%21 = load i32*, i32** %9
%22 = call i32* @EVP_PKEY_CTX_new(i32* %21, i32* null)
store i32* %22, i32** %8
%23 = load i32*, i32** %8
%24 = icmp ne i32* %23, null
br i1 %24, label %26, label %25
25: ; preds = %20
store i32 0, i32* %4
br label %93
26: ; preds = %20
%27 = load i32*, i32** %8
%28 = call i64 @EVP_PKEY_encrypt_init(i32* %27)
%29 = icmp sle i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
br label %73
31: ; preds = %26
%32 = load i32*, i32** %8
%33 = load i32, i32* @EVP_PKEY_OP_ENCRYPT
%34 = load i32, i32* @EVP_PKEY_CTRL_PKCS7_ENCRYPT
%35 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%36 = call i64 @EVP_PKEY_CTX_ctrl(i32* %32, i32 -1, i32 %33, i32 %34, i32 0, %struct.TYPE_4__* %35)
%37 = icmp sle i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %31
%39 = load i32, i32* @PKCS7_F_PKCS7_ENCODE_RINFO
%40 = load i32, i32* @PKCS7_R_CTRL_ERROR
%41 = call i32 @PKCS7err(i32 %39, i32 %40)
br label %73
42: ; preds = %31
%43 = load i32*, i32** %8
%44 = load i8*, i8** %6
%45 = load i32, i32* %7
%46 = call i64 @EVP_PKEY_encrypt(i32* %43, i8* null, i64* %12, i8* %44, i32 %45)
%47 = icmp sle i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %42
br label %73
49: ; preds = %42
%50 = load i64, i64* %12
%51 = call i8* @OPENSSL_malloc(i64 %50)
store i8* %51, i8** %10
%52 = load i8*, i8** %10
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %58
54: ; preds = %49
%55 = load i32, i32* @PKCS7_F_PKCS7_ENCODE_RINFO
%56 = load i32, i32* @ERR_R_MALLOC_FAILURE
%57 = call i32 @PKCS7err(i32 %55, i32 %56)
br label %73
58: ; preds = %49
%59 = load i32*, i32** %8
%60 = load i8*, i8** %10
%61 = load i8*, i8** %6
%62 = load i32, i32* %7
%63 = call i64 @EVP_PKEY_encrypt(i32* %59, i8* %60, i64* %12, i8* %61, i32 %62)
%64 = icmp sle i64 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %58
br label %73
66: ; preds = %58
%67 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%68 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %67, i32 0, i32 0
%69 = load i32, i32* %68
%70 = load i8*, i8** %10
%71 = load i64, i64* %12
%72 = call i32 @ASN1_STRING_set0(i32 %69, i8* %70, i64 %71)
store i8* null, i8** %10
store i32 1, i32* %11
br label %73
73: ; preds = %66, %65, %54, %48, %38, %30
%74 = load i32*, i32** %9
%75 = icmp ne i32* %74, null
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = load i32*, i32** %9
%78 = call i32 @EVP_PKEY_free(i32* %77)
br label %79
79: ; preds = %76, %73
%80 = load i32*, i32** %8
%81 = icmp ne i32* %80, null
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = load i32*, i32** %8
%84 = call i32 @EVP_PKEY_CTX_free(i32* %83)
br label %85
85: ; preds = %82, %79
%86 = load i8*, i8** %10
%87 = icmp ne i8* %86, null
br i1 %87, label %88, label %91
88: ; preds = %85
%89 = load i8*, i8** %10
%90 = call i32 @OPENSSL_free(i8* %89)
br label %91
91: ; preds = %88, %85
%92 = load i32, i32* %11
store i32 %92, i32* %4
br label %93
93: ; preds = %91, %25, %19
%94 = load i32, i32* %4
ret i32 %94
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32 }
@EVP_PKEY_OP_ENCRYPT = global i32 0
@EVP_PKEY_CTRL_PKCS7_ENCRYPT = global i32 0
@PKCS7_F_PKCS7_ENCODE_RINFO = global i32 0
@PKCS7_R_CTRL_ERROR = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32 @pkcs7_encode_rinfo(%struct.TYPE_4__* %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %0, i64 0, i32 1
%7 = load i32, i32* %6
%8 = tail call i32* @X509_get_pubkey(i32 %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %52, label %10
10: ; preds = %3
%11 = tail call i32* @EVP_PKEY_CTX_new(i32* %8, i32* null)
%12 = icmp eq i32* %11, null
br i1 %12, label %52, label %13
13: ; preds = %10
%14 = tail call i64 @EVP_PKEY_encrypt_init(i32* %11)
%15 = icmp slt i64 %14, 1
br i1 %15, label %44, label %16
16: ; preds = %13
%17 = load i32, i32* @EVP_PKEY_OP_ENCRYPT
%18 = load i32, i32* @EVP_PKEY_CTRL_PKCS7_ENCRYPT
%19 = tail call i64 @EVP_PKEY_CTX_ctrl(i32* %11, i32 -1, i32 %17, i32 %18, i32 0, %struct.TYPE_4__* %0)
%20 = icmp slt i64 %19, 1
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @PKCS7_F_PKCS7_ENCODE_RINFO
%23 = load i32, i32* @PKCS7_R_CTRL_ERROR
%24 = tail call i32 @PKCS7err(i32 %22, i32 %23)
br label %44
25: ; preds = %16
%26 = call i64 @EVP_PKEY_encrypt(i32* %11, i8* null, i64* %4, i8* %1, i32 %2)
%27 = icmp slt i64 %26, 1
br i1 %27, label %44, label %28
28: ; preds = %25
%29 = load i64, i64* %4
%30 = call i8* @OPENSSL_malloc(i64 %29)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* @PKCS7_F_PKCS7_ENCODE_RINFO
%34 = load i32, i32* @ERR_R_MALLOC_FAILURE
%35 = call i32 @PKCS7err(i32 %33, i32 %34)
br label %44
36: ; preds = %28
%37 = call i64 @EVP_PKEY_encrypt(i32* %11, i8* %30, i64* %4, i8* %1, i32 %2)
%38 = icmp slt i64 %37, 1
br i1 %38, label %44, label %39
39: ; preds = %36
%40 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %0, i64 0, i32 0
%41 = load i32, i32* %40
%42 = load i64, i64* %4
%43 = call i32 @ASN1_STRING_set0(i32 %41, i8* %30, i64 %42)
br label %44
44: ; preds = %21, %32, %39, %13, %25, %36
%45 = phi i8* [ null, %13 ], [ null, %21 ], [ null, %25 ], [ null, %32 ], [ %30, %36 ], [ null, %39 ]
%46 = phi i32 [ 0, %13 ], [ 0, %21 ], [ 0, %25 ], [ 0, %32 ], [ 0, %36 ], [ 1, %39 ]
%47 = call i32 @EVP_PKEY_free(i32* %8)
%48 = call i32 @EVP_PKEY_CTX_free(i32* %11)
%49 = icmp eq i8* %45, null
br i1 %49, label %52, label %50
50: ; preds = %44
%51 = call i32 @OPENSSL_free(i8* %45)
br label %52
52: ; preds = %44, %50, %10, %3
%53 = phi i32 [ 0, %3 ], [ 0, %10 ], [ %46, %50 ], [ %46, %44 ]
ret i32 %53
}