// File: /workspace/github_cpp_makefile_star2_O1/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
movzbl 0xf(%rax), %ecx
movl %ecx, %edi
xorl $0xf, %edi
testb %cl, %cl
cmovnsq %rax, %rsi
cmovnsl %edi, %edx
leaq (%rsi,%rdx), %r15
leaq (%rsi,%rdx), %r8
addq $-0x4, %r8
movl $0x3, -0x1c(%rsp)
xorl %eax, %eax
leaq 0x29cff(%rip), %r10     # 0x2f180
cmpq %r8, %rsi
ja 0x55e0
movzbl (%rsi), %ecx
movzbl 0x1(%rsi), %edx
movzbl 0x2(%rsi), %r14d
movb 0x3(%rsi), %r9b
movl %edx, %edi
orl %ecx, %edi
orb %r14b, %dil
orb %r9b, %dil
js 0x5571
movb (%rcx,%r10), %cl
movb (%rdx,%r10), %dil
movb (%r14,%r10), %r12b
addq $0x4, %rsi
movq %rsi, %r14
movzbl %r9b, %edx
movb (%rdx,%r10), %bpl
movzbl %cl, %r13d
movzbl %dil, %edx
movl %edx, %ecx
orl %r13d, %ecx
movl %ebp, %esi
orb %r12b, %sil
movzbl %sil, %esi
orl %ecx, %esi
cmpl $0x80, %esi
jb 0x5536
cmpl $0xff, %esi
jne 0x5575
cmpq %r15, %r14
je 0x55af
movl $0xff, %edi
cmpl %edi, %r13d
movl %r13d, %ecx
cmovel %edx, %ecx
cmovel %r13d, %edx
cmpb $-0x1, %dl
movzbl %r12b, %esi
cmovel %esi, %edx
cmovel %edi, %esi
cmpb $-0x1, %sil
movzbl %bpl, %r12d
cmovnel %esi, %r12d
movb (%r14), %r9b
incq %r14
xorl %ebp, %ebp
movl %edx, %edi
testb %r9b, %r9b
jns 0x54bd
jmp 0x55b1
movq %r14, %rsi
shll $0x12, %r13d
shll $0xc, %edx
orl %edx, %r13d
movzbl %r12b, %ecx
shll $0x6, %ecx
orl %ecx, %edx
shrl $0x10, %r13d
movb %r13b, (%rbx)
movb %dh, 0x1(%rbx)
orb %bpl, %cl
movb %cl, 0x2(%rbx)
addq $0x3, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
jne 0x5481
jmp 0x5621
xorl %ebp, %ebp
jmp 0x5563
cmpb $0x3d, %r9b
jne 0x55b6
movl $0x2, %esi
cmpb $-0x7f, %r12b
jne 0x559e
movl $0x1, -0x1c(%rsp)
xorl %ebp, %ebp
testb $0xf, %dil
jne 0x55aa
xorl %r12d, %r12d
movl $0x1, %esi
movl %esi, -0x1c(%rsp)
xorl %ebp, %ebp
testb $0x3, %r12b
je 0x55be
movq %r15, %rsi
jmp 0x55d3
xorl %ebp, %ebp
movq %r14, %rsi
jmp 0x5563
movq %r14, %rsi
movq %r11, %r14
jmp 0x55c1
movq %r15, %rsi
movl %ebp, %edi
orb %r12b, %dil
movzbl %dil, %edi
orl %edi, %ecx
cmpl $0x7f, %ecx
jbe 0x55d8
xorl %ebp, %ebp
movq %r14, %r11
jmp 0x5563
movq %r14, %r11
jmp 0x5539
movl -0x1c(%rsp), %eax
cmpl $0x3, %eax
movl %eax, %eax
cmoveq %rsi, %r11
leaq -0x3(%rbx,%rax), %rax
cmoveq %rbx, %rax
cmpq %r15, %r11
jae 0x5610
movsbq (%r11), %rcx
testq %rcx, %rcx
js 0x5610
cmpb $-0x1, (%rcx,%r10)
jne 0x5610
incq %r11
jmp 0x55f6
cmpq %r15, %r11
je 0x5619
xorl %eax, %eax
jmp 0x5621
movq -0x18(%rsp), %rcx
subq (%rcx), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq