// File: /workspace/github_c_makefile_star2_O1/archer1357[P]Offline-Raytracer/base64.c
// Function: base64_encode
void base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len, char *out) {
  //todo: test

  static const char * base64_chars =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz"
    "0123456789+/";

  int outInd=0;
  int i = 0;
  int j = 0;
  unsigned char char_array_3[3];
  unsigned char char_array_4[4];

  while(in_len--) {
    char_array_3[i++] = *(bytes_to_encode++);

    if(i == 3) {
      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
      char_array_4[3] = char_array_3[2] & 0x3f;

      for(i = 0; (i <4); i++) {
        out[outInd++]=base64_chars[char_array_4[i]];
      }

      i = 0;
    }
  }

  if(i) {
    for(j = i; j < 3; j++) {
      char_array_3[j] = '\0';
    }

    char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
    char_array_4[3] = char_array_3[2] & 0x3f;

    for(j = 0; (j < i + 1); j++) {
      out[outInd++]=base64_chars[char_array_4[j]];
    }

    while((i++ < 3)) {
      out[outInd++]='=';
    }
  }
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rdx, %rbx
testl %esi, %esi
je 0x4048c3
xorl %r14d, %r14d
leaq 0xd0b14(%rip), %rax     # 0x4d5349
xorl %ebp, %ebp
movb (%rdi), %cl
movl %r14d, %edx
incl %r14d
movb %cl, 0x9(%rsp,%rdx)
cmpl $0x3, %r14d
jne 0x4048b4
movb 0x9(%rsp), %cl
movb 0xa(%rsp), %dl
movl %ecx, %r8d
shrb $0x2, %r8b
movb %r8b, 0xc(%rsp)
movl %edx, %r8d
shrb $0x4, %r8b
shlb $0x4, %cl
orb %r8b, %cl
andb $0x3f, %cl
movb %cl, 0xd(%rsp)
movb 0xb(%rsp), %cl
movl %ecx, %r8d
shrb $0x6, %r8b
shlb $0x2, %dl
orb %r8b, %dl
andb $0x3f, %dl
movb %dl, 0xe(%rsp)
andb $0x3f, %cl
movb %cl, 0xf(%rsp)
movslq %ebp, %rcx
addq %rbx, %rcx
xorl %edx, %edx
movzbl 0xc(%rsp,%rdx), %r8d
movb (%r8,%rax), %r8b
movb %r8b, (%rcx,%rdx)
incq %rdx
cmpq $0x4, %rdx
jne 0x404898
addl %edx, %ebp
xorl %r14d, %r14d
decl %esi
incq %rdi
testl %esi, %esi
jne 0x404837
jmp 0x4048c8
xorl %ebp, %ebp
xorl %r14d, %r14d
testl %r14d, %r14d
je 0x40498b
cmpl $0x2, %r14d
jg 0x4048f4
movl %r14d, %eax
leaq (%rsp,%rax), %rdi
addq $0x9, %rdi
movl $0x2, %edx
subl %r14d, %edx
incq %rdx
xorl %esi, %esi
callq 0x4010e0
movb 0x9(%rsp), %al
movb 0xa(%rsp), %cl
movl %eax, %edx
shrb $0x2, %dl
movb %dl, 0xc(%rsp)
movl %ecx, %edx
shrb $0x4, %dl
shlb $0x4, %al
orb %dl, %al
andb $0x3f, %al
movb %al, 0xd(%rsp)
movb 0xb(%rsp), %al
movl %eax, %edx
shrb $0x6, %dl
shlb $0x2, %cl
orb %dl, %cl
andb $0x3f, %cl
movb %cl, 0xe(%rsp)
andb $0x3f, %al
movb %al, 0xf(%rsp)
testl %r14d, %r14d
js 0x40495f
movslq %ebp, %rax
leal 0x1(%r14), %ecx
addq %rbx, %rax
xorl %edx, %edx
leaq 0xd0a01(%rip), %rsi     # 0x4d5349
movzbl 0xc(%rsp,%rdx), %edi
movb (%rdi,%rsi), %dil
movb %dil, (%rax,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x404948
addl %edx, %ebp
cmpl $0x2, %r14d
jg 0x40498b
movslq %ebp, %rax
addq %rax, %rbx
movl $0x2, %edx
subl %r14d, %edx
incq %rdx
movq %rbx, %rdi
movl $0x3d, %esi
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
jmp 0x4010e0
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq