// File: /workspace/github_cpp_makefile_star2_O3/Alcaro[P]bridges/arlib/base64dec.cpp
// Function: base64_dec_raw(arrayvieww<unsigned char>, cstring)
size_t base64_dec_raw(arrayvieww<uint8_t> out, cstring text)
{
	uint8_t * outptr = out.ptr();
	const char * in = (char*)text.bytes().ptr();
	const char * inend = in + text.length();
	
	int final_chunk = 3;
	const char * in_real;
	
	while (in <= inend-4)
	{
		uint8_t c1 = in[0];
		uint8_t c2 = in[1];
		uint8_t c3 = in[2];
		uint8_t c4 = in[3];
		if (UNLIKELY((c1|c2|c3|c4) >= 0x80)) return 0;
		c1 = decode[c1];
		c2 = decode[c2];
		c3 = decode[c3];
		c4 = decode[c4];
		in += 4;
		
	space_again:
		if (UNLIKELY((c1|c2|c3|c4) >= 0x80))
		{
			if ((c1|c2|c3|c4) == SP)
			{
				if (in == inend) return 0;
				if (c1 == SP) { c1 = c2; c2 = SP; }
				if (c2 == SP) { c2 = c3; c3 = SP; }
				if (c3 == SP) { c3 = c4; c4 = SP; }
				c4 = *in++;
				if (c4 & 0x80) return 0;
				c4 = decode[c4];
				goto space_again;
			}
			if (c4 == PA)
			{
				in_real = in; // some goofy tricks to get a few instructions out of the fast path
				in = inend;
				final_chunk = 2;
				c4 = 0;
				if (c3 == PA)
				{
					final_chunk = 1;
					c3 = 0;
					if (c2&15) return 0; // reject padded data where the bits of the partial byte aren't zero
				}
				if (c3&3) return 0;
			}
			
			if ((c1|c2|c3|c4) >= 0x80) return 0;
		}
		
		uint32_t outraw = (c1<<18) | (c2<<12) | (c3<<6) | (c4<<0);
		outptr[0] = outraw>>16;
		outptr[1] = outraw>>8;
		outptr[2] = outraw>>0;
		outptr += 3;
	}
	
	if (final_chunk != 3)
	{
		in = in_real;
		outptr = outptr-3 + final_chunk;
	}
	while (in < inend && (uint8_t)*in < 0x80 && decode[(uint8_t)*in] == SP) in++;
	
	if (in != inend) return 0;
	return outptr-out.ptr();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq -0x10(%rsp), %rax
movq %rsi, (%rax)
movq %rdx, 0x8(%rax)
movq %rdi, -0x18(%rsp)
movq (%rdi), %rbx
movq %rdx, %rcx
shrq $0x38, %rcx
xorl $0xf, %ecx
testq %rdx, %rdx
cmovnsq %rax, %rsi
cmovsq %rdx, %rcx
movl %ecx, %eax
leaq (%rsi,%rax), %r12
leaq (%rsi,%rax), %rbp
addq $-0x4, %rbp
movl $0x3, %edi
cmpq %rbp, %rsi
jbe 0x3790
jmp 0x38dd
xorl %eax, %eax
leaq 0x2b9e7(%rip), %r8      # 0x2f180
movzbl (%rsi), %ecx
movzbl 0x1(%rsi), %edx
movzbl 0x2(%rsi), %r11d
movb 0x3(%rsi), %r10b
movl %edx, %r9d
orl %ecx, %r9d
orb %r11b, %r9b
orb %r10b, %r9b
js 0x3925
movb (%rcx,%r8), %cl
movb (%rdx,%r8), %r14b
movb (%r11,%r8), %r15b
addq $0x4, %rsi
movzbl %r10b, %edx
movb (%rdx,%r8), %r13b
movzbl %cl, %r11d
movzbl %r14b, %edx
movl %edx, %ecx
orl %r11d, %ecx
movl %r13d, %r9d
orb %r15b, %r9b
movzbl %r9b, %r9d
orl %ecx, %r9d
cmpl $0x80, %r9d
jb 0x38ad
cmpl $0xff, %r9d
jne 0x384d
cmpq %r12, %rsi
je 0x3925
movl $0xff, %r10d
cmpl %r10d, %r11d
movl %r11d, %ecx
cmovel %edx, %ecx
cmovel %r11d, %edx
cmpb $-0x1, %dl
movzbl %r15b, %r9d
cmovel %r9d, %edx
cmovel %r10d, %r9d
cmpb $-0x1, %r9b
movzbl %r13b, %r15d
cmovnel %r9d, %r15d
movb (%rsi), %r10b
incq %rsi
movl %edx, %r14d
testb %r10b, %r10b
jns 0x37cb
jmp 0x3925
cmpb $0x3d, %r10b
jne 0x3875
cmpb $-0x7f, %r15b
jne 0x387f
xorl %r15d, %r15d
movq %r12, %r10
movl $0x1, %edi
movl $0x0, %r13d
testb $0xf, %r14b
je 0x3894
jmp 0x3919
movq %rsi, %r10
movq -0x20(%rsp), %rsi
jmp 0x3894
xorl %r13d, %r13d
movq %r12, %r10
movl $0x2, %edi
testb $0x3, %r15b
jne 0x3919
movq %rsi, -0x20(%rsp)
movl %r13d, %esi
orb %r15b, %sil
movzbl %sil, %esi
orl %esi, %ecx
movq %r10, %rsi
cmpl $0x7f, %ecx
ja 0x3925
shll $0x12, %r11d
shll $0xc, %edx
orl %edx, %r11d
movzbl %r15b, %ecx
shll $0x6, %ecx
orl %ecx, %edx
shrl $0x10, %r11d
movb %r11b, (%rbx)
movb %dh, 0x1(%rbx)
orb %r13b, %cl
movb %cl, 0x2(%rbx)
addq $0x3, %rbx
cmpq %rbp, %rsi
jbe 0x3799
cmpl $0x3, %edi
movl %edi, %eax
cmovneq -0x20(%rsp), %rsi
leaq -0x3(%rbx,%rax), %rax
cmoveq %rbx, %rax
cmpq %r12, %rsi
jae 0x3914
leaq 0x2b883(%rip), %rcx     # 0x2f180
movsbq (%rsi), %rdx
testq %rdx, %rdx
js 0x3914
cmpb $-0x1, (%rdx,%rcx)
jne 0x3914
incq %rsi
cmpq %r12, %rsi
jb 0x38fd
cmpq %r12, %rsi
je 0x391d
xorl %eax, %eax
jmp 0x3925
movq -0x18(%rsp), %rcx
subq (%rcx), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq