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

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_end(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_new();
int /*<<< orphan*/ BN_CTX_start(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_bin2bn(int /*<<< orphan*/ const *, int,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_clear_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ *EC_KEY_get0_group(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_new_by_curve_name(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*/ const *,
                                int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_POINT_new(int /*<<< orphan*/ const *);
int /*<<< orphan*/ NID_secp256k1;
EC_KEY *bbp_ec_new_keypair(const uint8_t *priv_bytes) {
  EC_KEY *key;
  BIGNUM *priv;
  BN_CTX *ctx;
  const EC_GROUP *group;
  EC_POINT *pub;
  key = EC_KEY_new_by_curve_name(NID_secp256k1);
  priv = BN_new();
  BN_bin2bn(priv_bytes, 32, priv);
  EC_KEY_set_private_key(key, priv);
  ctx = BN_CTX_new();
  BN_CTX_start(ctx);
  group = EC_KEY_get0_group(key);
  pub = EC_POINT_new(group);
  EC_POINT_mul(group, pub, priv, NULL, NULL, ctx);
  EC_KEY_set_public_key(key, pub);
  EC_POINT_free(pub);
  BN_CTX_end(ctx);
  BN_CTX_free(ctx);
  BN_clear_free(priv);
  return key;
}


x86 assembly (clang, -O0):
<bbp_ec_new_keypair>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1100 <EC_KEY_new_by_curve_name@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x20,%esi
call   1060 <BN_bin2bn@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <EC_KEY_set_private_key@plt>
mov    $0x0,%al
call   10b0 <BN_CTX_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <BN_CTX_start@plt>
mov    -0x10(%rbp),%rdi
call   10c0 <EC_KEY_get0_group@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   10f0 <EC_POINT_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%r9
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   1050 <EC_POINT_mul@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <EC_KEY_set_public_key@plt>
mov    -0x30(%rbp),%rdi
call   10e0 <EC_POINT_free@plt>
mov    -0x20(%rbp),%rdi
call   1080 <BN_CTX_end@plt>
mov    -0x20(%rbp),%rdi
call   1070 <BN_CTX_free@plt>
mov    -0x18(%rbp),%rdi
call   10d0 <BN_clear_free@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbp_ec_new_keypair>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1100 <EC_KEY_new_by_curve_name@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    $0x20,%esi
mov    %rax,%rdx
call   1060 <BN_bin2bn@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   10a0 <EC_KEY_set_private_key@plt>
xor    %eax,%eax
call   10b0 <BN_CTX_new@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1090 <BN_CTX_start@plt>
mov    %r14,%rdi
call   10c0 <EC_KEY_get0_group@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   10f0 <EC_POINT_new@plt>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %rax,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
mov    %r12,%r9
call   1050 <EC_POINT_mul@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <EC_KEY_set_public_key@plt>
mov    %rbx,%rdi
call   10e0 <EC_POINT_free@plt>
mov    %r12,%rdi
call   1080 <BN_CTX_end@plt>
mov    %r12,%rdi
call   1070 <BN_CTX_free@plt>
mov    %r15,%rdi
call   10d0 <BN_clear_free@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@NID_secp256k1 = global i32 0
define i32* @bbp_ec_new_keypair(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
store i32* %0, i32** %2
%8 = load i32, i32* @NID_secp256k1
%9 = call i32* @EC_KEY_new_by_curve_name(i32 %8)
store i32* %9, i32** %3
%10 = call i32* (...) @BN_new()
store i32* %10, i32** %4
%11 = load i32*, i32** %2
%12 = load i32*, i32** %4
%13 = call i32 @BN_bin2bn(i32* %11, i32 32, i32* %12)
%14 = load i32*, i32** %3
%15 = load i32*, i32** %4
%16 = call i32 @EC_KEY_set_private_key(i32* %14, i32* %15)
%17 = call i32* (...) @BN_CTX_new()
store i32* %17, i32** %5
%18 = load i32*, i32** %5
%19 = call i32 @BN_CTX_start(i32* %18)
%20 = load i32*, i32** %3
%21 = call i32* @EC_KEY_get0_group(i32* %20)
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = call i32* @EC_POINT_new(i32* %22)
store i32* %23, i32** %7
%24 = load i32*, i32** %6
%25 = load i32*, i32** %7
%26 = load i32*, i32** %4
%27 = load i32*, i32** %5
%28 = call i32 @EC_POINT_mul(i32* %24, i32* %25, i32* %26, i32* null, i32* null, i32* %27)
%29 = load i32*, i32** %3
%30 = load i32*, i32** %7
%31 = call i32 @EC_KEY_set_public_key(i32* %29, i32* %30)
%32 = load i32*, i32** %7
%33 = call i32 @EC_POINT_free(i32* %32)
%34 = load i32*, i32** %5
%35 = call i32 @BN_CTX_end(i32* %34)
%36 = load i32*, i32** %5
%37 = call i32 @BN_CTX_free(i32* %36)
%38 = load i32*, i32** %4
%39 = call i32 @BN_clear_free(i32* %38)
%40 = load i32*, i32** %3
ret i32* %40
}

LLVM IR (-O2):
@NID_secp256k1 = global i32 0
define i32* @bbp_ec_new_keypair(i32* %0) {
%2 = load i32, i32* @NID_secp256k1
%3 = tail call i32* @EC_KEY_new_by_curve_name(i32 %2)
%4 = tail call i32* (...) @BN_new()
%5 = tail call i32 @BN_bin2bn(i32* %0, i32 32, i32* %4)
%6 = tail call i32 @EC_KEY_set_private_key(i32* %3, i32* %4)
%7 = tail call i32* (...) @BN_CTX_new()
%8 = tail call i32 @BN_CTX_start(i32* %7)
%9 = tail call i32* @EC_KEY_get0_group(i32* %3)
%10 = tail call i32* @EC_POINT_new(i32* %9)
%11 = tail call i32 @EC_POINT_mul(i32* %9, i32* %10, i32* %4, i32* null, i32* null, i32* %7)
%12 = tail call i32 @EC_KEY_set_public_key(i32* %3, i32* %10)
%13 = tail call i32 @EC_POINT_free(i32* %10)
%14 = tail call i32 @BN_CTX_end(i32* %7)
%15 = tail call i32 @BN_CTX_free(i32* %7)
%16 = tail call i32 @BN_clear_free(i32* %4)
ret i32* %3
}