// File: /workspace/github_c_makefile_star2_O1/uhlin[P]enhanced-duc/source/b64_encode.c
// Function: b64_encode
int
b64_encode(uint8_t const *src, size_t srclength, char *target, size_t targsize)
{
	size_t datalength = 0;
	uint8_t input[3];
	uint8_t output[4];
	size_t i;

	while (2 < srclength) {
		input[0] = *src++;
		input[1] = *src++;
		input[2] = *src++;
		srclength -= 3;

		output[0] = input[0] >> 2;
		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
		output[3] = input[2] & 0x3f;
		Assert(output[0] < 64);
		Assert(output[1] < 64);
		Assert(output[2] < 64);
		Assert(output[3] < 64);

		if (datalength + 4 > targsize)
			return (-1);
		target[datalength++] = Base64[output[0]];
		target[datalength++] = Base64[output[1]];
		target[datalength++] = Base64[output[2]];
		target[datalength++] = Base64[output[3]];
	}

	/* Now we worry about padding. */
	if (0 != srclength) {
		/* Get what's left. */
		input[0] = input[1] = input[2] = '\0';
		for (i = 0; i < srclength; i++)
			input[i] = *src++;

		output[0] = input[0] >> 2;
		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
		Assert(output[0] < 64);
		Assert(output[1] < 64);
		Assert(output[2] < 64);

		if (datalength + 4 > targsize)
			return (-1);
		target[datalength++] = Base64[output[0]];
		target[datalength++] = Base64[output[1]];
		if (srclength == 1)
			target[datalength++] = Pad64;
		else
			target[datalength++] = Base64[output[2]];
		target[datalength++] = Pad64;
	}
	if (datalength >= targsize)
		return (-1);
	target[datalength] = '\0';	/* Returned value doesn't count \0. */
	return (datalength);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
cmpq $0x3, %rsi
jb 0x4d05
xorl %r10d, %r10d
leaq 0x43a6(%rip), %r9       # 0x9010
movb (%rdi), %al
movb 0x1(%rdi), %bl
movb 0x2(%rdi), %r11b
leaq 0x4(%r10), %r8
cmpq %rcx, %r8
ja 0x4dc9
movzbl %al, %r15d
movzbl %r11b, %r12d
movl %r12d, %r14d
andl $0x3f, %r14d
shrb $0x6, %r12b
leal (,%rbx,4), %ebp
orb %r12b, %bpl
andb $0x3f, %bpl
movl %ebx, %r12d
shrb $0x4, %r12b
movl %r15d, %r13d
shlb $0x4, %r13b
orb %r12b, %r13b
andb $0x3f, %r13b
shrl $0x2, %r15d
addq $-0x3, %rsi
addq $0x3, %rdi
movb (%r15,%r9), %r15b
movb %r15b, (%rdx,%r10)
movzbl %r13b, %r15d
movb (%r15,%r9), %r15b
movb %r15b, 0x1(%rdx,%r10)
movzbl %bpl, %r15d
movb (%r15,%r9), %bpl
movb %bpl, 0x2(%rdx,%r10)
movb (%r14,%r9), %bpl
movb %bpl, 0x3(%rdx,%r10)
movq %r8, %r10
cmpq $0x2, %rsi
ja 0x4c6a
movb %bl, -0x2(%rsp)
movb %r11b, -0x1(%rsp)
jmp 0x4d0c
movb -0x3(%rsp), %al
xorl %r8d, %r8d
movb %al, -0x3(%rsp)
testq %rsi, %rsi
je 0x4db4
movb $0x0, -0x1(%rsp)
movw $0x0, -0x3(%rsp)
xorl %eax, %eax
movb (%rdi,%rax), %r9b
movb %r9b, -0x3(%rsp,%rax)
incq %rax
cmpq %rax, %rsi
jne 0x4d27
leaq 0x4(%r8), %rdi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rcx, %rdi
ja 0x4ddb
movzbl -0x2(%rsp), %eax
movzbl -0x1(%rsp), %r9d
movzbl -0x3(%rsp), %r10d
movl %r10d, %r11d
shll $0x4, %r11d
andl $0x30, %r11d
movl %eax, %ebx
shrl $0x4, %ebx
orl %r11d, %ebx
shrl $0x2, %r10d
leaq 0x4297(%rip), %r11      # 0x9010
movb (%r10,%r11), %r10b
movb %r10b, (%rdx,%r8)
movb (%rbx,%r11), %bl
movq %r8, %r10
orq $0x2, %r10
movb %bl, 0x1(%rdx,%r8)
movb $0x3d, %bl
cmpq $0x1, %rsi
je 0x4da8
andl $0xf, %eax
shrl $0x6, %r9d
leal (%r9,%rax,4), %eax
movb (%rax,%r11), %bl
movb %bl, (%rdx,%r10)
movb $0x3d, 0x3(%rdx,%r8)
jmp 0x4db7
movq %r8, %rdi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rcx, %rdi
jae 0x4ddb
movb $0x0, (%rdx,%rdi)
movl %edi, %eax
jmp 0x4ddb
movb %bl, -0x2(%rsp)
movb %r11b, -0x1(%rsp)
movb %al, -0x3(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop