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

/* Variables and functions */
int /*<<< orphan*/ BN_bn2bin(int /*<<< orphan*/ const *, unsigned char *);
int BN_num_bytes(int /*<<< orphan*/ const *);
int /*<<< orphan*/ *EC_KEY_get0_private_key(int /*<<< orphan*/ const *);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ vg_b58_encode_check(unsigned char *, int, char *);
void vg_encode_privkey(const EC_KEY *pkey, int addrtype, char *result) {
  unsigned char eckey_buf[128];
  const BIGNUM *bn;
  int nbytes;
  bn = EC_KEY_get0_private_key(pkey);
  eckey_buf[0] = addrtype;
  nbytes = BN_num_bytes(bn);
  assert(nbytes <= 32);
  if (nbytes < 32)
    memset(eckey_buf + 1, 0, 32 - nbytes);
  BN_bn2bin(bn, &eckey_buf[33 - nbytes]);
  vg_b58_encode_check(eckey_buf, 33, result);
}


x86 assembly (clang, -O0):
<vg_encode_privkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <EC_KEY_get0_private_key@plt>
mov    %rax,-0xa8(%rbp)
mov    -0xc(%rbp),%eax
mov    %al,-0xa0(%rbp)
mov    -0xa8(%rbp),%rdi
call   1060 <BN_num_bytes@plt>
mov    %eax,-0xac(%rbp)
cmpl   $0x20,-0xac(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x20,-0xac(%rbp)
jge    11df <vg_encode_privkey+0x7f>
lea    -0xa0(%rbp),%rdi
add    $0x1,%rdi
mov    $0x20,%edx
sub    -0xac(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0xa8(%rbp),%rdi
mov    $0x21,%eax
sub    -0xac(%rbp),%eax
cltq
lea    -0xa0(%rbp),%rsi
add    %rax,%rsi
call   1070 <BN_bn2bin@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x21,%esi
call   1080 <vg_b58_encode_check@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vg_encode_privkey>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
call   1030 <EC_KEY_get0_private_key@plt>
mov    %rax,%rbx
mov    %bpl,(%rsp)
mov    %rax,%rdi
call   1060 <BN_num_bytes@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    $0x21,%eax
setl   %dil
call   1050 <assert@plt>
cmp    $0x1f,%ebp
jg     11ac <vg_encode_privkey+0x4c>
lea    0x1(%rsp),%rdi
mov    $0x20,%edx
sub    %ebp,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    $0x21,%eax
sub    %ebp,%eax
cltq
lea    (%rsp,%rax,1),%rsi
mov    %rbx,%rdi
call   1070 <BN_bn2bin@plt>
mov    %rsp,%rdi
mov    $0x21,%esi
mov    %r14,%rdx
call   1080 <vg_b58_encode_check@plt>
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @vg_encode_privkey(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [128 x i8]
%8 = alloca i32*
%9 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i32*, i32** %4
%11 = call i32* @EC_KEY_get0_private_key(i32* %10)
store i32* %11, i32** %8
%12 = load i32, i32* %5
%13 = trunc i32 %12 to i8
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
store i8 %13, i8* %14
%15 = load i32*, i32** %8
%16 = call i32 @BN_num_bytes(i32* %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp sle i32 %17, 32
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 32
br i1 %22, label %23, label %29
23: ; preds = %3
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = load i32, i32* %9
%27 = sub nsw i32 32, %26
%28 = call i32 @memset(i8* %25, i32 0, i32 %27)
br label %29
29: ; preds = %23, %3
%30 = load i32*, i32** %8
%31 = load i32, i32* %9
%32 = sub nsw i32 33, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %33
%35 = call i32 @BN_bn2bin(i32* %30, i8* %34)
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%37 = load i8*, i8** %6
%38 = call i32 @vg_b58_encode_check(i8* %36, i32 33, i8* %37)
ret void
}

LLVM IR (-O2):
define void @vg_encode_privkey(i32* %0, i32 %1, i8* %2) {
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @EC_KEY_get0_private_key(i32* %0)
%7 = trunc i32 %1 to i8
store i8 %7, i8* %5
%8 = tail call i32 @BN_num_bytes(i32* %6)
%9 = icmp slt i32 %8, 33
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = icmp slt i32 %8, 32
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 1
%15 = sub nsw i32 32, %8
%16 = call i32 @memset(i8* %14, i32 0, i32 %15)
br label %17
17: ; preds = %13, %3
%18 = sub nsw i32 33, %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %19
%21 = call i32 @BN_bn2bin(i32* %6, i8* %20)
%22 = call i32 @vg_b58_encode_check(i8* %5, i32 33, i8* %2)
ret void
}