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

/* Variables and functions */
int /*<<< orphan*/ *EVP_PKEY_CTX_get0_peerkey(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_CTX_get0_pkey(int /*<<< orphan*/ *);
int /*<<< orphan*/ gost_get0_priv_key(int /*<<< orphan*/ *);
int make_cp_exchange_key(int /*<<< orphan*/, int /*<<< orphan*/ *,
                         unsigned char *);
int pkey_gost94_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen) {
  EVP_PKEY *pubk = EVP_PKEY_CTX_get0_peerkey(ctx);
  EVP_PKEY *mykey = EVP_PKEY_CTX_get0_pkey(ctx);
  *keylen = 32;
  if (key == NULL)
    return 1;
  return make_cp_exchange_key(gost_get0_priv_key(mykey), pubk, key);
}


x86 assembly (clang, -O0):
<pkey_gost94_derive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <EVP_PKEY_CTX_get0_peerkey@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <EVP_PKEY_CTX_get0_pkey@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x20,(%rax)
cmpq   $0x0,-0x18(%rbp)
jne    1190 <pkey_gost94_derive+0x50>
movl   $0x1,-0x4(%rbp)
jmp    11ab <pkey_gost94_derive+0x6b>
mov    -0x30(%rbp),%rdi
call   1030 <gost_get0_priv_key@plt>
mov    %eax,%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <make_cp_exchange_key@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkey_gost94_derive>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
call   1050 <EVP_PKEY_CTX_get0_peerkey@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <EVP_PKEY_CTX_get0_pkey@plt>
movq   $0x20,(%r12)
test   %r15,%r15
je     118e <pkey_gost94_derive+0x4e>
mov    %rax,%rdi
call   1030 <gost_get0_priv_key@plt>
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <make_cp_exchange_key@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @pkey_gost94_derive(i32* %0, i8* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i32*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64* %2, i64** %7
%10 = load i32*, i32** %5
%11 = call i32* @EVP_PKEY_CTX_get0_peerkey(i32* %10)
store i32* %11, i32** %8
%12 = load i32*, i32** %5
%13 = call i32* @EVP_PKEY_CTX_get0_pkey(i32* %12)
store i32* %13, i32** %9
%14 = load i64*, i64** %7
store i64 32, i64* %14
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 1, i32* %4
br label %24
18: ; preds = %3
%19 = load i32*, i32** %9
%20 = call i32 @gost_get0_priv_key(i32* %19)
%21 = load i32*, i32** %8
%22 = load i8*, i8** %6
%23 = call i32 @make_cp_exchange_key(i32 %20, i32* %21, i8* %22)
store i32 %23, i32* %4
br label %24
24: ; preds = %18, %17
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
define i32 @pkey_gost94_derive(i32* %0, i8* %1, i64* nocapture writeonly %2) {
%4 = tail call i32* @EVP_PKEY_CTX_get0_peerkey(i32* %0)
%5 = tail call i32* @EVP_PKEY_CTX_get0_pkey(i32* %0)
store i64 32, i64* %2
%6 = icmp eq i8* %1, null
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = tail call i32 @gost_get0_priv_key(i32* %5)
%9 = tail call i32 @make_cp_exchange_key(i32 %8, i32* %4, i8* %1)
br label %10
10: ; preds = %3, %7
%11 = phi i32 [ %9, %7 ], [ 1, %3 ]
ret i32 %11
}