#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*/ *ECDSA_do_sign_ex(unsigned char const *, int,
                                     int /*<<< orphan*/ const *,
                                     int /*<<< orphan*/ const *,
                                     int /*<<< orphan*/ *);
unsigned int i2d_ECDSA_SIG(int /*<<< orphan*/ *, unsigned char **);
int ECDSA_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;
  s = ECDSA_do_sign_ex(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):
<ECDSA_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
mov    -0x30(%rbp),%rdx
mov    0x10(%rbp),%rcx
mov    0x18(%rbp),%r8
call   1050 <ECDSA_do_sign_ex@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1193 <ECDSA_sign_ex+0x63>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11b8 <ECDSA_sign_ex+0x88>
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <i2d_ECDSA_SIG@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rdi
call   1040 <ECDSA_SIG_free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ECDSA_sign_ex>:
push   %r14
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rsi,%rdi
mov    0x20(%rsp),%rax
mov    0x28(%rsp),%r8
mov    %rcx,(%rsp)
mov    %edx,%esi
mov    %r9,%rdx
mov    %rax,%rcx
call   1050 <ECDSA_do_sign_ex@plt>
test   %rax,%rax
je     117a <ECDSA_sign_ex+0x4a>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1030 <i2d_ECDSA_SIG@plt>
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1040 <ECDSA_SIG_free@plt>
mov    $0x1,%eax
jmp    1183 <ECDSA_sign_ex+0x53>
movl   $0x0,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @ECDSA_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 = load i32*, i32** %15
%22 = load i32*, i32** %16
%23 = load i32*, i32** %17
%24 = call i32* @ECDSA_do_sign_ex(i8* %19, i32 %20, i32* %21, i32* %22, i32* %23)
store i32* %24, i32** %18
%25 = load i32*, i32** %18
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %8
%28 = load i32*, i32** %14
store i32 0, i32* %28
store i32 0, i32* %9
br label %35
29: ; preds = %8
%30 = load i32*, i32** %18
%31 = call i32 @i2d_ECDSA_SIG(i32* %30, i8** %13)
%32 = load i32*, i32** %14
store i32 %31, i32* %32
%33 = load i32*, i32** %18
%34 = call i32 @ECDSA_SIG_free(i32* %33)
store i32 1, i32* %9
br label %35
35: ; preds = %29, %27
%36 = load i32, i32* %9
ret i32 %36
}

LLVM IR (-O2):
define i32 @ECDSA_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* @ECDSA_do_sign_ex(i8* %1, i32 %2, i32* %5, i32* %6, i32* %7)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %8
store i32 0, i32* %4
br label %16
13: ; preds = %8
%14 = call i32 @i2d_ECDSA_SIG(i32* %10, i8** %9)
store i32 %14, i32* %4
%15 = call i32 @ECDSA_SIG_free(i32* %10)
br label %16
16: ; preds = %13, %12
%17 = phi i32 [ 0, %12 ], [ 1, %13 ]
ret i32 %17
}