#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 *, size_t,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_clear_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_get0_group(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_new_by_curve_name(int);
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*/ const *,
                                int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_POINT_new(int /*<<< orphan*/ const *);
EC_KEY *mk_eckey(int nid, const unsigned char *p, size_t plen) {
  EC_KEY *k = NULL;
  BIGNUM *priv = NULL;
  EC_POINT *pub = NULL;
  const EC_GROUP *grp;
  int ok = 0;
  k = EC_KEY_new_by_curve_name(nid);
  if (!k)
    goto err;
  priv = BN_bin2bn(p, plen, NULL);
  if (!priv)
    goto err;
  if (!EC_KEY_set_private_key(k, priv))
    goto err;
  grp = EC_KEY_get0_group(k);
  pub = EC_POINT_new(grp);
  if (!pub)
    goto err;
  if (!EC_POINT_mul(grp, pub, priv, NULL, NULL, NULL))
    goto err;
  if (!EC_KEY_set_public_key(k, pub))
    goto err;
  ok = 1;
err:
  BN_clear_free(priv);
  EC_POINT_free(pub);
  if (!ok) {
    EC_KEY_free(k);
    k = NULL;
  }
  return (k);
}


x86 assembly (clang, -O0):
<mk_eckey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x4(%rbp),%edi
call   10c0 <EC_KEY_new_by_curve_name@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ee <mk_eckey+0x4e>
jmp    12a4 <mk_eckey+0x104>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <BN_bin2bn@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1213 <mk_eckey+0x73>
jmp    12a4 <mk_eckey+0x104>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <EC_KEY_set_private_key@plt>
cmp    $0x0,%eax
jne    122e <mk_eckey+0x8e>
jmp    12a4 <mk_eckey+0x104>
mov    -0x20(%rbp),%rdi
call   1070 <EC_KEY_get0_group@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   10b0 <EC_POINT_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1258 <mk_eckey+0xb8>
jmp    12a4 <mk_eckey+0x104>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%rcx
mov    %r9,%r8
call   1040 <EC_POINT_mul@plt>
cmp    $0x0,%eax
jne    1282 <mk_eckey+0xe2>
jmp    12a4 <mk_eckey+0x104>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <EC_KEY_set_public_key@plt>
cmp    $0x0,%eax
jne    129d <mk_eckey+0xfd>
jmp    12a4 <mk_eckey+0x104>
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <BN_clear_free@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <EC_POINT_free@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    12d1 <mk_eckey+0x131>
mov    -0x20(%rbp),%rdi
call   1080 <EC_KEY_free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mk_eckey>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
call   10c0 <EC_KEY_new_by_curve_name@plt>
mov    %rax,%r12
test   %rax,%rax
je     1237 <mk_eckey+0x97>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <BN_bin2bn@plt>
test   %rax,%rax
je     1237 <mk_eckey+0x97>
mov    %rax,%r14
mov    %r12,%rdi
mov    %rax,%rsi
call   1060 <EC_KEY_set_private_key@plt>
test   %eax,%eax
je     123b <mk_eckey+0x9b>
mov    %r12,%rdi
call   1070 <EC_KEY_get0_group@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <EC_POINT_new@plt>
test   %rax,%rax
je     123b <mk_eckey+0x9b>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <EC_POINT_mul@plt>
test   %eax,%eax
je     1264 <mk_eckey+0xc4>
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <EC_KEY_set_public_key@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1090 <BN_clear_free@plt>
mov    %r15,%rdi
call   10a0 <EC_POINT_free@plt>
test   %ebx,%ebx
jne    1255 <mk_eckey+0xb5>
jmp    124a <mk_eckey+0xaa>
xor    %edi,%edi
jmp    123e <mk_eckey+0x9e>
mov    %r14,%rdi
call   1090 <BN_clear_free@plt>
xor    %edi,%edi
call   10a0 <EC_POINT_free@plt>
mov    %r12,%rdi
call   1080 <EC_KEY_free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1090 <BN_clear_free@plt>
mov    %r15,%rdi
jmp    1245 <mk_eckey+0xa5>

LLVM IR (-O0):
define i32* @mk_eckey(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32* null, i32** %7
store i32* null, i32** %8
store i32* null, i32** %9
store i32 0, i32* %11
%12 = load i32, i32* %4
%13 = call i32* @EC_KEY_new_by_curve_name(i32 %12)
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %3
br label %52
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = load i64, i64* %6
%20 = call i32* @BN_bin2bn(i8* %18, i64 %19, i32* null)
store i32* %20, i32** %8
%21 = load i32*, i32** %8
%22 = icmp ne i32* %21, null
br i1 %22, label %24, label %23
23: ; preds = %17
br label %52
24: ; preds = %17
%25 = load i32*, i32** %7
%26 = load i32*, i32** %8
%27 = call i32 @EC_KEY_set_private_key(i32* %25, i32* %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %24
br label %52
30: ; preds = %24
%31 = load i32*, i32** %7
%32 = call i32* @EC_KEY_get0_group(i32* %31)
store i32* %32, i32** %10
%33 = load i32*, i32** %10
%34 = call i32* @EC_POINT_new(i32* %33)
store i32* %34, i32** %9
%35 = load i32*, i32** %9
%36 = icmp ne i32* %35, null
br i1 %36, label %38, label %37
37: ; preds = %30
br label %52
38: ; preds = %30
%39 = load i32*, i32** %10
%40 = load i32*, i32** %9
%41 = load i32*, i32** %8
%42 = call i32 @EC_POINT_mul(i32* %39, i32* %40, i32* %41, i32* null, i32* null, i32* null)
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %38
br label %52
45: ; preds = %38
%46 = load i32*, i32** %7
%47 = load i32*, i32** %9
%48 = call i32 @EC_KEY_set_public_key(i32* %46, i32* %47)
%49 = icmp ne i32 %48, 0
br i1 %49, label %51, label %50
50: ; preds = %45
br label %52
51: ; preds = %45
store i32 1, i32* %11
br label %52
52: ; preds = %51, %50, %44, %37, %29, %23, %16
%53 = load i32*, i32** %8
%54 = call i32 @BN_clear_free(i32* %53)
%55 = load i32*, i32** %9
%56 = call i32 @EC_POINT_free(i32* %55)
%57 = load i32, i32* %11
%58 = icmp ne i32 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %52
%60 = load i32*, i32** %7
%61 = call i32 @EC_KEY_free(i32* %60)
store i32* null, i32** %7
br label %62
62: ; preds = %59, %52
%63 = load i32*, i32** %7
ret i32* %63
}

LLVM IR (-O2):
define i32* @mk_eckey(i32 %0, i8* %1, i64 %2) {
%4 = tail call i32* @EC_KEY_new_by_curve_name(i32 %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %36, label %6
6: ; preds = %3
%7 = tail call i32* @BN_bin2bn(i8* %1, i64 %2, i32* null)
%8 = icmp eq i32* %7, null
br i1 %8, label %33, label %9
9: ; preds = %6
%10 = tail call i32 @EC_KEY_set_private_key(i32* %4, i32* %7)
%11 = icmp eq i32 %10, 0
br i1 %11, label %30, label %12
12: ; preds = %9
%13 = tail call i32* @EC_KEY_get0_group(i32* %4)
%14 = tail call i32* @EC_POINT_new(i32* %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %27, label %16
16: ; preds = %12
%17 = tail call i32 @EC_POINT_mul(i32* %13, i32* %14, i32* %7, i32* null, i32* null, i32* null)
%18 = icmp eq i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = tail call i32 @EC_KEY_set_public_key(i32* %4, i32* %14)
%21 = icmp eq i32 %20, 0
%22 = tail call i32 @BN_clear_free(i32* %7)
%23 = tail call i32 @EC_POINT_free(i32* %14)
br i1 %21, label %39, label %41
24: ; preds = %16
%25 = tail call i32 @BN_clear_free(i32* %7)
%26 = tail call i32 @EC_POINT_free(i32* %14)
br label %39
27: ; preds = %12
%28 = tail call i32 @BN_clear_free(i32* %7)
%29 = tail call i32 @EC_POINT_free(i32* null)
br label %39
30: ; preds = %9
%31 = tail call i32 @BN_clear_free(i32* %7)
%32 = tail call i32 @EC_POINT_free(i32* null)
br label %39
33: ; preds = %6
%34 = tail call i32 @BN_clear_free(i32* null)
%35 = tail call i32 @EC_POINT_free(i32* null)
br label %39
36: ; preds = %3
%37 = tail call i32 @BN_clear_free(i32* null)
%38 = tail call i32 @EC_POINT_free(i32* null)
br label %39
39: ; preds = %36, %33, %30, %27, %24, %19
%40 = tail call i32 @EC_KEY_free(i32* %4)
br label %41
41: ; preds = %39, %19
%42 = phi i32* [ %4, %19 ], [ null, %39 ]
ret i32* %42
}