#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 */

/* Type definitions */
typedef int /*<<< orphan*/ X509;
typedef int /*<<< orphan*/ EVP_PKEY;
typedef int /*<<< orphan*/ CMS_ContentInfo;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
scalar_t__ BUF_strdup(char *);
int CMS_BINARY;
int CMS_PARTIAL;
int CMS_STREAM;
int /*<<< orphan*/ CMS_decrypt(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *, int);
int /*<<< orphan*/ CMS_decrypt_set1_password(int /*<<< orphan*/ *,
                                             unsigned char *, int);
int /*<<< orphan*/ CMS_decrypt_set1_pkey(int /*<<< orphan*/ *,
                                         int /*<<< orphan*/ *,
                                         int /*<<< orphan*/ *);
int /*<<< orphan*/ ERR_print_errors(int /*<<< orphan*/ *);
int /*<<< orphan*/ *d2i_CMS_bio(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int decrypt_cms_legacy(BIO *in, BIO *out, BIO *err, char *password, X509 *x509,
                       EVP_PKEY *key) {
  int flags = CMS_PARTIAL | CMS_STREAM | CMS_BINARY, ret = 1;
  CMS_ContentInfo *cms;
  cms = d2i_CMS_bio(in, NULL);
  if (password) {
    unsigned char *tmp = (unsigned char *)BUF_strdup((char *)password);
    if (!CMS_decrypt_set1_password(cms, tmp, -1)) {
      ERR_print_errors(err);
      goto end;
    }
  } else {
    if (key && x509) {
      if (!CMS_decrypt_set1_pkey(cms, key, x509)) {
        ERR_print_errors(err);
        goto end;
      }
    }
  }
  if (!CMS_decrypt(cms, NULL, NULL, NULL, out, flags)) {
    ERR_print_errors(err);
    goto end;
  }
  ret = 0;
end:
  return ret;
}


x86 assembly (clang, -O0):
<decrypt_cms_legacy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    0x2e50(%rip),%rcx
or     (%rcx),%eax
mov    0x2e57(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <d2i_CMS_bio@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11fc <decrypt_cms_legacy+0x9c>
mov    -0x20(%rbp),%rdi
call   1080 <BUF_strdup@plt>
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    $0xffffffff,%edx
call   1050 <CMS_decrypt_set1_password@plt>
cmp    $0x0,%eax
jne    11f7 <decrypt_cms_legacy+0x97>
mov    -0x18(%rbp),%rdi
call   1040 <ERR_print_errors@plt>
jmp    127d <decrypt_cms_legacy+0x11d>
jmp    1244 <decrypt_cms_legacy+0xe4>
cmpq   $0x0,-0x30(%rbp)
je     123f <decrypt_cms_legacy+0xdf>
cmpq   $0x0,-0x28(%rbp)
je     123f <decrypt_cms_legacy+0xdf>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <CMS_decrypt_set1_pkey@plt>
cmp    $0x0,%eax
jne    123a <decrypt_cms_legacy+0xda>
mov    -0x18(%rbp),%rdi
call   1040 <ERR_print_errors@plt>
jmp    127d <decrypt_cms_legacy+0x11d>
jmp    123f <decrypt_cms_legacy+0xdf>
jmp    1244 <decrypt_cms_legacy+0xe4>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%r8
mov    -0x34(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1030 <CMS_decrypt@plt>
cmp    $0x0,%eax
jne    1276 <decrypt_cms_legacy+0x116>
mov    -0x18(%rbp),%rdi
call   1040 <ERR_print_errors@plt>
jmp    127d <decrypt_cms_legacy+0x11d>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decrypt_cms_legacy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e56(%rip),%rax
mov    0x2e67(%rip),%rbp
mov    0x0(%rbp),%ebp
or     (%rax),%ebp
mov    %r9,%r13
mov    %r8,%r12
mov    %rcx,%rbx
mov    %rdx,(%rsp)
mov    %rsi,%r15
mov    0x2e5b(%rip),%rax
or     (%rax),%ebp
xor    %esi,%esi
call   1060 <d2i_CMS_bio@plt>
mov    %rax,%r14
test   %rbx,%rbx
je     11c4 <decrypt_cms_legacy+0x64>
mov    %rbx,%rdi
call   1080 <BUF_strdup@plt>
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0xffffffff,%edx
call   1050 <CMS_decrypt_set1_password@plt>
test   %eax,%eax
jne    11e0 <decrypt_cms_legacy+0x80>
jmp    11fa <decrypt_cms_legacy+0x9a>
test   %r12,%r12
je     11e0 <decrypt_cms_legacy+0x80>
test   %r13,%r13
je     11e0 <decrypt_cms_legacy+0x80>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1070 <CMS_decrypt_set1_pkey@plt>
test   %eax,%eax
je     11fa <decrypt_cms_legacy+0x9a>
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %ebp,%r9d
call   1030 <CMS_decrypt@plt>
test   %eax,%eax
jne    1208 <decrypt_cms_legacy+0xa8>
mov    (%rsp),%rdi
call   1040 <ERR_print_errors@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@CMS_PARTIAL = global i32 0
@CMS_STREAM = global i32 0
@CMS_BINARY = global i32 0
define i32 @decrypt_cms_legacy(i32* %0, i32* %1, i32* %2, i8* %3, i32* %4, i32* %5) {
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32*
%16 = alloca i8*
store i32* %0, i32** %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i8* %3, i8** %10
store i32* %4, i32** %11
store i32* %5, i32** %12
%17 = load i32, i32* @CMS_PARTIAL
%18 = load i32, i32* @CMS_STREAM
%19 = or i32 %17, %18
%20 = load i32, i32* @CMS_BINARY
%21 = or i32 %19, %20
store i32 %21, i32* %13
store i32 1, i32* %14
%22 = load i32*, i32** %7
%23 = call i32* @d2i_CMS_bio(i32* %22, i32* null)
store i32* %23, i32** %15
%24 = load i8*, i8** %10
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %38
26: ; preds = %6
%27 = load i8*, i8** %10
%28 = call i64 @BUF_strdup(i8* %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %16
%30 = load i32*, i32** %15
%31 = load i8*, i8** %16
%32 = call i32 @CMS_decrypt_set1_password(i32* %30, i8* %31, i32 -1)
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %26
%35 = load i32*, i32** %9
%36 = call i32 @ERR_print_errors(i32* %35)
br label %65
37: ; preds = %26
br label %55
38: ; preds = %6
%39 = load i32*, i32** %12
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %54
41: ; preds = %38
%42 = load i32*, i32** %11
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %54
44: ; preds = %41
%45 = load i32*, i32** %15
%46 = load i32*, i32** %12
%47 = load i32*, i32** %11
%48 = call i32 @CMS_decrypt_set1_pkey(i32* %45, i32* %46, i32* %47)
%49 = icmp ne i32 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %44
%51 = load i32*, i32** %9
%52 = call i32 @ERR_print_errors(i32* %51)
br label %65
53: ; preds = %44
br label %54
54: ; preds = %53, %41, %38
br label %55
55: ; preds = %54, %37
%56 = load i32*, i32** %15
%57 = load i32*, i32** %8
%58 = load i32, i32* %13
%59 = call i32 @CMS_decrypt(i32* %56, i32* null, i32* null, i32* null, i32* %57, i32 %58)
%60 = icmp ne i32 %59, 0
br i1 %60, label %64, label %61
61: ; preds = %55
%62 = load i32*, i32** %9
%63 = call i32 @ERR_print_errors(i32* %62)
br label %65
64: ; preds = %55
store i32 0, i32* %14
br label %65
65: ; preds = %64, %61, %50, %34
%66 = load i32, i32* %14
ret i32 %66
}

LLVM IR (-O2):
@CMS_PARTIAL = global i32 0
@CMS_STREAM = global i32 0
@CMS_BINARY = global i32 0
define i32 @decrypt_cms_legacy(i32* %0, i32* %1, i32* %2, i8* %3, i32* %4, i32* %5) {
%7 = load i32, i32* @CMS_PARTIAL
%8 = load i32, i32* @CMS_STREAM
%9 = or i32 %8, %7
%10 = load i32, i32* @CMS_BINARY
%11 = or i32 %9, %10
%12 = tail call i32* @d2i_CMS_bio(i32* %0, i32* null)
%13 = icmp eq i8* %3, null
br i1 %13, label %19, label %14
14: ; preds = %6
%15 = tail call i64 @BUF_strdup(i8* %3)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i32 @CMS_decrypt_set1_password(i32* %12, i8* %16, i32 -1)
%18 = icmp eq i32 %17, 0
br i1 %18, label %29, label %26
19: ; preds = %6
%20 = icmp ne i32* %5, null
%21 = icmp ne i32* %4, null
%22 = and i1 %21, %20
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = tail call i32 @CMS_decrypt_set1_pkey(i32* %12, i32* %5, i32* %4)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %14, %19, %23
%27 = tail call i32 @CMS_decrypt(i32* %12, i32* null, i32* null, i32* null, i32* %1, i32 %11)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26, %23, %14
%30 = tail call i32 @ERR_print_errors(i32* %2)
br label %31
31: ; preds = %29, %26
%32 = phi i32 [ 0, %26 ], [ 1, %29 ]
ret i32 %32
}