#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_ALGOR;
typedef int /*<<< orphan*/ EVP_PKEY;

/* Variables and functions */
#define ASN1_PKEY_CTRL_CMS_ENVELOPE 132
#define ASN1_PKEY_CTRL_CMS_SIGN 131
#define ASN1_PKEY_CTRL_DEFAULT_MD_NID 130
#define ASN1_PKEY_CTRL_PKCS7_ENCRYPT 129
#define ASN1_PKEY_CTRL_PKCS7_SIGN 128
int /*<<< orphan*/ CMS_RecipientInfo_ktri_get0_algs(void *,
                                                    int /*<<< orphan*/ *,
                                                    int /*<<< orphan*/ *,
                                                    int /*<<< orphan*/ **);
int /*<<< orphan*/ CMS_SignerInfo_get0_algs(void *, int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ **);
int /*<<< orphan*/ NID_rsaEncryption;
int NID_sha1;
int /*<<< orphan*/ OBJ_nid2obj(int /*<<< orphan*/);
int /*<<< orphan*/ PKCS7_RECIP_INFO_get0_alg(void *, int /*<<< orphan*/ **);
int /*<<< orphan*/ PKCS7_SIGNER_INFO_get0_algs(void *, int /*<<< orphan*/ *,
                                               int /*<<< orphan*/ *,
                                               int /*<<< orphan*/ **);
int /*<<< orphan*/ V_ASN1_NULL;
int /*<<< orphan*/ X509_ALGOR_set0(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                   int /*<<< orphan*/, int /*<<< orphan*/);
int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) {
  X509_ALGOR *alg = NULL;
  switch (op) {
  case ASN1_PKEY_CTRL_PKCS7_SIGN:
    if (arg1 == 0)
      PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
    break;
  case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
    if (arg1 == 0)
      PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
    break;
  case ASN1_PKEY_CTRL_CMS_SIGN:
    if (arg1 == 0)
      CMS_SignerInfo_get0_algs(arg2, NULL, NULL, NULL, &alg);
    break;
  case ASN1_PKEY_CTRL_CMS_ENVELOPE:
    if (arg1 == 0)
      CMS_RecipientInfo_ktri_get0_algs(arg2, NULL, NULL, &alg);
    break;
  case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
    *(int *)arg2 = NID_sha1;
    return 1;
  default:
    return -2;
  }
  if (alg)
    X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  return 1;
}


x86 assembly (clang, -O0):
<rsa_pkey_ctrl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x4,%eax
ja     124f <rsa_pkey_ctrl+0xef>
mov    -0x38(%rbp),%rax
lea    0xe61(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmpq   $0x0,-0x20(%rbp)
jne    11c7 <rsa_pkey_ctrl+0x67>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x30(%rbp),%rcx
mov    %rdx,%rsi
call   1050 <PKCS7_SIGNER_INFO_get0_algs@plt>
jmp    125b <rsa_pkey_ctrl+0xfb>
cmpq   $0x0,-0x20(%rbp)
jne    11e4 <rsa_pkey_ctrl+0x84>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <PKCS7_RECIP_INFO_get0_alg@plt>
jmp    125b <rsa_pkey_ctrl+0xfb>
cmpq   $0x0,-0x20(%rbp)
jne    120b <rsa_pkey_ctrl+0xab>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1060 <CMS_SignerInfo_get0_algs@plt>
jmp    125b <rsa_pkey_ctrl+0xfb>
cmpq   $0x0,-0x20(%rbp)
jne    122f <rsa_pkey_ctrl+0xcf>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x30(%rbp),%rcx
mov    %rdx,%rsi
call   1080 <CMS_RecipientInfo_ktri_get0_algs@plt>
jmp    125b <rsa_pkey_ctrl+0xfb>
mov    0x2d95(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1299 <rsa_pkey_ctrl+0x139>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1299 <rsa_pkey_ctrl+0x139>
cmpq   $0x0,-0x30(%rbp)
je     1292 <rsa_pkey_ctrl+0x132>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   1030 <OBJ_nid2obj@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1040 <X509_ALGOR_set0@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_pkey_ctrl>:
push   %r14
push   %rbx
push   %rax
movq   $0x0,(%rsp)
add    $0xffffff80,%esi
cmp    $0x4,%esi
ja     11a6 <rsa_pkey_ctrl+0x46>
lea    0xe85(%rip),%rax
movslq (%rax,%rsi,4),%rsi
add    %rax,%rsi
jmp    *%rsi
mov    $0x1,%ebx
test   %rdx,%rdx
jne    1247 <rsa_pkey_ctrl+0xe7>
mov    %rsp,%rax
mov    %rcx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %rax,%rcx
call   1050 <PKCS7_SIGNER_INFO_get0_algs@plt>
jmp    1216 <rsa_pkey_ctrl+0xb6>
mov    $0xfffffffe,%ebx
jmp    1247 <rsa_pkey_ctrl+0xe7>
mov    $0x1,%ebx
test   %rdx,%rdx
jne    1247 <rsa_pkey_ctrl+0xe7>
mov    %rsp,%rsi
mov    %rcx,%rdi
call   1070 <PKCS7_RECIP_INFO_get0_alg@plt>
jmp    1216 <rsa_pkey_ctrl+0xb6>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rcx)
mov    $0x1,%ebx
jmp    1247 <rsa_pkey_ctrl+0xe7>
mov    $0x1,%ebx
test   %rdx,%rdx
jne    1247 <rsa_pkey_ctrl+0xe7>
mov    %rsp,%r8
mov    %rcx,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <CMS_SignerInfo_get0_algs@plt>
jmp    1216 <rsa_pkey_ctrl+0xb6>
mov    $0x1,%ebx
test   %rdx,%rdx
jne    1247 <rsa_pkey_ctrl+0xe7>
mov    %rsp,%rax
mov    %rcx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %rax,%rcx
call   1080 <CMS_RecipientInfo_ktri_get0_algs@plt>
mov    (%rsp),%r14
mov    $0x1,%ebx
test   %r14,%r14
je     1247 <rsa_pkey_ctrl+0xe7>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1030 <OBJ_nid2obj@plt>
mov    0x2d9f(%rip),%rcx
mov    (%rcx),%edx
mov    %r14,%rdi
mov    %eax,%esi
xor    %ecx,%ecx
call   1040 <X509_ALGOR_set0@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@NID_sha1 = global i32 0
@NID_rsaEncryption = global i32 0
@V_ASN1_NULL = global i32 0
define i32 @rsa_pkey_ctrl(i32* %0, i32 %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32*
store i32* %0, i32** %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i32* null, i32** %10
%11 = load i32, i32* %7
switch i32 %11, label %44 [
i32 128, label %12
i32 129, label %19
i32 131, label %26
i32 132, label %33
i32 130, label %40
]
12: ; preds = %4
%13 = load i64, i64* %8
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i8*, i8** %9
%17 = call i32 @PKCS7_SIGNER_INFO_get0_algs(i8* %16, i32* null, i32* null, i32** %10)
br label %18
18: ; preds = %15, %12
br label %45
19: ; preds = %4
%20 = load i64, i64* %8
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i8*, i8** %9
%24 = call i32 @PKCS7_RECIP_INFO_get0_alg(i8* %23, i32** %10)
br label %25
25: ; preds = %22, %19
br label %45
26: ; preds = %4
%27 = load i64, i64* %8
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i8*, i8** %9
%31 = call i32 @CMS_SignerInfo_get0_algs(i8* %30, i32* null, i32* null, i32* null, i32** %10)
br label %32
32: ; preds = %29, %26
br label %45
33: ; preds = %4
%34 = load i64, i64* %8
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i8*, i8** %9
%38 = call i32 @CMS_RecipientInfo_ktri_get0_algs(i8* %37, i32* null, i32* null, i32** %10)
br label %39
39: ; preds = %36, %33
br label %45
40: ; preds = %4
%41 = load i32, i32* @NID_sha1
%42 = load i8*, i8** %9
%43 = bitcast i8* %42 to i32*
store i32 %41, i32* %43
store i32 1, i32* %5
br label %55
44: ; preds = %4
store i32 -2, i32* %5
br label %55
45: ; preds = %39, %32, %25, %18
%46 = load i32*, i32** %10
%47 = icmp ne i32* %46, null
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i32*, i32** %10
%50 = load i32, i32* @NID_rsaEncryption
%51 = call i32 @OBJ_nid2obj(i32 %50)
%52 = load i32, i32* @V_ASN1_NULL
%53 = call i32 @X509_ALGOR_set0(i32* %49, i32 %51, i32 %52, i32 0)
br label %54
54: ; preds = %48, %45
store i32 1, i32* %5
br label %55
55: ; preds = %54, %44, %40
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@NID_sha1 = global i32 0
@NID_rsaEncryption = global i32 0
@V_ASN1_NULL = global i32 0
define i32 @rsa_pkey_ctrl(i32* nocapture readnone %0, i32 %1, i64 %2, i8* %3) {
%5 = alloca i32*
%6 = bitcast i32** %5 to i8*
store i32* null, i32** %5
switch i32 %1, label %34 [
i32 128, label %7
i32 129, label %11
i32 131, label %15
i32 132, label %19
i32 130, label %23
]
7: ; preds = %4
%8 = icmp eq i64 %2, 0
br i1 %8, label %9, label %34
9: ; preds = %7
%10 = call i32 @PKCS7_SIGNER_INFO_get0_algs(i8* %3, i32* null, i32* null, i32** %5)
br label %26
11: ; preds = %4
%12 = icmp eq i64 %2, 0
br i1 %12, label %13, label %34
13: ; preds = %11
%14 = call i32 @PKCS7_RECIP_INFO_get0_alg(i8* %3, i32** %5)
br label %26
15: ; preds = %4
%16 = icmp eq i64 %2, 0
br i1 %16, label %17, label %34
17: ; preds = %15
%18 = call i32 @CMS_SignerInfo_get0_algs(i8* %3, i32* null, i32* null, i32* null, i32** %5)
br label %26
19: ; preds = %4
%20 = icmp eq i64 %2, 0
br i1 %20, label %21, label %34
21: ; preds = %19
%22 = call i32 @CMS_RecipientInfo_ktri_get0_algs(i8* %3, i32* null, i32* null, i32** %5)
br label %26
23: ; preds = %4
%24 = load i32, i32* @NID_sha1
%25 = bitcast i8* %3 to i32*
store i32 %24, i32* %25
br label %34
26: ; preds = %21, %17, %13, %9
%27 = load i32*, i32** %5
%28 = icmp eq i32* %27, null
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = load i32, i32* @NID_rsaEncryption
%31 = call i32 @OBJ_nid2obj(i32 %30)
%32 = load i32, i32* @V_ASN1_NULL
%33 = call i32 @X509_ALGOR_set0(i32* %27, i32 %31, i32 %32, i32 0)
br label %34
34: ; preds = %7, %11, %15, %19, %26, %29, %4, %23
%35 = phi i32 [ 1, %23 ], [ -2, %4 ], [ 1, %29 ], [ 1, %26 ], [ 1, %19 ], [ 1, %15 ], [ 1, %11 ], [ 1, %7 ]
ret i32 %35
}