// File: 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
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x100(%rcx), %eax
jae 0x2e0e
movb $0x0, -0x1(%rbp)
jmp 0x3025
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x100(%rax), %eax
subl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
jne 0x2e50
movq -0x18(%rbp), %rax
movl -0x20(%rbp), %ecx
movb $0x31, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x2e4e
jmp 0x2e5f
jmp 0x2e52
jmp 0x2e5f
jmp 0x2e54
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2e25
movl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x100(%rax), %eax
movq %rax, -0x140(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2390
cmpl $0x0, %eax
jl 0x2f51
movq -0x10(%rbp), %rdi
callq 0x2a90
movb %al, -0x131(%rbp)
movq -0x10(%rbp), %rax
movl 0x100(%rax), %eax
movl -0x20(%rbp), %ecx
addl $0x3, %ecx
cmpl %ecx, %eax
jae 0x2eec
movl -0x20(%rbp), %eax
addl $0x3, %eax
movl %eax, %eax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x100(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x2eea
movq -0x140(%rbp), %rax
movl -0x1c(%rbp), %ecx
cmpq %rcx, %rax
jbe 0x2ee8
movb $0x0, -0x1(%rbp)
jmp 0x3025
jmp 0x2eea
jmp 0x2eec
movzbl -0x131(%rbp), %eax
movl %eax, %ecx
leaq 0x2d214(%rip), %rax     # 0x30110
movb (%rax,%rcx), %dl
movq -0x18(%rbp), %rax
movl -0x20(%rbp), %ecx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rdi
movzbl -0x131(%rbp), %esi
callq 0x2990
leaq -0x130(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x100(%rax), %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x20b0
movq -0x10(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x2670
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2e76
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x100(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x2d19f(%rip), %rax     # 0x30110
movb (%rax,%rcx), %dl
movq -0x18(%rbp), %rax
movl -0x20(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movb $0x0, -0x141(%rbp)
movzbl -0x141(%rbp), %eax
movl -0x20(%rbp), %ecx
subl -0x28(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jae 0x3016
movq -0x18(%rbp), %rax
movzbl -0x141(%rbp), %ecx
addl -0x28(%rbp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movb %al, -0x142(%rbp)
movq -0x18(%rbp), %rax
movl -0x20(%rbp), %ecx
movzbl -0x141(%rbp), %edx
subl %edx, %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq -0x18(%rbp), %rax
movzbl -0x141(%rbp), %ecx
addl -0x28(%rbp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movb -0x142(%rbp), %dl
movq -0x18(%rbp), %rax
movl -0x20(%rbp), %ecx
movzbl -0x141(%rbp), %esi
subl %esi, %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movb -0x141(%rbp), %al
addb $0x1, %al
movb %al, -0x141(%rbp)
jmp 0x2f8e
movq -0x18(%rbp), %rax
movl -0x20(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw %cs:(%rax,%rax)