#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*/ PKCS7_R_CTRL_ERROR;
int /*<<< orphan*/ PKCS7error(int /*<<< orphan*/);
int /*<<< orphan*/ *X509_get_pubkey(int /*<<< orphan*/);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(size_t);
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) {
    PKCS7error(PKCS7_R_CTRL_ERROR);
    goto err;
  }
  if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
    goto err;
  ek = malloc(eklen);
  if (ek == NULL) {
    PKCS7error(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_free(pkey);
  EVP_PKEY_CTX_free(pctx);
  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    134b <pkcs7_encode_rinfo+0x19b>
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    134b <pkcs7_encode_rinfo+0x19b>
mov    -0x28(%rbp),%rdi
call   1040 <EVP_PKEY_encrypt_init@plt>
cmp    $0x0,%rax
jg     1249 <pkcs7_encode_rinfo+0x99>
jmp    132a <pkcs7_encode_rinfo+0x17a>
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   1070 <EVP_PKEY_CTX_ctrl@plt>
cmp    $0x0,%rax
jg     128d <pkcs7_encode_rinfo+0xdd>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   10c0 <PKCS7error@plt>
jmp    132a <pkcs7_encode_rinfo+0x17a>
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     12b5 <pkcs7_encode_rinfo+0x105>
jmp    132a <pkcs7_encode_rinfo+0x17a>
mov    -0x48(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12e0 <pkcs7_encode_rinfo+0x130>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
call   10c0 <PKCS7error@plt>
jmp    132a <pkcs7_encode_rinfo+0x17a>
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     1308 <pkcs7_encode_rinfo+0x158>
jmp    132a <pkcs7_encode_rinfo+0x17a>
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)
mov    -0x30(%rbp),%rdi
call   1060 <EVP_PKEY_free@plt>
mov    -0x28(%rbp),%rdi
call   10d0 <EVP_PKEY_CTX_free@plt>
mov    -0x38(%rbp),%rdi
call   1050 <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,%r12
mov    0x4(%rdi),%edi
call   1080 <X509_get_pubkey@plt>
test   %rax,%rax
je     1299 <pkcs7_encode_rinfo+0xe9>
mov    %rax,%r15
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <EVP_PKEY_CTX_new@plt>
test   %rax,%rax
je     12d1 <pkcs7_encode_rinfo+0x121>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <EVP_PKEY_encrypt_init@plt>
test   %rax,%rax
jle    12b4 <pkcs7_encode_rinfo+0x104>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    0x2dbc(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
mov    %r14,%rdi
mov    $0xffffffff,%esi
xor    %r8d,%r8d
mov    %r12,%r9
call   1070 <EVP_PKEY_CTX_ctrl@plt>
test   %rax,%rax
jle    129d <pkcs7_encode_rinfo+0xed>
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    1294 <pkcs7_encode_rinfo+0xe4>
mov    %r13d,0x14(%rsp)
mov    0x8(%rsp),%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     12a6 <pkcs7_encode_rinfo+0xf6>
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    12b9 <pkcs7_encode_rinfo+0x109>
mov    (%r12),%edi
mov    0x8(%rsp),%rdx
mov    %r13,%rsi
call   10a0 <ASN1_STRING_set0@plt>
mov    $0x1,%ebx
xor    %r13d,%r13d
jmp    12b9 <pkcs7_encode_rinfo+0x109>
xor    %ebx,%ebx
jmp    12d1 <pkcs7_encode_rinfo+0x121>
mov    0x2d44(%rip),%rax
jmp    12ad <pkcs7_encode_rinfo+0xfd>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   10c0 <PKCS7error@plt>
xor    %r13d,%r13d
xor    %ebx,%ebx
mov    %r15,%rdi
call   1060 <EVP_PKEY_free@plt>
mov    %r14,%rdi
call   10d0 <EVP_PKEY_CTX_free@plt>
mov    %r13,%rdi
call   1050 <free@plt>
mov    %ebx,%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_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 %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 %71
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 %41
38: ; preds = %31
%39 = load i32, i32* @PKCS7_R_CTRL_ERROR
%40 = call i32 @PKCS7error(i32 %39)
br label %71
41: ; preds = %31
%42 = load i32*, i32** %8
%43 = load i8*, i8** %6
%44 = load i32, i32* %7
%45 = call i64 @EVP_PKEY_encrypt(i32* %42, i8* null, i64* %12, i8* %43, i32 %44)
%46 = icmp sle i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %41
br label %71
48: ; preds = %41
%49 = load i64, i64* %12
%50 = call i8* @malloc(i64 %49)
store i8* %50, i8** %10
%51 = load i8*, i8** %10
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = load i32, i32* @ERR_R_MALLOC_FAILURE
%55 = call i32 @PKCS7error(i32 %54)
br label %71
56: ; preds = %48
%57 = load i32*, i32** %8
%58 = load i8*, i8** %10
%59 = load i8*, i8** %6
%60 = load i32, i32* %7
%61 = call i64 @EVP_PKEY_encrypt(i32* %57, i8* %58, i64* %12, i8* %59, i32 %60)
%62 = icmp sle i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %56
br label %71
64: ; preds = %56
%65 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%66 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = load i8*, i8** %10
%69 = load i64, i64* %12
%70 = call i32 @ASN1_STRING_set0(i32 %67, i8* %68, i64 %69)
store i8* null, i8** %10
store i32 1, i32* %11
br label %71
71: ; preds = %64, %63, %53, %47, %38, %30
%72 = load i32*, i32** %9
%73 = call i32 @EVP_PKEY_free(i32* %72)
%74 = load i32*, i32** %8
%75 = call i32 @EVP_PKEY_CTX_free(i32* %74)
%76 = load i8*, i8** %10
%77 = call i32 @free(i8* %76)
%78 = load i32, i32* %11
store i32 %78, i32* %4
br label %79
79: ; preds = %71, %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_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 %48, 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 %48, 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 %42, 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 %24
21: ; preds = %16
%22 = load i32, i32* @PKCS7_R_CTRL_ERROR
%23 = tail call i32 @PKCS7error(i32 %22)
br label %42
24: ; preds = %16
%25 = call i64 @EVP_PKEY_encrypt(i32* %11, i8* null, i64* %4, i8* %1, i32 %2)
%26 = icmp slt i64 %25, 1
br i1 %26, label %42, label %27
27: ; preds = %24
%28 = load i64, i64* %4
%29 = call i8* @malloc(i64 %28)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @ERR_R_MALLOC_FAILURE
%33 = call i32 @PKCS7error(i32 %32)
br label %42
34: ; preds = %27
%35 = call i64 @EVP_PKEY_encrypt(i32* %11, i8* %29, i64* %4, i8* %1, i32 %2)
%36 = icmp slt i64 %35, 1
br i1 %36, label %42, label %37
37: ; preds = %34
%38 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %0, i64 0, i32 0
%39 = load i32, i32* %38
%40 = load i64, i64* %4
%41 = call i32 @ASN1_STRING_set0(i32 %39, i8* %29, i64 %40)
br label %42
42: ; preds = %34, %24, %13, %37, %31, %21
%43 = phi i8* [ null, %13 ], [ null, %21 ], [ null, %24 ], [ null, %31 ], [ %29, %34 ], [ null, %37 ]
%44 = phi i32 [ 0, %13 ], [ 0, %21 ], [ 0, %24 ], [ 0, %31 ], [ 0, %34 ], [ 1, %37 ]
%45 = call i32 @EVP_PKEY_free(i32* %8)
%46 = call i32 @EVP_PKEY_CTX_free(i32* %11)
%47 = call i32 @free(i8* %43)
br label %48
48: ; preds = %10, %3, %42
%49 = phi i32 [ %44, %42 ], [ 0, %3 ], [ 0, %10 ]
ret i32 %49
}