#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_get0_private_key(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*/ const *,
                                int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_POINT_new(int /*<<< orphan*/ const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int compute_public_key(EC_KEY *key) {
  const EC_GROUP *group = EC_KEY_get0_group(key);
  const BIGNUM *priv_key = EC_KEY_get0_private_key(key);
  BN_CTX *ctx = NULL;
  EC_POINT *point = NULL;
  ctx = BN_CTX_new();
  if (!ctx) {
    fprintf(stderr, "BN_CTX_new failed\n");
    goto err;
  }
  point = EC_POINT_new(group);
  if (!point) {
    fprintf(stderr, "EC_POINT_new failed\n");
    goto err;
  }
  if (!EC_POINT_mul(group, point, priv_key, NULL, NULL, ctx)) {
    fprintf(stderr, "EC_POINT_mul failed\n");
    goto err;
  }
  if (!EC_KEY_set_public_key(key, point)) {
    fprintf(stderr, "EC_KEY_set_public_key failed\n");
    goto err;
  }
  EC_POINT_free(point);
  BN_CTX_free(ctx);
  return 0;
err:
  EC_POINT_free(point);
  BN_CTX_free(ctx);
  return 1;
}


x86 assembly (clang, -O0):
<compute_public_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <EC_KEY_get0_group@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <EC_KEY_get0_private_key@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1080 <BN_CTX_new@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f6 <compute_public_key+0x66>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12b6 <compute_public_key+0x126>
mov    -0x18(%rbp),%rdi
call   10b0 <EC_POINT_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1228 <compute_public_key+0x98>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12b6 <compute_public_key+0x126>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%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    1268 <compute_public_key+0xd8>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12b6 <compute_public_key+0x126>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <EC_KEY_set_public_key@plt>
cmp    $0x0,%eax
jne    1298 <compute_public_key+0x108>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12b6 <compute_public_key+0x126>
mov    -0x30(%rbp),%rdi
call   10a0 <EC_POINT_free@plt>
mov    -0x28(%rbp),%rdi
call   1060 <BN_CTX_free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cf <compute_public_key+0x13f>
mov    -0x30(%rbp),%rdi
call   10a0 <EC_POINT_free@plt>
mov    -0x28(%rbp),%rdi
call   1060 <BN_CTX_free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_public_key>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
call   1090 <EC_KEY_get0_group@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <EC_KEY_get0_private_key@plt>
mov    %rax,%r13
xor    %r15d,%r15d
xor    %eax,%eax
call   1080 <BN_CTX_new@plt>
mov    %rax,%r14
test   %rax,%rax
je     120e <compute_public_key+0x7e>
mov    %r12,%rdi
call   10b0 <EC_POINT_new@plt>
test   %rax,%rax
je     1217 <compute_public_key+0x87>
mov    %rax,%r15
mov    %r12,%rdi
mov    %rax,%rsi
mov    %r13,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
mov    %r14,%r9
call   1040 <EC_POINT_mul@plt>
test   %eax,%eax
je     1223 <compute_public_key+0x93>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <EC_KEY_set_public_key@plt>
test   %eax,%eax
je     122c <compute_public_key+0x9c>
mov    %r15,%rdi
call   10a0 <EC_POINT_free@plt>
mov    %r14,%rdi
call   1060 <BN_CTX_free@plt>
xor    %eax,%eax
jmp    1256 <compute_public_key+0xc6>
lea    0xdeb(%rip),%rsi
jmp    1233 <compute_public_key+0xa3>
lea    0xdf5(%rip),%rsi
xor    %r15d,%r15d
jmp    1233 <compute_public_key+0xa3>
lea    0xdfe(%rip),%rsi
jmp    1233 <compute_public_key+0xa3>
lea    0xe0a(%rip),%rsi
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1070 <fprintf@plt>
mov    %r15,%rdi
call   10a0 <EC_POINT_free@plt>
mov    %r14,%rdi
call   1060 <BN_CTX_free@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"BN_CTX_new failed\0A\00"
@.str.1 = [21 x i8] c"EC_POINT_new failed\0A\00"
@.str.2 = [21 x i8] c"EC_POINT_mul failed\0A\00"
@.str.3 = [30 x i8] c"EC_KEY_set_public_key failed\0A\00"
define i32 @compute_public_key(i32* %0) {
%2 = tail call i32* @EC_KEY_get0_group(i32* %0)
%3 = tail call i32* @EC_KEY_get0_private_key(i32* %0)
%4 = tail call i32* (...) @BN_CTX_new()
%5 = icmp eq i32* %4, null
br i1 %5, label %18, label %6
6: ; preds = %1
%7 = tail call i32* @EC_POINT_new(i32* %2)
%8 = icmp eq i32* %7, null
br i1 %8, label %18, label %9
9: ; preds = %6
%10 = tail call i32 @EC_POINT_mul(i32* %2, i32* %7, i32* %3, i32* null, i32* null, i32* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %9
%13 = tail call i32 @EC_KEY_set_public_key(i32* %0, i32* %7)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = tail call i32 @EC_POINT_free(i32* %7)
%17 = tail call i32 @BN_CTX_free(i32* %4)
br label %25
18: ; preds = %12, %9, %6, %1
%19 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %6 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), %9 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), %12 ]
%20 = phi i32* [ null, %1 ], [ null, %6 ], [ %7, %9 ], [ %7, %12 ]
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* %19)
%23 = tail call i32 @EC_POINT_free(i32* %20)
%24 = tail call i32 @BN_CTX_free(i32* %4)
br label %25
25: ; preds = %18, %15
%26 = phi i32 [ 0, %15 ], [ 1, %18 ]
ret i32 %26
}