void base64_encode(unsigned char *target, const unsigned char *source) {
  static const char *map =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  int i;
  int value;
  value = source[0];
  value *= 256;
  value += source[1];
  value *= 256;
  value += source[2];
  for (i = 0; i < 4; i++) {
    target[3 - i] = map[value % 64];
    value /= 64;
  }
}


x86 assembly (clang, -O0):
<base64_encode>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    11a3 <base64_encode+0xa3>
mov    0x2ec4(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x40,%ecx
cltd
idiv   %ecx
mov    -0x20(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
mov    $0x3,%ecx
sub    -0x14(%rbp),%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    $0x40,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114b <base64_encode+0x4b>
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode>:
movzbl (%rsi),%r9d
movzbl 0x1(%rsi),%ecx
mov    %r9d,%edx
shl    $0x10,%edx
shl    $0x8,%ecx
or     %ecx,%edx
movzbl 0x2(%rsi),%esi
or     %esi,%ecx
and    $0x3f,%esi
lea    0xedd(%rip),%r8
mov    (%rsi,%r8,1),%al
mov    %al,0x3(%rdi)
shr    $0x6,%ecx
and    $0x3f,%ecx
mov    (%rcx,%r8,1),%al
mov    %al,0x2(%rdi)
shr    $0xc,%edx
and    $0x3f,%edx
mov    (%rdx,%r8,1),%al
mov    %al,0x1(%rdi)
shr    $0x2,%r9
mov    (%r9,%r8,1),%al
mov    %al,(%rdi)
ret

LLVM IR (-O0):
@base64_encode.map = internal global i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i32 0, i32 0)
@.str = [65 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\00"
define void @base64_encode(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = zext i8 %9 to i32
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = mul nsw i32 %11, 256
store i32 %12, i32* %6
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = load i32, i32* %6
%18 = add nsw i32 %17, %16
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = mul nsw i32 %19, 256
store i32 %20, i32* %6
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 2
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = load i32, i32* %6
%26 = add nsw i32 %25, %24
store i32 %26, i32* %6
store i32 0, i32* %5
br label %27
27: ; preds = %44, %2
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 4
br i1 %29, label %30, label %47
30: ; preds = %27
%31 = load i8*, i8** @base64_encode.map
%32 = load i32, i32* %6
%33 = srem i32 %32, 64
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = load i8, i8* %35
%37 = load i8*, i8** %3
%38 = load i32, i32* %5
%39 = sub nsw i32 3, %38
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 %36, i8* %41
%42 = load i32, i32* %6
%43 = sdiv i32 %42, 64
store i32 %43, i32* %6
br label %44
44: ; preds = %30
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
br label %27
47: ; preds = %27
ret void
}

LLVM IR (-O2):
@.str = [65 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\00"
define void @base64_encode(i8* nocapture writeonly %0, i8* nocapture %1) {
%3 = load i8, i8* %1
%4 = zext i8 %3 to i32
%5 = getelementptr inbounds i8, i8* %1, i64 1
%6 = load i8, i8* %5
%7 = zext i8 %6 to i32
%8 = shl nuw nsw i32 %4, 16
%9 = shl nuw nsw i32 %7, 8
%10 = or i32 %9, %8
%11 = getelementptr inbounds i8, i8* %1, i64 2
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = or i32 %9, %13
%15 = and i32 %13, 63
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds [65 x i8], [65 x i8]* @.str, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %18, i8* %19
%20 = lshr i32 %14, 6
%21 = and i32 %20, 63
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [65 x i8], [65 x i8]* @.str, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %24, i8* %25
%26 = lshr i32 %10, 12
%27 = and i32 %26, 63
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds [65 x i8], [65 x i8]* @.str, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %30, i8* %31
%32 = lshr i32 %4, 2
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds [65 x i8], [65 x i8]* @.str, i64 0, i64 %33
%35 = load i8, i8* %34
store i8 %35, i8* %0
ret void
}