#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_7__ TYPE_1__;

/* Type definitions */
struct TYPE_7__ {
  int /*<<< orphan*/ *algorithm;
};
typedef TYPE_1__ X509_ALGOR;
typedef int /*<<< orphan*/ EVP_PKEY;

/* Variables and functions */
#define ASN1_PKEY_CTRL_CMS_SIGN 130
#define ASN1_PKEY_CTRL_DEFAULT_MD_NID 129
#define ASN1_PKEY_CTRL_PKCS7_SIGN 128
int /*<<< orphan*/ CMS_SignerInfo_get0_algs(void *, int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ *, TYPE_1__ **,
                                            TYPE_1__ **);
int /*<<< orphan*/ EVP_PKEY_id(int /*<<< orphan*/ *);
int NID_sha1;
int NID_undef;
int /*<<< orphan*/ OBJ_find_sigid_by_algs(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ OBJ_nid2obj(int);
int OBJ_obj2nid(int /*<<< orphan*/ *);
int /*<<< orphan*/ PKCS7_SIGNER_INFO_get0_algs(void *, int /*<<< orphan*/ *,
                                               TYPE_1__ **, TYPE_1__ **);
int /*<<< orphan*/ V_ASN1_UNDEF;
int /*<<< orphan*/ X509_ALGOR_set0(TYPE_1__ *, int /*<<< orphan*/,
                                   int /*<<< orphan*/, int /*<<< orphan*/);
int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) {
  switch (op) {
  case ASN1_PKEY_CTRL_PKCS7_SIGN:
    if (arg1 == 0) {
      int snid, hnid;
      X509_ALGOR *alg1, *alg2;
      PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
      if (alg1 == NULL || alg1->algorithm == NULL)
        return -1;
      hnid = OBJ_obj2nid(alg1->algorithm);
      if (hnid == NID_undef)
        return -1;
      if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
        return -1;
      X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
    }
    return 1;
  case ASN1_PKEY_CTRL_CMS_SIGN:
    if (arg1 == 0) {
      int snid, hnid;
      X509_ALGOR *alg1, *alg2;
      CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
      if (alg1 == NULL || alg1->algorithm == NULL)
        return -1;
      hnid = OBJ_obj2nid(alg1->algorithm);
      if (hnid == NID_undef)
        return -1;
      if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
        return -1;
      X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
    }
    return 1;
  case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
    *(int *)arg2 = NID_sha1;
    return 2;
  default:
    return -2;
  }
}


x86 assembly (clang, -O0):
<dsa_pkey_ctrl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
sub    $0x80,%eax
je     11c6 <dsa_pkey_ctrl+0x56>
jmp    11a0 <dsa_pkey_ctrl+0x30>
mov    -0x5c(%rbp),%eax
sub    $0x81,%eax
je     1375 <dsa_pkey_ctrl+0x205>
jmp    11b3 <dsa_pkey_ctrl+0x43>
mov    -0x5c(%rbp),%eax
sub    $0x82,%eax
je     129c <dsa_pkey_ctrl+0x12c>
jmp    1390 <dsa_pkey_ctrl+0x220>
cmpq   $0x0,-0x20(%rbp)
jne    1290 <dsa_pkey_ctrl+0x120>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1060 <PKCS7_SIGNER_INFO_get0_algs@plt>
cmpq   $0x0,-0x38(%rbp)
je     11ff <dsa_pkey_ctrl+0x8f>
mov    -0x38(%rbp),%rax
cmpq   $0x0,(%rax)
jne    120b <dsa_pkey_ctrl+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <dsa_pkey_ctrl+0x227>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   1030 <OBJ_obj2nid@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%eax
jne    1238 <dsa_pkey_ctrl+0xc8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <dsa_pkey_ctrl+0x227>
mov    -0x30(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <EVP_PKEY_id@plt>
mov    -0x60(%rbp),%esi
mov    %eax,%edx
lea    -0x2c(%rbp),%rdi
call   1070 <OBJ_find_sigid_by_algs@plt>
cmp    $0x0,%eax
jne    126a <dsa_pkey_ctrl+0xfa>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <dsa_pkey_ctrl+0x227>
mov    -0x40(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <OBJ_nid2obj@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%esi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1050 <X509_ALGOR_set0@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <dsa_pkey_ctrl+0x227>
cmpq   $0x0,-0x20(%rbp)
jne    1369 <dsa_pkey_ctrl+0x1f9>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x50(%rbp),%rcx
lea    -0x58(%rbp),%r8
mov    %rdx,%rsi
call   1080 <CMS_SignerInfo_get0_algs@plt>
cmpq   $0x0,-0x50(%rbp)
je     12d8 <dsa_pkey_ctrl+0x168>
mov    -0x50(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12e4 <dsa_pkey_ctrl+0x174>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <dsa_pkey_ctrl+0x227>
mov    -0x50(%rbp),%rax
mov    (%rax),%rdi
call   1030 <OBJ_obj2nid@plt>
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    0x2cd3(%rip),%rcx
cmp    (%rcx),%eax
jne    1311 <dsa_pkey_ctrl+0x1a1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <dsa_pkey_ctrl+0x227>
mov    -0x48(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <EVP_PKEY_id@plt>
mov    -0x6c(%rbp),%esi
mov    %eax,%edx
lea    -0x44(%rbp),%rdi
call   1070 <OBJ_find_sigid_by_algs@plt>
cmp    $0x0,%eax
jne    1343 <dsa_pkey_ctrl+0x1d3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <dsa_pkey_ctrl+0x227>
mov    -0x58(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x44(%rbp),%edi
call   1040 <OBJ_nid2obj@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%esi
mov    0x2c88(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1050 <X509_ALGOR_set0@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <dsa_pkey_ctrl+0x227>
mov    0x2c5c(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    1397 <dsa_pkey_ctrl+0x227>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dsa_pkey_ctrl>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
cmp    $0x82,%esi
je     11c7 <dsa_pkey_ctrl+0x57>
cmp    $0x81,%esi
je     1251 <dsa_pkey_ctrl+0xe1>
mov    $0xfffffffe,%ebx
cmp    $0x80,%esi
jne    1268 <dsa_pkey_ctrl+0xf8>
mov    $0x1,%ebx
test   %rdx,%rdx
jne    1268 <dsa_pkey_ctrl+0xf8>
lea    0x18(%rsp),%rdx
lea    0x10(%rsp),%rax
mov    %rcx,%rdi
xor    %esi,%esi
mov    %rax,%rcx
call   1060 <PKCS7_SIGNER_INFO_get0_algs@plt>
jmp    11ee <dsa_pkey_ctrl+0x7e>
mov    $0x1,%ebx
test   %rdx,%rdx
jne    1268 <dsa_pkey_ctrl+0xf8>
lea    0x18(%rsp),%rax
lea    0x10(%rsp),%r8
mov    %rcx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %rax,%rcx
call   1080 <CMS_SignerInfo_get0_algs@plt>
mov    0x18(%rsp),%rax
test   %rax,%rax
je     1263 <dsa_pkey_ctrl+0xf3>
mov    (%rax),%rdi
test   %rdi,%rdi
je     1263 <dsa_pkey_ctrl+0xf3>
call   1030 <OBJ_obj2nid@plt>
mov    %eax,%ebp
mov    0x2dc2(%rip),%rax
cmp    (%rax),%ebp
je     1263 <dsa_pkey_ctrl+0xf3>
mov    %r14,%rdi
call   1090 <EVP_PKEY_id@plt>
lea    0xc(%rsp),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <OBJ_find_sigid_by_algs@plt>
test   %eax,%eax
je     1263 <dsa_pkey_ctrl+0xf3>
mov    0x10(%rsp),%rbp
mov    0xc(%rsp),%edi
call   1040 <OBJ_nid2obj@plt>
mov    0x2da7(%rip),%rcx
mov    (%rcx),%edx
mov    %rbp,%rdi
mov    %eax,%esi
xor    %ecx,%ecx
call   1050 <X509_ALGOR_set0@plt>
jmp    1268 <dsa_pkey_ctrl+0xf8>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rcx)
mov    $0x2,%ebx
jmp    1268 <dsa_pkey_ctrl+0xf8>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_7__ = type { i32* }
@NID_undef = global i32 0
@V_ASN1_UNDEF = global i32 0
@NID_sha1 = global i32 0
define i32 @dsa_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
%11 = alloca i32
%12 = alloca %struct.TYPE_7__*
%13 = alloca %struct.TYPE_7__*
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.TYPE_7__*
%17 = alloca %struct.TYPE_7__*
store i32* %0, i32** %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%18 = load i32, i32* %7
switch i32 %18, label %97 [
i32 128, label %19
i32 130, label %56
i32 129, label %93
]
19: ; preds = %4
%20 = load i64, i64* %8
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %55
22: ; preds = %19
%23 = load i8*, i8** %9
%24 = call i32 @PKCS7_SIGNER_INFO_get0_algs(i8* %23, i32* null, %struct.TYPE_7__** %12, %struct.TYPE_7__** %13)
%25 = load %struct.TYPE_7__*, %struct.TYPE_7__** %12
%26 = icmp eq %struct.TYPE_7__* %25, null
br i1 %26, label %32, label %27
27: ; preds = %22
%28 = load %struct.TYPE_7__*, %struct.TYPE_7__** %12
%29 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %28, i32 0, i32 0
%30 = load i32*, i32** %29
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %27, %22
store i32 -1, i32* %5
br label %98
33: ; preds = %27
%34 = load %struct.TYPE_7__*, %struct.TYPE_7__** %12
%35 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %34, i32 0, i32 0
%36 = load i32*, i32** %35
%37 = call i32 @OBJ_obj2nid(i32* %36)
store i32 %37, i32* %11
%38 = load i32, i32* %11
%39 = load i32, i32* @NID_undef
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %33
store i32 -1, i32* %5
br label %98
42: ; preds = %33
%43 = load i32, i32* %11
%44 = load i32*, i32** %6
%45 = call i32 @EVP_PKEY_id(i32* %44)
%46 = call i32 @OBJ_find_sigid_by_algs(i32* %10, i32 %43, i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %42
store i32 -1, i32* %5
br label %98
49: ; preds = %42
%50 = load %struct.TYPE_7__*, %struct.TYPE_7__** %13
%51 = load i32, i32* %10
%52 = call i32 @OBJ_nid2obj(i32 %51)
%53 = load i32, i32* @V_ASN1_UNDEF
%54 = call i32 @X509_ALGOR_set0(%struct.TYPE_7__* %50, i32 %52, i32 %53, i32 0)
br label %55
55: ; preds = %49, %19
store i32 1, i32* %5
br label %98
56: ; preds = %4
%57 = load i64, i64* %8
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %92
59: ; preds = %56
%60 = load i8*, i8** %9
%61 = call i32 @CMS_SignerInfo_get0_algs(i8* %60, i32* null, i32* null, %struct.TYPE_7__** %16, %struct.TYPE_7__** %17)
%62 = load %struct.TYPE_7__*, %struct.TYPE_7__** %16
%63 = icmp eq %struct.TYPE_7__* %62, null
br i1 %63, label %69, label %64
64: ; preds = %59
%65 = load %struct.TYPE_7__*, %struct.TYPE_7__** %16
%66 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %65, i32 0, i32 0
%67 = load i32*, i32** %66
%68 = icmp eq i32* %67, null
br i1 %68, label %69, label %70
69: ; preds = %64, %59
store i32 -1, i32* %5
br label %98
70: ; preds = %64
%71 = load %struct.TYPE_7__*, %struct.TYPE_7__** %16
%72 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %71, i32 0, i32 0
%73 = load i32*, i32** %72
%74 = call i32 @OBJ_obj2nid(i32* %73)
store i32 %74, i32* %15
%75 = load i32, i32* %15
%76 = load i32, i32* @NID_undef
%77 = icmp eq i32 %75, %76
br i1 %77, label %78, label %79
78: ; preds = %70
store i32 -1, i32* %5
br label %98
79: ; preds = %70
%80 = load i32, i32* %15
%81 = load i32*, i32** %6
%82 = call i32 @EVP_PKEY_id(i32* %81)
%83 = call i32 @OBJ_find_sigid_by_algs(i32* %14, i32 %80, i32 %82)
%84 = icmp ne i32 %83, 0
br i1 %84, label %86, label %85
85: ; preds = %79
store i32 -1, i32* %5
br label %98
86: ; preds = %79
%87 = load %struct.TYPE_7__*, %struct.TYPE_7__** %17
%88 = load i32, i32* %14
%89 = call i32 @OBJ_nid2obj(i32 %88)
%90 = load i32, i32* @V_ASN1_UNDEF
%91 = call i32 @X509_ALGOR_set0(%struct.TYPE_7__* %87, i32 %89, i32 %90, i32 0)
br label %92
92: ; preds = %86, %56
store i32 1, i32* %5
br label %98
93: ; preds = %4
%94 = load i32, i32* @NID_sha1
%95 = load i8*, i8** %9
%96 = bitcast i8* %95 to i32*
store i32 %94, i32* %96
store i32 2, i32* %5
br label %98
97: ; preds = %4
store i32 -2, i32* %5
br label %98
98: ; preds = %97, %93, %92, %85, %78, %69, %55, %48, %41, %32
%99 = load i32, i32* %5
ret i32 %99
}

LLVM IR (-O2):
%struct.TYPE_7__ = type { i32* }
@NID_undef = global i32 0
@V_ASN1_UNDEF = global i32 0
@NID_sha1 = global i32 0
define i32 @dsa_pkey_ctrl(i32* %0, i32 %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.TYPE_7__*
%7 = alloca %struct.TYPE_7__*
%8 = alloca i32
%9 = alloca %struct.TYPE_7__*
%10 = alloca %struct.TYPE_7__*
switch i32 %1, label %70 [
i32 128, label %11
i32 130, label %39
i32 129, label %67
]
11: ; preds = %4
%12 = icmp eq i64 %2, 0
br i1 %12, label %13, label %70
13: ; preds = %11
%14 = bitcast i32* %5 to i8*
%15 = bitcast %struct.TYPE_7__** %6 to i8*
%16 = bitcast %struct.TYPE_7__** %7 to i8*
%17 = call i32 @PKCS7_SIGNER_INFO_get0_algs(i8* %3, i32* null, %struct.TYPE_7__** %6, %struct.TYPE_7__** %7)
%18 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%19 = icmp eq %struct.TYPE_7__* %18, null
br i1 %19, label %32, label %20
20: ; preds = %13
%21 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %18, i64 0, i32 0
%22 = load i32*, i32** %21
%23 = icmp eq i32* %22, null
br i1 %23, label %32, label %24
24: ; preds = %20
%25 = call i32 @OBJ_obj2nid(i32* %22)
%26 = load i32, i32* @NID_undef
%27 = icmp eq i32 %25, %26
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = call i32 @EVP_PKEY_id(i32* %0)
%30 = call i32 @OBJ_find_sigid_by_algs(i32* %5, i32 %25, i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %20, %13, %24, %28
br label %70
33: ; preds = %28
%34 = load %struct.TYPE_7__*, %struct.TYPE_7__** %7
%35 = load i32, i32* %5
%36 = call i32 @OBJ_nid2obj(i32 %35)
%37 = load i32, i32* @V_ASN1_UNDEF
%38 = call i32 @X509_ALGOR_set0(%struct.TYPE_7__* %34, i32 %36, i32 %37, i32 0)
br label %70
39: ; preds = %4
%40 = icmp eq i64 %2, 0
br i1 %40, label %41, label %70
41: ; preds = %39
%42 = bitcast i32* %8 to i8*
%43 = bitcast %struct.TYPE_7__** %9 to i8*
%44 = bitcast %struct.TYPE_7__** %10 to i8*
%45 = call i32 @CMS_SignerInfo_get0_algs(i8* %3, i32* null, i32* null, %struct.TYPE_7__** %9, %struct.TYPE_7__** %10)
%46 = load %struct.TYPE_7__*, %struct.TYPE_7__** %9
%47 = icmp eq %struct.TYPE_7__* %46, null
br i1 %47, label %60, label %48
48: ; preds = %41
%49 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %46, i64 0, i32 0
%50 = load i32*, i32** %49
%51 = icmp eq i32* %50, null
br i1 %51, label %60, label %52
52: ; preds = %48
%53 = call i32 @OBJ_obj2nid(i32* %50)
%54 = load i32, i32* @NID_undef
%55 = icmp eq i32 %53, %54
br i1 %55, label %60, label %56
56: ; preds = %52
%57 = call i32 @EVP_PKEY_id(i32* %0)
%58 = call i32 @OBJ_find_sigid_by_algs(i32* %8, i32 %53, i32 %57)
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %48, %41, %52, %56
br label %70
61: ; preds = %56
%62 = load %struct.TYPE_7__*, %struct.TYPE_7__** %10
%63 = load i32, i32* %8
%64 = call i32 @OBJ_nid2obj(i32 %63)
%65 = load i32, i32* @V_ASN1_UNDEF
%66 = call i32 @X509_ALGOR_set0(%struct.TYPE_7__* %62, i32 %64, i32 %65, i32 0)
br label %70
67: ; preds = %4
%68 = load i32, i32* @NID_sha1
%69 = bitcast i8* %3 to i32*
store i32 %68, i32* %69
br label %70
70: ; preds = %39, %61, %60, %11, %33, %32, %4, %67
%71 = phi i32 [ 2, %67 ], [ -2, %4 ], [ -1, %32 ], [ 1, %33 ], [ 1, %11 ], [ -1, %60 ], [ 1, %61 ], [ 1, %39 ]
ret i32 %71
}