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

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_new();
int /*<<< orphan*/ *EC_KEY_get0_group(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 EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key) {
  int ok = 0;
  BN_CTX *ctx = NULL;
  EC_POINT *pub_key = NULL;
  if (!eckey)
    return 0;
  const EC_GROUP *group = EC_KEY_get0_group(eckey);
  if ((ctx = BN_CTX_new()) == NULL)
    goto err;
  pub_key = EC_POINT_new(group);
  if (pub_key == NULL)
    goto err;
  if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
    goto err;
  EC_KEY_set_private_key(eckey, priv_key);
  EC_KEY_set_public_key(eckey, pub_key);
  ok = 1;
err:
  if (pub_key)
    EC_POINT_free(pub_key);
  if (ctx != NULL)
    BN_CTX_free(ctx);
  return (ok);
}


x86 assembly (clang, -O0):
<EC_KEY_regenerate_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11be <EC_KEY_regenerate_key+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    127c <EC_KEY_regenerate_key+0xfc>
mov    -0x10(%rbp),%rdi
call   1080 <EC_KEY_get0_group@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1070 <BN_CTX_new@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11e5 <EC_KEY_regenerate_key+0x65>
jmp    124e <EC_KEY_regenerate_key+0xce>
mov    -0x38(%rbp),%rdi
call   10a0 <EC_POINT_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1202 <EC_KEY_regenerate_key+0x82>
jmp    124e <EC_KEY_regenerate_key+0xce>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%r9
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   1040 <EC_POINT_mul@plt>
cmp    $0x0,%eax
jne    122d <EC_KEY_regenerate_key+0xad>
jmp    124e <EC_KEY_regenerate_key+0xce>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <EC_KEY_set_private_key@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <EC_KEY_set_public_key@plt>
movl   $0x1,-0x1c(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1262 <EC_KEY_regenerate_key+0xe2>
mov    -0x30(%rbp),%rdi
call   1090 <EC_POINT_free@plt>
cmpq   $0x0,-0x28(%rbp)
je     1276 <EC_KEY_regenerate_key+0xf6>
mov    -0x28(%rbp),%rdi
call   1050 <BN_CTX_free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EC_KEY_regenerate_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1203 <EC_KEY_regenerate_key+0x83>
mov    %rsi,%r15
mov    %rdi,%rbx
call   1080 <EC_KEY_get0_group@plt>
mov    %rax,%rbp
xor    %r12d,%r12d
xor    %eax,%eax
call   1070 <BN_CTX_new@plt>
test   %rax,%rax
je     1213 <EC_KEY_regenerate_key+0x93>
mov    %rax,%r14
mov    %rbp,%rdi
call   10a0 <EC_POINT_new@plt>
test   %rax,%rax
je     1208 <EC_KEY_regenerate_key+0x88>
mov    %rax,%r13
xor    %r12d,%r12d
mov    %rbp,%rdi
mov    %rax,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
mov    %r14,%r9
call   1040 <EC_POINT_mul@plt>
test   %eax,%eax
je     11f9 <EC_KEY_regenerate_key+0x79>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <EC_KEY_set_private_key@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1030 <EC_KEY_set_public_key@plt>
mov    $0x1,%r12d
mov    %r13,%rdi
call   1090 <EC_POINT_free@plt>
jmp    120b <EC_KEY_regenerate_key+0x8b>
xor    %r12d,%r12d
jmp    1213 <EC_KEY_regenerate_key+0x93>
xor    %r12d,%r12d
mov    %r14,%rdi
call   1050 <BN_CTX_free@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @EC_KEY_regenerate_key(i32* %0, i32* %1) {
%3 = icmp eq i32* %0, null
br i1 %3, label %23, label %4
4: ; preds = %2
%5 = tail call i32* @EC_KEY_get0_group(i32* %0)
%6 = tail call i32* (...) @BN_CTX_new()
%7 = icmp eq i32* %6, null
br i1 %7, label %23, label %8
8: ; preds = %4
%9 = tail call i32* @EC_POINT_new(i32* %5)
%10 = icmp eq i32* %9, null
br i1 %10, label %20, label %11
11: ; preds = %8
%12 = tail call i32 @EC_POINT_mul(i32* %5, i32* %9, i32* %1, i32* null, i32* null, i32* %6)
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 @EC_KEY_set_private_key(i32* %0, i32* %1)
%16 = tail call i32 @EC_KEY_set_public_key(i32* %0, i32* %9)
br label %17
17: ; preds = %11, %14
%18 = phi i32 [ 1, %14 ], [ 0, %11 ]
%19 = tail call i32 @EC_POINT_free(i32* %9)
br label %20
20: ; preds = %8, %17
%21 = phi i32 [ %18, %17 ], [ 0, %8 ]
%22 = tail call i32 @BN_CTX_free(i32* %6)
br label %23
23: ; preds = %4, %20, %2
%24 = phi i32 [ 0, %2 ], [ %21, %20 ], [ 0, %4 ]
ret i32 %24
}