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

/* Variables and functions */
scalar_t__ EVP_PKEY_get0(int /*<<< orphan*/ *);
int i2o_ECPublicKey(int /*<<< orphan*/ *, int /*<<< orphan*/ **);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, size_t);
int pkey_to_public_key(EVP_PKEY *pkey, uint8_t *keybuf, size_t keybuf_sz) {
  EC_KEY *key = NULL;
  int keylen = 0;
  if ((key = (EC_KEY *)EVP_PKEY_get0(pkey)) != NULL)
    if ((keylen = i2o_ECPublicKey(key, NULL)) != 0 && keybuf_sz >= keylen) {
      memset(keybuf, 0, keybuf_sz);
      i2o_ECPublicKey(key, &keybuf);
      return keylen;
    }
  return 0;
}


x86 assembly (clang, -O0):
<pkey_to_public_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <EVP_PKEY_get0@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11c0 <pkey_to_public_key+0x90>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <i2o_ECPublicKey@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     11bb <pkey_to_public_key+0x8b>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmp    %rcx,%rax
jb     11bb <pkey_to_public_key+0x8b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x28(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <i2o_ECPublicKey@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c7 <pkey_to_public_key+0x97>
jmp    11c0 <pkey_to_public_key+0x90>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkey_to_public_key>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rsi,0x8(%rsp)
call   1030 <EVP_PKEY_get0@plt>
test   %rax,%rax
je     118d <pkey_to_public_key+0x5d>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <i2o_ECPublicKey@plt>
test   %eax,%eax
je     118f <pkey_to_public_key+0x5f>
mov    %eax,%r12d
cltq
cmp    %r14,%rax
ja     118f <pkey_to_public_key+0x5f>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <memset@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <i2o_ECPublicKey@plt>
mov    %r12d,%ebp
jmp    118f <pkey_to_public_key+0x5f>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pkey_to_public_key(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
store i32* null, i32** %8
store i32 0, i32* %9
%10 = load i32*, i32** %5
%11 = call i64 @EVP_PKEY_get0(i32* %10)
%12 = inttoptr i64 %11 to i32*
store i32* %12, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %31
14: ; preds = %3
%15 = load i32*, i32** %8
%16 = call i32 @i2o_ECPublicKey(i32* %15, i32** null)
store i32 %16, i32* %9
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = load i64, i64* %7
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = icmp uge i64 %19, %21
br i1 %22, label %23, label %30
23: ; preds = %18
%24 = load i32*, i32** %6
%25 = load i64, i64* %7
%26 = call i32 @memset(i32* %24, i32 0, i64 %25)
%27 = load i32*, i32** %8
%28 = call i32 @i2o_ECPublicKey(i32* %27, i32** %6)
%29 = load i32, i32* %9
store i32 %29, i32* %4
br label %32
30: ; preds = %18, %14
br label %31
31: ; preds = %30, %3
store i32 0, i32* %4
br label %32
32: ; preds = %31, %23
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
define i32 @pkey_to_public_key(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32*
store i32* %1, i32** %4
%5 = tail call i64 @EVP_PKEY_get0(i32* %0)
%6 = inttoptr i64 %5 to i32*
%7 = icmp eq i64 %5, 0
br i1 %7, label %17, label %8
8: ; preds = %3
%9 = tail call i32 @i2o_ECPublicKey(i32* %6, i32** null)
%10 = icmp eq i32 %9, 0
%11 = sext i32 %9 to i64
%12 = icmp ugt i64 %11, %2
%13 = or i1 %10, %12
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = tail call i32 @memset(i32* %1, i32 0, i64 %2)
%16 = call i32 @i2o_ECPublicKey(i32* %6, i32** %4)
br label %17
17: ; preds = %3, %8, %14
%18 = phi i32 [ %9, %14 ], [ 0, %8 ], [ 0, %3 ]
ret i32 %18
}