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

/* Variables and functions */
int /*<<< orphan*/ ECDSA_SIG_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *ECDSA_SIG_new();
int /*<<< orphan*/ *d2i_ECDSA_SIG(int /*<<< orphan*/ **, unsigned char const **,
                                  int);
int sm2_do_verify(unsigned char const *, int, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *);
int SM2_verify(int type, const unsigned char *dgst, int dgst_len,
               const unsigned char *sigbuf, int sig_len, EC_KEY *eckey) {
  ECDSA_SIG *s;
  int ret = -1;
  s = ECDSA_SIG_new();
  if (s == NULL)
    return (ret);
  if (d2i_ECDSA_SIG(&s, &sigbuf, sig_len) == NULL)
    goto err;
  ret = sm2_do_verify(dgst, dgst_len, s, eckey);
err:
  ECDSA_SIG_free(s);
  return (ret);
}


x86 assembly (clang, -O0):
<SM2_verify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    $0x0,%al
call   1040 <ECDSA_SIG_new@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1186 <SM2_verify+0x46>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cb <SM2_verify+0x8b>
mov    -0x24(%rbp),%edx
lea    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <d2i_ECDSA_SIG@plt>
cmp    $0x0,%rax
jne    11a5 <SM2_verify+0x65>
jmp    11bc <SM2_verify+0x7c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1050 <sm2_do_verify@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <ECDSA_SIG_free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SM2_verify>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r14
mov    %r8d,%ebp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rcx,0x10(%rsp)
xor    %eax,%eax
call   1040 <ECDSA_SIG_new@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1199 <SM2_verify+0x59>
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %ebp,%edx
call   1060 <d2i_ECDSA_SIG@plt>
test   %rax,%rax
je     11a0 <SM2_verify+0x60>
mov    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1050 <sm2_do_verify@plt>
mov    %eax,%ebx
jmp    11a5 <SM2_verify+0x65>
mov    $0xffffffff,%ebx
jmp    11af <SM2_verify+0x6f>
mov    $0xffffffff,%ebx
mov    0x8(%rsp),%rdi
call   1030 <ECDSA_SIG_free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @SM2_verify(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
store i32 %0, i32* %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i8* %3, i8** %11
store i32 %4, i32* %12
store i32* %5, i32** %13
store i32 -1, i32* %15
%16 = call i32* (...) @ECDSA_SIG_new()
store i32* %16, i32** %14
%17 = load i32*, i32** %14
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %6
%20 = load i32, i32* %15
store i32 %20, i32* %7
br label %36
21: ; preds = %6
%22 = load i32, i32* %12
%23 = call i32* @d2i_ECDSA_SIG(i32** %14, i8** %11, i32 %22)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %21
br label %32
26: ; preds = %21
%27 = load i8*, i8** %9
%28 = load i32, i32* %10
%29 = load i32*, i32** %14
%30 = load i32*, i32** %13
%31 = call i32 @sm2_do_verify(i8* %27, i32 %28, i32* %29, i32* %30)
store i32 %31, i32* %15
br label %32
32: ; preds = %26, %25
%33 = load i32*, i32** %14
%34 = call i32 @ECDSA_SIG_free(i32* %33)
%35 = load i32, i32* %15
store i32 %35, i32* %7
br label %36
36: ; preds = %32, %19
%37 = load i32, i32* %7
ret i32 %37
}

LLVM IR (-O2):
define i32 @SM2_verify(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4, i32* %5) {
%7 = alloca i8*
%8 = alloca i32*
store i8* %3, i8** %7
%9 = bitcast i32** %8 to i8*
%10 = tail call i32* (...) @ECDSA_SIG_new()
store i32* %10, i32** %8
%11 = icmp eq i32* %10, null
br i1 %11, label %22, label %12
12: ; preds = %6
%13 = call i32* @d2i_ECDSA_SIG(i32** %8, i8** %7, i32 %4)
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i32*, i32** %8
%17 = call i32 @sm2_do_verify(i8* %1, i32 %2, i32* %16, i32* %5)
br label %18
18: ; preds = %12, %15
%19 = phi i32 [ -1, %12 ], [ %17, %15 ]
%20 = load i32*, i32** %8
%21 = call i32 @ECDSA_SIG_free(i32* %20)
br label %22
22: ; preds = %6, %18
%23 = phi i32 [ %19, %18 ], [ -1, %6 ]
ret i32 %23
}