// File: /workspace/github_cpp_makefile_star2_O1/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 0x294b
xorl %eax, %eax
jmp 0x2ab3
movq %rsi, %rbx
movq %rdi, %r14
leal -0x1(%r13), %ecx
xorl %eax, %eax
movl %ecx, %edx
cmpb $0x0, (%r14,%rdx)
jne 0x296e
movb $0x31, (%rbx,%rax)
incq %rax
addl $-0x1, %ecx
jb 0x2957
jmp 0x2971
movl %eax, %r13d
movl $0x8d3dcb09, %r12d      # imm = 0x8D3DCB09
movq %r13, -0x38(%rbp)
movl 0x100(%r14), %ecx
cmpl $0x1, %ecx
jbe 0x298b
xorl %eax, %eax
jmp 0x2992
cmpb $0x3a, (%r14)
setb %al
testb %al, %al
jne 0x2a58
leal -0x1(%rcx), %edx
addq %r14, %rdx
xorl %esi, %esi
movl %ecx, %edi
shll $0x3, %esi
leal (%rsi,%rsi,2), %r8d
movq %r8, %rsi
imulq %r12, %rsi
shrq $0x25, %rsi
imull $0x3a, %esi, %esi
subl %esi, %r8d
movb (%rdx), %sil
movl %esi, %r9d
shrb %r9b
movzbl %r9b, %r9d
imull $0x47, %r9d, %r9d
shrl $0xb, %r9d
imull $0x3a, %r9d, %r9d
subb %r9b, %sil
movzbl %sil, %esi
addl %r8d, %esi
imulq $0x469ee59, %rsi, %r8   # imm = 0x469EE59
shrq $0x20, %r8
imull $0x3a, %r8d, %r8d
subl %r8d, %esi
decq %rdx
decl %edi
jne 0x29a4
leal 0x3(%r13), %edx
cmpl %edx, %ecx
setb %cl
cmpl -0x2c(%rbp), %edx
seta %dl
testb %dl, %cl
jne 0x2ab3
movl %esi, %eax
leaq 0x146f8(%rip), %rcx     # 0x17110
movb (%rax,%rcx), %al
movl %r13d, %ecx
movb %al, (%rbx,%rcx)
movzbl %sil, %esi
movq %r14, %rdi
callq 0x26b6
movl 0x100(%r14), %edx
leaq -0x140(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x20b0
movq %r14, %rdi
movq %r15, %rsi
callq 0x2502
incl %r13d
jmp 0x297b
decl %ecx
movzbl (%r14,%rcx), %ecx
leaq 0x146aa(%rip), %rdx     # 0x17110
movb (%rcx,%rdx), %cl
movl %r13d, %edx
movb %cl, (%rbx,%rdx)
leal 0x1(%r13), %ecx
movl %ecx, %edx
movq -0x38(%rbp), %r11
subl %r11d, %edx
cmpl $0x2, %edx
jb 0x2aad
shrl %edx
movb $0x1, %sil
movl %r11d, %edi
movb (%rbx,%rdi), %r8b
movl %r13d, %r9d
movb (%rbx,%r9), %r10b
movb %r10b, (%rbx,%rdi)
movb %r8b, (%rbx,%r9)
movzbl %sil, %edi
incl %r11d
decl %r13d
leal 0x1(%rdi), %esi
cmpl %edi, %edx
ja 0x2a86
movl %ecx, %ecx
movb $0x0, (%rbx,%rcx)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq