// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
testl %esi, %esi
je 0x404955
movq %rdx, %rbx
xorl %r12d, %r12d
leaq 0xd1b00(%rip), %r15     # 0x4d6315
xorl %r14d, %r14d
movb (%rdi), %al
movl %r12d, %ecx
incl %r12d
movb %al, 0x1(%rsp,%rcx)
cmpl $0x3, %r12d
jne 0x404889
movb 0x1(%rsp), %al
movb 0x2(%rsp), %cl
movl %eax, %edx
shrb $0x2, %dl
movb %dl, 0x4(%rsp)
movl %ecx, %edx
shrb $0x4, %dl
shlb $0x4, %al
orb %dl, %al
andb $0x3f, %al
movb %al, 0x5(%rsp)
movb 0x3(%rsp), %al
movl %eax, %edx
shrb $0x6, %dl
shlb $0x2, %cl
orb %dl, %cl
andb $0x3f, %cl
movb %cl, 0x6(%rsp)
andb $0x3f, %al
movb %al, 0x7(%rsp)
movslq %r14d, %rax
addq %rbx, %rax
xorl %ecx, %ecx
movzbl 0x4(%rsp,%rcx), %edx
movb (%rdx,%r15), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpq $0x4, %rcx
jne 0x40486e
addl %ecx, %r14d
xorl %r12d, %r12d
decl %esi
incq %rdi
testl %esi, %esi
jne 0x404818
testl %r12d, %r12d
je 0x404955
cmpl $0x2, %r12d
jg 0x4048bd
movl %r12d, %eax
leaq (%rsp,%rax), %rdi
incq %rdi
movl $0x2, %edx
subl %r12d, %edx
incq %rdx
xorl %esi, %esi
callq 0x4010e0
movb 0x1(%rsp), %al
movb 0x2(%rsp), %cl
movl %eax, %edx
shrb $0x2, %dl
movb %dl, 0x4(%rsp)
movl %ecx, %edx
shrb $0x4, %dl
shlb $0x4, %al
orb %dl, %al
andb $0x3f, %al
movb %al, 0x5(%rsp)
movb 0x3(%rsp), %al
movl %eax, %edx
shrb $0x6, %dl
shlb $0x2, %cl
orb %dl, %cl
andb $0x3f, %cl
movb %cl, 0x6(%rsp)
andb $0x3f, %al
movb %al, 0x7(%rsp)
testl %r12d, %r12d
js 0x40492c
movslq %r14d, %rcx
leal 0x1(%r12), %edx
addq %rbx, %rcx
xorl %eax, %eax
movzbl 0x4(%rsp,%rax), %esi
movb (%rsi,%r15), %sil
movb %sil, (%rcx,%rax)
incq %rax
cmpq %rax, %rdx
jne 0x40490b
cmpl $0x2, %r12d
jg 0x404955
movl %r14d, %r14d
addq %rax, %r14
movslq %r14d, %rax
addq %rax, %rbx
movl $0x2, %edx
subl %r12d, %edx
incq %rdx
movq %rbx, %rdi
movl $0x3d, %esi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x4010e0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq