#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_MD;
typedef int /*<<< orphan*/ EC_GROUP;
typedef int /*<<< orphan*/ BN_CTX;

/* Variables and functions */
int /*<<< orphan*/ EC_GROUP_get0_generator(int /*<<< orphan*/ const *);
size_t EC_POINT_point2oct(int /*<<< orphan*/ const *, int /*<<< orphan*/,
                          int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ const *EVP_get_digestbynid(int);
#define NID_sha1 137
#define NID_sha224 136
#define NID_sha256 135
#define NID_sha384 134
#define NID_sha512 133
int /*<<< orphan*/ POINT_CONVERSION_COMPRESSED;
#define SHA224_DIGEST_LENGTH 132
#define SHA256_DIGEST_LENGTH 131
#define SHA384_DIGEST_LENGTH 130
#define SHA512_DIGEST_LENGTH 129
#define SHA_DIGEST_LENGTH 128
const EVP_MD *find_digest(const EC_GROUP *grp, BN_CTX *ctx) {
  static struct {
    size_t size;
    int nid;
  } hashes[] = {
      {SHA_DIGEST_LENGTH, NID_sha1},      {SHA224_DIGEST_LENGTH, NID_sha224},
      {SHA256_DIGEST_LENGTH, NID_sha256}, {SHA384_DIGEST_LENGTH, NID_sha384},
      {SHA512_DIGEST_LENGTH, NID_sha512}, {}};
  size_t size;
  int hnid;
  size = EC_POINT_point2oct(grp, EC_GROUP_get0_generator(grp),
                            POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  if (size-- == 0)
    return NULL;
  for (size_t i = 0; hashes[i].size != 0; i++) {
    hnid = hashes[i].nid;
    if (size <= hashes[i].size)
      break;
  }
  return EVP_get_digestbynid(hnid);
}


x86 assembly (clang, -O0):
<find_digest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <EC_GROUP_get0_generator@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
mov    0x2e8a(%rip),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%r9
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1040 <EC_POINT_point2oct@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
cmp    $0x0,%rax
jne    119a <find_digest+0x6a>
movq   $0x0,-0x8(%rbp)
jmp    121c <find_digest+0xec>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
lea    0x2e93(%rip),%rax
shl    $0x4,%rcx
add    %rcx,%rax
cmpq   $0x0,(%rax)
je     1210 <find_digest+0xe0>
mov    -0x30(%rbp),%rcx
lea    0x2e77(%rip),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rdx
lea    0x2e5b(%rip),%rcx
shl    $0x4,%rdx
add    %rdx,%rcx
cmp    (%rcx),%rax
ja     11fa <find_digest+0xca>
jmp    1210 <find_digest+0xe0>
jmp    11ff <find_digest+0xcf>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11a2 <find_digest+0x72>
mov    -0x24(%rbp),%edi
call   1030 <EVP_get_digestbynid@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_digest>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <EC_GROUP_get0_generator@plt>
mov    0x2ea2(%rip),%rcx
mov    (%rcx),%edx
mov    %rbx,%rdi
mov    %eax,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
mov    %r14,%r9
call   1040 <EC_POINT_point2oct@plt>
sub    $0x1,%rax
jae    116a <find_digest+0x3a>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %ecx,%ecx
cmp    $0x85,%rax
setae  %cl
xor    %edx,%edx
cmp    $0x81,%rax
lea    0x1(%rcx,%rcx,2),%rax
cmovae %rax,%rdx
shl    $0x4,%rdx
lea    0xe6f(%rip),%rax
mov    0x8(%rdx,%rax,1),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <EVP_get_digestbynid@plt>

LLVM IR (-O0):
%struct.anon = type { i64, i32 }
@find_digest.hashes = internal global [6 x %struct.anon] [%struct.anon { i64 128, i32 137 }, %struct.anon { i64 132, i32 136 }, %struct.anon { i64 131, i32 135 }, %struct.anon { i64 130, i32 134 }, %struct.anon { i64 129, i32 133 }, %struct.anon zeroinitializer]
@POINT_CONVERSION_COMPRESSED = global i32 0
define i32* @find_digest(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
store i32* %0, i32** %4
store i32* %1, i32** %5
%9 = load i32*, i32** %4
%10 = load i32*, i32** %4
%11 = call i32 @EC_GROUP_get0_generator(i32* %10)
%12 = load i32, i32* @POINT_CONVERSION_COMPRESSED
%13 = load i32*, i32** %5
%14 = call i64 @EC_POINT_point2oct(i32* %9, i32 %11, i32 %12, i32* null, i32 0, i32* %13)
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = add i64 %15, -1
store i64 %16, i64* %6
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32* null, i32** %3
br label %45
19: ; preds = %2
store i64 0, i64* %8
br label %20
20: ; preds = %39, %19
%21 = load i64, i64* %8
%22 = getelementptr inbounds [6 x %struct.anon], [6 x %struct.anon]* @find_digest.hashes, i64 0, i64 %21
%23 = getelementptr inbounds %struct.anon, %struct.anon* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %42
26: ; preds = %20
%27 = load i64, i64* %8
%28 = getelementptr inbounds [6 x %struct.anon], [6 x %struct.anon]* @find_digest.hashes, i64 0, i64 %27
%29 = getelementptr inbounds %struct.anon, %struct.anon* %28, i32 0, i32 1
%30 = load i32, i32* %29
store i32 %30, i32* %7
%31 = load i64, i64* %6
%32 = load i64, i64* %8
%33 = getelementptr inbounds [6 x %struct.anon], [6 x %struct.anon]* @find_digest.hashes, i64 0, i64 %32
%34 = getelementptr inbounds %struct.anon, %struct.anon* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = icmp ule i64 %31, %35
br i1 %36, label %37, label %38
37: ; preds = %26
br label %42
38: ; preds = %26
br label %39
39: ; preds = %38
%40 = load i64, i64* %8
%41 = add i64 %40, 1
store i64 %41, i64* %8
br label %20
42: ; preds = %37, %20
%43 = load i32, i32* %7
%44 = call i32* @EVP_get_digestbynid(i32 %43)
store i32* %44, i32** %3
br label %45
45: ; preds = %42, %18
%46 = load i32*, i32** %3
ret i32* %46
}

LLVM IR (-O2):
%struct.anon = type { i64, i32 }
@find_digest.hashes = internal unnamed_addr constant [6 x %struct.anon] [%struct.anon { i64 128, i32 137 }, %struct.anon { i64 132, i32 136 }, %struct.anon { i64 131, i32 135 }, %struct.anon { i64 130, i32 134 }, %struct.anon { i64 129, i32 133 }, %struct.anon zeroinitializer]
@POINT_CONVERSION_COMPRESSED = global i32 0
define i32* @find_digest(i32* %0, i32* %1) {
%3 = tail call i32 @EC_GROUP_get0_generator(i32* %0)
%4 = load i32, i32* @POINT_CONVERSION_COMPRESSED
%5 = tail call i64 @EC_POINT_point2oct(i32* %0, i32 %3, i32 %4, i32* null, i32 0, i32* %1)
%6 = add i64 %5, -1
%7 = icmp eq i64 %5, 0
br i1 %7, label %16, label %8
8: ; preds = %2
%9 = icmp ult i64 %6, 129
%10 = icmp ult i64 %6, 133
%11 = select i1 %10, i64 1, i64 4
%12 = select i1 %9, i64 0, i64 %11
%13 = getelementptr inbounds [6 x %struct.anon], [6 x %struct.anon]* @find_digest.hashes, i64 0, i64 %12, i32 1
%14 = load i32, i32* %13
%15 = tail call i32* @EVP_get_digestbynid(i32 %14)
br label %16
16: ; preds = %2, %8
%17 = phi i32* [ %15, %8 ], [ null, %2 ]
ret i32* %17
}