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

/* Variables and functions */
int /*<<< orphan*/ BN_bin2bn(unsigned char const *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ EC_GROUP_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_GROUP_new_by_curve_name(int);
int /*<<< orphan*/ EC_KEY_set_group(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_set_private_key(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_set_public_key(int /*<<< orphan*/ *,
                                         int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_POINT_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_POINT_mul(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_POINT_new(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_POINT_oct2point(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      unsigned char const *, int,
                                      int /*<<< orphan*/ *);
int ec_keys(EC_KEY *keys, const unsigned char *priv_key, int priv_key_len,
            const unsigned char *pub_key, int pub_key_len, int curve_id) {
  if (priv_key == NULL && pub_key == NULL) {
    return -1;
  }
  EC_GROUP *ec_group = EC_GROUP_new_by_curve_name(curve_id);
  BIGNUM *priv_key_bn = BN_new();
  EC_POINT *pub_key_point = EC_POINT_new(ec_group);
  if (priv_key != NULL) {
    BN_bin2bn(priv_key, priv_key_len, priv_key_bn);
    EC_KEY_set_private_key(keys, priv_key_bn);
  }
  if (pub_key != NULL) {
    EC_POINT_oct2point(ec_group, pub_key_point, pub_key, pub_key_len, NULL);
  } else {
    EC_POINT_mul(ec_group, pub_key_point, priv_key_bn, NULL, NULL, NULL);
  }
  EC_KEY_set_group(keys, ec_group);
  EC_KEY_set_public_key(keys, pub_key_point);
  EC_GROUP_free(ec_group);
  BN_free(priv_key_bn);
  EC_POINT_free(pub_key_point);
  return 1;
}


x86 assembly (clang, -O0):
<ec_keys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    %r9d,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1201 <ec_keys+0x41>
cmpq   $0x0,-0x28(%rbp)
jne    1201 <ec_keys+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <ec_keys+0x10e>
mov    -0x30(%rbp),%edi
call   10b0 <EC_GROUP_new_by_curve_name@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1050 <BN_new@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   10e0 <EC_POINT_new@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     124d <ec_keys+0x8d>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x40(%rbp),%rdx
call   1070 <BN_bin2bn@plt>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   10a0 <EC_KEY_set_private_key@plt>
cmpq   $0x0,-0x28(%rbp)
je     1276 <ec_keys+0xb6>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <EC_POINT_oct2point@plt>
jmp    1292 <ec_keys+0xd2>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%rcx
mov    %r9,%r8
call   1060 <EC_POINT_mul@plt>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10c0 <EC_KEY_set_group@plt>
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1030 <EC_KEY_set_public_key@plt>
mov    -0x38(%rbp),%rdi
call   1040 <EC_GROUP_free@plt>
mov    -0x40(%rbp),%rdi
call   1090 <BN_free@plt>
mov    -0x48(%rbp),%rdi
call   10d0 <EC_POINT_free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ec_keys>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbp
mov    $0xffffffff,%eax
mov    %rsi,%rcx
or     %rbp,%rcx
je     129a <ec_keys+0xda>
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %r8d,0xc(%rsp)
mov    %r9d,%edi
call   10b0 <EC_GROUP_new_by_curve_name@plt>
mov    %rax,%r13
xor    %eax,%eax
call   1050 <BN_new@plt>
mov    %rax,%r12
mov    %r13,%rdi
call   10e0 <EC_POINT_new@plt>
mov    %rax,0x10(%rsp)
test   %r15,%r15
je     122e <ec_keys+0x6e>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r12,%rdx
call   1070 <BN_bin2bn@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   10a0 <EC_KEY_set_private_key@plt>
mov    %r13,%rdi
test   %rbp,%rbp
je     124f <ec_keys+0x8f>
mov    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    0xc(%rsp),%ecx
xor    %r8d,%r8d
call   1080 <EC_POINT_oct2point@plt>
jmp    1267 <ec_keys+0xa7>
mov    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <EC_POINT_mul@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   10c0 <EC_KEY_set_group@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <EC_KEY_set_public_key@plt>
mov    %r13,%rdi
call   1040 <EC_GROUP_free@plt>
mov    %r12,%rdi
call   1090 <BN_free@plt>
mov    %rbx,%rdi
call   10d0 <EC_POINT_free@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ec_keys(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*
%16 = 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
%17 = load i8*, i8** %9
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %6
%20 = load i8*, i8** %11
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 -1, i32* %7
br label %66
23: ; preds = %19, %6
%24 = load i32, i32* %13
%25 = call i32* @EC_GROUP_new_by_curve_name(i32 %24)
store i32* %25, i32** %14
%26 = call i32* (...) @BN_new()
store i32* %26, i32** %15
%27 = load i32*, i32** %14
%28 = call i32* @EC_POINT_new(i32* %27)
store i32* %28, i32** %16
%29 = load i8*, i8** %9
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %39
31: ; preds = %23
%32 = load i8*, i8** %9
%33 = load i32, i32* %10
%34 = load i32*, i32** %15
%35 = call i32 @BN_bin2bn(i8* %32, i32 %33, i32* %34)
%36 = load i32*, i32** %8
%37 = load i32*, i32** %15
%38 = call i32 @EC_KEY_set_private_key(i32* %36, i32* %37)
br label %39
39: ; preds = %31, %23
%40 = load i8*, i8** %11
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = load i32*, i32** %14
%44 = load i32*, i32** %16
%45 = load i8*, i8** %11
%46 = load i32, i32* %12
%47 = call i32 @EC_POINT_oct2point(i32* %43, i32* %44, i8* %45, i32 %46, i32* null)
br label %53
48: ; preds = %39
%49 = load i32*, i32** %14
%50 = load i32*, i32** %16
%51 = load i32*, i32** %15
%52 = call i32 @EC_POINT_mul(i32* %49, i32* %50, i32* %51, i32* null, i32* null, i32* null)
br label %53
53: ; preds = %48, %42
%54 = load i32*, i32** %8
%55 = load i32*, i32** %14
%56 = call i32 @EC_KEY_set_group(i32* %54, i32* %55)
%57 = load i32*, i32** %8
%58 = load i32*, i32** %16
%59 = call i32 @EC_KEY_set_public_key(i32* %57, i32* %58)
%60 = load i32*, i32** %14
%61 = call i32 @EC_GROUP_free(i32* %60)
%62 = load i32*, i32** %15
%63 = call i32 @BN_free(i32* %62)
%64 = load i32*, i32** %16
%65 = call i32 @EC_POINT_free(i32* %64)
store i32 1, i32* %7
br label %66
66: ; preds = %53, %22
%67 = load i32, i32* %7
ret i32 %67
}

LLVM IR (-O2):
define i32 @ec_keys(i32* %0, i8* %1, i32 %2, i8* %3, i32 %4, i32 %5) {
%7 = icmp eq i8* %1, null
%8 = icmp eq i8* %3, null
%9 = and i1 %7, %8
br i1 %9, label %28, label %10
10: ; preds = %6
%11 = tail call i32* @EC_GROUP_new_by_curve_name(i32 %5)
%12 = tail call i32* (...) @BN_new()
%13 = tail call i32* @EC_POINT_new(i32* %11)
br i1 %7, label %17, label %14
14: ; preds = %10
%15 = tail call i32 @BN_bin2bn(i8* %1, i32 %2, i32* %12)
%16 = tail call i32 @EC_KEY_set_private_key(i32* %0, i32* %12)
br label %17
17: ; preds = %14, %10
br i1 %8, label %20, label %18
18: ; preds = %17
%19 = tail call i32 @EC_POINT_oct2point(i32* %11, i32* %13, i8* %3, i32 %4, i32* null)
br label %22
20: ; preds = %17
%21 = tail call i32 @EC_POINT_mul(i32* %11, i32* %13, i32* %12, i32* null, i32* null, i32* null)
br label %22
22: ; preds = %20, %18
%23 = tail call i32 @EC_KEY_set_group(i32* %0, i32* %11)
%24 = tail call i32 @EC_KEY_set_public_key(i32* %0, i32* %13)
%25 = tail call i32 @EC_GROUP_free(i32* %11)
%26 = tail call i32 @BN_free(i32* %12)
%27 = tail call i32 @EC_POINT_free(i32* %13)
br label %28
28: ; preds = %6, %22
%29 = phi i32 [ 1, %22 ], [ -1, %6 ]
ret i32 %29
}