// File: /workspace/github_cpp_makefile_star2_O1/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), %rax
movq %rax, 0x10(%rsp)
movq %rax, (%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 0x13562
movq %rsi, %r15
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x7700
testq %r14, %r14
je 0x1349e
xorl %r13d, %r13d
xorl %ebp, %ebp
movb (%r15,%r13), %al
movl %ebp, %ecx
incl %ebp
movb %al, 0xd(%rsp,%rcx)
cmpl $0x3, %ebp
jne 0x13490
movzbl 0xd(%rsp), %r12d
movl %r12d, %eax
shrl $0x2, %eax
movq 0x3b10b(%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 0x3b0e3(%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 0x3b0be(%rip), %rcx     # 0x4e528
movsbl (%rcx,%rax), %esi
movq %rbx, %rdi
callq 0x7420
andl $0x3f, %r12d
movq 0x3b0a7(%rip), %rax     # 0x4e528
movsbl (%rax,%r12), %esi
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x7420
incq %r13
cmpq %r13, %r14
jne 0x133f5
jmp 0x134a0
xorl %ebp, %ebp
cmpl $0x2, %ebp
je 0x134f2
cmpl $0x1, %ebp
jne 0x13562
movzbl 0xd(%rsp), %r14d
movl %r14d, %eax
shrl $0x2, %eax
movq 0x3b067(%rip), %rcx     # 0x4e528
movsbl (%rcx,%rax), %esi
movq %rbx, %rdi
callq 0x7420
shll $0x4, %r14d
andl $0x30, %r14d
movq 0x3b04c(%rip), %rax     # 0x4e528
movsbl (%rax,%r14), %esi
movq %rbx, %rdi
callq 0x7420
leaq 0x275a6(%rip), %rsi     # 0x3aa96
jmp 0x1355a
movzbl 0xd(%rsp), %r14d
movl %r14d, %eax
shrl $0x2, %eax
movq 0x3b023(%rip), %rcx     # 0x4e528
movsbl (%rcx,%rax), %esi
movq %rbx, %rdi
callq 0x7420
shll $0x4, %r14d
andl $0x30, %r14d
movzbl 0xe(%rsp), %r15d
movl %r15d, %eax
shrl $0x4, %eax
orl %r14d, %eax
movq 0x3aff9(%rip), %rcx     # 0x4e528
movsbl (%rcx,%rax), %esi
movq %rbx, %rdi
callq 0x7420
andl $0xf, %r15d
movq 0x3afe2(%rip), %rax     # 0x4e528
movsbl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x7420
leaq 0x2753d(%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
jmp 0x13576
movq %rax, %r14
movq (%rbx), %rdi
cmpq 0x10(%rsp), %rdi
je 0x13593
movq 0x10(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x74b0
movq %r14, %rdi
callq 0x7810
nop