// File: mbourand[P]webserv/srcs/Utils/Base64.cpp
// Function: base64_encode[abi:cxx11](char const*, unsigned long)
std::string	base64_encode(const char *str, size_t len)
{
	std::string		result;
	unsigned char	bytes[3];
	size_t			new_len;
	int 			i = 0;

	if ((new_len = (4 * ((len + 2) / 3))) < len)
		return (result);
	result.reserve(new_len);
	while (len--)
	{
		bytes[i++] = *(str++);
		if (i == 3)
		{
			result.push_back(base64_chars[((bytes[0] & 0b11111100) >> 2)]);
			result.push_back(base64_chars[((bytes[0] & 0b11) << 4) + ((bytes[1] & 0b11110000) >> 4)]);
			result.push_back(base64_chars[((bytes[1] & 0b1111) << 2) + ((bytes[2] & 0b11000000) >> 6)]);
			result.push_back(base64_chars[((bytes[2] & 0b111111))]);
			i = 0;
		}
	}
	switch (i)
	{
	case 1:
		result.push_back(base64_chars[((bytes[0] & 0b11111100) >> 2)]);
		result.push_back(base64_chars[((bytes[0] & 0b11) << 4)]);
		result += "==";
		break;
	case 2:
		result.push_back(base64_chars[((bytes[0] & 0b11111100) >> 2)]);
		result.push_back(base64_chars[((bytes[0] & 0b11) << 4) + ((bytes[1] & 0b11110000) >> 4)]);
		result.push_back(base64_chars[((bytes[1] & 0b1111) << 2)]);
		result += "=";
		break;
	default:
		break;
	}
	return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x50(%rbp)
movq %rdi, %rax
movq %rax, -0x48(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
callq 0x98c0
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
addq $0x2, %rax
movl $0x3, %ecx
xorl %edx, %edx
divq %rcx
shlq $0x2, %rax
movq %rax, -0x28(%rbp)
cmpq -0x18(%rbp), %rax
jae 0x1d63f
movb $0x1, -0x19(%rbp)
movl $0x1, -0x30(%rbp)
jmp 0x1d876
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x99b0
jmp 0x1d64e
jmp 0x1d650
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x18(%rbp)
cmpq $0x0, %rax
je 0x1d763
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x10(%rbp)
movb (%rax), %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x1c(%rbp,%rax)
cmpl $0x3, -0x2c(%rbp)
jne 0x1d75e
movzbl -0x1c(%rbp), %eax
shrl $0x2, %eax
movl %eax, %esi
leaq 0x7203b(%rip), %rdi     # 0x8f6e0
callq 0x9720
movq -0x50(%rbp), %rdi
movsbl (%rax), %esi
callq 0x95a0
jmp 0x1d6b8
movzbl -0x1c(%rbp), %eax
andl $0x3, %eax
shll $0x4, %eax
movzbl -0x1b(%rbp), %ecx
shrl $0x4, %ecx
orl %ecx, %eax
movl %eax, %esi
leaq 0x7200c(%rip), %rdi     # 0x8f6e0
callq 0x9720
movq -0x50(%rbp), %rdi
movsbl (%rax), %esi
callq 0x95a0
jmp 0x1d6e7
movzbl -0x1b(%rbp), %eax
andl $0xf, %eax
movzbl -0x1a(%rbp), %edx
shrl $0x6, %edx
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,4), %eax
movl %eax, %esi
leaq 0x71fdb(%rip), %rdi     # 0x8f6e0
callq 0x9720
movq -0x50(%rbp), %rdi
movsbl (%rax), %esi
callq 0x95a0
jmp 0x1d718
movzbl -0x1a(%rbp), %eax
andl $0x3f, %eax
movl %eax, %esi
leaq 0x71fb8(%rip), %rdi     # 0x8f6e0
callq 0x9720
movq -0x50(%rbp), %rdi
movsbl (%rax), %esi
callq 0x95a0
jmp 0x1d73b
movl $0x0, -0x2c(%rbp)
jmp 0x1d75e
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
callq 0x9c98
jmp 0x1d88f
jmp 0x1d650
movl -0x2c(%rbp), %eax
movl %eax, -0x54(%rbp)
subl $0x1, %eax
je 0x1d77d
jmp 0x1d770
movl -0x54(%rbp), %eax
subl $0x2, %eax
je 0x1d7dd
jmp 0x1d869
movzbl -0x1c(%rbp), %eax
shrl $0x2, %eax
movl %eax, %esi
leaq 0x71f53(%rip), %rdi     # 0x8f6e0
callq 0x9720
movq -0x50(%rbp), %rdi
movsbl (%rax), %esi
callq 0x95a0
jmp 0x1d7a0
movzbl -0x1c(%rbp), %eax
andl $0x3, %eax
shll $0x4, %eax
movl %eax, %esi
leaq 0x71f2d(%rip), %rdi     # 0x8f6e0
callq 0x9720
movq -0x50(%rbp), %rdi
movsbl (%rax), %esi
callq 0x95a0
jmp 0x1d7c6
movq -0x50(%rbp), %rdi
leaq 0x4d289(%rip), %rsi     # 0x6aa5a
callq 0x9650
jmp 0x1d7d8
jmp 0x1d86b
movzbl -0x1c(%rbp), %eax
shrl $0x2, %eax
movl %eax, %esi
leaq 0x71ef3(%rip), %rdi     # 0x8f6e0
callq 0x9720
movq -0x50(%rbp), %rdi
movsbl (%rax), %esi
callq 0x95a0
jmp 0x1d800
movzbl -0x1c(%rbp), %eax
andl $0x3, %eax
shll $0x4, %eax
movzbl -0x1b(%rbp), %ecx
shrl $0x4, %ecx
orl %ecx, %eax
movl %eax, %esi
leaq 0x71ec4(%rip), %rdi     # 0x8f6e0
callq 0x9720
movq -0x50(%rbp), %rdi
movsbl (%rax), %esi
callq 0x95a0
jmp 0x1d82f
movzbl -0x1b(%rbp), %eax
andl $0xf, %eax
shll $0x2, %eax
movl %eax, %esi
leaq 0x71e9e(%rip), %rdi     # 0x8f6e0
callq 0x9720
movq -0x50(%rbp), %rdi
movsbl (%rax), %esi
callq 0x95a0
jmp 0x1d855
movq -0x50(%rbp), %rdi
leaq 0x4d1fb(%rip), %rsi     # 0x6aa5b
callq 0x9650
jmp 0x1d867
jmp 0x1d86b
jmp 0x1d86b
movb $0x1, -0x19(%rbp)
movl $0x1, -0x30(%rbp)
testb $0x1, -0x19(%rbp)
jne 0x1d885
movq -0x50(%rbp), %rdi
callq 0x9c98
movq -0x48(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x9af0
nopl (%rax,%rax)