// File: /workspace/github_cpp_makefile_star2_O3/jeffchan[P]blockchain-parser/Base58.cpp
// Function: CBEncodeBase58(BigNumber*, char*, unsigned int)
bool CBEncodeBase58(BigNumber * bi,char *str,uint32_t maxStrLen)
{
	if ( maxStrLen < bi->length ) // must be at least twice the size of the binary
	{
		return false;
	}
	uint32_t x = 0;
	// Zeros
	for (uint32_t y = bi->length - 1;; y--)
	{
		if (! bi->data[y])
		{
			str[x] = '1';
			x++;
			if (! y)
				break;
		}
		else
		{
			break;
		}
	}
	uint32_t zeros = x;
	// Make temporary data store
	uint8_t temp[MAX_BIG_NUMBER];
	// Encode
	uint8_t mod;
	size_t size = bi->length;
	for (;BigNumberCompareTo58(bi) >= 0;x++) 
	{
		mod = BigNumberModuloWith58(bi);
		if (bi->length < x + 3) 
		{
			size = x + 3;
			if (size > bi->length) 
			{
				if ( size > maxStrLen )
				{
					return false;
				}
			}
		}
		str[x] = base58Characters[mod];
		BigNumberEqualsSubtractionByUInt8(bi, mod);
		memset(temp, 0, bi->length);
		BigNumberEqualsDivisionBy58(bi, temp);
	}
	str[x] = base58Characters[bi->data[bi->length-1]];
	x++;
	// Reversal
	for (uint8_t y = 0; y < (x-zeros) / 2; y++) 
	{
		char temp = str[y+zeros];
		str[y+zeros] = str[x-y-1];
		str[x-y-1] = temp;
	}
	str[x] = '\0';
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl 0x100(%rdi), %r13d
movl %edx, -0x2c(%rbp)
cmpl %edx, %r13d
jbe 0x2941
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rbx
movq %rdi, %r14
leal -0x1(%r13), %ecx
xorl %eax, %eax
movl %ecx, %edx
cmpb $0x0, (%r14,%rdx)
jne 0x2964
movb $0x31, (%rbx,%rax)
incq %rax
addl $-0x1, %ecx
jb 0x294d
jmp 0x2967
movl %eax, %r13d
movl $0x8d3dcb09, %r12d      # imm = 0x8D3DCB09
movq %r13, -0x38(%rbp)
movl 0x100(%r14), %eax
cmpl $0x1, %eax
ja 0x2987
cmpb $0x3a, (%r14)
jb 0x2a36
leal -0x1(%rax), %ecx
addq %r14, %rcx
xorl %esi, %esi
movl %eax, %edx
shll $0x3, %esi
leal (%rsi,%rsi,2), %esi
movq %rsi, %rdi
imulq %r12, %rdi
shrq $0x25, %rdi
imull $0x3a, %edi, %edi
subl %edi, %esi
movb (%rcx), %dil
movl %edi, %r8d
shrb %r8b
movzbl %r8b, %r8d
imull $0x47, %r8d, %r8d
shrl $0xb, %r8d
imull $0x3a, %r8d, %r8d
subb %r8b, %dil
movzbl %dil, %edi
addl %esi, %edi
movl %edi, %esi
subl $0x3a, %esi
cmovbl %edi, %esi
decq %rcx
decl %edx
jne 0x2991
leal 0x3(%r13), %ecx
cmpl %ecx, %eax
setb %al
cmpl -0x2c(%rbp), %ecx
seta %cl
testb %cl, %al
jne 0x292d
movzbl %sil, %esi
leaq 0x14716(%rip), %rax     # 0x17110
movb (%rsi,%rax), %al
movl %r13d, %ecx
movb %al, (%rbx,%rcx)
movq %r14, %rdi
callq 0x26a2
movl 0x100(%r14), %edx
leaq -0x140(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x20b0
movq %r14, %rdi
movq %r15, %rsi
callq 0x24f3
incl %r13d
jmp 0x2971
decl %eax
movzbl (%r14,%rax), %eax
leaq 0x146cc(%rip), %rcx     # 0x17110
movb (%rax,%rcx), %al
movl %r13d, %ecx
movb %al, (%rbx,%rcx)
leal 0x1(%r13), %eax
movl %eax, %ecx
movq -0x38(%rbp), %r10
subl %r10d, %ecx
cmpl $0x2, %ecx
jb 0x2a89
shrl %ecx
movb $0x1, %dl
movl %r10d, %esi
movb (%rbx,%rsi), %dil
movl %r13d, %r8d
movb (%rbx,%r8), %r9b
movb %r9b, (%rbx,%rsi)
movb %dil, (%rbx,%r8)
movzbl %dl, %esi
incl %r10d
decl %r13d
leal 0x1(%rsi), %edx
cmpl %esi, %ecx
ja 0x2a63
movl %eax, %eax
movb $0x0, (%rbx,%rax)
movb $0x1, %al
jmp 0x292f