// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0xd8(%rbp)
movq %rsi, -0x80(%rbp)
movq %rdx, -0x78(%rbp)
movq %rdi, -0x88(%rbp)
callq 0x7490
movq %rax, -0x90(%rbp)
leaq -0x80(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
leaq -0x68(%rbp), %rcx
movq %rcx, -0x28(%rbp)
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x6de6
movq -0xd0(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5cc0
movq -0xd0(%rbp), %rsi
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, %edx
leaq -0x68(%rbp), %rdi
callq 0x5ed0
jmp 0x6dfe
movq -0xd0(%rbp), %rax
movq (%rax), %rsi
movl 0x8(%rax), %eax
movl %eax, %edx
leaq -0x68(%rbp), %rdi
callq 0x5ed0
movups -0x68(%rbp), %xmm0
movaps %xmm0, -0x50(%rbp)
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, -0xa8(%rbp)
movq %rax, -0xa0(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x62c0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xe8(%rbp)
leaq -0x80(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x6e87
movq -0xe0(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x34(%rbp)
jmp 0x6e94
movq -0xe0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0xe8(%rbp), %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movl $0x3, -0xb4(%rbp)
movq -0x98(%rbp), %rax
movq -0xb0(%rbp), %rcx
addq $-0x4, %rcx
cmpq %rcx, %rax
ja 0x725b
movq -0x98(%rbp), %rax
movb (%rax), %al
movb %al, -0xc1(%rbp)
movq -0x98(%rbp), %rax
movb 0x1(%rax), %al
movb %al, -0xc2(%rbp)
movq -0x98(%rbp), %rax
movb 0x2(%rax), %al
movb %al, -0xc3(%rbp)
movq -0x98(%rbp), %rax
movb 0x3(%rax), %al
movb %al, -0xc4(%rbp)
movzbl -0xc1(%rbp), %eax
movzbl -0xc2(%rbp), %ecx
orl %ecx, %eax
movzbl -0xc3(%rbp), %ecx
orl %ecx, %eax
movzbl -0xc4(%rbp), %ecx
orl %ecx, %eax
cmpl $0x80, %eax
setge %al
xorb $-0x1, %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x6f40
jmp 0x6f4d
movq $0x0, -0x70(%rbp)
jmp 0x734c
movzbl -0xc1(%rbp), %eax
movl %eax, %ecx
leaq 0x5c213(%rip), %rax     # 0x63170
movb (%rax,%rcx), %al
movb %al, -0xc1(%rbp)
movzbl -0xc2(%rbp), %eax
movl %eax, %ecx
leaq 0x5c1fa(%rip), %rax     # 0x63170
movb (%rax,%rcx), %al
movb %al, -0xc2(%rbp)
movzbl -0xc3(%rbp), %eax
movl %eax, %ecx
leaq 0x5c1e1(%rip), %rax     # 0x63170
movb (%rax,%rcx), %al
movb %al, -0xc3(%rbp)
movzbl -0xc4(%rbp), %eax
movl %eax, %ecx
leaq 0x5c1c8(%rip), %rax     # 0x63170
movb (%rax,%rcx), %al
movb %al, -0xc4(%rbp)
movq -0x98(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x98(%rbp)
movzbl -0xc1(%rbp), %eax
movzbl -0xc2(%rbp), %ecx
orl %ecx, %eax
movzbl -0xc3(%rbp), %ecx
orl %ecx, %eax
movzbl -0xc4(%rbp), %ecx
orl %ecx, %eax
cmpl $0x80, %eax
setge %al
xorb $-0x1, %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x6ffa
jmp 0x71d2
movzbl -0xc1(%rbp), %eax
movzbl -0xc2(%rbp), %ecx
orl %ecx, %eax
movzbl -0xc3(%rbp), %ecx
orl %ecx, %eax
movzbl -0xc4(%rbp), %ecx
orl %ecx, %eax
cmpl $0xff, %eax
jne 0x7100
movq -0x98(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jne 0x7044
movq $0x0, -0x70(%rbp)
jmp 0x734c
movzbl -0xc1(%rbp), %eax
cmpl $0xff, %eax
jne 0x7065
movb -0xc2(%rbp), %al
movb %al, -0xc1(%rbp)
movb $-0x1, -0xc2(%rbp)
movzbl -0xc2(%rbp), %eax
cmpl $0xff, %eax
jne 0x7086
movb -0xc3(%rbp), %al
movb %al, -0xc2(%rbp)
movb $-0x1, -0xc3(%rbp)
movzbl -0xc3(%rbp), %eax
cmpl $0xff, %eax
jne 0x70a7
movb -0xc4(%rbp), %al
movb %al, -0xc3(%rbp)
movb $-0x1, -0xc4(%rbp)
movq -0x98(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x98(%rbp)
movb (%rax), %al
movb %al, -0xc4(%rbp)
movzbl -0xc4(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x70e2
movq $0x0, -0x70(%rbp)
jmp 0x734c
movzbl -0xc4(%rbp), %eax
movl %eax, %ecx
leaq 0x5c07e(%rip), %rax     # 0x63170
movb (%rax,%rcx), %al
movb %al, -0xc4(%rbp)
jmp 0x6fc3
movzbl -0xc4(%rbp), %eax
cmpl $0x81, %eax
jne 0x719a
movq -0x98(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x98(%rbp)
movl $0x2, -0xb4(%rbp)
movb $0x0, -0xc4(%rbp)
movzbl -0xc3(%rbp), %eax
cmpl $0x81, %eax
jne 0x717c
movl $0x1, -0xb4(%rbp)
movb $0x0, -0xc3(%rbp)
movzbl -0xc2(%rbp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x717a
movq $0x0, -0x70(%rbp)
jmp 0x734c
jmp 0x717c
movzbl -0xc3(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x7198
movq $0x0, -0x70(%rbp)
jmp 0x734c
jmp 0x719a
movzbl -0xc1(%rbp), %eax
movzbl -0xc2(%rbp), %ecx
orl %ecx, %eax
movzbl -0xc3(%rbp), %ecx
orl %ecx, %eax
movzbl -0xc4(%rbp), %ecx
orl %ecx, %eax
cmpl $0x80, %eax
jl 0x71d0
movq $0x0, -0x70(%rbp)
jmp 0x734c
jmp 0x71d2
movzbl -0xc1(%rbp), %eax
shll $0x12, %eax
movzbl -0xc2(%rbp), %ecx
shll $0xc, %ecx
orl %ecx, %eax
movzbl -0xc3(%rbp), %ecx
shll $0x6, %ecx
orl %ecx, %eax
movzbl -0xc4(%rbp), %ecx
shll $0x0, %ecx
orl %ecx, %eax
movl %eax, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
shrl $0x10, %eax
movb %al, %cl
movq -0x90(%rbp), %rax
movb %cl, (%rax)
movl -0xc8(%rbp), %eax
shrl $0x8, %eax
movb %al, %cl
movq -0x90(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0xc8(%rbp), %eax
shrl $0x0, %eax
movb %al, %cl
movq -0x90(%rbp), %rax
movb %cl, 0x2(%rax)
movq -0x90(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x90(%rbp)
jmp 0x6eb2
cmpl $0x3, -0xb4(%rbp)
je 0x728e
movq -0xc0(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
addq $-0x3, %rax
movslq -0xb4(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
jmp 0x7290
movq -0x98(%rbp), %rcx
xorl %eax, %eax
cmpq -0xb0(%rbp), %rcx
movb %al, -0xe9(%rbp)
jae 0x72e7
movq -0x98(%rbp), %rax
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x80, %ecx
movb %al, -0xe9(%rbp)
jge 0x72e7
movq -0x98(%rbp), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x5be9b(%rip), %rax     # 0x63170
movzbl (%rax,%rcx), %eax
cmpl $0xff, %eax
sete %al
movb %al, -0xe9(%rbp)
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0x72f3
jmp 0x7307
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x7290
movq -0x98(%rbp), %rax
cmpq -0xb0(%rbp), %rax
je 0x7321
movq $0x0, -0x70(%rbp)
jmp 0x734c
movq -0xd8(%rbp), %rdi
movq -0x90(%rbp), %rax
movq %rax, -0xf8(%rbp)
callq 0x7490
movq %rax, %rcx
movq -0xf8(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)