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

/* Variables and functions */
int /*<<< orphan*/ BN_bin2bn(unsigned char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_clear_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ OPENSSL_cleanse(unsigned char *, int);
int vg_b58_decode_check(char const *, unsigned char *, int);
int /*<<< orphan*/ vg_protect_crypt(int, unsigned char *, int, unsigned char *,
                                    char const *, int /*<<< orphan*/);
int vg_set_privkey(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int vg_protect_decode_privkey(EC_KEY *pkey, int *keytype, const char *encoded,
                              const char *pass) {
  unsigned char ecpriv[64];
  unsigned char ecenc[128];
  BIGNUM *bn;
  int restype;
  int res;
  res = vg_b58_decode_check(encoded, ecenc, sizeof(ecenc));
  if ((res < 2) || (res > sizeof(ecenc)))
    return 0;
  switch (ecenc[0]) {
  case 32:
    restype = 128;
    break;
  case 79:
    restype = 239;
    break;
  default:
    return 0;
  }
  if (!vg_protect_crypt(-1, ecenc + 1, res - 1, pkey ? ecpriv : NULL, pass, 0))
    return 0;
  res = 1;
  if (pkey) {
    bn = BN_new();
    BN_bin2bn(ecpriv, 32, bn);
    res = vg_set_privkey(bn, pkey);
    BN_clear_free(bn);
    OPENSSL_cleanse(ecpriv, sizeof(ecpriv));
  }
  *keytype = restype;
  return res;
}


x86 assembly (clang, -O0):
<vg_protect_decode_privkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0xf0(%rbp),%rsi
mov    $0x80,%edx
call   1060 <vg_b58_decode_check@plt>
mov    %eax,-0x100(%rbp)
cmpl   $0x2,-0x100(%rbp)
jl     11c6 <vg_protect_decode_privkey+0x56>
movslq -0x100(%rbp),%rax
cmp    $0x80,%rax
jbe    11d2 <vg_protect_decode_privkey+0x62>
movl   $0x0,-0x4(%rbp)
jmp    132c <vg_protect_decode_privkey+0x1bc>
movzbl -0xf0(%rbp),%eax
mov    %eax,-0x104(%rbp)
sub    $0x20,%eax
je     1201 <vg_protect_decode_privkey+0x91>
jmp    11ed <vg_protect_decode_privkey+0x7d>
mov    -0x104(%rbp),%eax
sub    $0x4f,%eax
je     1210 <vg_protect_decode_privkey+0xa0>
jmp    121f <vg_protect_decode_privkey+0xaf>
movl   $0x80,-0xfc(%rbp)
jmp    122b <vg_protect_decode_privkey+0xbb>
movl   $0xef,-0xfc(%rbp)
jmp    122b <vg_protect_decode_privkey+0xbb>
movl   $0x0,-0x4(%rbp)
jmp    132c <vg_protect_decode_privkey+0x1bc>
lea    -0xf0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x110(%rbp)
mov    -0x100(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x108(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1267 <vg_protect_decode_privkey+0xf7>
lea    -0x70(%rbp),%rax
mov    %rax,-0x118(%rbp)
jmp    1275 <vg_protect_decode_privkey+0x105>
xor    %eax,%eax
mov    %rax,-0x118(%rbp)
jmp    1275 <vg_protect_decode_privkey+0x105>
mov    -0x108(%rbp),%edx
mov    -0x110(%rbp),%rsi
mov    -0x118(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    $0xffffffff,%edi
xor    %r9d,%r9d
call   1070 <vg_protect_crypt@plt>
cmp    $0x0,%eax
jne    12af <vg_protect_decode_privkey+0x13f>
movl   $0x0,-0x4(%rbp)
jmp    132c <vg_protect_decode_privkey+0x1bc>
movl   $0x1,-0x100(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1317 <vg_protect_decode_privkey+0x1a7>
mov    $0x0,%al
call   1030 <BN_new@plt>
mov    %rax,-0xf8(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0xf8(%rbp),%rdx
mov    $0x20,%esi
call   1040 <BN_bin2bn@plt>
mov    -0xf8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <vg_set_privkey@plt>
mov    %eax,-0x100(%rbp)
mov    -0xf8(%rbp),%rdi
call   1090 <BN_clear_free@plt>
lea    -0x70(%rbp),%rdi
mov    $0x40,%esi
call   1050 <OPENSSL_cleanse@plt>
mov    -0xfc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x100(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vg_protect_decode_privkey>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc8,%rsp
mov    %rcx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x40(%rsp),%rsi
mov    $0x80,%r13d
mov    %rdx,%rdi
mov    $0x80,%edx
call   1060 <vg_b58_decode_check@plt>
lea    -0x81(%rax),%ecx
xor    %ebp,%ebp
cmp    $0xffffff81,%ecx
jb     123f <vg_protect_decode_privkey+0xcf>
mov    0x40(%rsp),%cl
cmp    $0x20,%cl
je     11c7 <vg_protect_decode_privkey+0x57>
cmp    $0x4f,%cl
jne    123f <vg_protect_decode_privkey+0xcf>
mov    $0xef,%r13d
lea    0x41(%rsp),%rsi
add    $0xffffffff,%eax
test   %rbx,%rbx
mov    %rsp,%rcx
cmove  %rbx,%rcx
xor    %ebp,%ebp
mov    $0xffffffff,%edi
mov    %eax,%edx
mov    %r15,%r8
xor    %r9d,%r9d
call   1070 <vg_protect_crypt@plt>
test   %eax,%eax
je     123f <vg_protect_decode_privkey+0xcf>
test   %rbx,%rbx
je     1237 <vg_protect_decode_privkey+0xc7>
xor    %eax,%eax
call   1030 <BN_new@plt>
mov    %rax,%r15
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rax,%rdx
call   1040 <BN_bin2bn@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <vg_set_privkey@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1090 <BN_clear_free@plt>
mov    %r12,%rdi
mov    $0x40,%esi
call   1050 <OPENSSL_cleanse@plt>
jmp    123c <vg_protect_decode_privkey+0xcc>
mov    $0x1,%ebp
mov    %r13d,(%r14)
mov    %ebp,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @vg_protect_decode_privkey(i32* %0, i32* nocapture writeonly %1, i8* %2, i8* %3) {
%5 = alloca [64 x i8]
%6 = alloca [128 x i8]
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%9 = call i32 @vg_b58_decode_check(i8* %2, i8* %8, i32 128)
%10 = add i32 %9, -129
%11 = icmp ult i32 %10, -127
br i1 %11, label %32, label %12
12: ; preds = %4
%13 = load i8, i8* %8
switch i8 %13, label %32 [
i8 32, label %15
i8 79, label %14
]
14: ; preds = %12
br label %15
15: ; preds = %12, %14
%16 = phi i32 [ 239, %14 ], [ 128, %12 ]
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 1
%18 = add nsw i32 %9, -1
%19 = icmp eq i32* %0, null
%20 = select i1 %19, i8* null, i8* %7
%21 = call i32 @vg_protect_crypt(i32 -1, i8* %17, i32 %18, i8* %20, i8* %3, i32 0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %15
br i1 %19, label %30, label %24
24: ; preds = %23
%25 = call i32* (...) @BN_new()
%26 = call i32 @BN_bin2bn(i8* %7, i32 32, i32* %25)
%27 = call i32 @vg_set_privkey(i32* %25, i32* %0)
%28 = call i32 @BN_clear_free(i32* %25)
%29 = call i32 @OPENSSL_cleanse(i8* %7, i32 64)
br label %30
30: ; preds = %24, %23
%31 = phi i32 [ %27, %24 ], [ 1, %23 ]
store i32 %16, i32* %1
br label %32
32: ; preds = %15, %12, %4, %30
%33 = phi i32 [ %31, %30 ], [ 0, %4 ], [ 0, %12 ], [ 0, %15 ]
ret i32 %33
}