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

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ algorithm;
};
typedef TYPE_1__ X509_ALGOR;
typedef int /*<<< orphan*/ EVP_MD;

/* Variables and functions */
int /*<<< orphan*/ *EVP_get_digestbyobj(int /*<<< orphan*/);
int /*<<< orphan*/ const *EVP_sha1();
scalar_t__ NID_mgf1;
scalar_t__ OBJ_obj2nid(int /*<<< orphan*/);
int /*<<< orphan*/ RSA_F_RSA_MGF1_TO_MD;
int /*<<< orphan*/ RSA_R_UNKNOWN_MASK_DIGEST;
int /*<<< orphan*/ RSA_R_UNSUPPORTED_MASK_ALGORITHM;
int /*<<< orphan*/ RSA_R_UNSUPPORTED_MASK_PARAMETER;
int /*<<< orphan*/ RSAerr(int /*<<< orphan*/, int /*<<< orphan*/);
const EVP_MD *rsa_mgf1_to_md(X509_ALGOR *alg, X509_ALGOR *maskHash) {
  const EVP_MD *md;
  if (!alg)
    return EVP_sha1();
  if (OBJ_obj2nid(alg->algorithm) != NID_mgf1) {
    RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNSUPPORTED_MASK_ALGORITHM);
    return NULL;
  }
  if (!maskHash) {
    RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNSUPPORTED_MASK_PARAMETER);
    return NULL;
  }
  md = EVP_get_digestbyobj(maskHash->algorithm);
  if (md == NULL) {
    RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNKNOWN_MASK_DIGEST);
    return NULL;
  }
  return md;
}


x86 assembly (clang, -O0):
<rsa_mgf1_to_md>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116b <rsa_mgf1_to_md+0x2b>
mov    $0x0,%al
call   1040 <EVP_sha1@plt>
mov    %rax,-0x8(%rbp)
jmp    121f <rsa_mgf1_to_md+0xdf>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <OBJ_obj2nid@plt>
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
je     11aa <rsa_mgf1_to_md+0x6a>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1060 <RSAerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    121f <rsa_mgf1_to_md+0xdf>
cmpq   $0x0,-0x18(%rbp)
jne    11d9 <rsa_mgf1_to_md+0x99>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
call   1060 <RSAerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    121f <rsa_mgf1_to_md+0xdf>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <EVP_get_digestbyobj@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1217 <rsa_mgf1_to_md+0xd7>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
call   1060 <RSAerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    121f <rsa_mgf1_to_md+0xdf>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_mgf1_to_md>:
test   %rdi,%rdi
je     116f <rsa_mgf1_to_md+0x2f>
push   %rbx
mov    %rsi,%rbx
mov    (%rdi),%edi
call   1030 <OBJ_obj2nid@plt>
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%rax
jne    1176 <rsa_mgf1_to_md+0x36>
test   %rbx,%rbx
je     1188 <rsa_mgf1_to_md+0x48>
mov    (%rbx),%edi
call   1050 <EVP_get_digestbyobj@plt>
test   %rax,%rax
je     119a <rsa_mgf1_to_md+0x5a>
pop    %rbx
ret
xor    %eax,%eax
jmp    1040 <EVP_sha1@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
jmp    11aa <rsa_mgf1_to_md+0x6a>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
jmp    11aa <rsa_mgf1_to_md+0x6a>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1060 <RSAerr@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32 }
@NID_mgf1 = global i64 0
@RSA_F_RSA_MGF1_TO_MD = global i32 0
@RSA_R_UNSUPPORTED_MASK_ALGORITHM = global i32 0
@RSA_R_UNSUPPORTED_MASK_PARAMETER = global i32 0
@RSA_R_UNKNOWN_MASK_DIGEST = global i32 0
define i32* @rsa_mgf1_to_md(%struct.TYPE_4__* %0, %struct.TYPE_4__* %1) {
%3 = alloca i32*
%4 = alloca %struct.TYPE_4__*
%5 = alloca %struct.TYPE_4__*
%6 = alloca i32*
store %struct.TYPE_4__* %0, %struct.TYPE_4__** %4
store %struct.TYPE_4__* %1, %struct.TYPE_4__** %5
%7 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%8 = icmp ne %struct.TYPE_4__* %7, null
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32* (...) @EVP_sha1()
store i32* %10, i32** %3
br label %42
11: ; preds = %2
%12 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%13 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @OBJ_obj2nid(i32 %14)
%16 = load i64, i64* @NID_mgf1
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = load i32, i32* @RSA_F_RSA_MGF1_TO_MD
%20 = load i32, i32* @RSA_R_UNSUPPORTED_MASK_ALGORITHM
%21 = call i32 @RSAerr(i32 %19, i32 %20)
store i32* null, i32** %3
br label %42
22: ; preds = %11
%23 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%24 = icmp ne %struct.TYPE_4__* %23, null
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = load i32, i32* @RSA_F_RSA_MGF1_TO_MD
%27 = load i32, i32* @RSA_R_UNSUPPORTED_MASK_PARAMETER
%28 = call i32 @RSAerr(i32 %26, i32 %27)
store i32* null, i32** %3
br label %42
29: ; preds = %22
%30 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%31 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32* @EVP_get_digestbyobj(i32 %32)
store i32* %33, i32** %6
%34 = load i32*, i32** %6
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i32, i32* @RSA_F_RSA_MGF1_TO_MD
%38 = load i32, i32* @RSA_R_UNKNOWN_MASK_DIGEST
%39 = call i32 @RSAerr(i32 %37, i32 %38)
store i32* null, i32** %3
br label %42
40: ; preds = %29
%41 = load i32*, i32** %6
store i32* %41, i32** %3
br label %42
42: ; preds = %40, %36, %25, %18, %9
%43 = load i32*, i32** %3
ret i32* %43
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32 }
@NID_mgf1 = global i64 0
@RSA_F_RSA_MGF1_TO_MD = global i32 0
@RSA_R_UNSUPPORTED_MASK_ALGORITHM = global i32 0
@RSA_R_UNSUPPORTED_MASK_PARAMETER = global i32 0
@RSA_R_UNKNOWN_MASK_DIGEST = global i32 0
define i32* @rsa_mgf1_to_md(%struct.TYPE_4__* %0, %struct.TYPE_4__* %1) {
%3 = icmp eq %struct.TYPE_4__* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32* (...) @EVP_sha1()
br label %31
6: ; preds = %2
%7 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i64 @OBJ_obj2nid(i32 %8)
%10 = load i64, i64* @NID_mgf1
%11 = icmp eq i64 %9, %10
br i1 %11, label %16, label %12
12: ; preds = %6
%13 = load i32, i32* @RSA_F_RSA_MGF1_TO_MD
%14 = load i32, i32* @RSA_R_UNSUPPORTED_MASK_ALGORITHM
%15 = tail call i32 @RSAerr(i32 %13, i32 %14)
br label %31
16: ; preds = %6
%17 = icmp eq %struct.TYPE_4__* %1, null
br i1 %17, label %18, label %22
18: ; preds = %16
%19 = load i32, i32* @RSA_F_RSA_MGF1_TO_MD
%20 = load i32, i32* @RSA_R_UNSUPPORTED_MASK_PARAMETER
%21 = tail call i32 @RSAerr(i32 %19, i32 %20)
br label %31
22: ; preds = %16
%23 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 0
%24 = load i32, i32* %23
%25 = tail call i32* @EVP_get_digestbyobj(i32 %24)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i32, i32* @RSA_F_RSA_MGF1_TO_MD
%29 = load i32, i32* @RSA_R_UNKNOWN_MASK_DIGEST
%30 = tail call i32 @RSAerr(i32 %28, i32 %29)
br label %31
31: ; preds = %22, %27, %18, %12, %4
%32 = phi i32* [ null, %12 ], [ null, %27 ], [ null, %18 ], [ %5, %4 ], [ %25, %22 ]
ret i32* %32
}