// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq %rdi, %rbx
leaq 0x10(%rdi), %r12
movq %r12, (%rdi)
movq $0x0, 0x8(%rdi)
movb $0x0, 0x10(%rdi)
leaq 0x2(%rdx), %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
addq %rdx, %rdx
andq $-0x4, %rdx
cmpq %r14, %rdx
jb 0x134d8
movq %rsi, %r15
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x7700
testq %r14, %r14
je 0x134d8
movq %r12, 0x10(%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
movb (%r15,%r13), %al
movl %ebp, %ecx
incl %ebp
movb %al, 0xd(%rsp,%rcx)
cmpl $0x3, %ebp
jne 0x13400
movzbl 0xd(%rsp), %r12d
movl %r12d, %eax
shrl $0x2, %eax
movq 0x3b19b(%rip), %rcx     # 0x4e528
movsbl (%rcx,%rax), %esi
movq %rbx, %rdi
callq 0x7420
shll $0x4, %r12d
andl $0x30, %r12d
movzbl 0xe(%rsp), %ebp
movl %ebp, %eax
shrl $0x4, %eax
orl %r12d, %eax
movq 0x3b173(%rip), %rcx     # 0x4e528
movsbl (%rcx,%rax), %esi
movq %rbx, %rdi
callq 0x7420
andl $0xf, %ebp
movzbl 0xf(%rsp), %r12d
movl %r12d, %eax
shrl $0x6, %eax
leal (%rax,%rbp,4), %eax
movq 0x3b14e(%rip), %rcx     # 0x4e528
movsbl (%rcx,%rax), %esi
movq %rbx, %rdi
callq 0x7420
andl $0x3f, %r12d
movq 0x3b137(%rip), %rax     # 0x4e528
movsbl (%rax,%r12), %esi
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x7420
incq %r13
cmpq %r13, %r14
jne 0x13365
cmpl $0x2, %ebp
je 0x13463
cmpl $0x1, %ebp
movq 0x10(%rsp), %r12
jne 0x134d8
movzbl 0xd(%rsp), %r14d
movl %r14d, %eax
shrl $0x2, %eax
movq 0x3b0f6(%rip), %rcx     # 0x4e528
movsbl (%rcx,%rax), %esi
movq %rbx, %rdi
callq 0x7420
shll $0x4, %r14d
andl $0x30, %r14d
movq 0x3b0db(%rip), %rax     # 0x4e528
movsbl (%rax,%r14), %esi
movq %rbx, %rdi
callq 0x7420
leaq 0x27635(%rip), %rsi     # 0x3aa96
jmp 0x134d0
movzbl 0xd(%rsp), %r14d
movl %r14d, %eax
shrl $0x2, %eax
movq 0x3b0b2(%rip), %rcx     # 0x4e528
movsbl (%rcx,%rax), %esi
movq %rbx, %rdi
movq 0x10(%rsp), %r12
callq 0x7420
shll $0x4, %r14d
andl $0x30, %r14d
movzbl 0xe(%rsp), %r15d
movl %r15d, %eax
shrl $0x4, %eax
orl %r14d, %eax
movq 0x3b083(%rip), %rcx     # 0x4e528
movsbl (%rcx,%rax), %esi
movq %rbx, %rdi
callq 0x7420
andl $0xf, %r15d
movq 0x3b06c(%rip), %rax     # 0x4e528
movsbl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x7420
leaq 0x275c7(%rip), %rsi     # 0x3aa97
movq %rbx, %rdi
callq 0x78d0
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, 0x10(%rsp)
jmp 0x134f1
movq %rax, %r14
movq (%rbx), %rdi
cmpq 0x10(%rsp), %rdi
je 0x1350e
movq 0x10(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x74b0
movq %r14, %rdi
callq 0x7810
nop