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

/* Variables and functions */
int EC_GROUP_get_curve_name(int /*<<< orphan*/);
int /*<<< orphan*/ EC_KEY_get0_group(int /*<<< orphan*/ const *const);
int const EVP_PKEY_EC;
int /*<<< orphan*/ *EVP_PKEY_get0_EC_KEY(int /*<<< orphan*/ *);
int EVP_PKEY_id(int /*<<< orphan*/ *);
int /*<<< orphan*/ const *EVP_sha256();
int /*<<< orphan*/ const *EVP_sha384();
#define NID_X9_62_prime256v1 130
#define NID_secp256k1 129
#define NID_secp384r1 128
const EVP_MD *select_digest_function(EVP_PKEY *key) {
  const int keytype = EVP_PKEY_id(key);
  if (keytype == EVP_PKEY_EC) {
    const EC_KEY *const eckey = EVP_PKEY_get0_EC_KEY(key);
    switch (EC_GROUP_get_curve_name(EC_KEY_get0_group(eckey))) {
    case NID_X9_62_prime256v1:
    case NID_secp256k1:
      return EVP_sha256();
    case NID_secp384r1:
      return EVP_sha384();
    default:
      return NULL;
    }
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<select_digest_function>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <EVP_PKEY_id@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%eax
jne    11ff <select_digest_function+0x9f>
mov    -0x10(%rbp),%rdi
call   1030 <EVP_PKEY_get0_EC_KEY@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <EC_KEY_get0_group@plt>
mov    %eax,%edi
call   1050 <EC_GROUP_get_curve_name@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0x80,%eax
je     11e2 <select_digest_function+0x82>
jmp    11bc <select_digest_function+0x5c>
mov    -0x24(%rbp),%eax
add    $0xffffff7f,%eax
sub    $0x1,%eax
ja     11f2 <select_digest_function+0x92>
jmp    11d2 <select_digest_function+0x72>
mov    $0x0,%al
call   1080 <EVP_sha256@plt>
mov    %rax,-0x8(%rbp)
jmp    1207 <select_digest_function+0xa7>
mov    $0x0,%al
call   1060 <EVP_sha384@plt>
mov    %rax,-0x8(%rbp)
jmp    1207 <select_digest_function+0xa7>
movq   $0x0,-0x8(%rbp)
jmp    1207 <select_digest_function+0xa7>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select_digest_function>:
push   %rbx
mov    %rdi,%rbx
call   1070 <EVP_PKEY_id@plt>
test   %eax,%eax
je     1171 <select_digest_function+0x11>
xor    %eax,%eax
pop    %rbx
ret
mov    %rbx,%rdi
call   1030 <EVP_PKEY_get0_EC_KEY@plt>
mov    %rax,%rdi
call   1040 <EC_KEY_get0_group@plt>
mov    %eax,%edi
call   1050 <EC_GROUP_get_curve_name@plt>
lea    -0x81(%rax),%ecx
cmp    $0x2,%ecx
jae    119b <select_digest_function+0x3b>
xor    %eax,%eax
pop    %rbx
jmp    1080 <EVP_sha256@plt>
cmp    $0x80,%eax
jne    116d <select_digest_function+0xd>
xor    %eax,%eax
pop    %rbx
jmp    1060 <EVP_sha384@plt>

LLVM IR (-O0):
@EVP_PKEY_EC = constant i32 0
define i32* @select_digest_function(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32*
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = call i32 @EVP_PKEY_id(i32* %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = load i32, i32* @EVP_PKEY_EC
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %22
11: ; preds = %1
%12 = load i32*, i32** %3
%13 = call i32* @EVP_PKEY_get0_EC_KEY(i32* %12)
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = call i32 @EC_KEY_get0_group(i32* %14)
%16 = call i32 @EC_GROUP_get_curve_name(i32 %15)
switch i32 %16, label %21 [
i32 130, label %17
i32 129, label %17
i32 128, label %19
]
17: ; preds = %11, %11
%18 = call i32* (...) @EVP_sha256()
store i32* %18, i32** %2
br label %23
19: ; preds = %11
%20 = call i32* (...) @EVP_sha384()
store i32* %20, i32** %2
br label %23
21: ; preds = %11
store i32* null, i32** %2
br label %23
22: ; preds = %1
store i32* null, i32** %2
br label %23
23: ; preds = %22, %21, %19, %17
%24 = load i32*, i32** %2
ret i32* %24
}

LLVM IR (-O2):
@EVP_PKEY_EC = constant i32 0
define i32* @select_digest_function(i32* %0) {
%2 = tail call i32 @EVP_PKEY_id(i32* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %12
4: ; preds = %1
%5 = tail call i32* @EVP_PKEY_get0_EC_KEY(i32* %0)
%6 = tail call i32 @EC_KEY_get0_group(i32* %5)
%7 = tail call i32 @EC_GROUP_get_curve_name(i32 %6)
switch i32 %7, label %12 [
i32 130, label %8
i32 129, label %8
i32 128, label %10
]
8: ; preds = %4, %4
%9 = tail call i32* (...) @EVP_sha256()
br label %12
10: ; preds = %4
%11 = tail call i32* (...) @EVP_sha384()
br label %12
12: ; preds = %1, %8, %10, %4
%13 = phi i32* [ %11, %10 ], [ %9, %8 ], [ null, %4 ], [ null, %1 ]
ret i32* %13
}