// File: /workspace/github_c_makefile_star2_O3/canboat[P]canboat/ikonvert-serial/../common/b64.c
// Function: sbAppendEncodeBase64
void sbAppendEncodeBase64(StringBuffer *sb, const uint8_t *data, size_t len, enum Base64Encoding encoding)
{
  /*
   * Since every 3 input characters are translated into 4 output characters
   * following buffer is big enough to translate B64_CHUNKSIZE
   * input characters (including boundary effects)
   */
  char           otemp[((B64_CHUNKSIZE + 2) / 3) * 4 + 3];
  char          *optr;
  size_t         count;
  const uint8_t *in = data;
  unsigned char  u1, u2, u3;
  const char    *encodeTable;

  switch (encoding)
  {
    case BASE64_AIS:
      encodeTable = AISEncodeTable;
      break;
    case BASE64_RFC:
    default:
      encodeTable = B64EncodeTable;
      break;
  }

  sbEnsureCapacity(sb, len * 4 / 3 + 8);

  while (len > 0)
  {
    count = CB_MIN(B64_CHUNKSIZE, len);

    len -= count;

    optr = otemp;
    while (count >= 3)
    {
      u1      = *(in++);
      u2      = *(in++);
      u3      = *(in++);
      *optr++ = encodeTable[u1 >> 2];
      *optr++ = encodeTable[((u1 & 0x03) << 4) | (u2 >> 4)];
      *optr++ = encodeTable[((u2 & 0x0f) << 2) | (u3 >> 6)];
      *optr++ = encodeTable[u3 & 0x3f];
      count -= 3;
    }

    if (count)
    {
      if (count == 2)
      {
        u1      = *(in++);
        u2      = *(in++);
        *optr++ = encodeTable[u1 >> 2];
        *optr++ = encodeTable[(unsigned) (((u1 & 0x03) << 4) | ((u2 & 0xf0) >> 4))];
        *optr++ = encodeTable[(unsigned) ((u2 & 0x0f) << 2)];
        if (encoding == BASE64_AIS)
        {
          *optr++ = encodeTable[64];
        }
      }
      else
      {
        u1      = *(in++);
        *optr++ = encodeTable[u1 >> 2];
        *optr++ = encodeTable[(unsigned) ((u1 & 0x03) << 4)];
        if (encoding == BASE64_AIS)
        {
          *optr++ = encodeTable[64];
          *optr++ = encodeTable[64];
        }
      }
    }
    *optr = 0;

    sbAppendString(sb, otemp);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebx
movq %rdx, %r14
movq %rsi, %r15
cmpl $0x1, %ecx
leaq 0x24e4(%rip), %rax      # 0x82a0
leaq 0x252d(%rip), %rbp      # 0x82f0
cmoveq %rax, %rbp
leaq (,%rdx,4), %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
movq %rdi, %r12
shrq %rdx
leaq 0x8(%rdx), %rsi
callq 0x3d7e
testq %r14, %r14
je 0x5f2a
leaq 0xd(%rsp), %r13
cmpq $0x6, %r14
movl $0x6, %eax
cmovbq %r14, %rax
cmpq $0x3, %r14
jb 0x5e7e
movq %rax, %rdx
movq %r13, %rcx
movzbl (%r15), %edi
movzbl 0x1(%r15), %r8d
movzbl 0x2(%r15), %esi
addq $0x3, %r15
movl %edi, %r9d
shrl $0x2, %r9d
movb (%rbp,%r9), %r9b
movb %r9b, (%rcx)
shll $0x4, %edi
andl $0x30, %edi
movl %r8d, %r9d
shrl $0x4, %r9d
orl %edi, %r9d
movb (%rbp,%r9), %dil
movb %dil, 0x1(%rcx)
andl $0xf, %r8d
movl %esi, %edi
shrl $0x6, %edi
leal (%rdi,%r8,4), %edi
movb (%rbp,%rdi), %dil
movb %dil, 0x2(%rcx)
andl $0x3f, %esi
movb (%rbp,%rsi), %sil
movb %sil, 0x3(%rcx)
addq $0x4, %rcx
addq $-0x3, %rdx
cmpq $0x2, %rdx
ja 0x5e12
jmp 0x5e84
movq %r13, %rcx
movq %rax, %rdx
testq %rdx, %rdx
je 0x5f10
cmpq $0x2, %rdx
jne 0x5ed8
movzbl (%r15), %edx
movzbl 0x1(%r15), %esi
addq $0x2, %r15
movl %edx, %edi
shrl $0x2, %edi
shll $0x4, %edx
andl $0x30, %edx
movl %esi, %r8d
shrl $0x4, %r8d
orl %edx, %r8d
andl $0xf, %esi
movb (%rbp,%rdi), %dl
movb %dl, (%rcx)
movb (%rbp,%r8), %dl
movb %dl, 0x1(%rcx)
movb (%rbp,%rsi,4), %dl
movb %dl, 0x2(%rcx)
cmpl $0x1, %ebx
je 0x5f02
addq $0x3, %rcx
jmp 0x5f10
movzbl (%r15), %edx
incq %r15
movl %edx, %esi
shrl $0x2, %esi
shll $0x4, %edx
andl $0x30, %edx
movb (%rbp,%rsi), %sil
movb %sil, (%rcx)
movb (%rbp,%rdx), %dl
movb %dl, 0x1(%rcx)
cmpl $0x1, %ebx
jne 0x5f0c
movb $0x0, 0x2(%rcx)
movb $0x0, 0x3(%rcx)
addq $0x4, %rcx
jmp 0x5f10
addq $0x2, %rcx
subq %rax, %r14
movb $0x0, (%rcx)
movq %r12, %rdi
movq %r13, %rsi
callq 0x40c9
testq %r14, %r14
jne 0x5df9
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq