#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ __base64_encode_block(int /*<<< orphan*/ const *,
                                         int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ *malloc(int);
uint8_t *base64_encode(const uint8_t *in, size_t input_len,
                       size_t *output_len) {
  uint8_t *data;
  int i, j, len = input_len;
  switch (input_len % 3) {
  case 1:
    len++;
  case 2:
    len++;
  default:
    break;
  }
  len = (size_t)(4 * (len / 3));
  if ((data = malloc(len)) == NULL)
    return NULL;
  for (i = 0, j = 0; j < len; i += 3, j += 4) {
    __base64_encode_block(in + i, data + j, input_len);
    input_len -= 3;
  }
  if (output_len)
    *output_len = len;
  return data;
}


x86 assembly (clang, -O0):
<base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
movabs $0xaaaaaaaaaaaaaaab,%rcx
aa aa aa
mul    %rcx
mov    -0x48(%rbp),%rax
shr    %rdx
lea    (%rdx,%rdx,2),%rcx
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
sub    $0x1,%rax
je     1183 <base64_encode+0x63>
jmp    1170 <base64_encode+0x50>
mov    -0x40(%rbp),%rax
sub    $0x2,%rax
je     118c <base64_encode+0x6c>
jmp    1195 <base64_encode+0x75>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    119a <base64_encode+0x7a>
mov    -0x34(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
shl    $0x2,%eax
cltq
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d0 <base64_encode+0xb0>
movq   $0x0,-0x8(%rbp)
jmp    1252 <base64_encode+0x132>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1234 <base64_encode+0x114>
mov    -0x10(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
movslq -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1030 <__base64_encode_block@plt>
mov    -0x18(%rbp),%rax
sub    $0x3,%rax
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x30(%rbp)
jmp    11de <base64_encode+0xbe>
cmpq   $0x0,-0x20(%rbp)
je     124a <base64_encode+0x12a>
movslq -0x34(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%r13
mov    %rdi,%r15
movabs $0xaaaaaaaaaaaaaaab,%rcx
aa aa aa
mov    %rsi,%rax
mul    %rcx
shr    %rdx
lea    (%rdx,%rdx,2),%rcx
mov    %rsi,%rax
sub    %rcx,%rax
cmp    $0x2,%rax
je     1166 <base64_encode+0x46>
cmp    $0x1,%rax
jne    116f <base64_encode+0x4f>
lea    0x1(%r13),%r14d
jmp    1169 <base64_encode+0x49>
mov    %r13d,%r14d
add    $0x1,%r14d
jmp    1172 <base64_encode+0x52>
mov    %r13d,%r14d
movslq %r14d,%rax
imul   $0x55555556,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
shr    $0x20,%rbx
add    %eax,%ebx
shl    $0x2,%ebx
mov    %ebx,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11f5 <base64_encode+0xd5>
mov    %rax,0x8(%rsp)
mov    %rbp,0x10(%rsp)
movslq %ebx,%r12
cmp    $0x3,%r14d
jl     11e3 <base64_encode+0xc3>
xor    %ebp,%ebp
mov    0x8(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1030 <__base64_encode_block@plt>
add    $0xfffffffffffffffd,%r13
add    $0x4,%rbp
add    $0xc,%r15
add    $0x10,%rbx
cmp    %r12,%rbp
jl     11c0 <base64_encode+0xa0>
mov    0x10(%rsp),%rcx
test   %rcx,%rcx
mov    0x8(%rsp),%rax
je     11f5 <base64_encode+0xd5>
mov    %r12,(%rcx)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @base64_encode(i32* %0, i64 %1, i64* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
%12 = load i64, i64* %6
%13 = trunc i64 %12 to i32
store i32 %13, i32* %11
%14 = load i64, i64* %6
%15 = urem i64 %14, 3
switch i64 %15, label %22 [
i64 1, label %16
i64 2, label %19
]
16: ; preds = %3
%17 = load i32, i32* %11
%18 = add nsw i32 %17, 1
store i32 %18, i32* %11
br label %19
19: ; preds = %3, %16
%20 = load i32, i32* %11
%21 = add nsw i32 %20, 1
store i32 %21, i32* %11
br label %22
22: ; preds = %3, %19
br label %23
23: ; preds = %22
%24 = load i32, i32* %11
%25 = sdiv i32 %24, 3
%26 = mul nsw i32 4, %25
%27 = sext i32 %26 to i64
%28 = trunc i64 %27 to i32
store i32 %28, i32* %11
%29 = load i32, i32* %11
%30 = call i32* @malloc(i32 %29)
store i32* %30, i32** %8
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %23
store i32* null, i32** %4
br label %65
33: ; preds = %23
store i32 0, i32* %9
store i32 0, i32* %10
br label %34
34: ; preds = %51, %33
%35 = load i32, i32* %10
%36 = load i32, i32* %11
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %56
38: ; preds = %34
%39 = load i32*, i32** %5
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32*, i32** %8
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i64, i64* %6
%48 = call i32 @__base64_encode_block(i32* %42, i32* %46, i64 %47)
%49 = load i64, i64* %6
%50 = sub i64 %49, 3
store i64 %50, i64* %6
br label %51
51: ; preds = %38
%52 = load i32, i32* %9
%53 = add nsw i32 %52, 3
store i32 %53, i32* %9
%54 = load i32, i32* %10
%55 = add nsw i32 %54, 4
store i32 %55, i32* %10
br label %34
56: ; preds = %34
%57 = load i64*, i64** %7
%58 = icmp ne i64* %57, null
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = load i32, i32* %11
%61 = sext i32 %60 to i64
%62 = load i64*, i64** %7
store i64 %61, i64* %62
br label %63
63: ; preds = %59, %56
%64 = load i32*, i32** %8
store i32* %64, i32** %4
br label %65
65: ; preds = %63, %32
%66 = load i32*, i32** %4
ret i32* %66
}

LLVM IR (-O2):
define i32* @base64_encode(i32* %0, i64 %1, i64* writeonly %2) {
%4 = trunc i64 %1 to i32
%5 = urem i64 %1, 3
switch i64 %5, label %11 [
i64 1, label %6
i64 2, label %8
]
6: ; preds = %3
%7 = add nsw i32 %4, 1
br label %8
8: ; preds = %3, %6
%9 = phi i32 [ %4, %3 ], [ %7, %6 ]
%10 = add nsw i32 %9, 1
br label %11
11: ; preds = %8, %3
%12 = phi i32 [ %4, %3 ], [ %10, %8 ]
%13 = sdiv i32 %12, 3
%14 = shl nsw i32 %13, 2
%15 = sext i32 %14 to i64
%16 = tail call i32* @malloc(i32 %14)
%17 = icmp eq i32* %16, null
br i1 %17, label %34, label %18
18: ; preds = %11
%19 = icmp sgt i32 %12, 2
br i1 %19, label %20, label %31
20: ; preds = %18, %20
%21 = phi i64 [ %28, %20 ], [ 0, %18 ]
%22 = phi i64 [ %29, %20 ], [ 0, %18 ]
%23 = phi i64 [ %27, %20 ], [ %1, %18 ]
%24 = getelementptr inbounds i32, i32* %0, i64 %21
%25 = getelementptr inbounds i32, i32* %16, i64 %22
%26 = tail call i32 @__base64_encode_block(i32* %24, i32* %25, i64 %23)
%27 = add i64 %23, -3
%28 = add nuw nsw i64 %21, 3
%29 = add nuw nsw i64 %22, 4
%30 = icmp slt i64 %29, %15
br i1 %30, label %20, label %31
31: ; preds = %20, %18
%32 = icmp eq i64* %2, null
br i1 %32, label %34, label %33
33: ; preds = %31
store i64 %15, i64* %2
br label %34
34: ; preds = %31, %33, %11
ret i32* %16
}