// File: /workspace/github_cpp_makefile_star2_O2/tenup-coin[P]seeder/util.cpp
// Function: DecodeBase32(char const*, bool*)
vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
{
    static const int decode32_table[256] =
    {
        -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)
        *pfInvalid = false;

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

    int mode = 0;
    int left = 0;

    while (1)
    {
         int dec = decode32_table[(unsigned char)*p];
         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)
        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 || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table[(unsigned char)p[6]] != -1)
                    *pfInvalid = true;
                break;

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

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

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

    return vchRet;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdi, %rbx
testq %rdx, %rdx
je 0x111a9
movb $0x0, (%rdx)
movq %rdx, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movq %r15, %rdi
callq 0x31b0
leaq (%rax,%rax,4), %rsi
shrq $0x3, %rsi
movq %rbx, %rdi
callq 0x1145c
xorl %eax, %eax
pushq $0x1
popq %rbp
leaq 0x20d7(%rip), %r14      # 0x132b4
xorl %r13d, %r13d
movl %r13d, %r12d
movzbl (%r15), %ecx
leaq 0x2132(%rip), %rdx      # 0x13320
movl (%rdx,%rcx,4), %r13d
movl %eax, %eax
cmpl $-0x1, %r13d
je 0x112ef
incq %r15
movslq (%r14,%rax,4), %rcx
addq %r14, %rcx
movl %ebp, %eax
jmpq *%rcx
shll $0x3, %r12d
movl %r13d, %eax
shrl $0x2, %eax
orl %r12d, %eax
movb %al, 0xf(%rsp)
movq %rbx, %rdi
leaq 0xf(%rsp), %rsi
callq 0x9706
andl $0x3, %r13d
pushq $0x2
popq %rax
jmp 0x111e0
shll $0x5, %r12d
orl %r12d, %r13d
pushq $0x6
popq %rax
jmp 0x111e0
addl %r12d, %r12d
movl %r13d, %eax
shrl $0x4, %eax
orl %r12d, %eax
movb %al, 0xe(%rsp)
movq %rbx, %rdi
leaq 0xe(%rsp), %rsi
callq 0x9706
andl $0xf, %r13d
pushq $0x4
popq %rax
jmp 0x111e0
shll $0x4, %r12d
movl %r13d, %eax
shrl %eax
orl %r12d, %eax
movb %al, 0xd(%rsp)
movq %rbx, %rdi
leaq 0xd(%rsp), %rsi
callq 0x9706
andl $0x1, %r13d
pushq $0x5
popq %rax
jmp 0x111e0
shll $0x5, %r12d
orl %r12d, %r13d
pushq $0x3
popq %rax
jmp 0x111e0
shll $0x2, %r12d
movl %r13d, %eax
shrl $0x3, %eax
orl %r12d, %eax
movb %al, 0xc(%rsp)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
callq 0x9706
andl $0x7, %r13d
pushq $0x7
popq %rax
jmp 0x111e0
movl %r12d, %eax
shll $0x5, %eax
orl %eax, %r13d
movb %r13b, 0xb(%rsp)
movq %rbx, %rdi
leaq 0xb(%rsp), %rsi
callq 0x9706
xorl %eax, %eax
movl %r12d, %r13d
jmp 0x111e0
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x1130c
leaq 0x1fd4(%rip), %rdx      # 0x132d4
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
movb $0x1, (%rsi)
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r12d, %r12d
setne %al
cmpb $0x3d, %cl
setne %cl
orb %al, %cl
jne 0x11309
cmpb $0x3d, 0x1(%r15)
jne 0x11309
cmpb $0x3d, 0x2(%r15)
jne 0x11309
cmpb $0x3d, 0x3(%r15)
jne 0x11309
cmpb $0x3d, 0x4(%r15)
jne 0x11309
cmpb $0x3d, 0x5(%r15)
jne 0x11309
movzbl 0x6(%r15), %eax
jmp 0x1136d
testl %r12d, %r12d
setne %al
cmpb $0x3d, %cl
setne %cl
orb %al, %cl
jne 0x11309
movzbl 0x1(%r15), %eax
leaq 0x1fac(%rip), %rcx      # 0x13320
cmpl $-0x1, (%rcx,%rax,4)
jne 0x11309
jmp 0x1130c
testl %r12d, %r12d
setne %al
cmpb $0x3d, %cl
setne %cl
orb %al, %cl
jne 0x11309
cmpb $0x3d, 0x1(%r15)
jne 0x11309
cmpb $0x3d, 0x2(%r15)
jne 0x11309
cmpb $0x3d, 0x3(%r15)
jne 0x11309
movzbl 0x4(%r15), %eax
jmp 0x1136d
testl %r12d, %r12d
setne %al
cmpb $0x3d, %cl
setne %cl
orb %al, %cl
jne 0x11309
cmpb $0x3d, 0x1(%r15)
jne 0x11309
cmpb $0x3d, 0x2(%r15)
jne 0x11309
movzbl 0x3(%r15), %eax
jmp 0x1136d
jmp 0x113f3
jmp 0x113f3
jmp 0x113f3
jmp 0x113f3
jmp 0x113f3
movq %rax, %r14
movq %rbx, %rdi
callq 0x96f8
movq %r14, %rdi
callq 0x3690