// File: king1600[P]cordpp/src/base64.cc
// Function: cordpp::b64_encode(char const*, unsigned long)
char* cordpp::b64_encode(const char *src, const size_t length) {
  // (length * 4 / 3) rounded up to nearest multiple of 4
  const size_t encoded_length = ((length<<2)/3+3)&-4;
  char *buffer = new char[encoded_length + 1];

  // perform base64 calculation
  size_t i;
  char *p = buffer;
  for(i = 0; i < length-2; i += 3) {
      *p++ = B64_CHARSET[(src[i] >> 2) & 0x3F];
      *p++ = B64_CHARSET[((uint8_t)src[i] << 4 | (uint8_t)src[i+1] >> 4) & 0x3F];
      *p++ = B64_CHARSET[((uint8_t)src[i+1] << 2 | (uint8_t)src[i+2] >> 6) & 0x3F];
      *p++ = B64_CHARSET[(uint8_t)src[i+2] & 0x3F];
  }

  // this returns 3 instead of 0 if we need no padding but
  // if we need no padding we dont need to do anything
  int padding = (length % 3)^3;
  if (padding == 1) {
      *p++ = B64_CHARSET[(src[i] >> 2) & 0x3F];
      *p++ = B64_CHARSET[(src[i] << 4 | src[i+1] >> 4) & 0x3F];
      *p++ = B64_CHARSET[(src[i+1] << 2) & 0x3F];
      *p++ = '=';
  } else if (padding == 2) {
      *p++ = B64_CHARSET[(src[i] >> 2) & 0x3F];
      *p++ = B64_CHARSET[(src[i] << 4) & 0x3F];
      *p++ = '=';
      *p++ = '=';
  }

  // add null terminator and return constructed buffer
  *p = '\0';
  return buffer;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
shlq $0x2, %rax
movl $0x3, %ecx
xorl %edx, %edx
divq %rcx
addq $0x3, %rax
andq $-0x4, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
addq $0x1, %rdi
callq 0xc640
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
subq $0x2, %rcx
cmpq %rcx, %rax
jae 0xffe8
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
sarl $0x2, %eax
andl $0x3f, %eax
movslq %eax, %rcx
leaq 0x7f100(%rip), %rax     # 0x8f010
movb (%rax,%rcx), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movzbl (%rax,%rcx), %eax
shll $0x4, %eax
movq -0x8(%rbp), %rcx
movq -0x28(%rbp), %rdx
movzbl 0x1(%rcx,%rdx), %ecx
sarl $0x4, %ecx
orl %ecx, %eax
andl $0x3f, %eax
movslq %eax, %rcx
leaq 0x7f0be(%rip), %rax     # 0x8f010
movb (%rax,%rcx), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movzbl 0x1(%rax,%rcx), %eax
shll $0x2, %eax
movq -0x8(%rbp), %rcx
movq -0x28(%rbp), %rdx
movzbl 0x2(%rcx,%rdx), %ecx
sarl $0x6, %ecx
orl %ecx, %eax
andl $0x3f, %eax
movslq %eax, %rcx
leaq 0x7f07b(%rip), %rax     # 0x8f010
movb (%rax,%rcx), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movzbl 0x2(%rax,%rcx), %eax
andl $0x3f, %eax
movslq %eax, %rcx
leaq 0x7f04d(%rip), %rax     # 0x8f010
movb (%rax,%rcx), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x28(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x28(%rbp)
jmp 0xfedf
movq -0x10(%rbp), %rax
movl $0x3, %ecx
xorl %edx, %edx
divq %rcx
xorq $0x3, %rdx
movl %edx, %eax
movl %eax, -0x34(%rbp)
cmpl $0x1, -0x34(%rbp)
jne 0x100c3
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
sarl $0x2, %eax
andl $0x3f, %eax
movslq %eax, %rcx
leaq 0x7efeb(%rip), %rax     # 0x8f010
movb (%rax,%rcx), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
shll $0x4, %eax
movq -0x8(%rbp), %rcx
movq -0x28(%rbp), %rdx
movsbl 0x1(%rcx,%rdx), %ecx
sarl $0x4, %ecx
orl %ecx, %eax
andl $0x3f, %eax
movslq %eax, %rcx
leaq 0x7efa9(%rip), %rax     # 0x8f010
movb (%rax,%rcx), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movsbl 0x1(%rax,%rcx), %eax
shll $0x2, %eax
andl $0x3f, %eax
movslq %eax, %rcx
leaq 0x7ef78(%rip), %rax     # 0x8f010
movb (%rax,%rcx), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movb $0x3d, (%rax)
jmp 0x10153
cmpl $0x2, -0x34(%rbp)
jne 0x10151
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
sarl $0x2, %eax
andl $0x3f, %eax
movslq %eax, %rcx
leaq 0x7ef27(%rip), %rax     # 0x8f010
movb (%rax,%rcx), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
shll $0x4, %eax
andl $0x3f, %eax
movslq %eax, %rcx
leaq 0x7eef7(%rip), %rax     # 0x8f010
movb (%rax,%rcx), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movb $0x3d, (%rax)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movb $0x3d, (%rax)
jmp 0x10153
movq -0x30(%rbp), %rax
movb $0x0, (%rax)
movq -0x20(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)