#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*/ PKCS7_R_CTRL_ERROR;
int /*<<< orphan*/ PKCS7error(int /*<<< orphan*/);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ freezero(unsigned char *, int);
unsigned char *malloc(size_t);
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) {
    PKCS7error(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 = malloc(eklen);
  if (ek == NULL) {
    PKCS7error(ERR_R_MALLOC_FAILURE);
    goto err;
  }
  if (EVP_PKEY_decrypt(pctx, ek, &eklen, ri->enc_key->data,
                       ri->enc_key->length) <= 0) {
    ret = 0;
    PKCS7error(ERR_R_EVP_LIB);
    goto err;
  }
  ret = 1;
  freezero(*pek, *peklen);
  *pek = ek;
  *peklen = eklen;
err:
  EVP_PKEY_CTX_free(pctx);
  if (!ret && ek)
    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    1348 <pkcs7_decrypt_rinfo+0x1b8>
mov    -0x30(%rbp),%rdi
call   10a0 <EVP_PKEY_decrypt_init@plt>
cmp    $0x0,%rax
jg     11ff <pkcs7_decrypt_rinfo+0x6f>
jmp    131b <pkcs7_decrypt_rinfo+0x18b>
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   1070 <EVP_PKEY_CTX_ctrl@plt>
cmp    $0x0,%rax
jg     1243 <pkcs7_decrypt_rinfo+0xb3>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1090 <PKCS7error@plt>
jmp    131b <pkcs7_decrypt_rinfo+0x18b>
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   1060 <EVP_PKEY_decrypt@plt>
cmp    $0x0,%rax
jg     1277 <pkcs7_decrypt_rinfo+0xe7>
jmp    131b <pkcs7_decrypt_rinfo+0x18b>
mov    -0x40(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12a2 <pkcs7_decrypt_rinfo+0x112>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1090 <PKCS7error@plt>
jmp    131b <pkcs7_decrypt_rinfo+0x18b>
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   1060 <EVP_PKEY_decrypt@plt>
cmp    $0x0,%rax
jg     12eb <pkcs7_decrypt_rinfo+0x15b>
movl   $0x0,-0x44(%rbp)
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   1090 <PKCS7error@plt>
jmp    131b <pkcs7_decrypt_rinfo+0x18b>
movl   $0x1,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1040 <freezero@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    1342 <pkcs7_decrypt_rinfo+0x1b2>
cmpq   $0x0,-0x38(%rbp)
je     1342 <pkcs7_decrypt_rinfo+0x1b2>
mov    -0x38(%rbp),%rdi
call   1050 <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     125f <pkcs7_decrypt_rinfo+0xcf>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <EVP_PKEY_decrypt_init@plt>
test   %rax,%rax
jle    1266 <pkcs7_decrypt_rinfo+0xd6>
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   1070 <EVP_PKEY_CTX_ctrl@plt>
test   %rax,%rax
jle    126d <pkcs7_decrypt_rinfo+0xdd>
mov    (%r12),%rax
mov    (%rax),%r8d
mov    0x4(%rax),%ecx
mov    %rsp,%rdx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <EVP_PKEY_decrypt@plt>
test   %rax,%rax
jle    1284 <pkcs7_decrypt_rinfo+0xf4>
mov    (%rsp),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1276 <pkcs7_decrypt_rinfo+0xe6>
mov    %rax,%r13
mov    (%r12),%rax
mov    (%rax),%r8d
mov    0x4(%rax),%ecx
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <EVP_PKEY_decrypt@plt>
test   %rax,%rax
jle    129d <pkcs7_decrypt_rinfo+0x10d>
mov    (%r15),%rdi
mov    (%r14),%esi
call   1040 <freezero@plt>
mov    %r13,(%r15)
mov    (%rsp),%eax
mov    %eax,(%r14)
mov    $0x1,%ebp
jmp    1284 <pkcs7_decrypt_rinfo+0xf4>
mov    $0xffffffff,%ebp
jmp    128c <pkcs7_decrypt_rinfo+0xfc>
mov    $0xffffffff,%ebp
jmp    1284 <pkcs7_decrypt_rinfo+0xf4>
mov    0x2d74(%rip),%rax
jmp    127d <pkcs7_decrypt_rinfo+0xed>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1090 <PKCS7error@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    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1090 <PKCS7error@plt>
mov    %rbx,%rdi
call   10b0 <EVP_PKEY_CTX_free@plt>
mov    %r13,%rdi
call   1050 <free@plt>
xor    %ebp,%ebp
jmp    128c <pkcs7_decrypt_rinfo+0xfc>

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_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 %86
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 %34
31: ; preds = %24
%32 = load i32, i32* @PKCS7_R_CTRL_ERROR
%33 = call i32 @PKCS7error(i32 %32)
br label %86
34: ; preds = %24
%35 = load i32*, i32** %10
%36 = load %struct.TYPE_6__*, %struct.TYPE_6__** %8
%37 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %36, i32 0, i32 0
%38 = load %struct.TYPE_5__*, %struct.TYPE_5__** %37
%39 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = load %struct.TYPE_6__*, %struct.TYPE_6__** %8
%42 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %41, i32 0, i32 0
%43 = load %struct.TYPE_5__*, %struct.TYPE_5__** %42
%44 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i64 @EVP_PKEY_decrypt(i32* %35, i8* null, i64* %12, i32 %40, i32 %45)
%47 = icmp sle i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %34
br label %86
49: ; preds = %34
%50 = load i64, i64* %12
%51 = call i8* @malloc(i64 %50)
store i8* %51, i8** %11
%52 = load i8*, i8** %11
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %57
54: ; preds = %49
%55 = load i32, i32* @ERR_R_MALLOC_FAILURE
%56 = call i32 @PKCS7error(i32 %55)
br label %86
57: ; preds = %49
%58 = load i32*, i32** %10
%59 = load i8*, i8** %11
%60 = load %struct.TYPE_6__*, %struct.TYPE_6__** %8
%61 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %60, i32 0, i32 0
%62 = load %struct.TYPE_5__*, %struct.TYPE_5__** %61
%63 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %62, i32 0, i32 1
%64 = load i32, i32* %63
%65 = load %struct.TYPE_6__*, %struct.TYPE_6__** %8
%66 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %65, i32 0, i32 0
%67 = load %struct.TYPE_5__*, %struct.TYPE_5__** %66
%68 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %67, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i64 @EVP_PKEY_decrypt(i32* %58, i8* %59, i64* %12, i32 %64, i32 %69)
%71 = icmp sle i64 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %57
store i32 0, i32* %13
%73 = load i32, i32* @ERR_R_EVP_LIB
%74 = call i32 @PKCS7error(i32 %73)
br label %86
75: ; preds = %57
store i32 1, i32* %13
%76 = load i8**, i8*** %6
%77 = load i8*, i8** %76
%78 = load i32*, i32** %7
%79 = load i32, i32* %78
%80 = call i32 @freezero(i8* %77, i32 %79)
%81 = load i8*, i8** %11
%82 = load i8**, i8*** %6
store i8* %81, i8** %82
%83 = load i64, i64* %12
%84 = trunc i64 %83 to i32
%85 = load i32*, i32** %7
store i32 %84, i32* %85
br label %86
86: ; preds = %75, %72, %54, %48, %31, %23
%87 = load i32*, i32** %10
%88 = call i32 @EVP_PKEY_CTX_free(i32* %87)
%89 = load i32, i32* %13
%90 = icmp ne i32 %89, 0
br i1 %90, label %97, label %91
91: ; preds = %86
%92 = load i8*, i8** %11
%93 = icmp ne i8* %92, null
br i1 %93, label %94, label %97
94: ; preds = %91
%95 = load i8*, i8** %11
%96 = call i32 @free(i8* %95)
br label %97
97: ; preds = %94, %91, %86
%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_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 %58, 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 %50, 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 %20
17: ; preds = %12
%18 = load i32, i32* @PKCS7_R_CTRL_ERROR
%19 = tail call i32 @PKCS7error(i32 %18)
br label %50
20: ; preds = %12
%21 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %2, i64 0, i32 0
%22 = load %struct.TYPE_5__*, %struct.TYPE_5__** %21
%23 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %22, i64 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %22, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i64 @EVP_PKEY_decrypt(i32* %7, i8* null, i64* %5, i32 %24, i32 %26)
%28 = icmp slt i64 %27, 1
br i1 %28, label %50, label %29
29: ; preds = %20
%30 = load i64, i64* %5
%31 = call i8* @malloc(i64 %30)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* @ERR_R_MALLOC_FAILURE
%35 = call i32 @PKCS7error(i32 %34)
br label %50
36: ; preds = %29
%37 = load %struct.TYPE_5__*, %struct.TYPE_5__** %21
%38 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %37, i64 0, i32 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %37, i64 0, i32 0
%41 = load i32, i32* %40
%42 = call i64 @EVP_PKEY_decrypt(i32* %7, i8* %31, i64* %5, i32 %39, i32 %41)
%43 = icmp slt i64 %42, 1
br i1 %43, label %53, label %44
44: ; preds = %36
%45 = load i8*, i8** %0
%46 = load i32, i32* %1
%47 = call i32 @freezero(i8* %45, i32 %46)
store i8* %31, i8** %0
%48 = load i64, i64* %5
%49 = trunc i64 %48 to i32
store i32 %49, i32* %1
br label %50
50: ; preds = %9, %17, %20, %33, %44
%51 = phi i32 [ 1, %44 ], [ -1, %33 ], [ -1, %20 ], [ -1, %17 ], [ -1, %9 ]
%52 = call i32 @EVP_PKEY_CTX_free(i32* %7)
br label %58
53: ; preds = %36
%54 = load i32, i32* @ERR_R_EVP_LIB
%55 = call i32 @PKCS7error(i32 %54)
%56 = call i32 @EVP_PKEY_CTX_free(i32* %7)
%57 = call i32 @free(i8* %31)
br label %58
58: ; preds = %50, %53, %4
%59 = phi i32 [ -1, %4 ], [ 0, %53 ], [ %51, %50 ]
ret i32 %59
}