#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*/ 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_get0_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_get0_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:
  EVP_PKEY_CTX_free(pctx);
  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   1050 <X509_get0_pubkey@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f9 <pkcs7_encode_rinfo+0x59>
movl   $0x0,-0x4(%rbp)
jmp    1344 <pkcs7_encode_rinfo+0x1a4>
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    1221 <pkcs7_encode_rinfo+0x81>
movl   $0x0,-0x4(%rbp)
jmp    1344 <pkcs7_encode_rinfo+0x1a4>
mov    -0x28(%rbp),%rdi
call   1040 <EVP_PKEY_encrypt_init@plt>
cmp    $0x0,%rax
jg     1239 <pkcs7_encode_rinfo+0x99>
jmp    132c <pkcs7_encode_rinfo+0x18c>
mov    -0x28(%rbp),%rdi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
mov    0x2d83(%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     1286 <pkcs7_encode_rinfo+0xe6>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
jmp    132c <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   10a0 <EVP_PKEY_encrypt@plt>
cmp    $0x0,%rax
jg     12ae <pkcs7_encode_rinfo+0x10e>
jmp    132c <pkcs7_encode_rinfo+0x18c>
mov    -0x48(%rbp),%rdi
call   1080 <OPENSSL_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12e2 <pkcs7_encode_rinfo+0x142>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
jmp    132c <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   10a0 <EVP_PKEY_encrypt@plt>
cmp    $0x0,%rax
jg     130a <pkcs7_encode_rinfo+0x16a>
jmp    132c <pkcs7_encode_rinfo+0x18c>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1090 <ASN1_STRING_set0@plt>
movq   $0x0,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
call   10c0 <EVP_PKEY_CTX_free@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <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
push   %rax
mov    %edx,%r12d
mov    %rsi,%r13
mov    %rdi,%r15
mov    0x4(%rdi),%edi
call   1050 <X509_get0_pubkey@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     12b1 <pkcs7_encode_rinfo+0x111>
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <EVP_PKEY_CTX_new@plt>
test   %rax,%rax
je     12b1 <pkcs7_encode_rinfo+0x111>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <EVP_PKEY_encrypt_init@plt>
test   %rax,%rax
jle    129d <pkcs7_encode_rinfo+0xfd>
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    0x2dd2(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
mov    %r14,%rdi
mov    $0xffffffff,%esi
xor    %r8d,%r8d
mov    %r15,%r9
call   1060 <EVP_PKEY_CTX_ctrl@plt>
test   %rax,%rax
jle    1274 <pkcs7_encode_rinfo+0xd4>
mov    %rsp,%rdx
mov    %r14,%rdi
xor    %esi,%esi
mov    %r13,%rcx
mov    %r12d,%r8d
call   10a0 <EVP_PKEY_encrypt@plt>
test   %rax,%rax
jle    1270 <pkcs7_encode_rinfo+0xd0>
mov    (%rsp),%rdi
call   1080 <OPENSSL_malloc@plt>
test   %rax,%rax
je     1286 <pkcs7_encode_rinfo+0xe6>
mov    %rax,%rbp
mov    %rsp,%rdx
mov    %r14,%rdi
mov    %rax,%rsi
mov    %r13,%rcx
mov    %r12d,%r8d
call   10a0 <EVP_PKEY_encrypt@plt>
test   %rax,%rax
jle    12a1 <pkcs7_encode_rinfo+0x101>
mov    (%r15),%edi
mov    (%rsp),%rdx
mov    %rbp,%rsi
call   1090 <ASN1_STRING_set0@plt>
mov    $0x1,%ebx
xor    %ebp,%ebp
jmp    12a1 <pkcs7_encode_rinfo+0x101>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    0x2d64(%rip),%rax
jmp    1296 <pkcs7_encode_rinfo+0xf6>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
xor    %ebp,%ebp
xor    %ebx,%ebx
mov    %r14,%rdi
call   10c0 <EVP_PKEY_CTX_free@plt>
mov    %rbp,%rdi
call   10b0 <OPENSSL_free@plt>
mov    %ebx,%eax
add    $0x8,%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_get0_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 %79
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 %79
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** %8
%75 = call i32 @EVP_PKEY_CTX_free(i32* %74)
%76 = load i8*, i8** %10
%77 = call i32 @OPENSSL_free(i8* %76)
%78 = load i32, i32* %11
store i32 %78, i32* %4
br label %79
79: ; preds = %73, %25, %19
%80 = load i32, i32* %4
ret i32 %80
}

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_get0_pubkey(i32 %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %49, 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 %49, 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 = %36, %25, %13, %39, %32, %21
%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_CTX_free(i32* %11)
%48 = call i32 @OPENSSL_free(i8* %45)
br label %49
49: ; preds = %10, %3, %44
%50 = phi i32 [ %46, %44 ], [ 0, %3 ], [ 0, %10 ]
ret i32 %50
}