// File: theuni[P]libbtcnet/src/base32.cpp
// Function: EncodeBase32[abi:cxx11](unsigned char const*, unsigned long)
std::string EncodeBase32(const unsigned char* pch, size_t len)
{
    static const char* pbase32 = "abcdefghijklmnopqrstuvwxyz234567";

    std::string strRet = "";
    strRet.reserve((len + 4) / 5 * 8);

    int mode = 0, left = 0;
    const unsigned char* pchEnd = pch + len;

    while (pch < pchEnd) {
        int enc = *(pch++);
        switch (mode) {
        case 0: // we have no bits
            strRet += pbase32[enc >> 3];
            left = (enc & 7) << 2;
            mode = 1;
            break;

        case 1: // we have three bits
            strRet += pbase32[left | (enc >> 6)];
            strRet += pbase32[(enc >> 1) & 31];
            left = (enc & 1) << 4;
            mode = 2;
            break;

        case 2: // we have one bit
            strRet += pbase32[left | (enc >> 4)];
            left = (enc & 15) << 1;
            mode = 3;
            break;

        case 3: // we have four bits
            strRet += pbase32[left | (enc >> 7)];
            strRet += pbase32[(enc >> 2) & 31];
            left = (enc & 3) << 3;
            mode = 4;
            break;

        case 4: // we have two bits
            strRet += pbase32[left | (enc >> 5)];
            strRet += pbase32[enc & 31];
            mode = 0;
        }
    }

    static constexpr std::array<int, 5> nPadding{{0, 6, 4, 3, 1}};
    if (mode != 0) {
        strRet += pbase32[left];
        for (int n = 0; n < nPadding.at(mode); n++)
            strRet += '=';
    }

    return strRet;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x58(%rbp)
movq %rdi, %rax
movq %rax, -0x60(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
leaq -0x1a(%rbp), %rdi
movq %rdi, -0x50(%rbp)
callq 0x5720
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0x18b67(%rip), %rsi     # 0x25049
callq 0x5550
jmp 0xc4e9
leaq -0x1a(%rbp), %rdi
callq 0x5460
movq -0x58(%rbp), %rdi
movq -0x18(%rbp), %rax
addq $0x4, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rcx
movq %rdx, %rsi
addq %rsi, %rsi
andq $-0x8, %rsi
callq 0x5630
jmp 0xc51c
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0xc721
movq -0x10(%rbp), %rax
movq %rax, %rcx
incq %rcx
movq %rcx, -0x10(%rbp)
movzbl (%rax), %eax
movl %eax, -0x44(%rbp)
movl -0x30(%rbp), %eax
movq %rax, -0x68(%rbp)
subq $0x4, %rax
ja 0xc71c
movq -0x68(%rbp), %rax
leaq 0x18d1c(%rip), %rcx     # 0x25290
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x1a(%rbp), %rdi
callq 0x5460
jmp 0xc7ae
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x52a0
jmp 0xc7ae
movq -0x58(%rbp), %rdi
movq 0x2be74(%rip), %rax     # 0x38430
movl -0x44(%rbp), %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %esi
callq 0x52d0
jmp 0xc5d0
movl -0x44(%rbp), %eax
andl $0x7, %eax
shll $0x2, %eax
movl %eax, -0x34(%rbp)
movl $0x1, -0x30(%rbp)
jmp 0xc71c
movq -0x58(%rbp), %rdi
movq 0x2be3d(%rip), %rax     # 0x38430
movl -0x34(%rbp), %ecx
movl -0x44(%rbp), %edx
sarl $0x6, %edx
orl %edx, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %esi
callq 0x52d0
jmp 0xc60c
movq -0x58(%rbp), %rdi
movq 0x2be19(%rip), %rax     # 0x38430
movl -0x44(%rbp), %ecx
shrl %ecx
andl $0x1f, %ecx
movsbl (%rax,%rcx), %esi
callq 0x52d0
jmp 0xc62a
movl -0x44(%rbp), %eax
andl $0x1, %eax
shll $0x4, %eax
movl %eax, -0x34(%rbp)
movl $0x2, -0x30(%rbp)
jmp 0xc71c
movq -0x58(%rbp), %rdi
movq 0x2bde3(%rip), %rax     # 0x38430
movl -0x34(%rbp), %ecx
movl -0x44(%rbp), %edx
sarl $0x4, %edx
orl %edx, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %esi
callq 0x52d0
jmp 0xc666
movl -0x44(%rbp), %eax
andl $0xf, %eax
shll %eax
movl %eax, -0x34(%rbp)
movl $0x3, -0x30(%rbp)
jmp 0xc71c
movq -0x58(%rbp), %rdi
movq 0x2bda8(%rip), %rax     # 0x38430
movl -0x34(%rbp), %ecx
movl -0x44(%rbp), %edx
sarl $0x7, %edx
orl %edx, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %esi
callq 0x52d0
jmp 0xc6a1
movq -0x58(%rbp), %rdi
movq 0x2bd84(%rip), %rax     # 0x38430
movl -0x44(%rbp), %ecx
shrl $0x2, %ecx
andl $0x1f, %ecx
movsbl (%rax,%rcx), %esi
callq 0x52d0
jmp 0xc6c0
movl -0x44(%rbp), %eax
andl $0x3, %eax
shll $0x3, %eax
movl %eax, -0x34(%rbp)
movl $0x4, -0x30(%rbp)
jmp 0xc71c
movq -0x58(%rbp), %rdi
movq 0x2bd50(%rip), %rax     # 0x38430
movl -0x34(%rbp), %ecx
movl -0x44(%rbp), %edx
sarl $0x5, %edx
orl %edx, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %esi
callq 0x52d0
jmp 0xc6f9
movq -0x58(%rbp), %rdi
movq 0x2bd2c(%rip), %rax     # 0x38430
movl -0x44(%rbp), %ecx
andl $0x1f, %ecx
movsbl (%rax,%rcx), %esi
callq 0x52d0
jmp 0xc715
movl $0x0, -0x30(%rbp)
jmp 0xc536
cmpl $0x0, -0x30(%rbp)
je 0xc78e
movq -0x58(%rbp), %rdi
movq 0x2bcfe(%rip), %rax     # 0x38430
movslq -0x34(%rbp), %rcx
movsbl (%rax,%rcx), %esi
callq 0x52d0
jmp 0xc741
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x74(%rbp)
movslq -0x30(%rbp), %rsi
leaq 0x18b8b(%rip), %rdi     # 0x252e4
callq 0xcd10
movq %rax, -0x70(%rbp)
jmp 0xc764
movl -0x74(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl (%rcx), %eax
jge 0xc78c
movq -0x58(%rbp), %rdi
movl $0x3d, %esi
callq 0x52d0
jmp 0xc77f
jmp 0xc781
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xc748
jmp 0xc78e
movb $0x1, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
jne 0xc7a1
movq -0x58(%rbp), %rdi
callq 0x52a0
movq -0x60(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x5710
nopw (%rax,%rax)