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

/* Variables and functions */
int /*<<< orphan*/ ECDSA_SIG_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ RAND_seed(unsigned char const *, int);
unsigned int i2d_ECDSA_SIG(int /*<<< orphan*/ *, unsigned char **);
int /*<<< orphan*/ *sm2_do_sign(unsigned char const *, int,
                                int /*<<< orphan*/ const *,
                                int /*<<< orphan*/ const *,
                                int /*<<< orphan*/ *);
int SM2_sign_ex(int type, const unsigned char *dgst, int dlen,
                unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv,
                const BIGNUM *r, EC_KEY *eckey) {
  ECDSA_SIG *s;
  RAND_seed(dgst, dlen);
  s = sm2_do_sign(dgst, dlen, kinv, r, eckey);
  if (s == NULL) {
    *siglen = 0;
    return 0;
  }
  *siglen = i2d_ECDSA_SIG(s, &sig);
  ECDSA_SIG_free(s);
  return 1;
}


x86 assembly (clang, -O0):
<SM2_sign_ex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <RAND_seed@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    0x10(%rbp),%rcx
mov    0x18(%rbp),%r8
call   1030 <sm2_do_sign@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11af <SM2_sign_ex+0x6f>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11d4 <SM2_sign_ex+0x94>
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <i2d_ECDSA_SIG@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rdi
call   1060 <ECDSA_SIG_free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SM2_sign_ex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r15
mov    %r8,%r14
mov    %edx,%ebp
mov    %rsi,%rbx
mov    0x40(%rsp),%r12
mov    0x48(%rsp),%r13
mov    %rcx,(%rsp)
mov    %rsi,%rdi
mov    %edx,%esi
call   1040 <RAND_seed@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %r13,%r8
call   1030 <sm2_do_sign@plt>
test   %rax,%rax
je     11a6 <SM2_sign_ex+0x66>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1050 <i2d_ECDSA_SIG@plt>
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1060 <ECDSA_SIG_free@plt>
mov    $0x1,%eax
jmp    11af <SM2_sign_ex+0x6f>
movl   $0x0,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @SM2_sign_ex(i32 %0, i8* %1, i32 %2, i8* %3, i32* nocapture writeonly %4, i32* %5, i32* %6, i32* %7) {
%9 = alloca i8*
store i8* %3, i8** %9
%10 = tail call i32 @RAND_seed(i8* %1, i32 %2)
%11 = tail call i32* @sm2_do_sign(i8* %1, i32 %2, i32* %5, i32* %6, i32* %7)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %8
store i32 0, i32* %4
br label %17
14: ; preds = %8
%15 = call i32 @i2d_ECDSA_SIG(i32* %11, i8** %9)
store i32 %15, i32* %4
%16 = call i32 @ECDSA_SIG_free(i32* %11)
br label %17
17: ; preds = %14, %13
%18 = phi i32 [ 0, %13 ], [ 1, %14 ]
ret i32 %18
}