#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*/ PKCS8_PRIV_KEY_INFO;
typedef int /*<<< orphan*/ EVP_PKEY;
typedef int /*<<< orphan*/ ASN1_STRING;
typedef int /*<<< orphan*/ ASN1_OBJECT;
typedef int /*<<< orphan*/ ASN1_INTEGER;

/* Variables and functions */
int /*<<< orphan*/ ASN1_INTEGER_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_to_ASN1_INTEGER(int /*<<< orphan*/,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_base_id(int /*<<< orphan*/ const *);
int /*<<< orphan*/ *OBJ_nid2obj(int /*<<< orphan*/);
int PKCS8_pkey_set0(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                    int /*<<< orphan*/, int /*<<< orphan*/,
                    int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ V_ASN1_SEQUENCE;
int /*<<< orphan*/ *encode_gost_algor_params(int /*<<< orphan*/ const *);
int /*<<< orphan*/ gost_get0_priv_key(int /*<<< orphan*/ const *);
int i2d_ASN1_INTEGER(int /*<<< orphan*/ *, unsigned char **);
int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk) {
  ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
  ASN1_STRING *params = encode_gost_algor_params(pk);
  unsigned char *priv_buf = NULL;
  int priv_len;
  ASN1_INTEGER *asn1key = NULL;
  if (!params) {
    return 0;
  }
  asn1key = BN_to_ASN1_INTEGER(gost_get0_priv_key(pk), NULL);
  priv_len = i2d_ASN1_INTEGER(asn1key, &priv_buf);
  ASN1_INTEGER_free(asn1key);
  return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params, priv_buf,
                         priv_len);
}


x86 assembly (clang, -O0):
<priv_encode_gost>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <EVP_PKEY_base_id@plt>
mov    %eax,%edi
call   1060 <OBJ_nid2obj@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <encode_gost_algor_params@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d8 <priv_encode_gost+0x58>
movl   $0x0,-0x4(%rbp)
jmp    1232 <priv_encode_gost+0xb2>
mov    -0x18(%rbp),%rdi
call   1050 <gost_get0_priv_key@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <BN_to_ASN1_INTEGER@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <i2d_ASN1_INTEGER@plt>
mov    %eax,-0x34(%rbp)
mov    -0x40(%rbp),%rdi
call   10a0 <ASN1_INTEGER_free@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2dc0(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
mov    -0x34(%rbp),%eax
xor    %edx,%edx
mov    %eax,(%rsp)
call   1040 <PKCS8_pkey_set0@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<priv_encode_gost>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <EVP_PKEY_base_id@plt>
mov    %eax,%edi
call   1060 <OBJ_nid2obj@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1080 <encode_gost_algor_params@plt>
movq   $0x0,0x8(%rsp)
test   %rax,%rax
je     120b <priv_encode_gost+0x8b>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <gost_get0_priv_key@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1070 <BN_to_ASN1_INTEGER@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1090 <i2d_ASN1_INTEGER@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   10a0 <ASN1_INTEGER_free@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%ecx
mov    0x8(%rsp),%r9
mov    %ebp,(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
xor    %edx,%edx
mov    %r12,%r8
call   1040 <PKCS8_pkey_set0@plt>
jmp    120d <priv_encode_gost+0x8d>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@V_ASN1_SEQUENCE = global i32 0
define i32 @priv_encode_gost(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
store i32* %0, i32** %4
store i32* %1, i32** %5
%11 = load i32*, i32** %5
%12 = call i32 @EVP_PKEY_base_id(i32* %11)
%13 = call i32* @OBJ_nid2obj(i32 %12)
store i32* %13, i32** %6
%14 = load i32*, i32** %5
%15 = call i32* @encode_gost_algor_params(i32* %14)
store i32* %15, i32** %7
store i8* null, i8** %8
store i32* null, i32** %10
%16 = load i32*, i32** %7
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %2
store i32 0, i32* %3
br label %34
19: ; preds = %2
%20 = load i32*, i32** %5
%21 = call i32 @gost_get0_priv_key(i32* %20)
%22 = call i32* @BN_to_ASN1_INTEGER(i32 %21, i32* null)
store i32* %22, i32** %10
%23 = load i32*, i32** %10
%24 = call i32 @i2d_ASN1_INTEGER(i32* %23, i8** %8)
store i32 %24, i32* %9
%25 = load i32*, i32** %10
%26 = call i32 @ASN1_INTEGER_free(i32* %25)
%27 = load i32*, i32** %4
%28 = load i32*, i32** %6
%29 = load i32, i32* @V_ASN1_SEQUENCE
%30 = load i32*, i32** %7
%31 = load i8*, i8** %8
%32 = load i32, i32* %9
%33 = call i32 @PKCS8_pkey_set0(i32* %27, i32* %28, i32 0, i32 %29, i32* %30, i8* %31, i32 %32)
store i32 %33, i32* %3
br label %34
34: ; preds = %19, %18
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@V_ASN1_SEQUENCE = global i32 0
define i32 @priv_encode_gost(i32* %0, i32* %1) {
%3 = alloca i8*
%4 = tail call i32 @EVP_PKEY_base_id(i32* %1)
%5 = tail call i32* @OBJ_nid2obj(i32 %4)
%6 = tail call i32* @encode_gost_algor_params(i32* %1)
%7 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%8 = icmp eq i32* %6, null
br i1 %8, label %17, label %9
9: ; preds = %2
%10 = tail call i32 @gost_get0_priv_key(i32* %1)
%11 = tail call i32* @BN_to_ASN1_INTEGER(i32 %10, i32* null)
%12 = call i32 @i2d_ASN1_INTEGER(i32* %11, i8** %3)
%13 = call i32 @ASN1_INTEGER_free(i32* %11)
%14 = load i32, i32* @V_ASN1_SEQUENCE
%15 = load i8*, i8** %3
%16 = call i32 @PKCS8_pkey_set0(i32* %0, i32* %5, i32 0, i32 %14, i32* %6, i8* %15, i32 %12)
br label %17
17: ; preds = %2, %9
%18 = phi i32 [ %16, %9 ], [ 0, %2 ]
ret i32 %18
}