// File: theuni[P]libbtcnet/src/base32.cpp
// Function: DecodeBase32(char const*, bool*)
std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
{
    static constexpr std::array<int, 256> decode32_table{
        {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}};

    if (pfInvalid != nullptr)
        *pfInvalid = false;

    std::vector<unsigned char> vchRet;
    vchRet.reserve((strlen(p)) * 5 / 8);

    int mode = 0;
    int left = 0;

    while (true) {
        int dec = decode32_table.at(static_cast<unsigned char>(p[0]));
        if (dec == -1)
            break;
        p++;
        switch (mode) {
        case 0: // we have no bits and get 5
            left = dec;
            mode = 1;
            break;

        case 1: // we have 5 bits and keep 2
            vchRet.push_back((left << 3) | (dec >> 2));
            left = dec & 3;
            mode = 2;
            break;

        case 2: // we have 2 bits and keep 7
            left = left << 5 | dec;
            mode = 3;
            break;

        case 3: // we have 7 bits and keep 4
            vchRet.push_back((left << 1) | (dec >> 4));
            left = dec & 15;
            mode = 4;
            break;

        case 4: // we have 4 bits, and keep 1
            vchRet.push_back((left << 4) | (dec >> 1));
            left = dec & 1;
            mode = 5;
            break;

        case 5: // we have 1 bit, and keep 6
            left = left << 5 | dec;
            mode = 6;
            break;

        case 6: // we have 6 bits, and keep 3
            vchRet.push_back((left << 2) | (dec >> 3));
            left = dec & 7;
            mode = 7;
            break;

        case 7: // we have 3 bits, and keep 0
            vchRet.push_back((left << 5) | dec);
            mode = 0;
            break;
        }
    }

    if (pfInvalid != nullptr)
        switch (mode) {
        case 0: // 8n base32 characters processed: ok
            break;

        case 1: // 8n+1 base32 characters processed: impossible
        case 3: //   +3
        case 6: //   +6
            *pfInvalid = true;
            break;

        case 2: // 8n+2 base32 characters processed: require '======'
            if (left != 0 || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table.at(static_cast<unsigned char>(p[6])) != -1)
                *pfInvalid = true;
            break;

        case 4: // 8n+4 base32 characters processed: require '===='
            if (left != 0 || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || decode32_table.at(static_cast<unsigned char>(p[4])) != -1)
                *pfInvalid = true;
            break;

        case 5: // 8n+5 base32 characters processed: require '==='
            if (left != 0 || p[0] != '=' || p[1] != '=' || p[2] != '=' || decode32_table.at(static_cast<unsigned char>(p[3])) != -1)
                *pfInvalid = true;
            break;

        case 7: // 8n+7 base32 characters processed: require '='
            if (left != 0 || p[0] != '=' || decode32_table.at(static_cast<unsigned char>(p[1])) != -1)
                *pfInvalid = true;
            break;
        }

    return vchRet;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x50(%rbp)
movq %rdi, %rax
movq %rax, -0x48(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xc840
movq -0x18(%rbp), %rax
movb $0x0, (%rax)
movq -0x50(%rbp), %rdi
movb $0x0, -0x19(%rbp)
callq 0x71a0
movq -0x10(%rbp), %rdi
callq 0x51c0
movq -0x50(%rbp), %rdi
leaq (%rax,%rax,4), %rsi
shrq $0x3, %rsi
callq 0xcd70
jmp 0xc869
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
jmp 0xc879
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
movl %eax, %esi
leaq 0x18a6f(%rip), %rdi     # 0x252f8
callq 0xce50
movq %rax, -0x58(%rbp)
jmp 0xc894
movq -0x58(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x38(%rbp)
cmpl $-0x1, -0x38(%rbp)
jne 0xc8c2
jmp 0xca26
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x5c60
jmp 0xcc17
movq -0x10(%rbp), %rax
incq %rax
movq %rax, -0x10(%rbp)
movl -0x30(%rbp), %eax
movq %rax, -0x60(%rbp)
subq $0x7, %rax
ja 0xca21
movq -0x60(%rbp), %rax
leaq 0x189bb(%rip), %rcx     # 0x252a4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x38(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x1, -0x30(%rbp)
jmp 0xca21
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %eax
shll $0x3, %eax
movl -0x38(%rbp), %ecx
shrl $0x2, %ecx
orl %ecx, %eax
movb %al, -0x39(%rbp)
leaq -0x39(%rbp), %rsi
callq 0xceb0
jmp 0xc924
movl -0x38(%rbp), %eax
andl $0x3, %eax
movl %eax, -0x34(%rbp)
movl $0x2, -0x30(%rbp)
jmp 0xca21
movl -0x34(%rbp), %eax
shll $0x5, %eax
orl -0x38(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x3, -0x30(%rbp)
jmp 0xca21
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %eax
addl %eax, %eax
movl -0x38(%rbp), %ecx
shrl $0x4, %ecx
orl %ecx, %eax
movb %al, -0x3a(%rbp)
leaq -0x3a(%rbp), %rsi
callq 0xceb0
jmp 0xc970
movl -0x38(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x34(%rbp)
movl $0x4, -0x30(%rbp)
jmp 0xca21
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %eax
shll $0x4, %eax
movl -0x38(%rbp), %ecx
shrl %ecx
orl %ecx, %eax
movb %al, -0x3b(%rbp)
leaq -0x3b(%rbp), %rsi
callq 0xceb0
jmp 0xc9a4
movl -0x38(%rbp), %eax
andl $0x1, %eax
movl %eax, -0x34(%rbp)
movl $0x5, -0x30(%rbp)
jmp 0xca21
movl -0x34(%rbp), %eax
shll $0x5, %eax
orl -0x38(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x6, -0x30(%rbp)
jmp 0xca21
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %eax
shll $0x2, %eax
movl -0x38(%rbp), %ecx
shrl $0x3, %ecx
orl %ecx, %eax
movb %al, -0x3c(%rbp)
leaq -0x3c(%rbp), %rsi
callq 0xceb0
jmp 0xc9eb
movl -0x38(%rbp), %eax
andl $0x7, %eax
movl %eax, -0x34(%rbp)
movl $0x7, -0x30(%rbp)
jmp 0xca21
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %eax
shll $0x5, %eax
movl -0x38(%rbp), %ecx
orl %ecx, %eax
movb %al, -0x3d(%rbp)
leaq -0x3d(%rbp), %rsi
callq 0xceb0
jmp 0xca1a
movl $0x0, -0x30(%rbp)
jmp 0xc877
cmpq $0x0, -0x18(%rbp)
je 0xcbf7
movl -0x30(%rbp), %eax
movq %rax, -0x68(%rbp)
subq $0x7, %rax
ja 0xcbf5
movq -0x68(%rbp), %rax
leaq 0x18877(%rip), %rcx     # 0x252c4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0xcbf5
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
jmp 0xcbf5
cmpl $0x0, -0x34(%rbp)
jne 0xcadf
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3d, %eax
jne 0xcadf
movq -0x10(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x3d, %eax
jne 0xcadf
movq -0x10(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x3d, %eax
jne 0xcadf
movq -0x10(%rbp), %rax
movsbl 0x3(%rax), %eax
cmpl $0x3d, %eax
jne 0xcadf
movq -0x10(%rbp), %rax
movsbl 0x4(%rax), %eax
cmpl $0x3d, %eax
jne 0xcadf
movq -0x10(%rbp), %rax
movsbl 0x5(%rax), %eax
cmpl $0x3d, %eax
jne 0xcadf
movq -0x10(%rbp), %rax
movzbl 0x6(%rax), %eax
movl %eax, %esi
leaq 0x1882d(%rip), %rdi     # 0x252f8
callq 0xce50
movq %rax, -0x70(%rbp)
jmp 0xcad6
movq -0x70(%rbp), %rax
cmpl $-0x1, (%rax)
je 0xcae6
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
jmp 0xcbf5
cmpl $0x0, -0x34(%rbp)
jne 0xcb49
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3d, %eax
jne 0xcb49
movq -0x10(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x3d, %eax
jne 0xcb49
movq -0x10(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x3d, %eax
jne 0xcb49
movq -0x10(%rbp), %rax
movsbl 0x3(%rax), %eax
cmpl $0x3d, %eax
jne 0xcb49
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
movl %eax, %esi
leaq 0x187c3(%rip), %rdi     # 0x252f8
callq 0xce50
movq %rax, -0x78(%rbp)
jmp 0xcb40
movq -0x78(%rbp), %rax
cmpl $-0x1, (%rax)
je 0xcb50
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
jmp 0xcbf5
cmpl $0x0, -0x34(%rbp)
jne 0xcba6
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3d, %eax
jne 0xcba6
movq -0x10(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x3d, %eax
jne 0xcba6
movq -0x10(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x3d, %eax
jne 0xcba6
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
movl %eax, %esi
leaq 0x18766(%rip), %rdi     # 0x252f8
callq 0xce50
movq %rax, -0x80(%rbp)
jmp 0xcb9d
movq -0x80(%rbp), %rax
cmpl $-0x1, (%rax)
je 0xcbad
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
jmp 0xcbf5
cmpl $0x0, -0x34(%rbp)
jne 0xcbec
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3d, %eax
jne 0xcbec
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
movl %eax, %esi
leaq 0x18726(%rip), %rdi     # 0x252f8
callq 0xce50
movq %rax, -0x88(%rbp)
jmp 0xcbe0
movq -0x88(%rbp), %rax
cmpl $-0x1, (%rax)
je 0xcbf3
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
jmp 0xcbf5
jmp 0xcbf7
movb $0x1, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
jne 0xcc0a
movq -0x50(%rbp), %rdi
callq 0x5c60
movq -0x48(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x5710