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

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

/* Variables and functions */
#define ASN1_PKEY_CTRL_DEFAULT_MD_NID 129
#define ASN1_PKEY_CTRL_PKCS7_SIGN 128
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 ec_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_DEFAULT_MD_NID:
    *(int *)arg2 = NID_sha1;
    return 2;
  default:
    return -2;
  }
}


x86 assembly (clang, -O0):
<ec_pkey_ctrl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
sub    $0x80,%eax
je     11a0 <ec_pkey_ctrl+0x40>
jmp    118d <ec_pkey_ctrl+0x2d>
mov    -0x44(%rbp),%eax
sub    $0x81,%eax
je     1276 <ec_pkey_ctrl+0x116>
jmp    1291 <ec_pkey_ctrl+0x131>
cmpq   $0x0,-0x20(%rbp)
jne    126a <ec_pkey_ctrl+0x10a>
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     11d9 <ec_pkey_ctrl+0x79>
mov    -0x38(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11e5 <ec_pkey_ctrl+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <ec_pkey_ctrl+0x138>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   1030 <OBJ_obj2nid@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%eax
jne    1212 <ec_pkey_ctrl+0xb2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <ec_pkey_ctrl+0x138>
mov    -0x30(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <EVP_PKEY_id@plt>
mov    -0x48(%rbp),%esi
mov    %eax,%edx
lea    -0x2c(%rbp),%rdi
call   1070 <OBJ_find_sigid_by_algs@plt>
cmp    $0x0,%eax
jne    1244 <ec_pkey_ctrl+0xe4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <ec_pkey_ctrl+0x138>
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <OBJ_nid2obj@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%esi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1050 <X509_ALGOR_set0@plt>
movl   $0x1,-0x4(%rbp)
jmp    1298 <ec_pkey_ctrl+0x138>
mov    0x2d5b(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    1298 <ec_pkey_ctrl+0x138>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ec_pkey_ctrl>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
cmp    $0x81,%esi
je     1210 <ec_pkey_ctrl+0xb0>
mov    $0xfffffffe,%ebx
cmp    $0x80,%esi
jne    1227 <ec_pkey_ctrl+0xc7>
mov    $0x1,%ebx
test   %rdx,%rdx
jne    1227 <ec_pkey_ctrl+0xc7>
mov    %rdi,%r14
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>
mov    0x18(%rsp),%rax
test   %rax,%rax
je     1222 <ec_pkey_ctrl+0xc2>
mov    (%rax),%rdi
test   %rdi,%rdi
je     1222 <ec_pkey_ctrl+0xc2>
call   1030 <OBJ_obj2nid@plt>
mov    %eax,%ebp
mov    0x2e03(%rip),%rax
cmp    (%rax),%ebp
je     1222 <ec_pkey_ctrl+0xc2>
mov    %r14,%rdi
call   1080 <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     1222 <ec_pkey_ctrl+0xc2>
mov    0x10(%rsp),%rbp
mov    0xc(%rsp),%edi
call   1040 <OBJ_nid2obj@plt>
mov    0x2de8(%rip),%rcx
mov    (%rcx),%edx
mov    %rbp,%rdi
mov    %eax,%esi
xor    %ecx,%ecx
call   1050 <X509_ALGOR_set0@plt>
jmp    1227 <ec_pkey_ctrl+0xc7>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rcx)
mov    $0x2,%ebx
jmp    1227 <ec_pkey_ctrl+0xc7>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i32* }
@NID_undef = global i32 0
@V_ASN1_UNDEF = global i32 0
@NID_sha1 = global i32 0
define i32 @ec_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_5__*
%13 = alloca %struct.TYPE_5__*
store i32* %0, i32** %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%14 = load i32, i32* %7
switch i32 %14, label %56 [
i32 128, label %15
i32 129, label %52
]
15: ; preds = %4
%16 = load i64, i64* %8
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %51
18: ; preds = %15
%19 = load i8*, i8** %9
%20 = call i32 @PKCS7_SIGNER_INFO_get0_algs(i8* %19, i32* null, %struct.TYPE_5__** %12, %struct.TYPE_5__** %13)
%21 = load %struct.TYPE_5__*, %struct.TYPE_5__** %12
%22 = icmp eq %struct.TYPE_5__* %21, null
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = load %struct.TYPE_5__*, %struct.TYPE_5__** %12
%25 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %24, i32 0, i32 0
%26 = load i32*, i32** %25
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %29
28: ; preds = %23, %18
store i32 -1, i32* %5
br label %57
29: ; preds = %23
%30 = load %struct.TYPE_5__*, %struct.TYPE_5__** %12
%31 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %30, i32 0, i32 0
%32 = load i32*, i32** %31
%33 = call i32 @OBJ_obj2nid(i32* %32)
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = load i32, i32* @NID_undef
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %29
store i32 -1, i32* %5
br label %57
38: ; preds = %29
%39 = load i32, i32* %11
%40 = load i32*, i32** %6
%41 = call i32 @EVP_PKEY_id(i32* %40)
%42 = call i32 @OBJ_find_sigid_by_algs(i32* %10, i32 %39, i32 %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %38
store i32 -1, i32* %5
br label %57
45: ; preds = %38
%46 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%47 = load i32, i32* %10
%48 = call i32 @OBJ_nid2obj(i32 %47)
%49 = load i32, i32* @V_ASN1_UNDEF
%50 = call i32 @X509_ALGOR_set0(%struct.TYPE_5__* %46, i32 %48, i32 %49, i32 0)
br label %51
51: ; preds = %45, %15
store i32 1, i32* %5
br label %57
52: ; preds = %4
%53 = load i32, i32* @NID_sha1
%54 = load i8*, i8** %9
%55 = bitcast i8* %54 to i32*
store i32 %53, i32* %55
store i32 2, i32* %5
br label %57
56: ; preds = %4
store i32 -2, i32* %5
br label %57
57: ; preds = %56, %52, %51, %44, %37, %28
%58 = load i32, i32* %5
ret i32 %58
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32* }
@NID_undef = global i32 0
@V_ASN1_UNDEF = global i32 0
@NID_sha1 = global i32 0
define i32 @ec_pkey_ctrl(i32* %0, i32 %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.TYPE_5__*
%7 = alloca %struct.TYPE_5__*
switch i32 %1, label %39 [
i32 128, label %8
i32 129, label %36
]
8: ; preds = %4
%9 = icmp eq i64 %2, 0
br i1 %9, label %10, label %39
10: ; preds = %8
%11 = bitcast i32* %5 to i8*
%12 = bitcast %struct.TYPE_5__** %6 to i8*
%13 = bitcast %struct.TYPE_5__** %7 to i8*
%14 = call i32 @PKCS7_SIGNER_INFO_get0_algs(i8* %3, i32* null, %struct.TYPE_5__** %6, %struct.TYPE_5__** %7)
%15 = load %struct.TYPE_5__*, %struct.TYPE_5__** %6
%16 = icmp eq %struct.TYPE_5__* %15, null
br i1 %16, label %29, label %17
17: ; preds = %10
%18 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %15, i64 0, i32 0
%19 = load i32*, i32** %18
%20 = icmp eq i32* %19, null
br i1 %20, label %29, label %21
21: ; preds = %17
%22 = call i32 @OBJ_obj2nid(i32* %19)
%23 = load i32, i32* @NID_undef
%24 = icmp eq i32 %22, %23
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = call i32 @EVP_PKEY_id(i32* %0)
%27 = call i32 @OBJ_find_sigid_by_algs(i32* %5, i32 %22, i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %17, %10, %21, %25
br label %39
30: ; preds = %25
%31 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%32 = load i32, i32* %5
%33 = call i32 @OBJ_nid2obj(i32 %32)
%34 = load i32, i32* @V_ASN1_UNDEF
%35 = call i32 @X509_ALGOR_set0(%struct.TYPE_5__* %31, i32 %33, i32 %34, i32 0)
br label %39
36: ; preds = %4
%37 = load i32, i32* @NID_sha1
%38 = bitcast i8* %3 to i32*
store i32 %37, i32* %38
br label %39
39: ; preds = %8, %30, %29, %4, %36
%40 = phi i32 [ 2, %36 ], [ -2, %4 ], [ -1, %29 ], [ 1, %30 ], [ 1, %8 ]
ret i32 %40
}