#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*/ const uint8_t;
typedef int /*<<< orphan*/ EVP_MD_CTX;
typedef int /*<<< orphan*/ EVP_MD;
typedef int /*<<< orphan*/ EC_KEY;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ *BN_bin2bn(int /*<<< orphan*/ const *, int const,
                              int /*<<< orphan*/ *);
int ERR_R_EVP_LIB;
int ERR_R_INTERNAL_ERROR;
int ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ EVP_DigestFinal(int /*<<< orphan*/ *,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestInit(int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ const *);
int /*<<< orphan*/ EVP_DigestUpdate(int /*<<< orphan*/ *,
                                    int /*<<< orphan*/ const *, int const);
int /*<<< orphan*/ EVP_MD_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_MD_CTX_new();
int EVP_MD_size(int /*<<< orphan*/ const *);
int /*<<< orphan*/ OPENSSL_free(int /*<<< orphan*/ const *);
int /*<<< orphan*/ const *OPENSSL_zalloc(int const);
int /*<<< orphan*/ SM2err(int /*<<< orphan*/, int);
int /*<<< orphan*/ sm2_compute_z_digest(int /*<<< orphan*/ const *,
                                        int /*<<< orphan*/ const *,
                                        int /*<<< orphan*/ const *,
                                        size_t const,
                                        int /*<<< orphan*/ const *);
BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest, const EC_KEY *key,
                             const uint8_t *id, const size_t id_len,
                             const uint8_t *msg, size_t msg_len) {
  EVP_MD_CTX *hash = EVP_MD_CTX_new();
  const int md_size = EVP_MD_size(digest);
  uint8_t *z = NULL;
  BIGNUM *e = NULL;
  if (md_size < 0) {
    SM2err(0, 102);
    goto done;
  }
  z = OPENSSL_zalloc(md_size);
  if (hash == NULL || z == NULL) {
    SM2err(0, ERR_R_MALLOC_FAILURE);
    goto done;
  }
  if (!sm2_compute_z_digest(z, digest, id, id_len, key)) {
    goto done;
  }
  if (!EVP_DigestInit(hash, digest) || !EVP_DigestUpdate(hash, z, md_size) ||
      !EVP_DigestUpdate(hash, msg, msg_len) ||
      !EVP_DigestFinal(hash, z, NULL)) {
    SM2err(0, ERR_R_EVP_LIB);
    goto done;
  }
  e = BN_bin2bn(z, md_size, NULL);
  if (e == NULL)
    SM2err(0, ERR_R_INTERNAL_ERROR);
done:
  OPENSSL_free(z);
  EVP_MD_CTX_free(hash);
  return e;
}


x86 assembly (clang, -O0):
<sm2_compute_msg_hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    $0x0,%al
call   10b0 <EVP_MD_CTX_new@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <EVP_MD_size@plt>
mov    %eax,-0x3c(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1212 <sm2_compute_msg_hash+0x62>
xor    %edi,%edi
mov    $0x66,%esi
call   1030 <SM2err@plt>
jmp    131e <sm2_compute_msg_hash+0x16e>
mov    -0x3c(%rbp),%edi
call   1090 <OPENSSL_zalloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1234 <sm2_compute_msg_hash+0x84>
cmpq   $0x0,-0x48(%rbp)
jne    1249 <sm2_compute_msg_hash+0x99>
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <SM2err@plt>
jmp    131e <sm2_compute_msg_hash+0x16e>
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%r8
call   1070 <sm2_compute_z_digest@plt>
cmp    $0x0,%eax
jne    1270 <sm2_compute_msg_hash+0xc0>
jmp    131e <sm2_compute_msg_hash+0x16e>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <EVP_DigestInit@plt>
cmp    $0x0,%eax
je     12d5 <sm2_compute_msg_hash+0x125>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   10a0 <EVP_DigestUpdate@plt>
cmp    $0x0,%eax
je     12d5 <sm2_compute_msg_hash+0x125>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
call   10a0 <EVP_DigestUpdate@plt>
cmp    $0x0,%eax
je     12d5 <sm2_compute_msg_hash+0x125>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10c0 <EVP_DigestFinal@plt>
cmp    $0x0,%eax
jne    12ea <sm2_compute_msg_hash+0x13a>
mov    0x2d0c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <SM2err@plt>
jmp    131e <sm2_compute_msg_hash+0x16e>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <BN_bin2bn@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1319 <sm2_compute_msg_hash+0x169>
mov    0x2cc8(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <SM2err@plt>
jmp    131e <sm2_compute_msg_hash+0x16e>
mov    -0x48(%rbp),%rdi
call   10d0 <OPENSSL_free@plt>
mov    -0x38(%rbp),%rdi
call   1050 <EVP_MD_CTX_free@plt>
mov    -0x50(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sm2_compute_msg_hash>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r12
mov    %r8,%rbp
mov    %rcx,0x20(%rsp)
mov    %rdx,0x10(%rsp)
mov    %rsi,0x18(%rsp)
mov    %rdi,%rbx
xor    %r13d,%r13d
xor    %eax,%eax
call   10b0 <EVP_MD_CTX_new@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <EVP_MD_size@plt>
test   %eax,%eax
js     129e <sm2_compute_msg_hash+0xee>
mov    %eax,%r15d
mov    %rbp,(%rsp)
mov    %r12,0x8(%rsp)
mov    %eax,%edi
call   1090 <OPENSSL_zalloc@plt>
mov    %rax,%r13
mov    %r14,%r12
test   %r14,%r14
je     12a8 <sm2_compute_msg_hash+0xf8>
test   %r13,%r13
je     12a8 <sm2_compute_msg_hash+0xf8>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    0x10(%rsp),%rdx
mov    0x20(%rsp),%rcx
mov    0x18(%rsp),%r8
call   1070 <sm2_compute_z_digest@plt>
test   %eax,%eax
je     12e5 <sm2_compute_msg_hash+0x135>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <EVP_DigestInit@plt>
test   %eax,%eax
je     12b1 <sm2_compute_msg_hash+0x101>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   10a0 <EVP_DigestUpdate@plt>
test   %eax,%eax
je     12b1 <sm2_compute_msg_hash+0x101>
mov    %r12,%rdi
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
call   10a0 <EVP_DigestUpdate@plt>
test   %eax,%eax
je     12b1 <sm2_compute_msg_hash+0x101>
mov    %r12,%rdi
mov    %r13,%rsi
xor    %edx,%edx
call   10c0 <EVP_DigestFinal@plt>
test   %eax,%eax
je     12b1 <sm2_compute_msg_hash+0x101>
mov    %r13,%rdi
mov    %r15d,%esi
xor    %edx,%edx
call   1060 <BN_bin2bn@plt>
test   %rax,%rax
je     12e9 <sm2_compute_msg_hash+0x139>
mov    %rax,%rbx
jmp    12c3 <sm2_compute_msg_hash+0x113>
mov    $0x66,%esi
mov    %r14,%r12
jmp    12ba <sm2_compute_msg_hash+0x10a>
mov    0x2d31(%rip),%rax
jmp    12b8 <sm2_compute_msg_hash+0x108>
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <SM2err@plt>
mov    %r13,%rdi
call   10d0 <OPENSSL_free@plt>
mov    %r12,%rdi
call   1050 <EVP_MD_CTX_free@plt>
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
jmp    12c3 <sm2_compute_msg_hash+0x113>
mov    0x2ce8(%rip),%rax
jmp    12b8 <sm2_compute_msg_hash+0x108>

LLVM IR (-O0):
@ERR_R_MALLOC_FAILURE = global i32 0
@ERR_R_EVP_LIB = global i32 0
@ERR_R_INTERNAL_ERROR = global i32 0
define i32* @sm2_compute_msg_hash(i32* %0, i32* %1, i32* %2, i64 %3, i32* %4, i64 %5) {
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32*
%16 = alloca i32*
store i32* %0, i32** %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i64 %3, i64* %10
store i32* %4, i32** %11
store i64 %5, i64* %12
%17 = call i32* (...) @EVP_MD_CTX_new()
store i32* %17, i32** %13
%18 = load i32*, i32** %7
%19 = call i32 @EVP_MD_size(i32* %18)
store i32 %19, i32* %14
store i32* null, i32** %15
store i32* null, i32** %16
%20 = load i32, i32* %14
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %6
%23 = call i32 @SM2err(i32 0, i32 102)
br label %80
24: ; preds = %6
%25 = load i32, i32* %14
%26 = call i32* @OPENSSL_zalloc(i32 %25)
store i32* %26, i32** %15
%27 = load i32*, i32** %13
%28 = icmp eq i32* %27, null
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = load i32*, i32** %15
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29, %24
%33 = load i32, i32* @ERR_R_MALLOC_FAILURE
%34 = call i32 @SM2err(i32 0, i32 %33)
br label %80
35: ; preds = %29
%36 = load i32*, i32** %15
%37 = load i32*, i32** %7
%38 = load i32*, i32** %9
%39 = load i64, i64* %10
%40 = load i32*, i32** %8
%41 = call i32 @sm2_compute_z_digest(i32* %36, i32* %37, i32* %38, i64 %39, i32* %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %44, label %43
43: ; preds = %35
br label %80
44: ; preds = %35
%45 = load i32*, i32** %13
%46 = load i32*, i32** %7
%47 = call i32 @EVP_DigestInit(i32* %45, i32* %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %67
49: ; preds = %44
%50 = load i32*, i32** %13
%51 = load i32*, i32** %15
%52 = load i32, i32* %14
%53 = call i32 @EVP_DigestUpdate(i32* %50, i32* %51, i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %67
55: ; preds = %49
%56 = load i32*, i32** %13
%57 = load i32*, i32** %11
%58 = load i64, i64* %12
%59 = trunc i64 %58 to i32
%60 = call i32 @EVP_DigestUpdate(i32* %56, i32* %57, i32 %59)
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %55
%63 = load i32*, i32** %13
%64 = load i32*, i32** %15
%65 = call i32 @EVP_DigestFinal(i32* %63, i32* %64, i32* null)
%66 = icmp ne i32 %65, 0
br i1 %66, label %70, label %67
67: ; preds = %62, %55, %49, %44
%68 = load i32, i32* @ERR_R_EVP_LIB
%69 = call i32 @SM2err(i32 0, i32 %68)
br label %80
70: ; preds = %62
%71 = load i32*, i32** %15
%72 = load i32, i32* %14
%73 = call i32* @BN_bin2bn(i32* %71, i32 %72, i32* null)
store i32* %73, i32** %16
%74 = load i32*, i32** %16
%75 = icmp eq i32* %74, null
br i1 %75, label %76, label %79
76: ; preds = %70
%77 = load i32, i32* @ERR_R_INTERNAL_ERROR
%78 = call i32 @SM2err(i32 0, i32 %77)
br label %79
79: ; preds = %76, %70
br label %80
80: ; preds = %79, %67, %43, %32, %22
%81 = load i32*, i32** %15
%82 = call i32 @OPENSSL_free(i32* %81)
%83 = load i32*, i32** %13
%84 = call i32 @EVP_MD_CTX_free(i32* %83)
%85 = load i32*, i32** %16
ret i32* %85
}

LLVM IR (-O2):
@ERR_R_MALLOC_FAILURE = global i32 0
@ERR_R_EVP_LIB = global i32 0
@ERR_R_INTERNAL_ERROR = global i32 0
define i32* @sm2_compute_msg_hash(i32* %0, i32* %1, i32* %2, i64 %3, i32* %4, i64 %5) {
%7 = tail call i32* (...) @EVP_MD_CTX_new()
%8 = tail call i32 @EVP_MD_size(i32* %0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %40, label %10
10: ; preds = %6
%11 = tail call i32* @OPENSSL_zalloc(i32 %8)
%12 = icmp eq i32* %7, null
%13 = icmp eq i32* %11, null
%14 = select i1 %12, i1 true, i1 %13
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = load i32, i32* @ERR_R_MALLOC_FAILURE
br label %40
17: ; preds = %10
%18 = tail call i32 @sm2_compute_z_digest(i32* %11, i32* %0, i32* %2, i64 %3, i32* %1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %44, label %20
20: ; preds = %17
%21 = tail call i32 @EVP_DigestInit(i32* %7, i32* %0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %33, label %23
23: ; preds = %20
%24 = tail call i32 @EVP_DigestUpdate(i32* %7, i32* %11, i32 %8)
%25 = icmp eq i32 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %23
%27 = trunc i64 %5 to i32
%28 = tail call i32 @EVP_DigestUpdate(i32* %7, i32* %4, i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %26
%31 = tail call i32 @EVP_DigestFinal(i32* %7, i32* %11, i32* null)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30, %26, %23, %20
%34 = load i32, i32* @ERR_R_EVP_LIB
br label %40
35: ; preds = %30
%36 = tail call i32* @BN_bin2bn(i32* %11, i32 %8, i32* null)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %44
38: ; preds = %35
%39 = load i32, i32* @ERR_R_INTERNAL_ERROR
br label %40
40: ; preds = %6, %15, %33, %38
%41 = phi i32 [ %39, %38 ], [ %34, %33 ], [ %16, %15 ], [ 102, %6 ]
%42 = phi i32* [ %11, %38 ], [ %11, %33 ], [ %11, %15 ], [ null, %6 ]
%43 = tail call i32 @SM2err(i32 0, i32 %41)
br label %44
44: ; preds = %40, %35, %17
%45 = phi i32* [ %11, %35 ], [ %11, %17 ], [ %42, %40 ]
%46 = phi i32* [ %36, %35 ], [ null, %17 ], [ null, %40 ]
%47 = tail call i32 @OPENSSL_free(i32* %45)
%48 = tail call i32 @EVP_MD_CTX_free(i32* %7)
ret i32* %46
}