#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_6__ TYPE_2__;
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
struct TYPE_6__ {
  TYPE_1__ *enc_key;
};
struct TYPE_5__ {
  int /*<<< orphan*/ length;
  int /*<<< orphan*/ data;
};
typedef TYPE_2__ PKCS7_RECIP_INFO;
typedef int /*<<< orphan*/ EVP_PKEY_CTX;
typedef int /*<<< orphan*/ EVP_PKEY;

/* Variables and functions */
int /*<<< orphan*/ ERR_R_EVP_LIB;
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ EVP_PKEY_CTRL_PKCS7_DECRYPT;
scalar_t__ EVP_PKEY_CTX_ctrl(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                             int /*<<< orphan*/, int /*<<< orphan*/,
                             TYPE_2__ *);
int /*<<< orphan*/ EVP_PKEY_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_CTX_new(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_OP_DECRYPT;
scalar_t__ EVP_PKEY_decrypt(int /*<<< orphan*/ *, unsigned char *, size_t *,
                            int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ EVP_PKEY_decrypt_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ OPENSSL_clear_free(unsigned char *, int);
int /*<<< orphan*/ OPENSSL_free(unsigned char *);
unsigned char *OPENSSL_malloc(size_t);
int /*<<< orphan*/ PKCS7_F_PKCS7_DECRYPT_RINFO;
int /*<<< orphan*/ PKCS7_R_CTRL_ERROR;
int /*<<< orphan*/ PKCS7err(int /*<<< orphan*/, int /*<<< orphan*/);
int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen, PKCS7_RECIP_INFO *ri,
                        EVP_PKEY *pkey) {
  EVP_PKEY_CTX *pctx = NULL;
  unsigned char *ek = NULL;
  size_t eklen;
  int ret = -1;
  pctx = EVP_PKEY_CTX_new(pkey, NULL);
  if (!pctx)
    return -1;
  if (EVP_PKEY_decrypt_init(pctx) <= 0)
    goto err;
  if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
                        EVP_PKEY_CTRL_PKCS7_DECRYPT, 0, ri) <= 0) {
    PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, PKCS7_R_CTRL_ERROR);
    goto err;
  }
  if (EVP_PKEY_decrypt(pctx, NULL, &eklen, ri->enc_key->data,
                       ri->enc_key->length) <= 0)
    goto err;
  ek = OPENSSL_malloc(eklen);
  if (ek == NULL) {
    PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_MALLOC_FAILURE);
    goto err;
  }
  if (EVP_PKEY_decrypt(pctx, ek, &eklen, ri->enc_key->data,
                       ri->enc_key->length) <= 0) {
    ret = 0;
    PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_EVP_LIB);
    goto err;
  }
  ret = 1;
  OPENSSL_clear_free(*pek, *peklen);
  *pek = ek;
  *peklen = eklen;
err:
  EVP_PKEY_CTX_free(pctx);
  if (!ret)
    OPENSSL_free(ek);
  return ret;
}


x86 assembly (clang, -O0):
<pkcs7_decrypt_rinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <EVP_PKEY_CTX_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e7 <pkcs7_decrypt_rinfo+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    1358 <pkcs7_decrypt_rinfo+0x1c8>
mov    -0x30(%rbp),%rdi
call   10a0 <EVP_PKEY_decrypt_init@plt>
cmp    $0x0,%rax
jg     11ff <pkcs7_decrypt_rinfo+0x6f>
jmp    1336 <pkcs7_decrypt_rinfo+0x1a6>
mov    -0x30(%rbp),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
mov    0x2db5(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r9
mov    $0xffffffff,%esi
xor    %r8d,%r8d
call   1050 <EVP_PKEY_CTX_ctrl@plt>
cmp    $0x0,%rax
jg     124c <pkcs7_decrypt_rinfo+0xbc>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
jmp    1336 <pkcs7_decrypt_rinfo+0x1a6>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x4(%rax),%ecx
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%r8d
xor    %eax,%eax
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
call   1040 <EVP_PKEY_decrypt@plt>
cmp    $0x0,%rax
jg     1280 <pkcs7_decrypt_rinfo+0xf0>
jmp    1336 <pkcs7_decrypt_rinfo+0x1a6>
mov    -0x40(%rbp),%rdi
call   1080 <OPENSSL_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12b4 <pkcs7_decrypt_rinfo+0x124>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
jmp    1336 <pkcs7_decrypt_rinfo+0x1a6>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x4(%rax),%ecx
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%r8d
lea    -0x40(%rbp),%rdx
call   1040 <EVP_PKEY_decrypt@plt>
cmp    $0x0,%rax
jg     1306 <pkcs7_decrypt_rinfo+0x176>
movl   $0x0,-0x44(%rbp)
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
jmp    1336 <pkcs7_decrypt_rinfo+0x1a6>
movl   $0x1,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1070 <OPENSSL_clear_free@plt>
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdi
call   10b0 <EVP_PKEY_CTX_free@plt>
cmpl   $0x0,-0x44(%rbp)
jne    1352 <pkcs7_decrypt_rinfo+0x1c2>
mov    -0x38(%rbp),%rdi
call   1090 <OPENSSL_free@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkcs7_decrypt_rinfo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rcx,%rdi
xor    %esi,%esi
call   1030 <EVP_PKEY_CTX_new@plt>
test   %rax,%rax
je     1263 <pkcs7_decrypt_rinfo+0xd3>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <EVP_PKEY_decrypt_init@plt>
test   %rax,%rax
jle    126a <pkcs7_decrypt_rinfo+0xda>
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
mov    0x2ded(%rip),%rax
mov    (%rax),%ecx
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
mov    $0xffffffff,%esi
xor    %r8d,%r8d
mov    %r12,%r9
call   1050 <EVP_PKEY_CTX_ctrl@plt>
test   %rax,%rax
jle    1271 <pkcs7_decrypt_rinfo+0xe1>
mov    (%r12),%rax
mov    (%rax),%r8d
mov    0x4(%rax),%ecx
mov    %rsp,%rdx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <EVP_PKEY_decrypt@plt>
test   %rax,%rax
jle    129a <pkcs7_decrypt_rinfo+0x10a>
mov    (%rsp),%rdi
call   1080 <OPENSSL_malloc@plt>
test   %rax,%rax
je     1283 <pkcs7_decrypt_rinfo+0xf3>
mov    %rax,%r13
mov    (%r12),%rax
mov    (%rax),%r8d
mov    0x4(%rax),%ecx
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <EVP_PKEY_decrypt@plt>
test   %rax,%rax
jle    12b3 <pkcs7_decrypt_rinfo+0x123>
mov    (%r15),%rdi
mov    (%r14),%esi
call   1070 <OPENSSL_clear_free@plt>
mov    %r13,(%r15)
mov    (%rsp),%eax
mov    %eax,(%r14)
mov    $0x1,%ebp
jmp    129a <pkcs7_decrypt_rinfo+0x10a>
mov    $0xffffffff,%ebp
jmp    12a2 <pkcs7_decrypt_rinfo+0x112>
mov    $0xffffffff,%ebp
jmp    129a <pkcs7_decrypt_rinfo+0x10a>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    0x2d67(%rip),%rax
jmp    1293 <pkcs7_decrypt_rinfo+0x103>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
mov    %rbx,%rdi
call   10b0 <EVP_PKEY_CTX_free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
mov    %rbx,%rdi
call   10b0 <EVP_PKEY_CTX_free@plt>
mov    %r13,%rdi
call   1090 <OPENSSL_free@plt>
xor    %ebp,%ebp
jmp    12a2 <pkcs7_decrypt_rinfo+0x112>

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

LLVM IR (-O2):
%struct.TYPE_6__ = type { %struct.TYPE_5__* }
%struct.TYPE_5__ = type { i32, i32 }
@EVP_PKEY_OP_DECRYPT = global i32 0
@EVP_PKEY_CTRL_PKCS7_DECRYPT = global i32 0
@PKCS7_F_PKCS7_DECRYPT_RINFO = global i32 0
@PKCS7_R_CTRL_ERROR = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@ERR_R_EVP_LIB = global i32 0
define i32 @pkcs7_decrypt_rinfo(i8** nocapture %0, i32* nocapture %1, %struct.TYPE_6__* %2, i32* %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
%7 = tail call i32* @EVP_PKEY_CTX_new(i32* %3, i32* null)
%8 = icmp eq i32* %7, null
br i1 %8, label %61, label %9
9: ; preds = %4
%10 = tail call i64 @EVP_PKEY_decrypt_init(i32* %7)
%11 = icmp slt i64 %10, 1
br i1 %11, label %52, label %12
12: ; preds = %9
%13 = load i32, i32* @EVP_PKEY_OP_DECRYPT
%14 = load i32, i32* @EVP_PKEY_CTRL_PKCS7_DECRYPT
%15 = tail call i64 @EVP_PKEY_CTX_ctrl(i32* %7, i32 -1, i32 %13, i32 %14, i32 0, %struct.TYPE_6__* %2)
%16 = icmp slt i64 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* @PKCS7_F_PKCS7_DECRYPT_RINFO
%19 = load i32, i32* @PKCS7_R_CTRL_ERROR
%20 = tail call i32 @PKCS7err(i32 %18, i32 %19)
br label %52
21: ; preds = %12
%22 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %2, i64 0, i32 0
%23 = load %struct.TYPE_5__*, %struct.TYPE_5__** %22
%24 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %23, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %23, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @EVP_PKEY_decrypt(i32* %7, i8* null, i64* %5, i32 %25, i32 %27)
%29 = icmp slt i64 %28, 1
br i1 %29, label %52, label %30
30: ; preds = %21
%31 = load i64, i64* %5
%32 = call i8* @OPENSSL_malloc(i64 %31)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @PKCS7_F_PKCS7_DECRYPT_RINFO
%36 = load i32, i32* @ERR_R_MALLOC_FAILURE
%37 = call i32 @PKCS7err(i32 %35, i32 %36)
br label %52
38: ; preds = %30
%39 = load %struct.TYPE_5__*, %struct.TYPE_5__** %22
%40 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %39, i64 0, i32 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %39, i64 0, i32 0
%43 = load i32, i32* %42
%44 = call i64 @EVP_PKEY_decrypt(i32* %7, i8* %32, i64* %5, i32 %41, i32 %43)
%45 = icmp slt i64 %44, 1
br i1 %45, label %55, label %46
46: ; preds = %38
%47 = load i8*, i8** %0
%48 = load i32, i32* %1
%49 = call i32 @OPENSSL_clear_free(i8* %47, i32 %48)
store i8* %32, i8** %0
%50 = load i64, i64* %5
%51 = trunc i64 %50 to i32
store i32 %51, i32* %1
br label %52
52: ; preds = %9, %17, %21, %34, %46
%53 = phi i32 [ 1, %46 ], [ -1, %34 ], [ -1, %21 ], [ -1, %17 ], [ -1, %9 ]
%54 = call i32 @EVP_PKEY_CTX_free(i32* %7)
br label %61
55: ; preds = %38
%56 = load i32, i32* @PKCS7_F_PKCS7_DECRYPT_RINFO
%57 = load i32, i32* @ERR_R_EVP_LIB
%58 = call i32 @PKCS7err(i32 %56, i32 %57)
%59 = call i32 @EVP_PKEY_CTX_free(i32* %7)
%60 = call i32 @OPENSSL_free(i8* %32)
br label %61
61: ; preds = %52, %55, %4
%62 = phi i32 [ -1, %4 ], [ 0, %55 ], [ %53, %52 ]
ret i32 %62
}