// File: /workspace/github_cpp_makefile_star2_O1/jeffchan[P]blockchain-parser/Base58.cpp
// Function: CBDecodeBase58(BigNumber*, char const*)
bool CBDecodeBase58(BigNumber * bi,const char * str)
{
	// ??? Quite likely these functions can be improved
	BigNumber bi2(NULL,1);

	uint32_t slen = (uint32_t)strlen(str);

	for (uint32_t x = slen - 1;; x--)
	{ // Working backwards
		// Get index in alphabet array
		uint8_t alphaIndex = str[x];
		if (alphaIndex != 49)
		{ // If not 1
			if (str[x] < 58)
			{ // Numbers
				alphaIndex -= 49;
			}
			else if (str[x] < 73)
			{ // A-H
				alphaIndex -= 56;
			}
			else if (str[x] < 79)
			{ // J-N
				alphaIndex -= 57;
			}
			else if (str[x] < 91)
			{ // P-Z
				alphaIndex -= 58;
			}
			else if (str[x] < 108)
			{ // a-k
				alphaIndex -= 64;
			}
			else
			{ // m-z
				alphaIndex -= 65;
			}
			if (! BigNumberFromPowUInt8(&bi2, 58, (uint8_t)(slen - 1 - x)))
			{
				// Error occured.
				return false;
			}
			if (! BigNumberEqualsMultiplicationByUInt8(&bi2, alphaIndex))
			{
				// Error occured.
				return false;
			}
			if (! BigNumberEqualsAdditionByBigInt(bi, &bi2))
			{
				// Error occured.
				return false;
			}
		}
		if (!x)
			break;
	}
	// Got BigNumber from base-58 string. Add zeros on end.
	uint8_t zeros = 0;
	for (uint8_t x = 0; x < slen; x++)
	{
		if (str[x] == '1')
		{
			zeros++;
		}
		else
		{
			break;
		}
	}
	if (zeros) 
	{
		bi->length += zeros;
		assert( bi->length < MAX_BIG_NUMBER );
		memset(bi->data + bi->length - zeros, 0, zeros);
	}
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rsi, %rbx
movq %rdi, %r14
leaq -0x154(%rbp), %r12
xorl %r13d, %r13d
movl $0x100, %edx            # imm = 0x100
movq %r12, %rdi
xorl %esi, %esi
callq 0x20b0
movl $0x1, 0x100(%r12)
movq %rbx, %rdi
callq 0x2090
movq %rbx, %rdx
movq %rax, -0x38(%rbp)
leal -0x1(%rax), %r15d
movq %r15, -0x40(%rbp)
movq %r14, -0x50(%rbp)
movq %rbx, -0x48(%rbp)
movb (%rdx,%r15), %bl
cmpb $0x31, %bl
je 0x2895
movb $-0x31, %al
cmpb $0x3a, %bl
jl 0x2848
movb $-0x38, %al
cmpb $0x49, %bl
jl 0x2848
movb $-0x39, %al
cmpb $0x4f, %bl
jl 0x2848
movb $-0x3a, %al
cmpb $0x5b, %bl
jl 0x2848
cmpb $0x6c, %bl
setl %al
addb $-0x41, %al
movb %al, -0x29(%rbp)
movl $0x1, -0x54(%rbp)
movb $0x1, -0x154(%rbp)
cmpb %r15b, -0x40(%rbp)
je 0x2874
movl %r13d, %r14d
movq %r12, %rdi
movl $0x3a, %esi
callq 0x2574
decb %r14b
jne 0x2862
addb -0x29(%rbp), %bl
movzbl %bl, %esi
movq %r12, %rdi
callq 0x2574
movq -0x50(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x2431
movq -0x48(%rbp), %rdx
incb %r13b
subq $0x1, %r15
jae 0x281b
movq -0x38(%rbp), %rsi
xorl %eax, %eax
testl %esi, %esi
je 0x28be
movzbl %al, %ecx
cmpb $0x31, (%rdx,%rcx)
jne 0x28be
incb %al
movzbl %al, %ecx
cmpl %esi, %ecx
jb 0x28ac
testb %al, %al
je 0x28ee
movzbl %al, %edx
movl 0x100(%r14), %eax
addl %edx, %eax
movl %eax, 0x100(%r14)
cmpl $0x100, %eax            # imm = 0x100
jae 0x2902
movl %eax, %eax
addq %rax, %r14
subq %rdx, %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x20b0
movb $0x1, %al
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x147a0(%rip), %rdi     # 0x170a9
leaq 0x1470f(%rip), %rsi     # 0x1701f
leaq 0x147ae(%rip), %rcx     # 0x170c5
movl $0x176, %edx            # imm = 0x176
callq 0x20c0