// File: /workspace/github_c_makefile_star2_O2/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, %r13
cmpl $0x1, %ecx
leaq 0x1788(%rip), %rax      # 0x72a0
leaq 0x17d1(%rip), %rbp      # 0x72f0
cmoveq %rax, %rbp
movq %rdi, %r15
leaq (,%rdx,4), %rax
pushq $0x3
popq %rcx
xorl %edx, %edx
divq %rcx
leaq 0x8(%rax), %rsi
callq 0x3c87
leaq 0xd(%rsp), %r12
testq %r14, %r14
je 0x5c9b
cmpq $0x6, %r14
pushq $0x6
popq %rax
cmovbq %r14, %rax
incq %r13
movq %r13, %rcx
xorl %edx, %edx
movq %rax, %rsi
cmpq $0x3, %rsi
jb 0x5bd5
movzbl -0x1(%rcx), %edi
movzbl (%rcx), %r8d
movzbl 0x1(%rcx), %r9d
movl %edi, %r10d
shrl $0x2, %r10d
movb (%rbp,%r10), %r10b
movb %r10b, 0xd(%rsp,%rdx)
shll $0x4, %edi
andl $0x30, %edi
movl %r8d, %r10d
shrl $0x4, %r10d
orl %edi, %r10d
movb (%rbp,%r10), %dil
movb %dil, 0xe(%rsp,%rdx)
andl $0xf, %r8d
movl %r9d, %edi
shrl $0x6, %edi
leal (%rdi,%r8,4), %edi
movb (%rbp,%rdi), %dil
movb %dil, 0xf(%rsp,%rdx)
andl $0x3f, %r9d
movb (%rbp,%r9), %dil
movb %dil, 0x10(%rsp,%rdx)
addq $-0x3, %rsi
addq $0x4, %rdx
addq $0x3, %rcx
jmp 0x5b63
cmpq $0x2, %rsi
je 0x5bf0
leaq -0x1(%rcx), %r13
testq %rsi, %rsi
jne 0x5c40
addq %rsp, %rdx
addq $0xd, %rdx
jmp 0x5c85
movzbl -0x1(%rcx), %esi
movzbl (%rcx), %edi
incq %rcx
movl %esi, %r8d
shrl $0x2, %r8d
shll $0x4, %esi
andl $0x30, %esi
movl %edi, %r9d
shrl $0x4, %r9d
orl %esi, %r9d
andl $0xf, %edi
movb (%rbp,%r8), %sil
movb %sil, 0xd(%rsp,%rdx)
movb (%rbp,%r9), %sil
movb %sil, 0xe(%rsp,%rdx)
movb (%rbp,%rdi,4), %sil
movb %sil, 0xf(%rsp,%rdx)
cmpl $0x1, %ebx
je 0x5c6e
addq %rsp, %rdx
addq $0x10, %rdx
jmp 0x5c82
movzbl (%r13), %esi
movl %esi, %edi
shrl $0x2, %edi
shll $0x4, %esi
andl $0x30, %esi
movb (%rbp,%rdi), %dil
movb %dil, 0xd(%rsp,%rdx)
movb (%rbp,%rsi), %sil
movb %sil, 0xe(%rsp,%rdx)
cmpl $0x1, %ebx
jne 0x5c7b
movb $0x0, 0xf(%rsp,%rdx)
addq %rsp, %rdx
addq $0x11, %rdx
movb $0x0, -0x1(%rdx)
jmp 0x5c82
addq %rsp, %rdx
addq $0xf, %rdx
movq %rcx, %r13
subq %rax, %r14
movb $0x0, (%rdx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3faf
jmp 0x5b44
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq