void base64_encode_atom(unsigned char *data, int n, char *out) {
  static const char base64_chars[] =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  unsigned word;
  word = data[0] << 16;
  if (n > 1)
    word |= data[1] << 8;
  if (n > 2)
    word |= data[2];
  out[0] = base64_chars[(word >> 18) & 0x3F];
  out[1] = base64_chars[(word >> 12) & 0x3F];
  if (n > 1)
    out[2] = base64_chars[(word >> 6) & 0x3F];
  else
    out[2] = '=';
  if (n > 2)
    out[3] = base64_chars[word & 0x3F];
  else
    out[3] = '=';
}


x86 assembly (clang, -O0):
<base64_encode_atom>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movzbl (%rax),%eax
shl    $0x10,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0xc(%rbp)
jle    1137 <base64_encode_atom+0x37>
mov    -0x8(%rbp),%rax
movzbl 0x1(%rax),%eax
shl    $0x8,%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x2,-0xc(%rbp)
jle    114f <base64_encode_atom+0x4f>
mov    -0x8(%rbp),%rax
movzbl 0x2(%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
shr    $0x12,%eax
and    $0x3f,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0xe9d(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
shr    $0xc,%eax
and    $0x3f,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0xe80(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x1(%rax)
cmpl   $0x1,-0xc(%rbp)
jle    11b7 <base64_encode_atom+0xb7>
mov    -0x1c(%rbp),%eax
shr    $0x6,%eax
and    $0x3f,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0xe58(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x2(%rax)
jmp    11bf <base64_encode_atom+0xbf>
mov    -0x18(%rbp),%rax
movb   $0x3d,0x2(%rax)
cmpl   $0x2,-0xc(%rbp)
jle    11e9 <base64_encode_atom+0xe9>
mov    -0x1c(%rbp),%eax
and    $0x3f,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0xe26(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x3(%rax)
jmp    11f1 <base64_encode_atom+0xf1>
mov    -0x18(%rbp),%rax
movb   $0x3d,0x3(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode_atom>:
movzbl (%rdi),%eax
shl    $0x10,%eax
cmp    $0x2,%esi
jl     1119 <base64_encode_atom+0x19>
movzbl 0x1(%rdi),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
cmp    $0x2,%esi
jne    111e <base64_encode_atom+0x1e>
xor    %r8d,%r8d
jmp    1127 <base64_encode_atom+0x27>
movzbl 0x2(%rdi),%ecx
or     %ecx,%eax
mov    $0x1,%r8b
mov    %eax,%ecx
shr    $0x12,%ecx
lea    0xecd(%rip),%rdi
mov    (%rcx,%rdi,1),%cl
mov    %cl,(%rdx)
mov    %eax,%ecx
shr    $0xc,%ecx
and    $0x3f,%ecx
mov    (%rcx,%rdi,1),%cl
mov    %cl,0x1(%rdx)
mov    $0x3d,%r9b
mov    $0x3d,%cl
cmp    $0x2,%esi
jl     115b <base64_encode_atom+0x5b>
mov    %eax,%ecx
shr    $0x6,%ecx
and    $0x3f,%ecx
mov    (%rcx,%rdi,1),%cl
mov    %cl,0x2(%rdx)
test   %r8b,%r8b
je     116a <base64_encode_atom+0x6a>
and    $0x3f,%eax
mov    (%rax,%rdi,1),%r9b
mov    %r9b,0x3(%rdx)
ret

LLVM IR (-O0):
@base64_encode_atom.base64_chars = internal constant [65 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\00"
define void @base64_encode_atom(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i8*, i8** %4
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = zext i8 %10 to i32
%12 = shl i32 %11, 16
store i32 %12, i32* %7
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %23
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = shl i32 %19, 8
%21 = load i32, i32* %7
%22 = or i32 %21, %20
store i32 %22, i32* %7
br label %23
23: ; preds = %15, %3
%24 = load i32, i32* %5
%25 = icmp sgt i32 %24, 2
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 2
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = load i32, i32* %7
%32 = or i32 %31, %30
store i32 %32, i32* %7
br label %33
33: ; preds = %26, %23
%34 = load i32, i32* %7
%35 = lshr i32 %34, 18
%36 = and i32 %35, 63
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds [65 x i8], [65 x i8]* @base64_encode_atom.base64_chars, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i64 0
store i8 %39, i8* %41
%42 = load i32, i32* %7
%43 = lshr i32 %42, 12
%44 = and i32 %43, 63
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds [65 x i8], [65 x i8]* @base64_encode_atom.base64_chars, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = load i8*, i8** %6
%49 = getelementptr inbounds i8, i8* %48, i64 1
store i8 %47, i8* %49
%50 = load i32, i32* %5
%51 = icmp sgt i32 %50, 1
br i1 %51, label %52, label %61
52: ; preds = %33
%53 = load i32, i32* %7
%54 = lshr i32 %53, 6
%55 = and i32 %54, 63
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds [65 x i8], [65 x i8]* @base64_encode_atom.base64_chars, i64 0, i64 %56
%58 = load i8, i8* %57
%59 = load i8*, i8** %6
%60 = getelementptr inbounds i8, i8* %59, i64 2
store i8 %58, i8* %60
br label %64
61: ; preds = %33
%62 = load i8*, i8** %6
%63 = getelementptr inbounds i8, i8* %62, i64 2
store i8 61, i8* %63
br label %64
64: ; preds = %61, %52
%65 = load i32, i32* %5
%66 = icmp sgt i32 %65, 2
br i1 %66, label %67, label %75
67: ; preds = %64
%68 = load i32, i32* %7
%69 = and i32 %68, 63
%70 = zext i32 %69 to i64
%71 = getelementptr inbounds [65 x i8], [65 x i8]* @base64_encode_atom.base64_chars, i64 0, i64 %70
%72 = load i8, i8* %71
%73 = load i8*, i8** %6
%74 = getelementptr inbounds i8, i8* %73, i64 3
store i8 %72, i8* %74
br label %78
75: ; preds = %64
%76 = load i8*, i8** %6
%77 = getelementptr inbounds i8, i8* %76, i64 3
store i8 61, i8* %77
br label %78
78: ; preds = %75, %67
ret void
}

LLVM IR (-O2):
@base64_encode_atom.base64_chars = internal unnamed_addr constant [65 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\00"
define void @base64_encode_atom(i8* nocapture %0, i32 %1, i8* nocapture writeonly %2) {
%4 = load i8, i8* %0
%5 = zext i8 %4 to i32
%6 = shl nuw nsw i32 %5, 16
%7 = icmp sgt i32 %1, 1
br i1 %7, label %8, label %20
8: ; preds = %3
%9 = getelementptr inbounds i8, i8* %0, i64 1
%10 = load i8, i8* %9
%11 = zext i8 %10 to i32
%12 = shl nuw nsw i32 %11, 8
%13 = or i32 %12, %6
%14 = icmp eq i32 %1, 2
br i1 %14, label %20, label %15
15: ; preds = %8
%16 = getelementptr inbounds i8, i8* %0, i64 2
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = or i32 %13, %18
br label %20
20: ; preds = %3, %15, %8
%21 = phi i1 [ true, %15 ], [ false, %8 ], [ false, %3 ]
%22 = phi i32 [ %19, %15 ], [ %13, %8 ], [ %6, %3 ]
%23 = lshr i32 %22, 18
%24 = and i32 %23, 63
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds [65 x i8], [65 x i8]* @base64_encode_atom.base64_chars, i64 0, i64 %25
%27 = load i8, i8* %26
store i8 %27, i8* %2
%28 = lshr i32 %22, 12
%29 = and i32 %28, 63
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds [65 x i8], [65 x i8]* @base64_encode_atom.base64_chars, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = getelementptr inbounds i8, i8* %2, i64 1
store i8 %32, i8* %33
br i1 %7, label %34, label %40
34: ; preds = %20
%35 = lshr i32 %22, 6
%36 = and i32 %35, 63
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds [65 x i8], [65 x i8]* @base64_encode_atom.base64_chars, i64 0, i64 %37
%39 = load i8, i8* %38
br label %40
40: ; preds = %20, %34
%41 = phi i8 [ %39, %34 ], [ 61, %20 ]
%42 = getelementptr inbounds i8, i8* %2, i64 2
store i8 %41, i8* %42
br i1 %21, label %43, label %48
43: ; preds = %40
%44 = and i32 %22, 63
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds [65 x i8], [65 x i8]* @base64_encode_atom.base64_chars, i64 0, i64 %45
%47 = load i8, i8* %46
br label %48
48: ; preds = %40, %43
%49 = phi i8 [ %47, %43 ], [ 61, %40 ]
%50 = getelementptr inbounds i8, i8* %2, i64 3
store i8 %49, i8* %50
ret void
}