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

/* Variables and functions */
int /*<<< orphan*/ EVP_PKEY_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_CTX_new_id(int /*<<< orphan*/,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_X25519;
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int EVP_PKEY_get_raw_private_key(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                 size_t *);
int EVP_PKEY_get_raw_public_key(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                size_t *);
int EVP_PKEY_keygen(int /*<<< orphan*/ *, int /*<<< orphan*/ **);
int EVP_PKEY_keygen_init(int /*<<< orphan*/ *);
int glome_generate_key(uint8_t private_key[32], uint8_t public_key[32]) {
  size_t public_key_len = 32;
  size_t private_key_len = 32;
  EVP_PKEY *pkey = NULL;
  EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_X25519, NULL);
  int err =
      (ctx == NULL || EVP_PKEY_keygen_init(ctx) != 1 ||
       EVP_PKEY_keygen(ctx, &pkey) != 1 ||
       EVP_PKEY_get_raw_public_key(pkey, public_key, &public_key_len) != 1 ||
       public_key_len != 32 ||
       EVP_PKEY_get_raw_private_key(pkey, private_key, &private_key_len) != 1 ||
       private_key_len != 32);
  EVP_PKEY_CTX_free(ctx);
  EVP_PKEY_free(pkey);
  return err;
}


x86 assembly (clang, -O0):
<glome_generate_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x20,-0x18(%rbp)
movq   $0x20,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <EVP_PKEY_CTX_new_id@plt>
mov    %rax,-0x30(%rbp)
mov    $0x1,%al
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x35(%rbp)
je     1251 <glome_generate_key+0xe1>
mov    -0x30(%rbp),%rdi
call   1070 <EVP_PKEY_keygen_init@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x1,%ecx
mov    %al,-0x35(%rbp)
jne    1251 <glome_generate_key+0xe1>
mov    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <EVP_PKEY_keygen@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x1,%ecx
mov    %al,-0x35(%rbp)
jne    1251 <glome_generate_key+0xe1>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1050 <EVP_PKEY_get_raw_public_key@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x1,%ecx
mov    %al,-0x35(%rbp)
jne    1251 <glome_generate_key+0xe1>
mov    $0x1,%al
cmpq   $0x20,-0x18(%rbp)
mov    %al,-0x35(%rbp)
jne    1251 <glome_generate_key+0xe1>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <EVP_PKEY_get_raw_private_key@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x1,%ecx
mov    %al,-0x35(%rbp)
jne    1251 <glome_generate_key+0xe1>
cmpq   $0x20,-0x20(%rbp)
setne  %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <EVP_PKEY_CTX_free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <EVP_PKEY_free@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<glome_generate_key>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
movq   $0x20,0x10(%rsp)
movq   $0x20,0x8(%rsp)
movq   $0x0,(%rsp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <EVP_PKEY_CTX_new_id@plt>
mov    %rax,%rbx
mov    $0x1,%ebp
test   %rax,%rax
je     1214 <glome_generate_key+0xa4>
mov    %rbx,%rdi
call   1070 <EVP_PKEY_keygen_init@plt>
cmp    $0x1,%eax
jne    1214 <glome_generate_key+0xa4>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <EVP_PKEY_keygen@plt>
cmp    $0x1,%eax
jne    1214 <glome_generate_key+0xa4>
mov    (%rsp),%rdi
lea    0x10(%rsp),%rdx
mov    %r15,%rsi
call   1050 <EVP_PKEY_get_raw_public_key@plt>
cmp    $0x1,%eax
jne    1214 <glome_generate_key+0xa4>
cmpq   $0x20,0x10(%rsp)
jne    1214 <glome_generate_key+0xa4>
mov    (%rsp),%rdi
lea    0x8(%rsp),%rdx
mov    %r14,%rsi
call   1080 <EVP_PKEY_get_raw_private_key@plt>
cmp    $0x1,%eax
jne    1214 <glome_generate_key+0xa4>
xor    %ebp,%ebp
cmpq   $0x20,0x8(%rsp)
setne  %bpl
mov    %rbx,%rdi
call   1090 <EVP_PKEY_CTX_free@plt>
mov    (%rsp),%rdi
call   1040 <EVP_PKEY_free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_PKEY_X25519 = global i32 0
define i32 @glome_generate_key(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
store i64 32, i64* %5
store i64 32, i64* %6
store i32* null, i32** %7
%10 = load i32, i32* @EVP_PKEY_X25519
%11 = call i32* @EVP_PKEY_CTX_new_id(i32 %10, i32* null)
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %38, label %14
14: ; preds = %2
%15 = load i32*, i32** %8
%16 = call i32 @EVP_PKEY_keygen_init(i32* %15)
%17 = icmp ne i32 %16, 1
br i1 %17, label %38, label %18
18: ; preds = %14
%19 = load i32*, i32** %8
%20 = call i32 @EVP_PKEY_keygen(i32* %19, i32** %7)
%21 = icmp ne i32 %20, 1
br i1 %21, label %38, label %22
22: ; preds = %18
%23 = load i32*, i32** %7
%24 = load i32*, i32** %4
%25 = call i32 @EVP_PKEY_get_raw_public_key(i32* %23, i32* %24, i64* %5)
%26 = icmp ne i32 %25, 1
br i1 %26, label %38, label %27
27: ; preds = %22
%28 = load i64, i64* %5
%29 = icmp ne i64 %28, 32
br i1 %29, label %38, label %30
30: ; preds = %27
%31 = load i32*, i32** %7
%32 = load i32*, i32** %3
%33 = call i32 @EVP_PKEY_get_raw_private_key(i32* %31, i32* %32, i64* %6)
%34 = icmp ne i32 %33, 1
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = load i64, i64* %6
%37 = icmp ne i64 %36, 32
br label %38
38: ; preds = %35, %30, %27, %22, %18, %14, %2
%39 = phi i1 [ true, %30 ], [ true, %27 ], [ true, %22 ], [ true, %18 ], [ true, %14 ], [ true, %2 ], [ %37, %35 ]
%40 = zext i1 %39 to i32
store i32 %40, i32* %9
%41 = load i32*, i32** %8
%42 = call i32 @EVP_PKEY_CTX_free(i32* %41)
%43 = load i32*, i32** %7
%44 = call i32 @EVP_PKEY_free(i32* %43)
%45 = load i32, i32* %9
ret i32 %45
}

LLVM IR (-O2):
@EVP_PKEY_X25519 = global i32 0
define i32 @glome_generate_key(i32* %0, i32* %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32*
%6 = bitcast i64* %3 to i8*
store i64 32, i64* %3
%7 = bitcast i64* %4 to i8*
store i64 32, i64* %4
%8 = bitcast i32** %5 to i8*
store i32* null, i32** %5
%9 = load i32, i32* @EVP_PKEY_X25519
%10 = tail call i32* @EVP_PKEY_CTX_new_id(i32 %9, i32* null)
%11 = icmp eq i32* %10, null
br i1 %11, label %33, label %12
12: ; preds = %2
%13 = tail call i32 @EVP_PKEY_keygen_init(i32* %10)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %33
15: ; preds = %12
%16 = call i32 @EVP_PKEY_keygen(i32* %10, i32** %5)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %33
18: ; preds = %15
%19 = load i32*, i32** %5
%20 = call i32 @EVP_PKEY_get_raw_public_key(i32* %19, i32* %1, i64* %3)
%21 = icmp ne i32 %20, 1
%22 = load i64, i64* %3
%23 = icmp ne i64 %22, 32
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %33, label %25
25: ; preds = %18
%26 = load i32*, i32** %5
%27 = call i32 @EVP_PKEY_get_raw_private_key(i32* %26, i32* %0, i64* %4)
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i64, i64* %4
%31 = icmp ne i64 %30, 32
%32 = zext i1 %31 to i32
br label %33
33: ; preds = %29, %25, %18, %15, %12, %2
%34 = phi i32 [ 1, %25 ], [ 1, %18 ], [ 1, %15 ], [ 1, %12 ], [ 1, %2 ], [ %32, %29 ]
%35 = call i32 @EVP_PKEY_CTX_free(i32* %10)
%36 = load i32*, i32** %5
%37 = call i32 @EVP_PKEY_free(i32* %36)
ret i32 %34
}