#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*/ ecpriv;
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*/ OPENSSL_cleanse(unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int strlen(char *);
int /*<<< orphan*/ vg_b58_encode_check(unsigned char *, int, char *);
int vg_protect_crypt(int, unsigned char *, int, unsigned char *, char const *,
                     int);
int vg_protect_encode_privkey(char *out, const EC_KEY *pkey, int keytype,
                              int parameter_group, const char *pass) {
  unsigned char ecpriv[64];
  unsigned char ecenc[128];
  const BIGNUM *privkey;
  int nbytes;
  int restype;
  restype = (keytype & 1) ? 79 : 32;
  privkey = EC_KEY_get0_private_key(pkey);
  nbytes = BN_num_bytes(privkey);
  if (nbytes < 32)
    memset(ecpriv, 0, 32 - nbytes);
  BN_bn2bin(privkey, ecpriv + 32 - nbytes);
  nbytes = vg_protect_crypt(parameter_group, ecpriv, 32, &ecenc[1], pass, 1);
  if (nbytes <= 0)
    return 0;
  OPENSSL_cleanse(ecpriv, sizeof(ecpriv));
  ecenc[0] = restype;
  vg_b58_encode_check(ecenc, nbytes + 1, out);
  nbytes = strlen(out);
  return nbytes;
}


x86 assembly (clang, -O0):
<vg_protect_encode_privkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x1c(%rbp),%edx
and    $0x1,%edx
mov    $0x20,%eax
mov    $0x4f,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x100(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <EC_KEY_get0_private_key@plt>
mov    %rax,-0xf8(%rbp)
mov    -0xf8(%rbp),%rdi
call   1080 <BN_num_bytes@plt>
mov    %eax,-0xfc(%rbp)
cmpl   $0x20,-0xfc(%rbp)
jge    11fe <vg_protect_encode_privkey+0x7e>
lea    -0x70(%rbp),%rdi
mov    $0x20,%edx
sub    -0xfc(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0xf8(%rbp),%rdi
lea    -0x70(%rbp),%rsi
add    $0x20,%rsi
movslq -0xfc(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rsi
call   1090 <BN_bn2bin@plt>
mov    -0x20(%rbp),%edi
lea    -0x70(%rbp),%rsi
lea    -0xf0(%rbp),%rcx
add    $0x1,%rcx
mov    -0x28(%rbp),%r8
mov    $0x20,%edx
mov    $0x1,%r9d
call   1070 <vg_protect_crypt@plt>
mov    %eax,-0xfc(%rbp)
cmpl   $0x0,-0xfc(%rbp)
jg     1266 <vg_protect_encode_privkey+0xe6>
movl   $0x0,-0x4(%rbp)
jmp    12b1 <vg_protect_encode_privkey+0x131>
lea    -0x70(%rbp),%rdi
mov    $0x40,%esi
call   1060 <OPENSSL_cleanse@plt>
mov    -0x100(%rbp),%eax
mov    %al,-0xf0(%rbp)
lea    -0xf0(%rbp),%rdi
mov    -0xfc(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rdx
call   10a0 <vg_b58_encode_check@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xfc(%rbp)
mov    -0xfc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vg_protect_encode_privkey>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc8,%rsp
mov    %r8,%r12
mov    %ecx,%r13d
mov    %edx,%r14d
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <EC_KEY_get0_private_key@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1080 <BN_num_bytes@plt>
mov    %eax,%ebx
cmp    $0x1f,%eax
jg     11c8 <vg_protect_encode_privkey+0x48>
mov    $0x20,%edx
sub    %ebx,%edx
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
movslq %ebx,%rax
neg    %rax
lea    (%rsp,%rax,1),%rsi
add    $0x20,%rsi
mov    %rbp,%rdi
call   1090 <BN_bn2bin@plt>
lea    0x41(%rsp),%rcx
mov    %rsp,%rsi
mov    $0x20,%ebp
mov    %r13d,%edi
mov    $0x20,%edx
mov    %r12,%r8
mov    $0x1,%r9d
call   1070 <vg_protect_crypt@plt>
test   %eax,%eax
jle    1240 <vg_protect_encode_privkey+0xc0>
mov    %eax,%ebx
mov    %rsp,%rdi
mov    $0x40,%esi
call   1060 <OPENSSL_cleanse@plt>
test   $0x1,%r14b
mov    $0x4f,%eax
cmove  %ebp,%eax
mov    %al,0x40(%rsp)
add    $0x1,%ebx
lea    0x40(%rsp),%rdi
mov    %ebx,%esi
mov    %r15,%rdx
call   10a0 <vg_b58_encode_check@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
jmp    1242 <vg_protect_encode_privkey+0xc2>
xor    %eax,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @vg_protect_encode_privkey(i8* %0, i32* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca [64 x i8]
%13 = alloca [128 x i8]
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %7
store i32* %1, i32** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%17 = load i32, i32* %9
%18 = and i32 %17, 1
%19 = icmp ne i32 %18, 0
%20 = zext i1 %19 to i64
%21 = select i1 %19, i32 79, i32 32
store i32 %21, i32* %16
%22 = load i32*, i32** %8
%23 = call i32* @EC_KEY_get0_private_key(i32* %22)
store i32* %23, i32** %14
%24 = load i32*, i32** %14
%25 = call i32 @BN_num_bytes(i32* %24)
store i32 %25, i32* %15
%26 = load i32, i32* %15
%27 = icmp slt i32 %26, 32
br i1 %27, label %28, label %33
28: ; preds = %5
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%30 = load i32, i32* %15
%31 = sub nsw i32 32, %30
%32 = call i32 @memset(i8* %29, i32 0, i32 %31)
br label %33
33: ; preds = %28, %5
%34 = load i32*, i32** %14
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%36 = getelementptr inbounds i8, i8* %35, i64 32
%37 = load i32, i32* %15
%38 = sext i32 %37 to i64
%39 = sub i64 0, %38
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = call i32 @BN_bn2bin(i32* %34, i8* %40)
%42 = load i32, i32* %10
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 1
%45 = load i8*, i8** %11
%46 = call i32 @vg_protect_crypt(i32 %42, i8* %43, i32 32, i8* %44, i8* %45, i32 1)
store i32 %46, i32* %15
%47 = load i32, i32* %15
%48 = icmp sle i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %33
store i32 0, i32* %6
br label %64
50: ; preds = %33
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%52 = call i32 @OPENSSL_cleanse(i8* %51, i32 64)
%53 = load i32, i32* %16
%54 = trunc i32 %53 to i8
%55 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
store i8 %54, i8* %55
%56 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%57 = load i32, i32* %15
%58 = add nsw i32 %57, 1
%59 = load i8*, i8** %7
%60 = call i32 @vg_b58_encode_check(i8* %56, i32 %58, i8* %59)
%61 = load i8*, i8** %7
%62 = call i32 @strlen(i8* %61)
store i32 %62, i32* %15
%63 = load i32, i32* %15
store i32 %63, i32* %6
br label %64
64: ; preds = %50, %49
%65 = load i32, i32* %6
ret i32 %65
}

LLVM IR (-O2):
define i32 @vg_protect_encode_privkey(i8* %0, i32* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca [64 x i8]
%7 = alloca [128 x i8]
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%10 = and i32 %2, 1
%11 = icmp eq i32 %10, 0
%12 = tail call i32* @EC_KEY_get0_private_key(i32* %1)
%13 = tail call i32 @BN_num_bytes(i32* %12)
%14 = icmp slt i32 %13, 32
br i1 %14, label %15, label %18
15: ; preds = %5
%16 = sub nsw i32 32, %13
%17 = call i32 @memset(i8* %8, i32 0, i32 %16)
br label %18
18: ; preds = %15, %5
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 32
%20 = sext i32 %13 to i64
%21 = sub nsw i64 0, %20
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = call i32 @BN_bn2bin(i32* %12, i8* %22)
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 1
%25 = call i32 @vg_protect_crypt(i32 %3, i8* %8, i32 32, i8* %24, i8* %4, i32 1)
%26 = icmp slt i32 %25, 1
br i1 %26, label %33, label %27
27: ; preds = %18
%28 = call i32 @OPENSSL_cleanse(i8* %8, i32 64)
%29 = select i1 %11, i8 32, i8 79
store i8 %29, i8* %9
%30 = add nuw nsw i32 %25, 1
%31 = call i32 @vg_b58_encode_check(i8* %9, i32 %30, i8* %0)
%32 = call i32 @strlen(i8* %0)
br label %33
33: ; preds = %18, %27
%34 = phi i32 [ %32, %27 ], [ 0, %18 ]
ret i32 %34
}