// File: akw0088[P]openssl/base64.c
// Function: base64encode
int base64encode(const void* data_buf, size_t dataLength, char* result, size_t resultSize)
{
	const char base64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	const uint8_t *data = (const uint8_t *)data_buf;
	size_t resultIndex = 0;
	size_t x;
	uint32_t n = 0;
	int padCount = dataLength % 3;
	uint8_t n0, n1, n2, n3;

	/* increment over the length of the string, three characters at a time */
	for (x = 0; x < dataLength; x += 3)
	{
		/* these three 8-bit (ASCII) characters become one 24-bit number */
		n = ((uint32_t)data[x]) << 16; //parenthesis needed, compiler depending on flags can do the shifting before conversion to uint32_t, resulting to 0

		if ((x + 1) < dataLength)
			n += ((uint32_t)data[x + 1]) << 8;//parenthesis needed, compiler depending on flags can do the shifting before conversion to uint32_t, resulting to 0

		if ((x + 2) < dataLength)
			n += data[x + 2];

		/* this 24-bit number gets separated into four 6-bit numbers */
		n0 = (uint8_t)(n >> 18) & 63;
		n1 = (uint8_t)(n >> 12) & 63;
		n2 = (uint8_t)(n >> 6) & 63;
		n3 = (uint8_t)n & 63;

		/*
		* if we have one byte available, then its encoding is spread
		* out over two characters
		*/
		if (resultIndex >= resultSize) return 1;   /* indicate failure: buffer too small */
		result[resultIndex++] = base64chars[n0];
		if (resultIndex >= resultSize) return 1;   /* indicate failure: buffer too small */
		result[resultIndex++] = base64chars[n1];

		/*
		* if we have only two bytes available, then their encoding is
		* spread out over three chars
		*/
		if ((x + 1) < dataLength)
		{
			if (resultIndex >= resultSize) return 1;   /* indicate failure: buffer too small */
			result[resultIndex++] = base64chars[n2];
		}

		/*
		* if we have all three bytes available, then their encoding is spread
		* out over four characters
		*/
		if ((x + 2) < dataLength)
		{
			if (resultIndex >= resultSize) return 1;   /* indicate failure: buffer too small */
			result[resultIndex++] = base64chars[n3];
		}
	}

	/*
	* create and add padding that is required if we did not have a multiple of 3
	* number of characters available
	*/
	if (padCount > 0)
	{
		for (; padCount < 3; padCount++)
		{
			if (resultIndex >= resultSize) return 1;   /* indicate failure: buffer too small */
			result[resultIndex++] = '=';
		}
	}
	if (resultIndex >= resultSize) return 1;   /* indicate failure: buffer too small */
	result[resultIndex] = 0;
	return 0;   /* indicate success */
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
leaq -0x70(%rbp), %rdi
leaq 0xbda(%rip), %rsi       # 0x20b0
movl $0x41, %edx
callq 0x10a0
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movl $0x0, -0x8c(%rbp)
movq -0x18(%rbp), %rax
movl $0x3, %ecx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x90(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x1709
movq -0x78(%rbp), %rax
movq -0x88(%rbp), %rcx
movzbl (%rax,%rcx), %eax
shll $0x10, %eax
movl %eax, -0x8c(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
cmpq -0x18(%rbp), %rax
jae 0x1574
movq -0x78(%rbp), %rax
movq -0x88(%rbp), %rcx
movzbl 0x1(%rax,%rcx), %eax
shll $0x8, %eax
addl -0x8c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movq -0x88(%rbp), %rax
addq $0x2, %rax
cmpq -0x18(%rbp), %rax
jae 0x15a1
movq -0x78(%rbp), %rax
movq -0x88(%rbp), %rcx
movzbl 0x2(%rax,%rcx), %eax
addl -0x8c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
shrl $0x12, %eax
movzbl %al, %eax
andl $0x3f, %eax
movb %al, -0x91(%rbp)
movl -0x8c(%rbp), %eax
shrl $0xc, %eax
movzbl %al, %eax
andl $0x3f, %eax
movb %al, -0x92(%rbp)
movl -0x8c(%rbp), %eax
shrl $0x6, %eax
movzbl %al, %eax
andl $0x3f, %eax
movb %al, -0x93(%rbp)
movl -0x8c(%rbp), %eax
movzbl %al, %eax
andl $0x3f, %eax
movb %al, -0x94(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x28(%rbp), %rax
jb 0x1608
movl $0x1, -0x4(%rbp)
jmp 0x1780
movzbl -0x91(%rbp), %eax
movb -0x70(%rbp,%rax), %dl
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x80(%rbp)
movb %dl, (%rax,%rcx)
movq -0x80(%rbp), %rax
cmpq -0x28(%rbp), %rax
jb 0x163f
movl $0x1, -0x4(%rbp)
jmp 0x1780
movzbl -0x92(%rbp), %eax
movb -0x70(%rbp,%rax), %dl
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x80(%rbp)
movb %dl, (%rax,%rcx)
movq -0x88(%rbp), %rax
addq $0x1, %rax
cmpq -0x18(%rbp), %rax
jae 0x16a8
movq -0x80(%rbp), %rax
cmpq -0x28(%rbp), %rax
jb 0x1687
movl $0x1, -0x4(%rbp)
jmp 0x1780
movzbl -0x93(%rbp), %eax
movb -0x70(%rbp,%rax), %dl
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x80(%rbp)
movb %dl, (%rax,%rcx)
movq -0x88(%rbp), %rax
addq $0x2, %rax
cmpq -0x18(%rbp), %rax
jae 0x16f0
movq -0x80(%rbp), %rax
cmpq -0x28(%rbp), %rax
jb 0x16cf
movl $0x1, -0x4(%rbp)
jmp 0x1780
movzbl -0x94(%rbp), %eax
movb -0x70(%rbp,%rax), %dl
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x80(%rbp)
movb %dl, (%rax,%rcx)
jmp 0x16f2
movq -0x88(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x88(%rbp)
jmp 0x151b
cmpl $0x0, -0x90(%rbp)
jle 0x175a
jmp 0x1714
cmpl $0x3, -0x90(%rbp)
jge 0x1758
movq -0x80(%rbp), %rax
cmpq -0x28(%rbp), %rax
jb 0x1730
movl $0x1, -0x4(%rbp)
jmp 0x1780
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x80(%rbp)
movb $0x3d, (%rax,%rcx)
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x1714
jmp 0x175a
movq -0x80(%rbp), %rax
cmpq -0x28(%rbp), %rax
jb 0x176d
movl $0x1, -0x4(%rbp)
jmp 0x1780
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)