#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;

/* Variables and functions */
int /*<<< orphan*/ EVP_PKEY_X25519;
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int EVP_PKEY_get_raw_public_key(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                size_t *);
int /*<<< orphan*/ *EVP_PKEY_new_raw_private_key(int /*<<< orphan*/,
                                                 int /*<<< orphan*/ *,
                                                 int /*<<< orphan*/ const *,
                                                 int);
int glome_derive_key(const uint8_t private_key[32], uint8_t public_key[32]) {
  size_t public_key_length = 32;
  EVP_PKEY *pkey =
      EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, private_key, 32);
  int err =
      (pkey == NULL ||
       EVP_PKEY_get_raw_public_key(pkey, public_key, &public_key_length) != 1 ||
       public_key_length != 32);
  EVP_PKEY_free(pkey);
  return err;
}


x86 assembly (clang, -O0):
<glome_derive_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x20,-0x18(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
mov    $0x20,%ecx
call   1050 <EVP_PKEY_new_raw_private_key@plt>
mov    %rax,-0x20(%rbp)
mov    $0x1,%al
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x25(%rbp)
je     11a3 <glome_derive_key+0x73>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <EVP_PKEY_get_raw_public_key@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x1,%ecx
mov    %al,-0x25(%rbp)
jne    11a3 <glome_derive_key+0x73>
cmpq   $0x20,-0x18(%rbp)
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <EVP_PKEY_free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<glome_derive_key>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rdx
movq   $0x20,0x8(%rsp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x20,%ecx
call   1050 <EVP_PKEY_new_raw_private_key@plt>
mov    %rax,%rbx
mov    $0x1,%ebp
test   %rax,%rax
je     118a <glome_derive_key+0x5a>
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <EVP_PKEY_get_raw_public_key@plt>
cmp    $0x1,%eax
jne    118a <glome_derive_key+0x5a>
xor    %ebp,%ebp
cmpq   $0x20,0x8(%rsp)
setne  %bpl
mov    %rbx,%rdi
call   1030 <EVP_PKEY_free@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EVP_PKEY_X25519 = global i32 0
define i32 @glome_derive_key(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
store i64 32, i64* %5
%8 = load i32, i32* @EVP_PKEY_X25519
%9 = load i32*, i32** %3
%10 = call i32* @EVP_PKEY_new_raw_private_key(i32 %8, i32* null, i32* %9, i32 32)
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %21, label %13
13: ; preds = %2
%14 = load i32*, i32** %6
%15 = load i32*, i32** %4
%16 = call i32 @EVP_PKEY_get_raw_public_key(i32* %14, i32* %15, i64* %5)
%17 = icmp ne i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i64, i64* %5
%20 = icmp ne i64 %19, 32
br label %21
21: ; preds = %18, %13, %2
%22 = phi i1 [ true, %13 ], [ true, %2 ], [ %20, %18 ]
%23 = zext i1 %22 to i32
store i32 %23, i32* %7
%24 = load i32*, i32** %6
%25 = call i32 @EVP_PKEY_free(i32* %24)
%26 = load i32, i32* %7
ret i32 %26
}

LLVM IR (-O2):
@EVP_PKEY_X25519 = global i32 0
define i32 @glome_derive_key(i32* %0, i32* %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i64 32, i64* %3
%5 = load i32, i32* @EVP_PKEY_X25519
%6 = tail call i32* @EVP_PKEY_new_raw_private_key(i32 %5, i32* null, i32* %0, i32 32)
%7 = icmp eq i32* %6, null
br i1 %7, label %15, label %8
8: ; preds = %2
%9 = call i32 @EVP_PKEY_get_raw_public_key(i32* %6, i32* %1, i64* %3)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i64, i64* %3
%13 = icmp ne i64 %12, 32
%14 = zext i1 %13 to i32
br label %15
15: ; preds = %11, %8, %2
%16 = phi i32 [ 1, %8 ], [ 1, %2 ], [ %14, %11 ]
%17 = call i32 @EVP_PKEY_free(i32* %6)
ret i32 %16
}