#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;

/* Variables and functions */
int /*<<< orphan*/ ECDSA_sign(int /*<<< orphan*/, unsigned char const *, int,
                              unsigned char *, unsigned int *,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_new();
int /*<<< orphan*/ ec_keys(int /*<<< orphan*/ *, unsigned char const *, int,
                           int /*<<< orphan*/ *, int /*<<< orphan*/, int);
unsigned int *malloc(int);
unsigned int ec_sign(unsigned char *sig, const unsigned char *hash,
                     int hash_len, const unsigned char *priv_key,
                     int priv_key_len, int curve_id) {
  EC_KEY *keys = EC_KEY_new();
  ec_keys(keys, priv_key, priv_key_len, NULL, 0, curve_id);
  unsigned int *sig_len = malloc(sizeof(unsigned int));
  ECDSA_sign(0, hash, hash_len, sig, sig_len, keys);
  EC_KEY_free(keys);
  return *sig_len;
}


x86 assembly (clang, -O0):
<ec_sign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
mov    $0x0,%al
call   1060 <EC_KEY_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1070 <ec_keys@plt>
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    -0x30(%rbp),%r9
xor    %edi,%edi
call   1030 <ECDSA_sign@plt>
mov    -0x30(%rbp),%rdi
call   1050 <EC_KEY_free@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ec_sign>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8d,%ebp
mov    %rcx,%rbx
mov    %edx,0x4(%rsp)
mov    %rsi,%r12
mov    %rdi,%r13
xor    %eax,%eax
call   1060 <EC_KEY_new@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
mov    %r14d,%r9d
call   1070 <ec_keys@plt>
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
mov    %r12,%rsi
mov    0x4(%rsp),%edx
mov    %r13,%rcx
mov    %rax,%r8
mov    %r15,%r9
call   1030 <ECDSA_sign@plt>
mov    %r15,%rdi
call   1050 <EC_KEY_free@plt>
mov    (%rbx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ec_sign(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%15 = call i32* (...) @EC_KEY_new()
store i32* %15, i32** %13
%16 = load i32*, i32** %13
%17 = load i8*, i8** %10
%18 = load i32, i32* %11
%19 = load i32, i32* %12
%20 = call i32 @ec_keys(i32* %16, i8* %17, i32 %18, i32* null, i32 0, i32 %19)
%21 = call i32* @malloc(i32 4)
store i32* %21, i32** %14
%22 = load i8*, i8** %8
%23 = load i32, i32* %9
%24 = load i8*, i8** %7
%25 = load i32*, i32** %14
%26 = load i32*, i32** %13
%27 = call i32 @ECDSA_sign(i32 0, i8* %22, i32 %23, i8* %24, i32* %25, i32* %26)
%28 = load i32*, i32** %13
%29 = call i32 @EC_KEY_free(i32* %28)
%30 = load i32*, i32** %14
%31 = load i32, i32* %30
ret i32 %31
}

LLVM IR (-O2):
define i32 @ec_sign(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4, i32 %5) {
%7 = tail call i32* (...) @EC_KEY_new()
%8 = tail call i32 @ec_keys(i32* %7, i8* %3, i32 %4, i32* null, i32 0, i32 %5)
%9 = tail call i32* @malloc(i32 4)
%10 = tail call i32 @ECDSA_sign(i32 0, i8* %1, i32 %2, i8* %0, i32* %9, i32* %7)
%11 = tail call i32 @EC_KEY_free(i32* %7)
%12 = load i32, i32* %9
ret i32 %12
}