// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rbx, %rdi
callq 0x2090
movq %rbx, %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rax, -0x38(%rbp)
addq %rax, %rcx
movq %rcx, -0x40(%rbp)
movl %ecx, %ebx
movq %r14, -0x50(%rbp)
movq %rdx, -0x48(%rbp)
movb (%rdx,%rbx), %r15b
cmpb $0x31, %r15b
je 0x287e
movb $-0x31, %al
cmpb $0x3a, %r15b
jl 0x2830
movb $-0x38, %al
cmpb $0x49, %r15b
jb 0x2830
movb $-0x39, %al
cmpb $0x4f, %r15b
jb 0x2830
movb $-0x3a, %al
cmpb $0x5b, %r15b
jb 0x2830
cmpb $0x6c, %r15b
movl $0x0, %eax
adcb $-0x41, %al
movl %eax, -0x2c(%rbp)
movl $0x1, -0x54(%rbp)
movb $0x1, -0x154(%rbp)
cmpb %bl, -0x40(%rbp)
je 0x285b
movl %r13d, %r14d
movq %r12, %rdi
movl $0x3a, %esi
callq 0x255d
decb %r14b
jne 0x2849
addb -0x2c(%rbp), %r15b
movzbl %r15b, %esi
movq %r12, %rdi
callq 0x255d
movq -0x50(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x2431
movq -0x48(%rbp), %rdx
incb %r13b
subq $0x1, %rbx
jae 0x27fb
movq -0x38(%rbp), %rsi
testl %esi, %esi
je 0x28d7
xorl %eax, %eax
movzbl %al, %ecx
cmpb $0x31, (%rdx,%rcx)
jne 0x28a7
incb %al
movzbl %al, %ecx
cmpl %esi, %ecx
jb 0x2895
testb %al, %al
je 0x28d7
movzbl %al, %edx
movl 0x100(%r14), %eax
addl %edx, %eax
movl %eax, 0x100(%r14)
cmpl $0x100, %eax            # imm = 0x100
jae 0x28eb
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 0x147b7(%rip), %rdi     # 0x170a9
leaq 0x14726(%rip), %rsi     # 0x1701f
leaq 0x147c5(%rip), %rcx     # 0x170c5
movl $0x176, %edx            # imm = 0x176
callq 0x20c0